111
啊鑫
3 天以前 96ee6437185cc02f17a8286967fd0f0eb88d9328
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
using System.Globalization;
using Masuit.Tools;
using MES.Service.DB;
using MES.Service.Dto.webApi;
using MES.Service.Modes;
using MES.Service.util;
using SqlSugar;
 
namespace MES.Service.service;
 
public class WomcaaManager : Repository<Womcaa>
{
    //当前类已经继承了 Repository 增、删、查、改的方法
 
    //这里面写的代码不会给覆盖,如果要重新生成请删除 WomcaaManager.cs
 
    private readonly WomcabManager _womcabManager = new();
 
    public bool SaveList(List<ErpWOM> rohIns)
    {
        var result = rohIns.Select(Save).ToList();
        return result.All(b => b);
    }
 
    public bool Save(ErpWOM wom)
    {
        var womErpCaa = wom.ErpCaa;
 
        var mesWomcaa = MapErpCAAtoWomcaa(womErpCaa, wom.ErpCabs[0].FBillNo);
        var mesWomcabs =
            MapErpCABtoWomcab(wom.ErpCabs);
 
        return UseTransaction(db =>
        {
            switch (womErpCaa.Type)
            {
                case "3":
                    return UpdateData(db, mesWomcaa, mesWomcabs) ? 1 : 0;
                case "2":
                case "4":
                //kyy 2024-09-13 变更
                case "5":
                    return SaveOrUpdateData(db, mesWomcaa, mesWomcabs)
                        ? 1
                        : 0;
                default:
                    throw new NotImplementedException(
                        $"type没有{womErpCaa.Type}这个类型");
            }
        }) > 0;
    }
 
    private bool SaveOrUpdateData(SqlSugarScope db, Womcaa mesWomcaa,
        List<Womcab> mesWomcabs)
    {
        if (StringUtil.CheckGuid(mesWomcaa.Guid))
            base.DeleteById(mesWomcaa.Guid);
 
 
        if (mesWomcabs.Count > 0)
            db.Deleteable<Womcab>()
                .Where(s => s.Pid == mesWomcaa.Erpid).ExecuteCommand();
 
        //单条插入忽略空字段
        var orUpdate = db.Insertable(mesWomcaa)
            .IgnoreColumns(true).ExecuteCommand() > 0;
 
        //批量插入忽略空字段
        var baOrUpdate = db.Insertable(mesWomcabs).PageSize(1)
            .IgnoreColumnsNull()
            .ExecuteCommand() > 0;
 
        // if (orUpdate && baOrUpdate) 
        // throw new NotImplementedException("插入或更新失败");
 
        if (orUpdate && baOrUpdate) 
        {
            // 调用存储过程更新用料清单
            db.Ado.ExecuteCommand("exec [dbo].[prc_update_womdab] @outMsg output,@outSum output,@inEdtUserGuid,@inCaaGuid",
                new {
                    outMsg = (string)null,  // 输出参数
                    outSum = (int?)null,     // 输出参数
                    inEdtUserGuid = new Guid("11111111-1111-1111-1111-111111111111"),
                    inCaaGuid = new Guid(mesWomcaa.Guid.ToString())
                });
            return true;
        }
        throw new NotImplementedException("插入或更新失败");
        
 
    }
 
    private bool UpdateData(SqlSugarScope db, Womcaa mesWomcaa,
        List<Womcab> mesWomcabs)
    {
        var decimals = mesWomcabs.Select(s => s.Guid).ToArray();
        var update = base.DeleteById(mesWomcaa.Guid);
        var insertOrUpdate = db
            .Deleteable<Womcab>().In(decimals)
            .ExecuteCommand() > 0;
 
        if (update && insertOrUpdate) return true;
        throw new NotImplementedException("更新失败");
    }
 
