啊鑫
2 天以前 00a72fff219241eb5b0405a066cc859d07cf7735
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
220
221
222
223
using Masuit.Tools;
using NewPdaSqlServer.DB;
using NewPdaSqlServer.Dto.service;
using NewPdaSqlServer.entity;
using NewPdaSqlServer.entity.Base;
using NewPdaSqlServer.util;
using SqlSugar;
using System.Data;
using System.Data.SqlClient;
using System.Security.Cryptography;
using DbHelperSQL = NewPdaSqlServer.DB.DbHelperSQL;
 
namespace NewPdaSqlServer.service.Warehouse;
 
/// <summary>
///     MES物料表管理类
/// </summary>
public class MesItemTblManager : Repository<MesItemTbl>
{
    /// <summary>
    ///     获取生产退料单号列表
    /// </summary>
    /// <returns>退料单号列表</returns>
    public dynamic GetSCTLBillNo(WarehouseQuery query, dynamic RequestInfo)
    {
        var orgId = RequestInfo.OrgId;
 
        if (orgId == null)
            throw new Exception("组织不存在!");
 
        // 获取未完成的退料单号列表
        var parameters = new[]
        {
        new SugarParameter("@pi_orgId", orgId),
        new SugarParameter("@type", query.Type),
        new SugarParameter("@inP1", null),
        new SugarParameter("@inP2", null),
        new SugarParameter("@inP3", null),
        new SugarParameter("@inP4", null)
        };
        try
        {
            // 返回单号字符串列表
            var blDetails = Db.Ado.SqlQuery<string>(
                "EXEC prc_pda_sctl_list @pi_orgId,@type,@inP1,@inP2,@inP3,@inP4", parameters);
            return blDetails;
        }
        catch (Exception ex)
        {
            throw new Exception($"{ex.Message}");
        }
    }
 
    /// <summary>
    ///     根据单号获取MES物料表明细
    /// </summary>
    /// <param name="query">仓库查询参数</param>
    /// <returns>物料明细列表</returns>
    public dynamic GetMesItemTblDetailByBillNo(dynamic query, dynamic RequestInfo)
    {
        if (string.IsNullOrEmpty(query.billNo))
            throw new Exception("请选单据号!");
 
        if (query == null)
            throw new ArgumentNullException(nameof(query), "参数对象不能为null");
 
        if (string.IsNullOrEmpty(query.billNo?.ToString()))
            throw new ArgumentException("单据号不能为空", nameof(query.billNo));
 
        var orgId = RequestInfo.OrgId;
 
        if (orgId == null)
            throw new Exception("组织不存在!");
 
        // 获取未完成的销售退货明细
        var parameters = new[]
{
        new SugarParameter("@billNo", query.billNo),
        new SugarParameter("@pi_orgId",orgId),
        new SugarParameter("@inP1", null),
        new SugarParameter("@inP2", null),
        new SugarParameter("@inP3", null),
        new SugarParameter("@inP4", null)
    };
        try
        {
            List<dynamic>? blDetails = Db.Ado.SqlQuery<dynamic>(
                "EXEC prc_pda_sctl_detailList @billNo,@pi_orgId,@inP1,@inP2,@inP3,@inP4", parameters);
            var items = blDetails.Where(x => x.DSQty > 0).ToList();    // 待扫物料
            var ysitems = blDetails.Where(x => x.SQty > 0).ToList();   // 已扫物料
            return new
            {
                items = items,
                ysitems = ysitems,
                Count = items.Count + ysitems.Count
            };
        }
        catch (Exception ex)
        {
            // 保异常处理逻辑
            throw new Exception($"{ex.Message}");
        }
    }
 
