111
tjx
2025-11-26 949f77af1da12b69fc4d3b273062abd0b2b2c7c0
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
using System.Globalization;
using MES.Service.DB;
using MES.Service.Dto.webApi;
using MES.Service.Modes;
using MES.Service.util;
using SqlSugar;
 
namespace MES.Service.service.BasicData;
 
public class ProductionOrderManager : Repository<ProductionOrder>
{
    //当前类已经继承了 Repository 增、删、查、改的方法
 
    private readonly ProductionOrderSubManager _productionOrderSubManager =
        new();
 
    private string ORDERNO = "";
 
 
    //ErpWYOrder
    public bool Save(ErpWYOrder wyOrder)
    {
        var erpProductionOrderDto = wyOrder.OrderDto;
        var mesRohIn = ConvertErpToProductionOrder(erpProductionOrderDto,
            wyOrder.Items[0].FBILLNO);
        var mesRohInDatas =
            ConvertErpToProductionOrderSub(wyOrder.Items);
 
        return UseTransaction(db =>
        {
            switch (erpProductionOrderDto.Type)
            {
                // case "2":
                //     return InsertData(db, mesRohIn, mesRohInDatas,
                //         rohInErpRohIn.FBILLTYPE)
                //         ? 1
                //         : 0;
                case "3":
                    return UpdateData(db, wyOrder.Items[0].FBILLNO) ? 1 : 0;
                case "2":
                case "4":
                    return SaveOrUpdateData(db, mesRohIn, mesRohInDatas,
                        erpProductionOrderDto.Type)
                        ? 1
                        : 0;
                default:
                    throw new NotImplementedException(
                        $"type没有{erpProductionOrderDto.Type}这个类型");
            }
        }) > 0;
    }
 
    private bool UpdateData(SqlSugarScope db, string FBILLNO)
    {
        // var decimals = mesRohInDatas.Select(s => s.Guid).ToArray();
 
        var orderNoList = db.Queryable<ProductionOrder>()
            .Where(s => s.OrderNo.Contains(FBILLNO))
            .Select(s => s.OrderNo)
            .ToList();
 
        var update = db.Deleteable<ProductionOrder>()
            .Where(s => orderNoList.Contains(s.OrderNo))
            .ExecuteCommand() > 0;
 
        //s.WorkOrderNo
        var insertOrUpdate = db
            .Deleteable<ProductionOrderSub>()
            .Where(s => orderNoList.Contains(s.WorkOrderNo))
            .ExecuteCommand() > 0;
 
        if (update && insertOrUpdate) return true;
        throw new NotImplementedException("更新失败");
    }
 
    // 插入或更新数据的方法
    private bool SaveOrUpdateData(SqlSugarScope db, ProductionOrder mesRohIn,
        List<ProductionOrderSub> mesRohInDatas, string type)
    {
        if (StringUtil.CheckGuid(mesRohIn.Guid))
            db.Deleteable<ProductionOrder>()
                .Where(s => s.OrderNo == mesRohIn.OrderNo)
                .ExecuteCommand();
 
        if (mesRohInDatas.Count > 0)
            db.Deleteable<ProductionOrderSub>()
                .Where(s =>
                    s.WorkOrderNo == mesRohIn.OrderNo)
                .ExecuteCommand();
 
        var orUpdate = db.Insertable(mesRohIn)
            .IgnoreColumns(true).ExecuteCommand() > 0;
 
 
        var baOrUpdate = db.Insertable(mesRohInDatas).PageSize(1)
            .IgnoreColumnsNull()
            .ExecuteCommand() > 0;
 
        if (orUpdate && baOrUpdate) return true;
 
        throw new NotImplementedException("插入或更新失败");
    }
 
    // 批量保存记录的方法
    public bool SaveList(List<ErpWYOrder> rohIns)
    {
        var result = rohIns.Select(Save).ToList();
        return result.All(b => b);
    }
 