    private Womcaa MapErpCAAtoWomcaa(ErpCAA dto,string PPBOMNO)
    {
        var entity = new Womcaa
        {
            Erpid = Convert.ToInt32(dto.Id), ///     ERPID
            Caa001 = PPBOMNO, ///     单号
            Caa021 = dto.FWorkShopID, ///     工作车间
            RoutingId = dto.FRoutingId, ///     工艺路线
            WorkShopId = dto.FREMWorkShopId, ///     产线
            DepotCode = dto.FStockId, ///     仓库
            Caa004 = dto.FBILLTYPE, ///     单据类型
            Caa023 = dto.FStatus, ///     状态 完工,未完工
            Caa006 = dto.FMaterialId, ///     产品编码
            Caa009 = dto.FUnitId, ///     单位
            Caa012 = !string.IsNullOrEmpty(dto.FQty)
                ? Convert.ToDecimal(dto.FQty)
                : null, ///     数量
            WorkGroupId = dto.FWorkGroupId, ///     计划组
            PlanId = dto.FPlannerID, ///     计划员
            Caa010 = dto.FPlanStartDate, ///     预计开工时间
            Caa011 = dto.FPlanFinishDate, ///     预计完工时间
            Caa005 = DateTime.TryParse(dto.FConveyDate, out var conveyDate) 
            ? (conveyDate <= new DateTime(1900, 1, 1) ? null : conveyDate.ToString())
                : null, ///     开单日期
            StockInlimith = dto.FStockInLimitH, ///     入库上限
            StockInlimitl = dto.FStockInLimitL, ///     入库下限
            Mtono = dto.FMTONO, ///     计划跟踪号
            Lot = dto.FLot, ///     批号
            Caa013 = dto.FBomId, ///     BOM版次
 
            CreateType = dto.FCreateType, ///     生成方式
            SrcBillType = dto.FSrcBillType, ///     源单类型
            SrcBillNo = dto.FSrcBillNo, ///     源单编号
            SrcBillentryseq = dto.FSrcBillEntrySeq, ///     源单分录行号
            SaleOrderNo = dto.FSaleOrderNo, ///     需求单据号
            SaleOrderEntryseq = dto.FSaleOrderEntrySeq, ///     需求单据行号
            ForceCloserid = dto.FForceCloserId, ///     结案人
            CloseType = dto.FCloseType, ///     结案类型
            SrcSplitBillno = dto.FSrcSplitBillNo, ///     源拆分订单编号
            Caa016 = dto.FDESCRIPTION, ///     备注
 
            Caa018 = !string.IsNullOrEmpty(dto.PLAN_ID)
                ? Convert.ToInt32(dto.PLAN_ID)
                : null, ///     ERP源单ID
            Caa019 = !string.IsNullOrEmpty(dto.PLAN_SEQ)
                ? Convert.ToInt32(dto.PLAN_SEQ)
                : null, ///     ERP源单行号
            Caa020 = dto.PLAN_NUM, ///     ERP源单单号(任务单号)
            //Caa015 = dto.F_UNW_Text_xsddh, ///     销售订单号
            // ErpSczz = Convert.ToInt32(dto.FPrdOrgId), ///     生产组织ID
            // ErpWtzz = Convert.ToInt32(dto.FEnTrustOrgId), ///     委托组织ID
            // Btbz = dto.Btbz, ///     委托组织ID
            //Caa015Head = dto.F_XIFG_Integer_tzk, ///     销售订单行号
            //MainProductNo = dto.F_XIFG_Base_w5c, ///     主产品编码
            // MainProductQty =
            //     Convert.ToInt32(dto.F_XIFG_Qty_yrr), ///     主产品生产数量
            // CustomerItemNumber = dto.F_XIFG_Text_fg2, ///     客户货号
            // PackingListNumber = dto.F_XIFG_Text_yrr, ///     包装单号
            // CustomerName = dto.F_XIFG_Text_qtr1, ///     客户名称
            // Customer = dto.F_XIFG_Base_83g1, ///     客户
            Caa0111 = dto.FPlanFinishDate, ///     预计完工时间备份
            Typea = dto.TypeA, ///     预计完工时间备份
            //Typeb = dto.TypeB ///     预计完工时间备份
            SynchronousDate = DateTime.Now
 
        };
 
        var single = base.GetSingle(it => it.Erpid == entity.Erpid);
        if (single != null) entity.Guid = single.Guid;
 
        // 查找并设置对应的MES系统ID
        // CAA021 工作车间 -> SYS_DEPARTMENT.id
        if (!string.IsNullOrEmpty(entity.Caa021))
        {
            var department = Db.Queryable<SysDepartment>()
                .Where(d => d.Departmentcode == entity.Caa021)
                .Select(d => d.Id)
                .First();
            entity.Caa021 = department.ToString();
        }
 
        // DEPOT_CODE 仓库 -> MES_DEPOTS.id
        if (!string.IsNullOrEmpty(entity.DepotCode))
        {
            var depot = Db.Queryable<MesDepots>()
                .Where(d => d.DepotCode == entity.DepotCode)
                .Select(d => d.DepotId)
                .First();
            entity.DepotCode = depot.ToString();
        }
 
        // ERP_WTZZ 委托组织 -> 1 (固定值)
        entity.ErpWtzz = 1;
 
        // ERP_SCZZ 生产组织 -> 1 (固定值)
        entity.ErpSczz = 1;
 
        // CAA006 产品编码 -> MES_ITEMS.id
        if (!string.IsNullOrEmpty(entity.Caa006))
        {
            var item = Db.Queryable<MesItems>()
                .Where(i => i.ItemNo == entity.Caa006)
                .Select(i => i.Id)
                .First();
            entity.Caa006 = item.ToString();
        }
 
        // CAA009 单位 -> MES_UNIT.id
        if (!string.IsNullOrEmpty(entity.Caa009))
        {
            var unit = Db.Queryable<MesUnit>()
                .Where(u => u.Fnumber == entity.Caa009)
                .Select(u => u.Id)
                .First();
            entity.Caa009 = unit.ToString();
        }
        
        //CreateDate = dto.FDate, ///     单据日期
        if (!dto.FDate.IsNullOrEmpty())
        {
            if (!DateTime.TryParseExact(dto.FDate, "yyyy-MM-d H:m:s",
                    CultureInfo.InvariantCulture,
                    DateTimeStyles.None, out DateTime createDate))
            {
                throw new FormatException(
                    $"创建日期(FCreateDate)解析失败!值:【{dto.FDate}】,支持格式:yyyy-MM-d H:m:s");
            }
 
            entity.CreateDate = createDate;
        }
        else
        {
            entity.CreateDate = null;
        }
 
        return entity;
    }
 
