sjz
2025-09-02 5612526544b3532c5efc9c002d1c97b4af286bd1
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
 
 
using MES.Service.DB;
using MES.Service.Dto.webApi;
using MES.Service.Dto.webApi.FbsDb;
using MES.Service.Modes;
using MES.Service.Modes.FBSDB;
using MES.Service.service.BasicData;
using SqlSugar;
 
namespace MES.Service.service.FBSDB;
 
public class FbsDbManager:Repository<MesInvItemMoves>
{
    private readonly FbsDbBManager _dbBManager = new();
 
    public bool SaveList(List<ErpDb> dbs)
    {
        var result = dbs.Select(Save).ToList();
        return result.All(b => b);
    }
 
    public bool Save(ErpDb dbs)
    {
        var dba = dbs.erpDbcka;
        var mesDbA = FbaDbA(dba);
        var mesDbB = FbsDbB(dbs.erpDbckB,dba.FBillNo);
        return UseTransaction(db =>
        {
            switch (dba.Type)
            {
                case "3":
                    return DeleteData(db, mesDbA, mesDbB) ? 1 : 0;
                case "1":
                case "2":
                case "4":
                case "5":
                    return SaveOrUpdateData(db, mesDbA, mesDbB) ? 1 : 0;
                default:
                    throw new NotImplementedException($"type没有{dba.Type}这个类型");
            }
        }) > 0;
    }
 
    // 插入或更新数据的方法
    private bool SaveOrUpdateData(SqlSugarScope db, MesInvItemMoves fbsdba, List<MesInvItemMoveItems> fbsdbb)
    {
        if (fbsdba.Id != null)
        {
            base.DeleteById(fbsdba.Id);
        }
 
        if (fbsdbb.Count > 0)
        {
            db.Deleteable<MesInvItemMoveItems>().Where(s => s.ItemMoveId == fbsdba.Erpid).ExecuteCommand();
        }
        var orUpdate = base.Insert(fbsdba);
        var baOrUpdate = _dbBManager.InsertRange(fbsdbb);
        if (orUpdate && baOrUpdate)
        {
            return true;
        }
        throw new NotImplementedException("插入或更新失败");
    }
 
    private bool DeleteData(SqlSugarScope db, MesInvItemMoves mesDbA, List<MesInvItemMoveItems> mesDbB)
    {
        var decimals = mesDbB.Select(s => s.Id).ToArray();
        var update = base.DeleteById(mesDbA.Id);
        var insertOrUpdate = db.Deleteable<MesInvItemMoveItems>().In(decimals).ExecuteCommand() > 0;
 
        if (update && insertOrUpdate)
        {
            return true;
        }
        throw new NotImplementedException("删除失败");
    }
 
    private MesInvItemMoves FbaDbA(FbsDbA dto)
    {
 
        var entity = new MesInvItemMoves
        {
            Erpid = Convert.ToDecimal(dto.Erpid),
            BillNo = dto.FBillNo,
            CreateBy = dto.FCreateBy,
            CreateDate = dto.FDate != null ? DateTime.ParseExact(dto.FDate, "yyyy-MM-dd HH:mm:ss", null) : null,
            Status = 1,
            Remark = dto.FRemarks,
            BillTypeId = 300,
            TransactionNo = 302,
            SapStatus = 0,
            Factory = "1000",
            Company = "1000",
            Ts =0,
            DbStatus="未调拨",
            FBillTypeId=dto.FBillTypeID,
            FDocumentStatus=dto.FDocumentStatus,
            FTransferDirect=dto.FTransferDirect,
            FTransType=dto.FTRANSTYPE,
            FBusinessType=dto.FBusinessType,
            FOwnerTypeIdHead=dto.FOwnerTypeIdHead,
            FOwnerTypeInIdHead=dto.FOwnerTypeInIdHead,
            FAppOrgId=Convert.ToDecimal(dto.FAPPORGID)
 
        };
 
        var single = base.GetSingle(it => it.Erpid == entity.Erpid);
        if (single != null)
        {
            entity.Id = single.Id;
        }
 
        return entity;
    }
 
    private List<MesInvItemMoveItems> FbsDbB(List<FbsDbB> dtoList,string billno)
    {
        var dbList = new List<MesInvItemMoveItems>();
 
        foreach (var dto in dtoList)
        {
            var entitys = new MesInvItemMoveItems
            {
                ErpId = Convert.ToDecimal(dto.erpid),
                ItemMoveId = Convert.ToDecimal(dto.eid),
                ItemId =Convert.ToDecimal(dto.FMATERIALID),
                ItemUnit = Convert.ToDecimal(dto.FUNITID),
                SqNum = Convert.ToDecimal(dto.FQty),
                Remark = dto.FNote,
                BillNo = billno,
                MoveIn = 0,
                MoveOut = 0,
                CreateDate = DateTime.Now,
                IsTb = 0,
                FOwnerTypeId = dto.FOwnerTypeId,
                FOwnerId = dto.FOwnerId,
                FStockId = dto.FStockId,
                FOwnerTypeInId = dto.FOwnerTypeInId,
                FOwnerInId = dto.FOwnerInId,
                FStockInId = dto.FStockInId,
                FStockOrgId = Convert.ToDecimal(dto.FStockOrgId),
                FStockOrgInId = Convert.ToDecimal(dto.FStockOrgInId),
                FLot=dto.FLot,
                FMtono=dto.FMtoNo
            };
 
            var entity = Db.Queryable<MesInvItemMoveItems>().Where(s => s.ErpId == entitys.ErpId).Single();
            if (entity != null)
            {
                entitys.Id = entity.Id;
            }
 
            dbList.Add(entitys);
        }
 
        return dbList;
    }
}