xwt
2025-05-20 974e3af4e6316b59e761925b1a511a0470f2a365
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
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using MES.Service.DB;
using MES.Service.Modes;
using MES.Service.Dto.webApi;
 
namespace MES.Service.service
{
    public class ShtzdManager : Repository<Shtzd>
    {
        public bool Save(Sh sh)
        {
            var SH = sh.ErpShtzd;
            var mesShtzd = GetMesShtzd(SH);
            var shtzdDetails = GetMesShtzdDetail(sh.ErpShzdDetail);
 
            return UseTransaction(db =>
            {
                return SH.Type switch
                {
                    "2" or "4" or "5" => SaveOrUpdateData(db, mesShtzd, shtzdDetails) ? 1 : 0,
                    "3" => UpdateData(db, mesShtzd, shtzdDetails) ? 1 : 0,
                    _ => throw new NotImplementedException($"类型 Type = {SH.Type} 不存在")
                };
            }) > 0;
        }
        public bool SaveList(List<Sh> shList)
        {
            return UseTransaction(db =>
            {
                foreach (var sh in shList)
                {
                    var SH = sh.ErpShtzd;
                    var mesShtzd = GetMesShtzd(SH);
                    var shtzdDetails = GetMesShtzdDetail(sh.ErpShzdDetail);
 
                    switch (SH.Type)
                    {
                        case "2":
                        case "4":
                        case "5":
                            SaveOrUpdateData(db, mesShtzd, shtzdDetails);
                            break;
                        case "3":
                            UpdateData(db, mesShtzd, shtzdDetails);
                            break;
                        default:
                            throw new NotImplementedException($"类型 Type = {SH.Type} 暂不支持处理");
                    }
                }
                return shList.Count;
            }) > 0;
        }
 
        public Shtzd GetMesShtzd(ErpShzd sh)
        {
            return new Shtzd
            {
                ID = Convert.ToInt32(sh.ERPID),
                Deliveryno = sh.FBillNo,
                Depotno = sh.FStockID,
                Supplierid = sh.FSupplierId,
                Fdate = sh.FDate,
                Remark = sh.FNote,
                FZjxfSfgx = sh.F_ZJXF_sfgx
            };
        }
 
        public List<ShtzdDetail> GetMesShtzdDetail(List<ErpShzdDetail> erpShzdDetails)
        {
            return erpShzdDetails.Select(s => new ShtzdDetail
            {
                Erpid = Convert.ToInt32(s.ERPID),
                Eid = Convert.ToInt32(s.EID),
                Lineno = s.LineNo,
                Fbillno = s.FORDERBILLNO,
                Productcode = s.FMATERIALID,
                Purchaseqty = s.FPOQTY,
                Deliveryqty = s.FACTRECEIVEQTY,
                Includeqty = s.FACTREQTY,
                Purchaseunit = s.FUNITID,
                Inventoryunit = s.FSTOCKUNITID,
                Remark = s.FDESCRIPTION,
                Salesorderid = s.FSRCBILLNO,
                Orderlineid = s.FSRCENTRYID,
                Fxshth = s.FXSHTH,
                Fmtono = s.FMtoNo,
                Flot = s.FLot
            }).ToList();
        }
 
        protected int UseTransaction(Func<SqlSugarScope, int> action)
        {
            try
            {
                Db.Ado.BeginTran();
                var affectedRows = action.Invoke(Db);
                Db.Ado.CommitTran();
                return affectedRows;
            }
            catch (Exception)
            {
                Db.Ado.RollbackTran();
                throw;
            }
        }
 
        private bool SaveOrUpdateData(SqlSugarScope db, Shtzd header, List<ShtzdDetail> details)
        {
            var exists = db.Queryable<Shtzd>().Any(x => x.ID == header.ID);
            if (exists)
            {
                db.Updateable(header).ExecuteCommand();
                db.Deleteable<ShtzdDetail>().Where(x => x.Eid == header.ID).ExecuteCommand();
            }
            else
            {
                db.Insertable(header).ExecuteCommand();
            }
 
            foreach (var item in details)
            {
                item.Eid = header.ID;
            }
            db.Insertable(details).ExecuteCommand();
            return true;
        }
 
        private bool UpdateData(SqlSugarScope db, Shtzd header, List<ShtzdDetail> details)
        {
            db.Updateable(header).ExecuteCommand();
            db.Deleteable<ShtzdDetail>().Where(x => x.Eid == header.ID).ExecuteCommand();
 
            foreach (var item in details)
            {
                item.Eid = header.ID;
            }
            db.Insertable(details).ExecuteCommand();
            return true;
        }
    }
}