hao
11 小时以前 161574a0dd7d9e1da6e00c65855a0c8b401c934e
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
using System.Globalization;
using MES.Service.DB;
using MES.Service.Dto.webApi;
using MES.Service.Modes;
using SqlSugar;
 
namespace MES.Service.service.BasicData.Production;
 
public class ProductionOrderManager : Repository<Womcaa>
{
    //当前类已经继承了 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, Womcaa womcaaWW,
        List<Womcab> mesRohInDatas)
    {
        var decimals = mesRohInDatas.Select(s => s.Id).ToArray();
 
        var update = true;
        if (womcaaWW != null)
        {
            if (womcaaWW.Id != null)
            {
                update = base.DeleteById(womcaaWW.Id);
            }
        }
        var insertOrUpdate = true;
        if (decimals != null || decimals.Length > 0)
        {
            insertOrUpdate = db.Deleteable<Womcab>().In(decimals).ExecuteCommand() > 0;
        }
 
        if (update && insertOrUpdate)
        {
            return true;
        }
        throw new NotImplementedException("更新失败");
    }
 
    // 插入或更新数据的方法
    private bool SaveOrUpdateData(SqlSugarScope db, Womcaa mesWomcaa,
        List<Womcab> mesWomcabss, string type)
    {
        //if (StringUtil.CheckGuid(mesRohIn.Guid))
        //    db.Deleteable<ProductionOrder>()
        //        .Where(s => s.Guid == mesRohIn.Guid)
        //        .ExecuteCommand();
 
        if (mesWomcaa.Id != null) base.DeleteById(mesWomcaa.Id);
 
        if (mesWomcabss.Count > 0)
            db.Deleteable<Womcab>()
                .Where(s => s.Eid == mesWomcaa.Erpid).ExecuteCommand();
 
        var orUpdate = db.Insertable(mesWomcaa)
            .IgnoreColumns(true).ExecuteCommand() > 0;
 
 
        var baOrUpdate = db.Insertable(mesWomcabss).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 Womcaa 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 entity = new Womcaa
        {
            Caa001 = erpDto.FBillNo, ///     单号
            //Caa001 = erpDto.FBillNo,
            DepotCode = erpDto.FStockID,
            CreateDate = erpDto.FDate, // ParseDateTime(erpDto.FDate) ?? null,
            Caa004 = erpDto.FBillType,
            Caa023 = erpDto.FStatus,
            Caa006 = erpDto.FMaterialId,
            Caa009 = erpDto.FUnitID,
            Caa012 = Convert.ToDecimal(erpDto.FQty),
            WorkGroupId = erpDto.FWorkGroupId,
            PlanId = erpDto.FPlannerID,
            Caa010 = erpDto.FPlanStartDate,
            //ParseDateTime(erpDto.FPlanStartDate) ?? null,
            Caa011 = erpDto.FPlanFinishDate,
            //ParseDateTime(erpDto.FPlanFinishDate) ?? null,
 
            Planconfirmation = erpDto.PlanConfirmation,
            StockInlimith =
                erpDto
                    .FStockInLimitH, //Convert.ToDecimal(erpDto.FStockInLimitH),
            StockInlimitl =
                erpDto
                    .FStockInLimitL, //Convert.ToDecimal(erpDto.FStockInLimitL),
            Mtono = erpDto.FMTONO,
            Lot = erpDto.FLot,
            Caa013 = erpDto.FBomId,
            //SalesOrderNo = erpDto.F_UNW_XSDDH,
            CreateType = erpDto.FCreateType,
            Fsubid = erpDto.FSUBID,
            Fsubbillnoseq = erpDto.FSUBBILLNOSEQ,
            Fsubbillno = erpDto.FSUBBILLNO,
            Caa020 = erpDto.FSUBBILLNO,
            SrcBillType = erpDto.FSrcBillType,
            SrcBillNo = erpDto.FSrcBillNo,
            SrcBillentryseq = erpDto.FSrcBillEntrySeq,
            Freqsrc = erpDto.FReqSrc,
            SaleOrderNo = erpDto.FSALEORDERNO,
            SaleOrderEntryseq = erpDto.FSaleOrderEntrySeq,
            ForceCloserid = erpDto.FFORCECLOSERID,
            CloseType = erpDto.FCloseType,
            SrcSplitBillno = erpDto.FDescription,
            Srcsplitseq = erpDto.SrcSplitSeq,
            Erpid = Convert.ToDecimal(erpDto.FID),
            Fsubentryid = Convert.ToDecimal(erpDto.FSUBENTRYID),
            Fpurorderno = erpDto.FPurOrderNo,
            Fpurorderentryseq =
                erpDto
                    .FPurOrderEntrySeq, //Convert.ToInt32(erpDto.FPurOrderEntrySeq),
            Fbasestockinqty =  erpDto.FBaseStockInQty,
            Fbasenostockinqty =  erpDto.FBaseNoStockInQty,
            Finstockownerid = erpDto.FInStockOwnerId,
            //PlanConfirmation = erpDto.PlanConfirmation != null
            //    ? DateTime.ParseExact(erpDto.PlanConfirmation,
            //        "yyyy-MM-dd HH:mm:ss", null)
            //    : null,
            Typea = erpDto.TypeA,
          //  F_WWDD_SCTZD = erpDto.F_WWDD_SCTZD,
          //  F_WWDD_XH = erpDto.F_WWDD_XH,
            //F_XIFG_TEXT_TZK = erpDto.F_XIFG_Text_tzk
            WwOrg=erpDto.WW_ZZ,
            CgOrg=erpDto.CG_ZZ,
            SuppId=erpDto.FSUPPLIERID,
 
        };
 
        var single = base.GetSingle(it => it.Erpid == entity.Erpid);
        if (single != null) entity.Id = single.Id;
 
        return entity;
    }
 
    private List<Womcab> ConvertErpToProductionOrderSub(
        List<ErpProductionOrderSubDto> erpDtoList)
    {
        var productionOrderSubList =
            new List<Womcab>();
 
        foreach (var erpDto in erpDtoList)
        {
            var productionOrderSub = new Womcab
            {
                Cab001 = erpDto.FBillNo,
                Cab002 = erpDto.FSEQ, //Convert.ToInt32(erpDto.FSEQ),
                Cab003 = erpDto.FMaterialID2,
                MustQty = Convert.ToDecimal(erpDto.FMustQty),
                Cab006 = Convert.ToDecimal(erpDto.FMustQty),
                Cab007 = Convert.ToDecimal(erpDto.FPickedQty),
                PositionNo = erpDto.FPositionNO,
                OwnerId = erpDto.FOwnerID,
                Mtono = erpDto.FMTONO,
                Lot = erpDto.FLot,
                DepotCode = erpDto.FStockID,
                IssueType = erpDto.FIssueType,
                //Unit = erpDto.FUnitID2,
                SupplyType = erpDto.FSupplyType,
 
 
                Numerator = Convert.ToDecimal(erpDto.FNumerator),
                Denominator = Convert.ToDecimal(erpDto.FDenominator),
                Erpid = Convert.ToDecimal(erpDto.FPPOMENTRYID),
                Eid = Convert.ToDecimal(erpDto.FPPOMID),
                Scrapqty = Convert.ToDecimal(erpDto.FFixScrapQty),
                Scraprate = Convert.ToDecimal(erpDto.FScrapRate),
                MaterialType = erpDto.FMaterialType,
                //Unit2 = erpDto.FUnitID3,
                Freplacegroup = Convert.ToInt32(erpDto.FReplaceGroup),
                //OwnerType = erpDto.FOwnerTypeId,
                //Owner = erpDto.FOwnerID2,
 
                Fisgetscrap = erpDto.FISGETSCRAP,
                Fiskeycomponent = erpDto.FISKEYCOMPONENT,
                Fsrctransorgid = erpDto.FSRCTRANSORGID,
                Fsrctransstockid = erpDto.FSRCTRANSSTOCKID,
                Fstockstatusid = erpDto.FSTOCKSTATUSID,
                Fneeddate = erpDto.FNEEDDATE != null
                    ? DateTime.ParseExact(erpDto.FNEEDDATE,
                        "yyyy-MM-dd HH:mm:ss", null)
                    : null,
                Freservetype = erpDto.FRESERVETYPE,
                Fmemo = erpDto.FMEMO,
                Typeb = erpDto.TypeB,
                ErpProductionEntryCode = erpDto.FSUBENTRYID,
             //   FOWNERTYPEID = erpDto.FOwnerTypeId,
               // F_XIFG_BASE_RE5 = erpDto.F_XIFG_Base_re5,
               // FUNITID2 = erpDto.FUnitID2,
                //FUNITID3 = erpDto.FUnitID3
            };
 
            var single =
                base.GetSingle(it => it.Erpid == productionOrderSub.Erpid);
            if (single != null) productionOrderSub.Id = single.Id;
 
            productionOrderSubList.Add(productionOrderSub);
        }
 
        return productionOrderSubList;
    }
}