llj
15 小时以前 a91fdba94705dd25cecccc7a4a5dcc2b3f2c09a6
WebAPI/Controllers/GZGL/Gy_SteppedPriceCoefficientController .cs
@@ -117,6 +117,7 @@
            try
            {
                errorLogs.Add($"=== {DateTime.Now:yyyy-MM-dd HH:mm:ss} 保存开始 ===");
                // 1. 解析数据
@@ -124,6 +125,16 @@
                string[] sArray = _value.Split(';');
                string jsonData = sArray[0];
                string user = sArray.Length > 1 ? sArray[1] : "unknown";
                // 查看权限检查
                if (!DBUtility.ClsPub.Security_Log("Gy_SteppedPriceCoefficient_Edit", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无编辑权限";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                errorLogs.Add($"解析数据: user={user}");
@@ -308,7 +319,7 @@
                            HMainSourceBillType, HMainSourceInterID, HMainSourceEntryID, HMainSourceBillNo,
                            HPrintQty, HProcID, HEmpID, HDeptID, HStockOrgID
                        ) VALUES (
                            {HYear}, {HPeriod}, 'GYST', '',
                            {HYear}, {HPeriod}, '3341', '',
                            {HInterID}, '{HDate.ToString("yyyy-MM-dd HH:mm:ss")}', '{safeHBillNo}', 1, '{safeHRemark}',
                            '', '{safeUser}', '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}',
                            '', 0, 0, '', 0, 
@@ -457,212 +468,133 @@
        }
        #endregion
        #region 查询
        #region 阶梯工价系数 查询-页面赋值
        [Route("Gy_SteppedPriceCoefficientBill/list")]
        [HttpGet]
        public object getSteppedPriceCoefficient(string sWhere = "", string user = "", int page = 1, int size = 50)
        public object list(string sWhere, string user)
        {
            try
            {
                // 添加调试信息
                Console.WriteLine("=== 查询开始 ===");
                Console.WriteLine($"参数: sWhere={sWhere}, user={user}, page={page}, size={size}");
                List<object> columnNameList = new List<object>();
                // 确保user不为空
                if (string.IsNullOrEmpty(user))
                // 查看权限检查
                if (!DBUtility.ClsPub.Security_Log("Gy_SteppedPriceCoefficient_Query", 1, false, user))
                {
                    user = "admin";
                    Console.WriteLine("警告: user参数为空,使用默认值: admin");
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无查看权限";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                // 构建查询SQL
                string baseSql = @"
                    SELECT
                        m.HInterID,
                        m.HItemMainID,
                        m.HBillNo,
                        m.HDate,
                        m.HYear,
                        m.HPeriod,
                        m.HBillStatus,
                        m.HRemark,
                        m.HChecker,
                        m.HCheckDate,
                        m.HMaker,
                        m.HMakeDate,
                        m.HUpDater,
                        m.HUpDateDate,
                        m.HCloseMan,
                        m.HCloseDate,
                        m.HProcID,
                        m.HEmpID,
                        m.HDeptID,
                        m.HStockOrgID,
                        -- 子表统计信息
                        ISNULL((SELECT COUNT(*) FROM Gy_SteppedPriceCoefficientBillSub s WHERE s.HInterID = m.HInterID), 0) as SubCount,
                        ISNULL((SELECT SUM(HMinQty) FROM Gy_SteppedPriceCoefficientBillSub s WHERE s.HInterID = m.HInterID), 0) as TotalMinQty,
                        ISNULL((SELECT SUM(HMaxQty) FROM Gy_SteppedPriceCoefficientBillSub s WHERE s.HInterID = m.HInterID), 0) as TotalMaxQty,
                        ISNULL((SELECT SUM(HPriceCoefficient) FROM Gy_SteppedPriceCoefficientBillSub s WHERE s.HInterID = m.HInterID), 0) as TotalCoefficient,
                        ISNULL((SELECT SUM(HMaxPrice) FROM Gy_SteppedPriceCoefficientBillSub s WHERE s.HInterID = m.HInterID), 0) as TotalMaxPrice
                    FROM Gy_SteppedPriceCoefficientBillMain m
                    WHERE m.HDeleteMan = '' ";
                // 构建基础SQL
                string baseSql = @"select * from h_v_Gy_SteppedPriceCoefficientBillWithSub";
                string whereClause = "";
                // 调试:记录接收到的参数
                Console.WriteLine($"API 接收参数 - sWhere: '{sWhere}', user: '{user}'");
                if (!string.IsNullOrEmpty(sWhere))
                // 处理查询条件(约定:前端只发送条件表达式,可能以 AND 开头)
                string whereClause = ProcessWhereClause(sWhere);
                string orderByClause = "order by hmainid";
                // 构建完整SQL
                string sql;
                if (string.IsNullOrWhiteSpace(whereClause))
                {
                    // 处理HMakeDate查询条件 - 确保正确转换
                    if (sWhere.Contains("HMakeDate"))
                    {
                        sWhere = System.Text.RegularExpressions.Regex.Replace(
                            sWhere,
                            @"HMakeDate\s*(>=|<=|>|<|=)\s*'([^']*)'",
                            match => $"CONVERT(date, HMakeDate) {match.Groups[1].Value} '{match.Groups[2].Value}'"
                        );
                    }
                    whereClause = " AND " + sWhere;
                }
                Console.WriteLine($"完整查询条件: {whereClause}");
                // 1. 先获取总记录数
                string countSql = "SELECT COUNT(*) as TotalCount FROM Gy_SteppedPriceCoefficientBillMain WHERE HDeleteMan = '' " + whereClause;
                Console.WriteLine($"统计SQL: {countSql}");
                DataSet dsCount = oCN.RunProcReturn(countSql, "TotalCount");
                int totalCount = 0;
                if (dsCount != null && dsCount.Tables.Count > 0 && dsCount.Tables[0].Rows.Count > 0)
                {
                    object countValue = dsCount.Tables[0].Rows[0]["TotalCount"];
                    if (countValue != null && countValue != DBNull.Value)
                    {
                        totalCount = Convert.ToInt32(countValue);
                    }
                }
                Console.WriteLine($"总记录数: {totalCount}");
                // 2. 执行分页查询
                int startRow = (page - 1) * size;
                string pageSql = $@"
                    SELECT * FROM (
                        SELECT ROW_NUMBER() OVER (ORDER BY HInterID DESC) AS RowNum, *
                        FROM ({baseSql + whereClause}) AS T
                    ) AS T2
                    WHERE RowNum > {startRow} AND RowNum <= {startRow + size}";
                Console.WriteLine($"分页查询SQL: {pageSql}");
                ds = oCN.RunProcReturn(pageSql, "Gy_SteppedPriceCoefficientList");
                // 调试信息
                if (ds == null)
                {
                    Console.WriteLine("查询返回的DataSet为null");
                    // 没有条件时,不需要WHERE关键字
                    sql = $"{baseSql} {orderByClause}";
                }
                else
                {
                    Console.WriteLine($"查询返回的DataSet表数量: {ds.Tables.Count}");
                    if (ds.Tables.Count > 0)
                    {
                        Console.WriteLine($"第一张表行数: {ds.Tables[0].Rows.Count}");
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            Console.WriteLine("第一行数据示例:");
                            foreach (DataColumn col in ds.Tables[0].Columns)
                            {
                                Console.WriteLine($"  {col.ColumnName}: {ds.Tables[0].Rows[0][col.ColumnName]}");
                            }
                        }
                    }
                    // 有条件时,添加WHERE关键字
                    sql = $"{baseSql} WHERE {whereClause} {orderByClause}";
                }
                List<object> columnNameList = new List<object>();
                // 调试:输出最终SQL
                Console.WriteLine($"最终执行的SQL: {sql}");
                // 3. 获取列信息
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                // 执行查询
                ds = oCN.RunProcReturn(sql, "h_v_Gy_SteppedPriceCoefficientBillWithSub");
                // 添加列名
                if (ds != null && ds.Tables.Count > 0)
                {
                    foreach (DataColumn col in ds.Tables[0].Columns)
                    {
                        Type dataType = col.DataType;
                        string colName = col.ColumnName;
                        string typeName = dataType.Name;
                        columnNameList.Add(new
                        {
                            ColmCols = colName,
                            ColmType = typeName
                        });
                    }
                    // 添加子表相关的列
                    columnNameList.Add(new { ColmCols = "SubCount", ColmType = "Int32" });
                    columnNameList.Add(new { ColmCols = "TotalMinQty", ColmType = "Decimal" });
                    columnNameList.Add(new { ColmCols = "TotalMaxQty", ColmType = "Decimal" });
                    columnNameList.Add(new { ColmCols = "TotalCoefficient", ColmType = "Decimal" });
                    columnNameList.Add(new { ColmCols = "TotalMaxPrice", ColmType = "Decimal" });
                }
                // 4. 将DataTable转换为对象列表(前端layui table需要数组格式)
                List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
                if (ds != null && ds.Tables.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        Dictionary<string, object> rowDict = new Dictionary<string, object>();
                        foreach (DataColumn col in ds.Tables[0].Columns)
                        {
                            rowDict[col.ColumnName] = row[col] == DBNull.Value ? null : row[col];
                        }
                        dataList.Add(rowDict);
                        string colmString = "{\"ColmCols\":\"" + col.ColumnName + "\",\"ColmType\":\"" + dataType.Name + "\"}";
                        columnNameList.Add(JsonConvert.DeserializeObject(colmString));
                    }
                }
                // 5. 构建返回结果
                var result = new
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    code = "1",
                    count = totalCount,
                    Message = totalCount > 0 ? $"查询成功,共{totalCount}条记录" : "查询成功,无数据",
                    data = dataList,  // 使用转换后的列表
                    list = columnNameList,
                    debug = new
                    {
                        totalCount = totalCount,
                        returnedCount = dataList.Count,
                        queryTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                    }
                };
                Console.WriteLine($"查询完成,返回{dataList.Count}条数据");
                return result;
                    objJsonResult.code = "1";
                    objJsonResult.count = ds.Tables[0].Rows.Count;
                    objJsonResult.Message = "Success!";
                    objJsonResult.data = ds.Tables[0];
                    objJsonResult.list = columnNameList;
                    return objJsonResult;
                }
                else
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "没有查询到数据,请联系系统管理员进行核对";
                    objJsonResult.data = ds?.Tables[0] ?? new DataTable();
                    objJsonResult.list = columnNameList;
                    return objJsonResult;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"=== 查询异常 ===");
                Console.WriteLine($"错误信息: {e.Message}");
                Console.WriteLine($"堆栈跟踪: {e.StackTrace}");
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "查询失败!错误:" + e.Message;
                objJsonResult.data = null;
                var errorResult = new
                {
                    code = "0",
                    count = 0,
                    Message = $"查询失败!错误:{e.Message}",
                    data = new List<object>(),
                    list = new List<object>(),
                    debug = new
                    {
                        error = e.Message,
                        stackTrace = e.StackTrace,
                        queryTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                    }
                };
                // 记录完整异常信息
                Console.WriteLine($"API 异常: {e.ToString()}");
                return errorResult;
                return objJsonResult;
            }
        }
        /// <summary>
        /// 处理WHERE条件子句
        /// 约定:前端只发送条件表达式,可能以 AND 开头
        /// 返回:不带 WHERE 和开头 AND 的条件字符串
        /// </summary>
        private string ProcessWhereClause(string sWhere)
        {
            if (string.IsNullOrWhiteSpace(sWhere))
                return string.Empty;
            string condition = sWhere.Trim();
            Console.WriteLine($"ProcessWhereClause 输入: '{sWhere}'");
            // 如果前端意外发送了 WHERE 开头,移除它
            if (condition.StartsWith("WHERE ", StringComparison.OrdinalIgnoreCase))
            {
                condition = condition.Substring(6).Trim();
                Console.WriteLine($"移除了 WHERE 关键字,处理后: '{condition}'");
            }
            // 移除开头的 AND
            if (condition.StartsWith("AND ", StringComparison.OrdinalIgnoreCase))
            {
                condition = condition.Substring(4).Trim();
                Console.WriteLine($"移除了 AND 关键字,处理后: '{condition}'");
            }
            // 额外检查:确保开头没有 WHERE 或 AND
            condition = condition.Trim();
            Console.WriteLine($"ProcessWhereClause 最终输出: '{condition}'");
            return condition;
        }
        #endregion
@@ -675,6 +607,8 @@
            {
                Console.WriteLine($"=== 编辑页面初始化开始 ===");
                Console.WriteLine($"参数 - HInterID: {HInterID}, User: {user}");
                if (string.IsNullOrEmpty(HInterID) || HInterID == "0")
                {
@@ -831,7 +765,15 @@
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                // 查看权限检查
                if (!DBUtility.ClsPub.Security_Log("Gy_SteppedPriceCoefficient_Drop", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无删除权限";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                //检查单据状态
                string sql = "SELECT HBillStatus, HChecker FROM Gy_SteppedPriceCoefficientBillMain WHERE HInterID = " + HInterID;
                ds = oCN.RunProcReturn(sql, "CheckStatus");
@@ -892,6 +834,18 @@
        {
            try
            {
                // 查看权限检查
                if (!DBUtility.ClsPub.Security_Log("Gy_SteppedPriceCoefficient_Check", 1, false, CurUserName))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无审核权限";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                //检查单据状态
                string checkSql = "SELECT HBillStatus, HChecker FROM Gy_SteppedPriceCoefficientBillMain WHERE HInterID = " + HInterID;
                ds = oCN.RunProcReturn(checkSql, "CheckStatus");