    private ProductionOrder ConvertErpToProductionOrder(
        ErpProductionOrderDto erpDto, string SUBBOM)
    {
        DateTime parsedDate;
 
        // 时间格式转换函数,ERP时间格式可能为 "yyyy-MM-dd" 或 "yyyy-M-d"
        DateTime? ParseDateTime(string dateStr)
        {
            if (string.IsNullOrEmpty(dateStr)) return null;
 
            // 尝试解析多种日期格式,支持不补零的月份和日期
            if (DateTime.TryParse(dateStr, out DateTime result))
            {
                return result;
            }
 
            return null;
        }
 
        var productionOrder = new ProductionOrder
        {
            OrderNo = SUBBOM,
 
            OrderDate = ParseDateTime(erpDto.FDate) ?? null,
            OrderType = erpDto.FBillType,
            BusinessStatus = erpDto.FStatus,
 
            WorkOrderQty = Convert.ToDecimal(erpDto.FQty),
            PlanningGroup = erpDto.FWorkGroupId,
            Planner = erpDto.FPlannerID,
            EstimatedStartTime =
                ParseDateTime(erpDto.FPlanStartDate) ?? null,
            EstimatedEndTime =
                ParseDateTime(erpDto.FPlanFinishDate) ?? null,
            StorageUpperLimit = Convert.ToDecimal(erpDto.FStockInLimitH),
            StorageLowerLimit = Convert.ToDecimal(erpDto.FStockInLimitL),
            TrackingNo = erpDto.FMTONO,
            BatchNo = erpDto.FLot,
            BomVersion = erpDto.FBomId,
            SalesOrderNo = erpDto.F_UNW_XSDDH,
            GenerationMethod = erpDto.FCreateType,
            ErpProductionOrderId = erpDto.FSUBID,
            ErpProductionOrderLineNo = erpDto.FSUBBILLNOSEQ,
            ErpProductionOrderNo = erpDto.FSUBBILLNO,
 
            SourceOrderNo = erpDto.FSrcBillNo,
            SourceOrderEntryNo = erpDto.FSrcBillEntrySeq,
            DemandOrderNo = erpDto.FSALEORDERNO,
            DemandOrderLineNo = erpDto.FSaleOrderEntrySeq,
            ClosingPerson = erpDto.FFORCECLOSERID,
            ClosingType = erpDto.FCloseType,
            Remarks = erpDto.FDescription,
            ErpId = erpDto.FID,
            // ErpProductionEntryCode = erpDto.FSUBENTRYID,
            ErpProductionEntryCode = SUBBOM,
            Purchaseorderno = erpDto.FPurOrderNo,
            Purchaseorderentryseq = Convert.ToInt32(erpDto.FPurOrderEntrySeq),
            Stockinqty = Convert.ToDecimal(erpDto.FBaseStockInQty),
            Nostockinqty = Convert.ToDecimal(erpDto.FBaseNoStockInQty),
            // Stockowner = erpDto.FInStockOwnerId,
            Stockowner = "1",
            PlanConfirmation = !string.IsNullOrEmpty(erpDto.PlanConfirmation)
                ? (DateTime.TryParse(erpDto.PlanConfirmation, out DateTime planDate) ? planDate : (DateTime?)null)
                : null,
            ReqSrc = erpDto.FReqSrc,
            SrcSplitSeq = erpDto.SrcSplitSeq,
            Typea = erpDto.TypeA,
        };
 
        productionOrder.SourceOrderType = erpDto.FSrcBillType switch
        {
            //SourceOrderType = erpDto.FSrcBillType,
            "0" => "无来源",
            "1" => "销售订单行号",
            "2" => "预测",
            "3" => "出口订单行号",
            "4" => "需求分类代号",
            "5" => "销售订单号",
            "6" => "出口订单号",
            "7" or "8" => "生产订单",
            _ => "委外订单"
        };
 
        //Unit = ,
        // CAA009 单位 -> MES_UNIT.id
        if (!string.IsNullOrEmpty(erpDto.FUnitID))
        {
            var unit = Db.Queryable<MesUnit>()
                .Where(u => u.Fnumber == erpDto.FUnitID)
                .Select(u => u.Id)
                .First();
            productionOrder.Unit = unit.ToString();
        }
 
        // CAA006 产品编码 -> MES_ITEMS.id
        if (!string.IsNullOrEmpty(erpDto.FMaterialId))
        {
            var item = Db.Queryable<MesItems>()
                .Where(i => i.ItemNo == erpDto.FMaterialId)
                .Select(i => i.Id)
                .First();
            productionOrder.ProductCode = item.ToString();
        }
 
        // Warehouse = erpDto.FStockID,
        if (!string.IsNullOrEmpty(erpDto.FStockID))
        {
            var depot = Db.Queryable<MesDepots>()
                .Where(d => d.DepotCode == erpDto.FStockID)
                .Select(d => d.DepotId)
                .First();
            productionOrder.Warehouse = depot.ToString();
        }
 
        var single = base.GetSingle(it =>
            it.OrderNo == erpDto.FBillNo);
        if (single != null) productionOrder.Guid = single.Guid;
 
        return productionOrder;
    }
 
