| | |
| | | } |
| | | return j; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 查询列表,支持分页(保持输出参数方式) |
| | | /// </summary> |
| | | [RequestMethod(RequestMethods.POST)] |
| | | public ReturnDto<PageList<MesStaff>> GetListCgy(PageQuery query) |
| | | { |
| | | var pageList = new PageList<MesStaff>(); |
| | | try |
| | | { |
| | | // 检查基本对象 |
| | | if (query == null) throw new ArgumentNullException(nameof(query)); |
| | | if (Db?.Ado == null) throw new InvalidOperationException("数据库连接不可用"); |
| | | |
| | | // 存储过程参数 - 注意现在有输出参数了 |
| | | var parameters = new[] |
| | | { |
| | | 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, "读取成功(总数获取失败)"); |
| | | } |
| | | } |
| | | 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}"); |
| | | } |
| | | } |
| | | } |