xwt
8 天以前 65438b8c1f9ec323c3c2ae265561dae4ff536379
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
using SqlSugar;
using System;
using System.Collections.Generic;
using MES.Service.DB;
using MES.Service.Modes;
using MES.Service.Dto.webApi;
 
namespace MES.Service.service
{
public class CgthSqManager : Repository<CgthSq>
{
 
        private readonly CgthSqDetailManager _CgthSqManager = new();
        public bool SaveList(List<ERPCGTH> rohIns)
        {
            var result = rohIns.Select(Save).ToList();
            return result.All(b => b);
        }
        public bool Save(ERPCGTH wom)
        {
            var ErpCgtha = wom.ErpCgtha;
            var mesWomCgth = MapErpCgthatoCGTHA(ErpCgtha);
            var mesWomcabs =
                MapErpCgthBtoCGTHA(wom.ErpCgthB);
 
            return UseTransaction(db =>
            {
                switch (ErpCgtha.Type)
                {
                    case "3":
                        return UpdateData(db, mesWomCgth, mesWomcabs) ? 1 : 0;
                    case "2":
                    case "4": 
                    case "5":
                        return SaveOrUpdateData(db, mesWomCgth, mesWomcabs)
                            ? 1
                            : 0;
                    default:
                        throw new NotImplementedException(
                            $"type没有{ErpCgtha.Type}这个类型");
                }
            }) > 0;
        }
        private CgthSq MapErpCgthatoCGTHA(ErpCGTHA dto)
        {
 
            var entity = new CgthSq
            {
                BillNo = dto.BillNo,         //单据编号 
                SuppId = dto.Suppid,          //供应商ID
                SuppNo = dto.SuppNo,          //供应商编码
                DjLx = dto.BillNo_type,        //单据类型
                YwFs = dto.Yw_lx,              //业务类型
                ThFs = dto.th_fs,           //退货方式
                ThYy = dto.th_yy,         //退货原因 
                FType = decimal.TryParse(dto.is_ww, out var itemIdVal) ? itemIdVal : null,  //是否委外
                ErpCreateBy = dto.createby,               //创建人名称
                CgOrg = dto.cg_org,             //采购组织
                TlOrg = dto.tl_org,                               //退料组织
                XqOrg = dto.xq_org,                            //需求组织
                CgBm = dto.cg_bm,                              //采购部门
                TlBm = dto.tl_bm,                //退料部门
                CgMc = dto.cg,                   //仓管员名称
                CgyMc = dto.cgy,               //采购员名称
                KcDepot = dto.kc_dpot,        //库存组名称
                CgDepot = dto.cg_depot,               //采购组名称
                YsFs = dto.Ys_fs,                               //验收方式
                Bz = dto.bz,                                 //备注
                Erpid = dto.ERPID,                              //单据头id
                Ydid=dto.yd_id,
                CreateDate =DateTime.Now,
            }; 
            var single = base.GetSingle(it => it.Erpid == entity.Erpid);
            if (single != null) entity.Id = single.Id; 
            return entity;
        }
        private List<CgthSqDetail> MapErpCgthBtoCGTHA(List<ErpCGTHB> dtoList)
        {
            var CgthSqDetailList = new List<CgthSqDetail>();
 
            foreach (var dto in dtoList)
            {
                var CgthSqDetail = new CgthSqDetail
                {
                   WorkNo=dto.ebeln,             //采购订单
                    ItemId = decimal.TryParse(dto.itemid, out var itemIdVal) ? itemIdVal : null,         //待退物料ID
                    Unit =dto.unit,             //单位
                    EbelnK3id = decimal.TryParse(dto.ebeln_id, out var ebelnIdVal) ? ebelnIdVal : null,  //采购订单id
                    LineK3id = decimal.TryParse(dto.line_id, out var lineIdVal) ? lineIdVal : null,         //采购订单行id
                    DepotId =dto.depot_id,                    //仓库ID
                    DepotCode=dto.depot_code,                   //仓库编码
                     Erpid=dto.erpid,                           //单据体id
                     Eid  = dto.eid,                 //单据头id
                     Flot=dto.flot,                           //批号
                     Fmto=dto.fmoto,                           //计划跟踪号
                    YdflId = dto.ydfl_id,                           //源单分录id
                    Quantity = decimal.TryParse(dto.quantity, out var qtyVal) ? qtyVal : null,        //数量
 
                };
 
                var entity = Db.Queryable<CgthSqDetail>()
                    .Where(s => s.Erpid == CgthSqDetail.Erpid).Single();
                if (entity != null)
                {
                    // 删除已有的 Erpid 相关的 Womcab 数据
                    //  Db.Deleteable<Womcab>().Where(s => s.Erpid == womcab.Erpid).ExecuteCommand();
                    CgthSqDetail.Id = entity.Id;
                }
                CgthSqDetailList.Add(CgthSqDetail);
            }
 
            return CgthSqDetailList;
        }
 
