| | |
| | | using System.Data; |
| | | using System.Data.SqlClient; |
| | | using System.Text; |
| | | using Gs.Entity.BaseInfo; |
| | | using Gs.Entity.Sys; |
| | |
| | | /// 查询列表,支持分页(保持输出参数方式) |
| | | /// </summary> |
| | | [RequestMethod(RequestMethods.POST)] |
| | | public ReturnDto<PageList<MesStaff>> GetListCgy(PageQuery query) |
| | | public ReturnDto<PageList<dynamic>> GetListCgy(PageQuery model) |
| | | { |
| | | var pageList = new PageList<MesStaff>(); |
| | | string currentId = "";//默认采购员 |
| | | int currentPage = model.currentPage; |
| | | int everyPageSize = model.everyPageSize; |
| | | string sortName = model.sortName; |
| | | string keyWhere = model.keyWhere; |
| | | SqlParameter[] parameters = |
| | | { |
| | | new("@inCurrentPage", currentPage), |
| | | new("@inEveryPageSize", everyPageSize), |
| | | new("@inSortName", sortName), |
| | | new("@inSortOrder", ""), |
| | | new("@inQueryWhere", keyWhere), |
| | | new("@inFid", ""), |
| | | new("@inP1", ""), |
| | | new("@inP2", ""), |
| | | new("@inP3", ""), |
| | | new("@inP4", _userGuid)//当前登录用户guid,将根据他读取仓管员 |
| | | }; |
| | | var dset = new DataSet(); |
| | | var _pglist = new PageList<dynamic> |
| | | { |
| | | total = 0, |
| | | everyPageSize = 0, |
| | | pages = 0, |
| | | list = new List<dynamic>() |
| | | }; |
| | | try |
| | | { |
| | | // 检查基本对象 |
| | | if (query == null) throw new ArgumentNullException(nameof(query)); |
| | | if (Db?.Ado == null) throw new InvalidOperationException("数据库连接不可用"); |
| | | |
| | | // 存储过程参数 - 注意现在有输出参数了 |
| | | var parameters = new[] |
| | | dset = DbHelperSQL.RunProcedure("prc_cgy_lst", parameters, "0"); |
| | | if (dset != null && dset.Tables.Count > 0 && |
| | | dset.Tables[0].Rows.Count > 0) //有数据 |
| | | { |
| | | new SugarParameter("@PageIndex", query.currentPage), |
| | | new SugarParameter("@PageSize", query.everyPageSize), |
| | | new SugarParameter("@SortField", string.IsNullOrEmpty(query.sortName) ? "staff_no" : query.sortName), |
| | | new SugarParameter("@SortOrder", string.IsNullOrEmpty(query.sortOrder) ? "DESC" : query.sortOrder), |
| | | new SugarParameter("@WhereCondition", query.keyWhere ?? ""), |
| | | new SugarParameter("@Fid", DBNull.Value), |
| | | new SugarParameter("@P1", DBNull.Value), |
| | | new SugarParameter("@P2", DBNull.Value), |
| | | new SugarParameter("@P3", DBNull.Value), |
| | | new SugarParameter("@P4", DBNull.Value), |
| | | // 注意:输出参数必须指定 ParameterDirection.Output |
| | | new SugarParameter("@outTotalCount", 0, System.Data.DbType.Int32, ParameterDirection.Output) |
| | | }; |
| | | |
| | | // 注意:调用语句必须包含 OUTPUT 关键字 |
| | | var itemsList = Db.Ado.SqlQuery<MesStaff>( |
| | | "EXEC prc_cgy_lst @PageIndex, @PageSize, @SortField, @SortOrder, @WhereCondition, @Fid, @P1, @P2, @P3, @P4, @outTotalCount OUTPUT", |
| | | parameters |
| | | ); |
| | | |
| | | // 获取输出参数 - 等待执行完成后再获取 |
| | | var totalCountParam = parameters.FirstOrDefault(p => p.ParameterName == "@outTotalCount"); |
| | | if (totalCountParam != null && totalCountParam.Value != null) |
| | | { |
| | | try |
| | | { |
| | | // 确保值是整数类型 |
| | | var totalCount = Convert.ToInt32(totalCountParam.Value); |
| | | pageList = new PageList<MesStaff>(itemsList ?? new List<MesStaff>(), totalCount, query.everyPageSize); |
| | | return ReturnDto<PageList<MesStaff>>.QuickReturn(pageList, ReturnCode.Success, "读取成功"); |
| | | } |
| | | catch (FormatException) |
| | | { |
| | | // 如果转换失败,使用默认值 |
| | | pageList = new PageList<MesStaff>(itemsList ?? new List<MesStaff>(), itemsList?.Count ?? 0, query.everyPageSize); |
| | | return ReturnDto<PageList<MesStaff>>.QuickReturn(pageList, ReturnCode.Success, "读取成功(总数获取失败)"); |
| | | } |
| | | var intTotal = int.Parse(dset.Tables[1].Rows[0]["intTotal"].ToString()); |
| | | var pages = intTotal % everyPageSize != 0 |
| | | ? intTotal / everyPageSize + 1 |
| | | : intTotal / everyPageSize; |
| | | _pglist.total = intTotal; |
| | | _pglist.everyPageSize = everyPageSize; |
| | | _pglist.pages = pages; |
| | | var _dy = dset.Tables[0].TableToDynamicList(); |
| | | _pglist.list = _dy; |
| | | currentId = dset.Tables[1].Rows[0]["mrCgy"].ToString(); |
| | | } |
| | | else |
| | | { |
| | | // 输出参数不存在,使用列表长度 |
| | | pageList = new PageList<MesStaff>(itemsList ?? new List<MesStaff>(), itemsList?.Count ?? 0, query.everyPageSize); |
| | | return ReturnDto<PageList<MesStaff>>.QuickReturn(pageList, ReturnCode.Success, "读取成功"); |
| | | } |
| | | } |
| | | catch (ArgumentNullException argEx) |
| | | { |
| | | return ReturnDto<PageList<MesStaff>>.QuickReturn(pageList, ReturnCode.Default, $"参数错误: {argEx.Message}"); |
| | | } |
| | | catch (InvalidOperationException opEx) |
| | | { |
| | | return ReturnDto<PageList<MesStaff>>.QuickReturn(pageList, ReturnCode.Default, $"操作错误: {opEx.Message}"); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | return ReturnDto<PageList<MesStaff>>.QuickReturn(pageList, ReturnCode.Default, $"系统错误: {ex.Message}"); |
| | | LogHelper.Debug(ToString(), ex.Message); |
| | | return ReturnDto<PageList<dynamic>>.QuickReturn(_pglist, |
| | | ReturnCode.Exception,"-1"); |
| | | } |
| | | |
| | | return ReturnDto<PageList<dynamic>>.QuickReturn(_pglist, |
| | | ReturnCode.Success, currentId); |
| | | |
| | | |
| | | } |
| | | } |