    private List<Womcab> MapErpCABtoWomcab(List<ErpCAB> dtoList)
    {
        var womcabList = new List<Womcab>();
 
        foreach (var dto in dtoList)
        {
            var womcab = new Womcab
            {
                Erpid = Convert.ToInt32(dto.Id), /// ERPID 
                Cab001 = dto.FBillNo, /// 工单单号 
                Cab002 = Convert.ToInt32(dto.FSeq), /// 序号 
                Cab003 = dto.FMaterialID, /// 材料编码 
                Cab006 = !string.IsNullOrEmpty(dto.FNeedQty)
                    ? Convert.ToDecimal(dto.FNeedQty)
                    : null, /// 需领用量 
                Cab007 = !string.IsNullOrEmpty(dto.FPickedQty)
                    ? Convert.ToDecimal(dto.FPickedQty)
                    : null, /// 已领用量 
                PositionNo = dto.FPositionNO, /// 位置号 
                SupplyOrganization = dto.FChildSupplyOrgId, /// 供应组织 
                IssuingOrganization = dto.FSUPPLYORG, /// 发料组织 
                OwnerId = dto.FOwnerID, /// 货主 
                Mtono = dto.FMTONO, /// 计划跟踪号 
                Lot = dto.FLot, /// 批号 
                DepotCode = dto.FStockID, /// 仓库 
                IssueType = dto.FIssueType, /// 发料方式 
                // Cab008 = dto.F_UNW_Text_tpgy, /// 工艺 
                Cab009 = dto.FUnitID, /// 单位 
                SupplyType = dto.FSupplyType, /// 供应类型(C采购 Z自制 W委外) 
                Cab012 = !string.IsNullOrEmpty(dto.FStdQty)
                    ? Convert.ToDecimal(dto.FStdQty)
                    : null, /// 组成用量 
                Cab014 = dto.FIsKeyItem, /// 是否替料 
                Numerator = dto.FNumerator, /// 分子 
                Denominator = dto.FDenominator, /// 分母 
                Pid = Convert.ToInt32(dto.PID), /// ERP头ID 
                Eid = Convert.ToInt32(dto.Id), /// ERP ID 
 
                Scrapqty = dto.FFixScrapQty, /// 固定损耗
                Scraprate = dto.FScrapRate, /// 变动损耗率
                Freplacegroup = !string.IsNullOrEmpty(dto.FReplaceGroup)
                    ? Convert.ToInt32(dto.FReplaceGroup)
                    : 0,
                UseRate = !string.IsNullOrEmpty(dto.FUseRate)
                    ? Convert.ToDecimal(dto.FUseRate)
                    : 0,
                MaterialType = dto.FMaterialType,
                MustQty = !string.IsNullOrEmpty(dto.FMustQty)
                    ? Convert.ToDecimal(dto.FMustQty)
                    : 0,
                Typeb = dto.TypeB
                // Fsaleorderno = dto.FSALEORDERNO, /// 销售订单 
                //
                // CustomerItemNumber = dto.F_XIFG_Text_re5, /// 客户货号 
                // PackingListNumber = dto.F_XIFG_Text_apv, /// 包装单号 
                // MainProduct = dto.F_XIFG_Base_re5, /// 主产品 
                // MainProductQty =
                //     Convert.ToInt32(dto.F_XIFG_Qty_apv), /// 主产品数量 
                // ParentItemName = dto.F_XIFG_Base_apv /// 父项物料名称 
            };
 
            var entity = Db.Queryable<Womcab>()
                .Where(s => s.Eid == womcab.Eid).Single();
            if (entity != null) womcab.Guid = entity.Guid;
 
            // CAB003 材料编码 => MES_ITEMS.id
            if (!string.IsNullOrEmpty(womcab.Cab003))
            {
                var item = Db.Queryable<MesItems>()
                    .Where(i => i.ItemNo == womcab.Cab003)
                    .Select(i => i.Id)
                    .First();
                if (item != null)
                {
                    womcab.Cab003 = item.ToString();
                }
            }
 
            // CAB009 单位 => MES_UNIT.id
            if (!string.IsNullOrEmpty(womcab.Cab009))
            {
                var unit = Db.Queryable<MesUnit>()
                    .Where(u => u.Fnumber == womcab.Cab009)
                    .Select(u => u.Id)
                    .First();
                if (unit != null)
                {
                    womcab.Cab009 = unit.ToString();
                }
            }
 
            // DEPOT_CODE 仓库 => MES_DEPOTS.DEPOT_ID
            if (!string.IsNullOrEmpty(womcab.DepotCode))
            {
                var depot = Db.Queryable<MesDepots>()
                    .Where(d => d.DepotCode == womcab.DepotCode)
                    .Select(d => d.DepotId)
                    .First();
                if (depot != null)
                {
                    womcab.DepotCode = depot.ToString();
                }
            }
 
            // ISSUING_ORGANIZATION 发料组织 => 1 (固定值)
            womcab.IssuingOrganization = "1";
 
            // SUPPLY_ORGANIZATION 供应组织 => 1 (固定值)
            womcab.SupplyOrganization = "1";
 
            womcabList.Add(womcab);
        }
 
        return womcabList;
    }
}