11
啊鑫
2025-01-24 2b38e425925d194c5cd4767c8c13e5f5d23ab120
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
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 MesSupplierManager : Repository<MesSupplier>
{
    public bool Save(ErpSupplier unit)
    {
        var entity = GetMesSupplier(unit);
 
        return UseTransaction(db =>
        {
            switch (unit.Type)
            {
                case "0":
                    if (UpdateSupplierStatus(db, entity, "A"))
                        return 1;
                    break;
                case "1":
                    if (UpdateSupplierStatus(db, entity, "B"))
                        return 1;
                    break;
                case "3":
                    if (DeleteSupplier(entity))
                        return 1;
                    break;
                case "2":
                case "4":
                    if (InsertOrUpdate(db, entity))
                        return 1;
                    break;
                default:
                    throw new ArgumentNullException(
                        $"type没有{unit.Type}这个类型的参数");
            }
 
            throw new NotImplementedException("操作失败");
        }) > 0;
    }
 
    public bool SaveList(List<ErpSupplier> suppliers)
    {
        var result = suppliers.Select(Save).ToList();
        return result.All(b => b);
    }
 
    private bool UpdateSupplierStatus(SqlSugarScope db,
        List<MesSupplier> supplierId,
        string status)
    {
        var ids = supplierId.Select(it => it.Id).ToArray();
 
        var result = db.Updateable<MesSupplier>()
            .SetColumns(s => s.Fforbidstatus == status)
            .Where(s => ids.Contains(s.Id)).ExecuteCommand();
 
        if (result > 0)
            return true;
 
        throw new NotImplementedException(status == "A" ? "启用失败" : "禁用失败");
    }
 
    private bool DeleteSupplier(List<MesSupplier> supplierId)
    {
        // 提取所有需要的 MesId 组合 
        var ids = supplierId.Select(customer => customer.Id.ToString())
            .ToList();
        
        // 使用这些 MesId 去查询 MesLinkU9 表,找到对应的 U9Id
        var u9Ids = Db.Queryable<MesLinkU9>()
            .Where(u =>
                ids.Contains(u.MesId) && u.TableType == "MES_SUPPLIER")
            .Select(u => u.U9Id)
            .Distinct() // 应用 Distinct 去重
            .ToList();
 
        // 根据 U9Id 找到所有对应的 MesId,进行删除
        // 注意:这里假设您确实想要删除这些记录。如果不确定,请先查询以验证结果。
        var idsToDelete = Db.Queryable<MesLinkU9>()
            .Where(u => u9Ids.Contains(u.U9Id) && u.TableType == "MES_SUPPLIER")
            .Select(u => u.MesId)
            .Distinct() // 应用 Distinct 去重
            .ToList();
 
 
        //以上方法可以考虑优化,将U9id找个新建字段存在MES_CUSTOMER中,就可以少一步查询了。
 
        var deleteByIds = Db.Deleteable<MesSupplier>().In(idsToDelete)
            .ExecuteCommand();
        // if (deleteByIds > 0)
        //     return true;
        return true;
        //throw new NotImplementedException("删除失败");
    }
 
    private List<MesSupplier> GetMesSupplier(ErpSupplier supplier)
    {
        List<MesSupplier> list = new();
        foreach (var se in supplier.FUseOrgId)
        {
            var exists = Db.Queryable<MesLinkU9>().Any(u =>
                u.U9Id == supplier.Id && u.OrgId == se.FUseOrgId &&
                u.TableType == "MES_SUPPLIER");
 
            decimal mesId = 0;
            if (exists)
            {
                //获取mesid
                mesId = Convert.ToDecimal(Db.Queryable<MesLinkU9>()
                    .Where(u =>
                        u.U9Id == supplier.Id && u.OrgId == se.FUseOrgId &&
                        u.TableType == "MES_SUPPLIER")
                    .Select(u => u.MesId) // 选择 MesId 字段
                    .First());
            }
            else
            {
                mesId = GetSeqUtil.GetSeq("SEQ_BASEINFO_ID");
                var entity = new MesLinkU9
                {
                    CreateDate = DateTime.Now,
                    MesId = mesId.ToString(),
                    U9Id = supplier.Id,
                    OrgId = se.FUseOrgId,
                    TableType = "MES_SUPPLIER"
                };
                Db.Insertable(entity).ExecuteCommand();
            }
 
            var mesSupplier = new MesSupplier
            {
                Id = mesId,
                SuppNo = supplier.FNumber,
                SuppSname = supplier.FShortName,
                SuppName = supplier.FName,
                Lxr = supplier.FContact,
                Telf1 = supplier.FTel,
                Fstaffid = supplier.FStaffId,
                /// <summary>
                /// 接口字段调整:标准版接口新增 ERP单据状态 字段。
                /// </summary>
                /// <remarks>
                /// 修改人:<池南骏>
                /// 修改日期:<2024-12-28>
                /// 修改说明:
                /// - 原代码:空
                /// - 修改后:    
                ///FDOCUMENTSTATUS = supplier.FDocumentStatus,
                /// </remarks>
                DocumentStatus = supplier.FDocumentStatus,
                Fforbidstatus = supplier.FForbidStatus,
                CreateDate = DateTime.Now,
                //CreateOrg= Convert.ToDecimal(supplier.FCreateOrgId),
                UseOrg = se.FUseOrgId,
                //Remark=supplier.fremarks,
                PrivateDescSeg = supplier.PrivateDescSeg,
                Company = "1000",
                Factory = "1000"
            };
 
            list.Add(mesSupplier);
        }
 
        return list;
    }
 
    /// <summary>
    /// "插入或更新的方法"调整:修改为先删除再插入。
    /// </summary>
    /// <remarks>
    /// 修改人:池南骏
    /// 修改日期:2024-12-30
    /// 修改说明:`
    /// </remarks>
 
    //// - 原代码:
    //private bool InsertOrUpdate(SqlSugarScope db, MesSupplier entity)
    //{
    //    var exists = db.Queryable<MesSupplier>().Any(e => e.Id == entity.Id);
    //    if (exists)
    //    {
    //        var update = db.Updateable(entity).ExecuteCommand();
    //        if (update > 0)
    //            return true;
    //    }
    //    else
    //    {
    //        var insert = db.Insertable(entity).ExecuteCommand();
    //        if (insert > 0)
    //            return true;
    //    }
 
    //    return false;
    //}
    ///新代码
    private bool InsertOrUpdate(SqlSugarScope db, List<MesSupplier> entity)
    {
        DeleteSupplier(entity);
         
        var insert = db.Insertable(entity).PageSize(1).IgnoreColumnsNull().ExecuteCommand();
        return insert > 0;
    }
}