南骏 池
2025-08-27 459621921ba77d8cc8bf0789209b76d387b820e8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System.Data;
using System.Data.SqlClient;
using Masuit.Tools;
using Masuit.Tools.Hardware;
using NewPdaSqlServer.DB;
using NewPdaSqlServer.Dto.service;
using NewPdaSqlServer.entity;
using SqlSugar;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
 
namespace NewPdaSqlServer.service.Warehouse;
 
/// <summary>
/// 库位变更管理类,继承自通用仓储类Repository,操作采购退货申请表(MesCgthSq)
/// </summary>
public class KwbgManager : Repository<MesCgthSq>
{
    /// <summary>
    /// 根据传入的WarehouseQuery对象(包含条码信息),查询物料库存表MES_INV_ITEM_STOCKS,获取对应的库存信息
    /// </summary>
    /// <param name="unity">WarehouseQuery对象,包含条码编号等查询条件</param>
    /// <returns>
    /// 返回MES_INV_ITEM_STOCKS表中与条码匹配的第一条库存信息(MesInvItemStocks类型)。
    /// 如果未找到,则抛出异常提示“该条码库存不存在!”
    /// resultInfos.tbBillList = m.GetBarInfo(unity);
    /// </returns>
    ///public MesInvItemStocks GetBarInfo(WarehouseQuery unity)
    public dynamic GetBarInfo(dynamic query)
    {
        if (query == null)
            throw new ArgumentNullException(nameof(query), "参数对象不能为null");
 
        if (string.IsNullOrEmpty(query.barcode?.ToString()))
            throw new ArgumentException("物料条码不能为空", nameof(query.barcode));
 
        var parameters = new[]
    {
        new SugarParameter("@pi_barcode", query.barcode),
        new SugarParameter("@inP1", null),
        new SugarParameter("@inP2", null),
        new SugarParameter("@inP3", null),
        new SugarParameter("@inP4", null)
    };
 
        try
        {
            List<dynamic>? ZsBarInfo = Db.Ado.SqlQuery<dynamic>(
                "EXEC prc_pda_kwbg_selKwInfoByBarcode @pi_barcode,@inP1,@inP2,@inP3,@inP4", parameters);
 
            return ZsBarInfo;
        }
        catch (Exception ex)
        {
            // 保留原有异常处理逻辑
            throw new Exception($"{ex.Message}");
        }
    }
 
    /// <summary>
    /// 根据物料ID查询物料表MES_ITEMS,获取物料详细信息
    /// </summary>
    /// <param name="strItemId">物料ID(decimal类型)</param>
    /// <returns>
    /// 返回MES_ITEMS表中与ID匹配且未禁用(Fforbidstatus为"A")的第一条物料信息(MesItems类型)。
    /// 如果未找到,则抛出异常提示“该条码对应物料信息不存在或已禁用!”
    /// resultInfos.tbMesItems = m.GetItemNo(resultInfos.tbBillList.ItemId);
    /// </returns>
    public MesItems GetItemNo(decimal strItemId)
    {
        // 使用SqlSugar的Queryable方法,查询MES_ITEMS表
        // 条件:Id字段等于strItemId,且Fforbidstatus字段为"A"(表示未禁用)
        var itemInfo = Db.Queryable<MesItems>()
            .Where(s => s.Id == strItemId && s.Fforbidstatus == "A")
            .First(); // 获取第一条匹配记录
 
        // 如果未查询到结果(itemInfo为null),则抛出异常,提示物料信息不存在或已禁用
        if (itemInfo is null) throw new Exception("该条码对应物料信息不存在或已禁用!");
 
        // 返回查询到的物料信息对象
        return itemInfo;
    }
    /// <summary>
    /// 仓库信息表
    /// </summary>
    /// <param name="depot_id"></param>
    /// <returns></returns>
    public MesDepots GetDepot(decimal depot_id)
    {
        var depotInfo = Db.Queryable< MesDepots >()
            .Where(s => s.DepotId == depot_id)
            .First(); // 获取第一条匹配记录
        // 如果未查询到结果(itemInfo为null),则抛出异常,提示物料信息不存在或已禁用
        if (depotInfo is null) throw new Exception("该条码库存不存在仓库!");
        return depotInfo;
    }
 
 
 