   private bool UpdateData(SqlSugarScope db, CgthSq mesWomCgth,
      List<CgthSqDetail> mesWomcabs)
        {
            var decimals = mesWomcabs.Select(s => s.Id).ToArray();
 
            //扫码不允许删除
             
            throw new NotImplementedException("扫码不允许删除");
            var update = base.DeleteById(mesWomCgth.Id);
            var insertOrUpdate = db
                .Deleteable<CgthSqDetail>().In(decimals)
                .ExecuteCommand() > 0;
 
 
            if (update && insertOrUpdate) return true;
            throw new NotImplementedException("更新失败");
        }
   private bool SaveOrUpdateData(SqlSugarScope db, CgthSq mesWomcaa,
      List<CgthSqDetail> mesWomcabs)
        {
            if (mesWomcaa.Id != null) base.DeleteById(mesWomcaa.Id);
 
            if (mesWomcabs.Count > 0)
                db.Deleteable<CgthSqDetail>()
                    .Where(s => s.Eid == mesWomcaa.Erpid).ExecuteCommand();
 
            //单条插入忽略空字段
            /*  var orUpdate = db.Insertable(mesWomcaa)
                  .IgnoreColumns(true).ExecuteCommand() > 0;*/
            // 插入主表并返回完整实体(含主键 Id)
            var insertedId = db.Insertable(mesWomcaa)
      .IgnoreColumns(true)
      .ExecuteReturnBigIdentity(); // 返回 decimal 类型 ID
 
            mesWomcaa.Id = insertedId; // 填入主表实体
 
            // 赋值每条明细的 Pid = 主表.Id
            foreach (var detail in mesWomcabs)
            {
                detail.Pid = insertedId;
            }
            //批量插入忽略空字段
            var baOrUpdate = db.Insertable(mesWomcabs).PageSize(10)
                .IgnoreColumnsNull()
                .ExecuteCommand() > 0;
 
            //if (orUpdate && baOrUpdate) return true;
            if (insertedId != null && baOrUpdate) return true;
            throw new NotImplementedException("插入或更新失败");
        }
        #region 教学方法
        /// <summary>
        /// 仓储方法满足不了复杂业务需求,业务代码请在这里面定义方法
        /// </summary>
        public void Study()
    {
         
       /*********查询*********/
 
        var data1 = base.GetById(1);//根据ID查询
        var data2 = base.GetList();//查询所有
        var data3 = base.GetList(it => 1 == 1);  //根据条件查询  
        //var data4 = base.GetSingle(it => 1 == 1);//根据条件查询一条,如果超过一条会报错
 
        var p = new PageModel() { PageIndex = 1, PageSize = 2 };// 分页查询
        var data5 = base.GetPageList(it => 1 == 1, p);
        Console.Write(p.TotalCount);//返回总数
 
        var data6 = base.GetPageList(it => 1 == 1, p, it => SqlFunc.GetRandom(), OrderByType.Asc);// 分页查询加排序
        Console.Write(p.TotalCount);//返回总数
     
        List<IConditionalModel> conModels = new List<IConditionalModel>(); //组装条件查询作为条件实现 分页查询加排序
        conModels.Add(new ConditionalModel() { FieldName= typeof(CgthSq).GetProperties()[0].Name, ConditionalType = ConditionalType.Equal, FieldValue = "1" });//id=1
        var data7 = base.GetPageList(conModels, p, it => SqlFunc.GetRandom(), OrderByType.Asc);
 
        base.AsQueryable().Where(x => 1 == 1).ToList();//支持了转换成queryable,我们可以用queryable实现复杂功能
 
 
 
        /*********插入*********/
        var insertData = new CgthSq() { };//测试参数
        var insertArray = new CgthSq[] { insertData };
        base.Insert(insertData);//插入
        base.InsertRange(insertArray);//批量插入
        var id = base.InsertReturnIdentity(insertData);//插入返回自增列
        base.AsInsertable(insertData).ExecuteCommand();//我们可以转成 Insertable实现复杂插入
 
 
 
        /*********更新*********/
        var updateData = new CgthSq() {  };//测试参数
        var updateArray = new CgthSq[] { updateData };//测试参数
        base.Update(updateData);//根据实体更新
        base.UpdateRange(updateArray);//批量更新
        //base.Update(it => new CgthSq() { ClassName = "a", CreateTime = DateTime.Now }, it => it.id==1);// 只更新ClassName列和CreateTime列,其它列不更新,条件id=1
        base.AsUpdateable(updateData).ExecuteCommand();  //转成Updateable可以实现复杂的插入
 
 
 
        /*********删除*********/
        var deldata = new CgthSq() {  };//测试参数
        base.Delete(deldata);//根据实体删除
        base.DeleteById(1);//根据主键删除
        base.DeleteById(new int[] { 1,2});//根据主键数组删除
        base.Delete(it=>1==2);//根据条件删除
        base.AsDeleteable().Where(it=>1==2).ExecuteCommand();//转成Deleteable实现复杂的操作
    } 
    #endregion
 
 
 }
}