啊鑫
2024-08-01 b64efba9b005c074b3963c9d650091c70e46108b
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System.Data;
using MES.Service.DB;
using MES.Service.Dto.service;
using MES.Service.Modes;
using Newtonsoft.Json;
using SqlSugar;
using DbType = System.Data.DbType;
 
namespace MES.Service.service.Warehouse;
 
public class MesInvItemInCDetailsManager : Repository<MesInvItemInCDetails>
{
    //当前类已经继承了 Repository 增、删、查、改的方法
 
    private readonly MessageCenterManager _messageCenterManager = new();
 
    public PurchaseInventory SaveBarCodes(WarehouseQuery entity)
    {
        // 采购入库[FILED3[userName[sectionCode[barcode
        var inputParam = "采购入库[FILED3[" + entity.userName + "[" +
                         entity.sectionCode + "[" + entity.barcode;
 
        // 定义输出参数
        var outputParam = new SugarParameter("c_result", null,
            DbType.String, ParameterDirection.Output,
            4000);
 
        // 使用 SqlSugar 执行存储过程
        Db.Ado.ExecuteCommand(
            "BEGIN prc_rf_pda_scan_in_barcode(:c_in_str, :c_result); END;",
            new SugarParameter("c_in_str", inputParam,
                DbType.String), outputParam);
 
        // 获取输出参数的值
        var resultValue = outputParam.Value?.ToString();
 
// 根据返回值处理逻辑
        if (resultValue.StartsWith("001"))
        {
            // 截取并处理结果
            // 去掉前缀 "001[" 和最后的 "]"
            var content = resultValue.Substring(4).TrimEnd(']');
            // 提取第一个逗号之前的部分
            var parts = content.Split(',');
 
 
            var mesInvItemInCDetails =
                base.GetSingle(it => it.ItemBarcode == entity.barcode);
 
            if (mesInvItemInCDetails == null)
                throw new Exception("出现异常"); // 抛出异常以供前台处理
 
            var itemInId = mesInvItemInCDetails.ItemInId;
            entity.id = itemInId;
            entity.PageIndex = 1;
            entity.Limit = 1;
 
            var inventory = getPurchaseInventory(entity);
            inventory.ItemNo = parts[0];
            inventory.SumQuantity = Convert.ToDecimal(parts[1]);
            return inventory;
        }
 
        if (resultValue.StartsWith("002"))
        {
            // 提取并抛出异常信息
            var errorMessage = resultValue.Substring(4); // 获取 "002" 后面的部分
            throw new Exception(errorMessage); // 抛出异常以供前台处理
        }
 
        throw new Exception("未知错误: " + resultValue);
    }
 
 
    public PurchaseInventory getPurchaseInventory(WarehouseQuery query)
    {
        return new PurchaseInventory
        {
            ItemIns = GetInvItemInsList(query)[0],
            ItemInDetails = GetItemInDetails(query.id),
            InvItemInCDetails = GetInvItemInCDetails(query.id)
        };
    }
 
    public List<MesInvItemIns> GetInvItemInsList(WarehouseQuery query)
    {
        return Db.Queryable<MesInvItemIns, MesDepots, MesSupplier>((a, b, c) =>
                new JoinQueryInfos(JoinType.Left, a.DepotsCode == b.DepotCode
                                                  && b.Factory == a.Factory
                                                  && b.Company == a.Company,
                    JoinType.Left, a.SuppNo == c.SuppNo))
            .WhereIF(query.id > 0, (a, b, c) => a.Id == query.id)
            .Select((a, b, c) => new MesInvItemIns
            {
                Id = a.Id,
                SuppNo = a.SuppNo,
                InsDate = a.InsDate,
                PaperBillNo = a.PaperBillNo,
                Remark = a.Remark,
                DepotsCode = a.DepotsCode,
                CbillNo = a.CbillNo,
                Status = a.Status,
                BillNo = a.BillNo,
                CreateDate = a.CreateDate,
                CreateBy = a.CreateBy,
                DepotName = b.DepotName,
                SuppName = c.SuppName
            }).ToPageList(query.PageIndex, query.Limit);
    }
 
    public List<MesInvItemInCDetails> GetItemInDetails(decimal? pid)
    {
        var result = Db.Queryable<MesInvItemInCDetails, MesItems>(
                (g, c) => new JoinQueryInfos(
                    JoinType.Left,
                    g.ItemNo == c.ItemNo && g.Company == c.Company &&
                    g.Factory == c.Factory
                )
            )
            .WhereIF(pid > 0, (g, c) => g.ItemInId == pid)
            .GroupBy((g, c) => new
            {
                g.Company, g.Factory, g.ItemInId, g.WorkNo, g.ItemNo,
                g.BoardStyle, g.WorkLine, c.ItemName, c.ItemModel, c.ItemUnit
            })
            .Select((g, c) => new MesInvItemInCDetails
            {
                Company = g.Company,
                Factory = g.Factory,
                ItemInId = g.ItemInId,
                WorkNo = g.WorkNo,
                ItemNo = g.ItemNo,
                ItemName = c.ItemName,
                ItemModel = c.ItemModel,
                ItemUnit = c.ItemUnit,
                BoardStyle = g.BoardStyle,
                WorkLine = g.WorkLine,
                SumQuantity = SqlFunc.AggregateSum(g.Quantity) // 聚合计算
            })
            .ToList();
 
        return result;
    }
 