    private List<ProductionOrderSub> ConvertErpToProductionOrderSub(
        List<ErpProductionOrderSubDto> erpDtoList)
    {
        var productionOrderSubList =
            new List<ProductionOrderSub>();
 
        foreach (var erpDto in erpDtoList)
        {
            var productionOrderSub = new ProductionOrderSub
            {
                SequenceNo = Convert.ToInt32(erpDto.FSEQ),
                WorkOrderNo = erpDto.FBILLNO,
                RequiredQty = Convert.ToDecimal(erpDto.FMustQty),
                IssuedQty = Convert.ToDecimal(erpDto.FPickedQty),
                LocationNo = erpDto.FPositionNO,
                // StockOwner = erpDto.FOwnerID,
                StockOwner = "1",
                TrackingNo = erpDto.FMTONO,
                BatchNo = erpDto.FLot,
 
                IssuingMethod = erpDto.FIssueType,
                Unit = erpDto.FUnitID2,
                Unit2 = erpDto.FUnitID3,
                SupplyingType = erpDto.FSupplyType,
                Numerator = Convert.ToDecimal(erpDto.FNumerator),
                Denominator = Convert.ToDecimal(erpDto.FDenominator),
                ErpId = erpDto.FPPOMENTRYID,
                ErpHeaderId = erpDto.FPPOMID,
                FixedLoss = Convert.ToDecimal(erpDto.FFixScrapQty),
                VariableLossRate = Convert.ToDecimal(erpDto.FScrapRate),
                SubItemType = erpDto.FMaterialType,
                ItemNo = erpDto.FReplaceGroup,
                OwnerType = erpDto.FOwnerTypeId,
                // Owner = erpDto.FOwnerID2,
                Owner = "1",
                Fisgetscrap = erpDto.FISGETSCRAP,
                Fiskeycomponent = erpDto.FISKEYCOMPONENT,
                // Fsrctransorgid = erpDto.FSRCTRANSORGID,
                Fsrctransorgid = "1",
                Fsrctransstockid = erpDto.FSRCTRANSSTOCKID,
                Fstockstatusid = erpDto.FSTOCKSTATUSID,
                Fneeddate = erpDto.FNEEDDATE != null
                    ? DateTime.Parse(erpDto.FNEEDDATE)
                    : null,
                Freservetype = erpDto.FRESERVETYPE,
                Fmemo = erpDto.FMEMO,
                Typeb = erpDto.TypeB,
                // ErpProductionEntryCode = erpDto.FSUBENTRYID
                ErpProductionEntryCode = erpDto.FBILLNO
            };
 
            var single = _productionOrderSubManager.GetSingle(it =>
                it.ErpId == productionOrderSub.ErpId);
            if (single != null) productionOrderSub.Guid = single.Guid;
 
            if (!string.IsNullOrEmpty(erpDto.FMaterialID2))
            {
                var item = Db.Queryable<MesItems>()
                    .Where(i => i.ItemNo == erpDto.FMaterialID2)
                    .Select(i => i.Id)
                    .First();
                if (item != null)
                {
                    productionOrderSub.MaterialCode = item.ToString();
                }
            }
 
            if (!string.IsNullOrEmpty(erpDto.FStockID))
            {
                var item = Db.Queryable<MesDepots>()
                    .Where(i => i.DepotCode == erpDto.FStockID)
                    .Select(i => i.DepotId)
                    .First();
                if (item != null)
                {
                    productionOrderSub.Warehouse = item.ToString();
                }
            }
 
            productionOrderSubList.Add(productionOrderSub);
        }
 
        return productionOrderSubList;
    }
}