    /// <summary>
    /// 变更库位
    /// </summary>
    /// <param name="unity"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    public dynamic ChangeDepotSection(dynamic unity)
    {
        // 存储过程输出信息
        var _strMsg = "";
        // 存储过程输出的数量(用于判断变更是否成功)
        var _intSum = "";
        // 存储过程输出的新库位代码
        var _NewDepotSectionCode = ""; // 新库位代码
 
        // 创建SQL连接,使用配置的连接字符串
        using (var conn = new SqlConnection(DbHelperSQL.strConn))
        {
            //if (string.IsNullOrEmpty(query.barcode?.ToString()))
            //throw new ArgumentException("物料条码不能为空", nameof(query.barcode));
 
            if (string.IsNullOrEmpty(unity.userName?.ToString()))
                throw new ArgumentException("用户名不允许为空", nameof(unity.userName));
            if (string.IsNullOrEmpty(unity.OldDepotSectionCode?.ToString()))
                throw new ArgumentException("原库位不允许为空", nameof(unity.OldDepotSectionCode));
            if (string.IsNullOrEmpty(unity.NewDepotSectionCode?.ToString()))
                throw new ArgumentException("新库位不允许为空", nameof(unity.NewDepotSectionCode));
            if (string.IsNullOrEmpty(unity.barcode?.ToString()))
                throw new ArgumentException("物料条码不允许为空", nameof(unity.barcode));
 
            //if (unity.userName.IsNullOrEmpty()) throw new Exception("用户名不允许为空");
            //if (unity.OldDepotSectionCode.IsNullOrEmpty()) throw new Exception("原库位不允许为空");
            //if (unity.NewDepotSectionCode.IsNullOrEmpty()) throw new Exception("新库位不允许为空");
            //if (unity.barcode.IsNullOrEmpty()) throw new Exception("物料条码不允许为空");
 
            // 创建SQL命令,指定要执行的存储过程
            using (var cmd = new SqlCommand("[prc_pda_change_depot]", conn))
            {
                try
                {
                    // 打开数据库连接
                    conn.Open();
                    // 设置命令类型为存储过程
                    cmd.CommandType = CommandType.StoredProcedure;
 
                    // 构造存储过程参数数组
                    SqlParameter[] parameters =
                    {
                    // 输出参数:返回信息
                    new("@outMsg", SqlDbType.NVarChar, 2000),
                    // 输出参数:返回数量
                    new("@outSum", SqlDbType.Int),
                    // 输出参数:返回新库位代码
                    new("@new_depotSectionsCode", SqlDbType.NVarChar, 300),
                    // 输入参数:操作用户
                    new SqlParameter("@c_user", unity.userName?.ToString() ?? ""),
                    // 输入参数:原条码
                    new SqlParameter("@p_barcode", unity.barcode?.ToString() ?? ""),
                    // 输入参数:原库位代码
                    new("@old_depotSectionsCode", unity.OldDepotSectionCode?.ToString() ??""),
                    // 输入参数:新库位代码
                    new("@new_depotSectionsCode_input", unity.NewDepotSectionCode?.ToString() ??""),
                    // 输入参数:仓库代码
                    new("@DepotCode", unity.DepotCode) { Value = unity.DepotCode ?? "" },
                    // 输入参数:仓库id
                    new SqlParameter("@DepotId", (int?)unity.DepotId ?? (object)DBNull.Value)
                };
                    // 设置输出参数方向
                    parameters[0].Direction = ParameterDirection.Output;
                    parameters[1].Direction = ParameterDirection.Output;
                    parameters[2].Direction = ParameterDirection.Output;
 
                    // 添加所有参数到命令对象
                    foreach (var parameter in parameters)
                        cmd.Parameters.Add(parameter);
 
                    // 执行存储过程
                    cmd.ExecuteNonQuery();
 
                    // 获取存储过程输出参数的值
                    _strMsg = parameters[0].Value.ToString();    // 返回信息
                    _intSum = parameters[1].Value.ToString();    // 返回数量
                    _NewDepotSectionCode = parameters[2].Value.ToString();     // 新库位代码
 
                    // 判断变更结果,如果数量小于等于0,则表示变更失败,抛出异常并返回错误信息
                    var result = Convert.ToInt32(_intSum);
                    if (result <= 0) throw new Exception(_strMsg);
 
                    // 使用匿名对象而不是 dynamic? 更合适
                    var dto = new
                    {
                        barcode = unity.barcode,
                        OldDepotSectionCode = unity.OldDepotSectionCode,
                        NewDepotSectionCode = _NewDepotSectionCode
                    };
 
                    return dto;
                }
                catch (Exception ex)
                {
                    // 捕获异常并抛出,保留原始异常信息
                    throw new Exception(ex.Message);
                }
                finally
                {
                    // 关闭数据库连接
                    conn.Close();
                }
            }
        }
    }
    /*ProductionPickDto、WarehouseQuery有过添加
    public string? OldDepotSectionCode { get; set; }    //旧库位编码
    public string? NewDepotSectionCode { get; set; }    //新库位编码
 
    ProductionPickDto有过添加
    public int? DepotId { get; set; }    //仓库id
     */
 
 
}