cnf
2025-09-16 934c57635b05680e9ce3fca53d109a77e8da5b9a
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
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using MES.Service.DB;
using MES.Service.Dto.webApi;
using MES.Service.Modes;
 
namespace MES.Service.service
{
    public class ZzcxdManager : Repository<Zzcxa>
    {
        public bool SaveList(List<ERPZZCXD> list)
        {
            var result = list.Select(Save).ToList();
            return result.All(b => b);
        }
 
        public bool Save(ERPZZCXD data)
        {
            var head = MapErpToMesHead(data.ERPZZCXA);
            var details = MapErpDbckbToDbckb(data.ERPZZCXB);
 
            return UseTransaction(db =>
            {
                switch (data.ERPZZCXA.type)
                {
                    case "3":
                        return UpdateData(db, head, details) ? 1 : 0;
                    case "2":
                    case "4":
                    case "5":
                        return SaveOrUpdateData(db, head, details) ? 1 : 0;
                    default:
                        throw new NotImplementedException($"type没有 {data.ERPZZCXA.type} 这个类型");
                }
            }) > 0;
        }
 
        private Zzcxa MapErpToMesHead(ERPZZCXA dto)
        {
            var entity = new Zzcxa
            {
                BillNo = dto.bill_no,          // 单据编码
                DjLx = dto.dj_lx,              // 单据类型
                KcZz = dto.kc_zz,              // 库存组织
                SwLx = dto.sw_lx,              // 事务类型
                Time =  dto.time , // 日期
                Bm = dto.bm,                   // 部门
                Cgy = dto.cgy,                 // 仓管员
                Kcz = dto.kcz,                 // 库存组
                DjZt = dto.dj_zt,              // 单据状态
                Memo = dto.memo,               // 备注
                Cphzlx = dto.cphzlx,           // 成品货主类型
                Cphz = dto.cphz,               // 成品货主
                Zjhzlx = dto.zjhzlx,           // 子件货主类型
                Zjhz = dto.zjhz,               // 子件货主
                ItemId = string.IsNullOrEmpty(dto.item_id) ? null : Convert.ToDecimal(dto.item_id), // 物料编码id
                Qty = string.IsNullOrEmpty(dto.qty) ? null : Convert.ToDecimal(dto.qty),            // 数量
                Unit = dto.unit,               // 单位
                DepotId = dto.depot_id,        // 仓库
                Erpid = dto.erpid              // ERP 主表id
            };
 
            var single = Db.Queryable<Zzcxa>()
                           .Where(it => it.Erpid == entity.Erpid)
                           .First();
            if (single != null)
                entity.Id = single.Id;
 
            return entity;
        }
 
        private List<Zzcxb> MapErpDbckbToDbckb(List<ERPZZCXB> list)
        {
            var result = new List<Zzcxb>();
 
            foreach (var dto in list)
            {
                var item = new Zzcxb
                {
                    Erpid = dto.erpid,               // ERP 主表id
                    Eid = dto.eid,                   // ERP 单据头id
                    ItemId = string.IsNullOrEmpty(dto.item_id) ? null : Convert.ToDecimal(dto.item_id), // 物料编码id
                    Qty = string.IsNullOrEmpty(dto.qty) ? null : Convert.ToDecimal(dto.qty),            // 数量
                    Unit = dto.unit,                 // 单位
                    DepotId = dto.depot_id,          // 仓库
                    LotNo = dto.lot_no,              // 批号
                    Memo = dto.memo                  // 备注
                };
 
                var existing = Db.Queryable<Zzcxb>().Where(s => s.Erpid == item.Erpid).Single();
                if (existing != null) item.Id = existing.Id;
 
                result.Add(item);
            }
 
            return result;
        }
 
        private bool SaveOrUpdateData(SqlSugarScope db, Zzcxa head, List<Zzcxb> details)
        {
            if (head.Id != null)
                base.DeleteById(head.Id);
 
            db.Deleteable<Zzcxb>()
              .Where(d => d.Eid == head.Erpid)
              .ExecuteCommand();
 
            var insertedHead = db.Insertable(head)
                .IgnoreColumns(true)
                .ExecuteReturnIdentity();
            if (insertedHead <= 0)
                throw new Exception("主表插入失败,未返回ID");
 
            var success = db.Insertable(details)
                .PageSize(10)
                .IgnoreColumnsNull()
                .ExecuteCommand() > 0;
 
            if (insertedHead >= 0 && success) return true;
            throw new NotImplementedException("插入或更新失败");
        }
 
        private bool UpdateData(SqlSugarScope db, Zzcxa head, List<Zzcxb> details)
        {
            var ids = details.Select(d => d.Id).ToArray();
            var deletedHead = base.DeleteById(head.Id);
            var deletedDetails = db.Deleteable<Zzcxb>().In(ids).ExecuteCommand() > 0;
 
            if (deletedHead && deletedDetails) return true;
            throw new NotImplementedException("更新失败");
        }
    }
}