    public List<MesInvItemInCDetails> GetInvItemInCDetails(decimal? pid)
    {
        var result = Db.Queryable<MesInvItemInCDetails, MesItems, MesUnit>(
                (g, c, d) => new JoinQueryInfos(
                    JoinType.Left,
                    g.ItemNo == c.ItemNo && g.Company == c.Company &&
                    g.Factory == c.Factory, JoinType.Inner,
                    d.Id == Convert.ToDecimal(c.ItemUnit)
                )
            ).WhereIF(pid > 0, (g, c, d) => g.ItemInId == pid)
            .Select((g, c, d) => new MesInvItemInCDetails
            {
                Id = g.Id,
                ItemName = c.ItemName,
                ItemModel = c.ItemModel,
                ItemUnit = d.Fname,
                DepotSectionCode = g.DepotSectionCode,
                CbillNo = g.CbillNo,
                ItemBarcode = g.ItemBarcode,
                UrgentFlag = g.UrgentFlag,
                ItemNo = g.ItemNo,
                WorkNo = g.WorkNo,
                Quantity = g.Quantity
            })
            .ToList();
 
        return result;
    }
 
    public MessageCenter MesToErpParam(WarehouseQuery query)
    {
        var erpParameters = "";
        var title = "";
        var tableName = "MES_INV_ITEM_INS_" + query.Type;
        if ("A".Equals(query.Type))
        {
            erpParameters = GetErpParameters(query.billNo);
            title = "采购入库单" + query.billNo + "审核";
        }
        else if ("B".Equals(query.Type))
        {
            erpParameters = GetDeApprovePam(query.id);
            title = "采购入库单" + query.billNo + "反审核";
        }
 
        var message = new MessageCenter
        {
            TableName = tableName,
            Url = "http://192.168.11.120:8098/WebService1.asmx/mesToErpinfo",
            Status = 1,
            CreateBy = query.userName,
            Route = query.billNo,
            Title = title,
            PageName = "Warehouse/PurchaseInventory/Add?id=" + query.id +
                       "&billNo=" + query.billNo,
            CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
            Method = "POST",
            Seq = 1,
            Data = erpParameters,
            ContentType = "application/x-www-form-urlencoded"
        };
        return message;
    }
 
    public MessageCenter SaveMessageCenter(WarehouseQuery query)
    {
        var message = MesToErpParam(query);
 
        var executeReturnIdentity =
            Db.Insertable(message).ExecuteReturnIdentity();
        if (executeReturnIdentity > 0)
        {
            message.Id = executeReturnIdentity;
            message.Pid = executeReturnIdentity;
            return message;
        }
 
        throw new Exception("获取数据失败");
    }
 
    //audit
    public bool audit(WarehouseQuery entity)
    {
        entity.date = DateTime.Now;
        entity.status = 1;
        return Update(entity);
    }
 
    public bool deApprove(WarehouseQuery entity)
    {
        entity.date = null;
        entity.status = 0;
        return Update(entity);
    }
 
    private bool Update(WarehouseQuery entity)
    {
        return Db.Updateable<MesInvItemIns>()
            .SetColumns(x => x.Status == entity.status)
            .SetColumns(x => x.InsDate == entity.date)
            .Where(x => x.BillNo == entity.billNo)
            .ExecuteCommand() > 0;
    }
 
    private string GetErpParameters(string? billNo)
    {
        var invItemIns = Db.Queryable<MesInvItemIns>()
            .Single(x => x.BillNo == billNo);
 
        if (invItemIns == null) throw new Exception("入库单号不存在");
 
        if (invItemIns.Status == 1) throw new Exception("入库单已审核,不能重复推送");
 
        var materials = Db.Queryable<MesInvItemInCDetails, MesItems, MesUnit,
            MesInvItemArnDetail>(
            (g, c, d, a) => new JoinQueryInfos(
                JoinType.Left,
                g.ItemNo == c.ItemNo && g.Company == c.Company &&
                g.Factory == c.Factory, JoinType.Inner,
                d.Id == Convert.ToDecimal(c.ItemUnit),
                JoinType.Inner,
                a.Ebeln == g.WorkNo && a.WorkLine == g.EbelnLineNo
                                    && g.CbillNo == a.CbillNo
            )
        ).Where((g, c, d, a) => g.BillNo == billNo).Select<Material>(
            (g, c, d, a) => new Material
            {
                FstockId = g.DepotCode,
                FuintId = d.Fnumber,
                FsrcEntryId = a.Id,
                FmesEntryId = g.Id,
                FmaterialId = a.ItemNo,
                DepotSectionCode = g.DepotSectionCode,
                WorkNo = g.WorkNo,
                Frealqty = g.Quantity
            }).ToList();
 
 
        if (materials == null || materials.Count == 0)
            throw new Exception("没有找到相关数据"); // 抛出异常以供前台处理
        // 构造 JSON
 
        var jsonEntries = materials.Select(d => new
        {
            FMaterialId = d.FmaterialId,
            FUintId = d.FuintId,
            FRealQty = d.Frealqty,
            FStockId = d.FstockId,
            FSRCENTRYID = d.FsrcEntryId.ToString(),
            F_MES_ENTRYID = d.FmesEntryId.ToString()
        }).ToList();
 
        var fdate = DateTime.Now.ToString("yyyy-MM-dd");
 
        var jsonString = JsonConvert.SerializeObject(jsonEntries);
        var encodedUrl = "taskname=CGRK&mesid=" + invItemIns.Id +
                         "&optype=create&datajson={\"F_MES_ID\":\"" +
                         invItemIns.Id + "\",\"FDate\":\"" + fdate +
                         "\",\"cgrkentry\":" + jsonString + "}";
 
        return encodedUrl;
    }
 
 
    private string GetDeApprovePam(decimal? id)
    {
        var sid = (int)id;
        var encodedUrl = "taskname=CGRK&mesid=" + sid +
                         "&optype=delete&datajson={}";
 
        return encodedUrl;
    }
}