    /// <summary>
    ///     生产退料扫描库位
    /// </summary>
    /// <param name="query">仓库查询参数</param>
    /// <returns>库位信息</returns>
    public WarehouseQuery SctlScanDepots(WarehouseQuery query)
    {
        if (query.DepotCode.IsNullOrEmpty())
            throw new NullReferenceException("请扫库位条码");
 
        // 关联查询库位分区和库位信息
        var warehouseQuery = Db.Queryable<MesDepotSections, MesDepots>((a, b) =>
                new JoinQueryInfos(JoinType.Inner, a.DepotGuid == b.Guid))
            .Where((a, b) => a.DepotSectionCode == query.DepotCode)
            .Select<WarehouseQuery>((a, b) => new WarehouseQuery
            {
                DepotSectionName = a.DepotSectionName,
                DepotName = b.DepotName,
                DepotCode = b.DepotCode
            }).First();
 
        if (warehouseQuery == null)
            throw new Exception("库位编码" + query.DepotCode + " 不存在,请确认!");
 
        return warehouseQuery;
    }
 
    /// <summary>
    ///     生产退料扫描条码
    /// </summary>
    /// <param name="query">仓库查询参数</param>
    /// <returns>处理结果</returns>
    public dynamic SctlScanBarcode(WarehouseQuery query,dynamic RequestInfo)
    {
        // 验证条码信息
        var barcode = Db.Queryable<MesInvItemBarcodes>()
            .Where(x => x.ItemBarcode == query.barcode)
            .First();
        if (barcode == null)
            throw new Exception($"无此条码,请核对!{query.barcode}");
 
        // 验证物料信息
        var item = Db.Queryable<MesItems>()
            .Where(x => x.Id == barcode.ItemId)
            .First();
        if (item == null)
            throw new Exception($"无此物料,请核对!{query.barcode}");
 
        // 验证退料单信息
        var returnOrder = Db.Queryable<MesItemTbl>()
            .Where(x => x.BillNo == query.billNo && (x.Tbl013 ?? 0) == 1)
            .First();
        if (returnOrder == null)
            throw new Exception("申请单已撤回,无法扫码!");
 
        if (returnOrder.Tbl020 == 1)
            throw new Exception("扫码完成,申请单已完结!");
 
        // 验证组织是否存在
        var orgId = RequestInfo.OrgId;
 
        if (orgId == null)
            throw new Exception("组织不存在!");
 
        // 生产退料
        using (var conn = new SqlConnection(DbHelperSQL.strConn))
        using (var cmd = new SqlCommand("PRC_PDA_SCTL", conn))
        {
            try
            {
                conn.Open();
                cmd.CommandType = CommandType.StoredProcedure;
 
                var parameters = new[]
                {
                    new SqlParameter("@PI_USER", query.userName),
                    new SqlParameter("@PI_BARCODE", query.barcode),
                    new SqlParameter("@PI_BILL_NO", query.billNo),
                    new SqlParameter("@PI_SECTION_CODE", query.DepotCode),
                    new SqlParameter("@pi_orgId", orgId),
                    new SqlParameter("@PI_INP1", DBNull.Value),
                    new SqlParameter("@PI_INP2", DBNull.Value),
                    new SqlParameter("@PO_OUT_MSG", SqlDbType.NVarChar) { Direction = ParameterDirection.Output, Size = 2000 },
                    new SqlParameter("@PO_OUT_SUM", SqlDbType.Int) { Direction = ParameterDirection.Output },
                    new SqlParameter("@PO_ITEM_NO", SqlDbType.NVarChar) { Direction = ParameterDirection.Output, Size = 100 },
                    new SqlParameter("@PO_QUANTITY", SqlDbType.Decimal)
                    {
                        Direction = ParameterDirection.Output,
                        Precision = 18,   // 根据实际数据库精度调整
                        Scale = 10         // 根据实际数据库小数位调整
                    }
                };
 
                cmd.Parameters.AddRange(parameters);
                cmd.ExecuteNonQuery();
 
                var outMsg = parameters[7].Value?.ToString() ?? string.Empty;
                var outSum = parameters[8].Value != DBNull.Value ? Convert.ToInt32(parameters[8].Value) : -1;
                var itemNo = parameters[9].Value?.ToString() ?? string.Empty;
                var quantity = parameters[10].Value != DBNull.Value ? Convert.ToDecimal(parameters[10].Value) : 0m;
 
                if (outSum == -1) throw new Exception(outMsg);
 
                query.itemNo = itemNo;
                query.Num = quantity;
                query.message = outMsg;
                return query;
            }
            catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    conn.Close();
                }
        }
    }
}