啊鑫
2024-11-20 fcd41558f0e07ea4c082a1b5e073686d91e09505
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
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();
 
 
    //ErpWYOrder
    public bool Save(ErpWYOrder wyOrder)
    {
        var erpProductionOrderDto = wyOrder.OrderDto;
        var mesRohIn = ConvertErpToProductionOrder(erpProductionOrderDto);
        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, mesRohIn, mesRohInDatas) ? 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, ProductionOrder mesRohIn,
        List<ProductionOrderSub> mesRohInDatas)
    {
        var decimals = mesRohInDatas.Select(s => s.Guid).ToArray();
        var update = db.Deleteable<ProductionOrder>()
            .Where(s => s.Guid == mesRohIn.Guid)
            .ExecuteCommand() > 0;
 
        var insertOrUpdate = db
            .Deleteable<ProductionOrderSub>()
            .Where(s => decimals.Contains(s.Guid))
            .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.Guid == mesRohIn.Guid)
                .ExecuteCommand();
 
        if (mesRohInDatas.Count > 0)
            db.Deleteable<ProductionOrderSub>()
                .Where(s => s.ErpHeaderId == mesRohIn.ErpId).ExecuteCommand();
 
        var orUpdate = base.Insert(mesRohIn);
        var baOrUpdate = _productionOrderSubManager.InsertRange(mesRohInDatas);
        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)
    {
        DateTime parsedDate;
 
        // 时间格式转换函数,ERP时间格式为 "yyyy-MM-dd HH:mm:ss.fff"
        DateTime? ParseDateTime(string dateStr)
        {
            if (DateTime.TryParseExact(dateStr, "yyyy-MM-dd HH:mm:ss.fff",
                    CultureInfo.InvariantCulture,
                    DateTimeStyles.None,
                    out parsedDate))
                return parsedDate;
 
            return null; // 如果转换失败,返回null
        }
 
        var productionOrder = new ProductionOrder
        {
            OrderNo = erpDto.FBillNo,
            Warehouse = erpDto.FStockID,
            OrderDate = ParseDateTime(erpDto.FDate) ?? null,
            OrderType = erpDto.FBillType,
            BusinessStatus = erpDto.FStatus,
            ProductCode = erpDto.FMaterialId,
            Unit = erpDto.FUnitID,
            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,
            SourceOrderType = erpDto.FSrcBillType,
            SourceOrderNo = erpDto.FSrcBillNo,
            SourceOrderEntryNo = erpDto.FSrcBillEntrySeq,
            DemandOrderNo = erpDto.FSALEORDERNO,
            DemandOrderLineNo = erpDto.FSaleOrderEntrySeq,
            ClosingPerson = erpDto.FFORCECLOSERID,
            ClosingType = erpDto.FCloseType,
            Remarks = erpDto.FDescription,
            ErpId = erpDto.FPPOMID,
            ErpProductionEntryCode = erpDto.FSUBENTRYID,
            Purchaseorderno = erpDto.FPurOrderNo,
            Purchaseorderentryseq = Convert.ToInt32(erpDto.FPurOrderEntrySeq),
            Stockinqty = Convert.ToDecimal(erpDto.FBaseStockInQty),
            Nostockinqty = Convert.ToDecimal(erpDto.FBaseNoStockInQty),
            Stockowner = erpDto.FInStockOwnerId
        };
 
        var single = base.GetSingle(it => it.ErpId == erpDto.FPPOMID);
        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),
                MaterialCode = erpDto.FMaterialID2,
                RequiredQty = Convert.ToDecimal(erpDto.FMustQty),
                IssuedQty = Convert.ToDecimal(erpDto.FPickedQty),
                LocationNo = erpDto.FPositionNO,
                StockOwner = erpDto.FOwnerID,
                TrackingNo = erpDto.FMTONO,
                BatchNo = erpDto.FLot,
                Warehouse = erpDto.FStockID,
                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,
                ErpProductionEntryCode = erpDto.FSUBENTRYID
            };
 
            var single = _productionOrderSubManager.GetSingle(it =>
                it.ErpId == productionOrderSub.ErpId);
            if (single != null) productionOrderSub.Guid = single.Guid;
 
            productionOrderSubList.Add(productionOrderSub);
        }
 
        return productionOrderSubList;
    }
}