bklLiudl
2025-04-02 1bbbbc8bb49411b544626996a1370788142300e0
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
using Model.InterFaceModel;
using Model.ModelDto.BllAsnDto;
using Model.ModelVm.BllAsnVm;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using WMS.BLL.LogServer;
using WMS.DAL;
using WMS.Entity.BllAsnEntity;
using WMS.Entity.Context;
using WMS.Entity.SysEntity;
using WMS.IBLL.IBllAsnServer;
 
namespace WMS.BLL.BllAsnServer
{
    public class ProcurePlanServer : DbHelper<BllProcurePlanNotice>, IProcurePlanServer
    {
        #region 依赖注入 
        private static readonly SqlSugarScope Db = DataContext.Db;
 
        public ProcurePlanServer() : base(Db)
        {
        }
        #endregion
 
        /// <summary>
        /// 获取采购单信息
        /// </summary>
        /// <param name="model"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<ProcurePlanNoticeDto> GetProcurePlanNoticeList(ProcurePlanNoticeVm model, out int count)
        {
            string sqlString = string.Empty;
            string sqlCount = string.Empty;
            string sqlPub = string.Empty;
            try
            {
                sqlCount += "SELECT DISTINCT COUNT(tb1.ID) FROM BllProcurePlanNotice AS tb1 ";
                sqlString += "SELECT DISTINCT tb1.*,tb3.RealName as CreateUserName,tb4.RealName as UpdateUserName FROM BllProcurePlanNotice AS tb1 ";
                sqlPub += "LEFT JOIN BllProcurePlanNoticeDetail AS tb2 ON tb1.Id = tb2.ParentId ";
                sqlPub += "LEFT JOIN SysUserInfor AS tb3 ON tb1.CreateUser = tb3.Id ";
                sqlPub += "LEFT JOIN SysUserInfor AS tb4 ON tb1.UpdateUser = tb4.Id ";
                sqlPub += $"WHERE tb1.IsDel = '0' ";
                sqlPub += $"AND tb2.SkuNo LIKE '%{model.SkuNo}%' AND tb2.SkuName LIKE '%{model.SkuName}%' ";
                sqlPub += $"AND tb2.CustomerName  LIKE '%{model.CustomerName}%' ";
                if (!string.IsNullOrEmpty(model.Status))
                {
                    sqlPub += $"AND tb1.Status = '{model.Status}' ";
                }
                if (!string.IsNullOrEmpty(model.StartTime))
                {
                    sqlPub += $"AND tb1.CreateTime >= '{model.StartTime}' ";
                }
                if (!string.IsNullOrEmpty(model.EndTime))
                {
                    sqlPub += $"AND tb1.CreateTime <= '{model.EndTime}' ";
                }
                sqlCount += sqlPub;
                sqlPub += " order by tb1.Id desc ";
                if (model.Page == 0)
                {
                    model.Page = 1;
                }
                sqlString += sqlPub + $" offset {((model.Page - 1) * model.Limit)} rows fetch next {model.Limit} rows only;";
 
                var com = new Common();
                count = com.GetRowCount(sqlCount);
 
                var modelList = Db.Ado.SqlQuery<ProcurePlanNoticeDto>(sqlString);
 
                return modelList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取采购单明细信息
        /// </summary>
        /// <param name="model"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<ProcurePlanNoticeDetailDto> GetProcurePlanNoticeDetailList(ProcurePlanNoticeDetailVm model, out int count)
        {
            string sqlString = string.Empty;
            string sqlCount = string.Empty;
            int rowCount = 1;
            try
            {
                if (model.Page == 0)
                {
                    model.Page = 1;
                }
                sqlCount += $"SELECT COUNT(ID) FROM BllProcurePlanNoticeDetail where ParentId = '{model.ParentId}' and IsDel = '0';";
                var com = new Common();
                count = com.GetRowCount(sqlCount);
                if (count != 0)
                {
                    rowCount = count;
                }
 
                sqlString += "SELECT DISTINCT tb1.*,tb3.RealName as CreateUserName, ";
                sqlString += "tb4.RealName as UpdateUserName,isnull(tb6.UnitName,tb5.UnitNo) as UnitName,tb7.PackagName ";
                sqlString += "FROM BllProcurePlanNoticeDetail AS tb1 ";
                sqlString += "LEFT JOIN SysUserInfor AS tb3 ON tb1.CreateUser = tb3.Id ";
                sqlString += "LEFT JOIN SysUserInfor AS tb4 ON tb1.UpdateUser = tb4.Id ";
                sqlString += "LEFT JOIN SysMaterials AS tb5 on tb1.SkuNo = tb5.SkuNo ";
                sqlString += "LEFT JOIN SysUnit AS tb6 on tb5.UnitNo = tb6.UnitNo ";
                sqlString += "LEFT JOIN SysPackag AS tb7 on tb1.PackagNo = tb7.PackagNo ";
                sqlString += $"WHERE tb1.ParentId = '{model.ParentId}' AND tb1.IsDel = '0' order by tb1.SkuNo desc ";
                sqlString += $"offset {((model.Page - 1) * model.Limit)} rows fetch next {rowCount} rows only;";
 
                var modelList = Db.Ado.SqlQuery<ProcurePlanNoticeDetailDto>(sqlString);
 
                return modelList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 通过采购单生成入库单据
        /// </summary>
        /// <param name="planDetailId"></param>
        /// <param name="userId"></param>
        public void CreateAsnByProcurePlan(int planDetailId,int userId)
        {
            try
            {
                //采购单明细
                var planDetail= Db.Queryable<BllProcurePlanNoticeDetail>().First(it => it.Id == planDetailId && it.IsDel == "0");
                if (planDetail == null)
                {
                    throw new Exception("未查询到采购单据明细信息");
                }
                //采购总单信息
                var planOrd = Db.Queryable<BllProcurePlanNotice>().First(it => it.Id == planDetail.ParentId && it.IsDel == "0");
                if (planOrd == null)
                {
                    throw new Exception("未查询到采购单据总单信息");
                }
                if (planOrd.Status == "3")
                {
                    throw new Exception("采购单据总单已关单");
                }
 
                // 根据客户编号获取客户名称
                var CustomerModel = Db.Queryable<SysCustomer>().First(it => it.CustomerNo == planDetail.CustomerNo && it.IsDel == "0");
                if (CustomerModel == null)
                {
                    throw new Exception("客户编号不存在!");
                }
                // 入库总表信息
                string asnNo = new Common().GetMaxNo("ASN");
                var asnModel = new BllArrivalNotice()
                {
                    ASNNo = asnNo,
                    Status = "0",//执行状态,0:等待执行
                    Type = "1",//单据类型,1:采购入库
                    Origin = "采购单",
                    CustomerNo = CustomerModel.CustomerNo,
                    CustomerName = CustomerModel.CustomerName,
                    OrderCode = planOrd.OrderCode,
                    CreateUser = userId
                };               
                // 获取物料详细信息
                var skuModel = Db.Queryable<SysMaterials>().First(it => it.SkuNo == planDetail.SkuNo && it.IsDel == "0");
                if (skuModel == null)
                {
                    throw new Exception("不存在当前物料信息!");
                }
                // 验证包装信息是否存在
                var packagModel = Db.Queryable<SysPackag>().First(it => it.PackagNo == planDetail.PackagNo && it.IsDel == "0");
                if (packagModel == null)
                {
                    throw new Exception("不存在当前包装信息!");
                }
                //入库单明细
                var detailModel = new BllArrivalNoticeDetail()
                {
                    ASNNo = asnNo,
                    SkuNo = skuModel.SkuNo,
                    SkuName = skuModel.SkuName,
                    Standard = skuModel.Standard,
                    LotNo = "",
                    LotText = "",
                    Qty = 0,
                    FactQty = 0,
                    CompleteQty = 0,
                    PackagNo = packagModel.PackagNo,
                    SupplierLot = "",
                    Status = "0",
                    IsSampling = "0",
                    InspectStatus = "0",
                    OrderDetailCode = planDetail.OrderDetailCode,
                    CreateUser = userId
                };
 
                //开启事务
                Db.BeginTran();
 
                // 插入入库单总表
                Db.Insertable(asnModel).ExecuteCommand();
                // 插入入库单明细
                Db.Insertable(detailModel).ExecuteCommand();
 
                //添加操作日志
                new OperationASNServer().AddLogOperationAsn("入库作业", "入库单据", asnNo, "添加", $"在采购单页面添加了单据号为{asnNo}的单据信息", userId);
 
                //提交事务
                Db.CommitTran();
            }
            catch (Exception ex)
            {
                Db.RollbackTran();
                throw new Exception(ex.Message);
            }
        }
 
        /// <summary>
        /// 关闭采购单据
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="userId"></param>
        public void CloseProcurePlan(int Id,int userId)
        {
            try
            {
                //采购总单信息
                var planOrd = Db.Queryable<BllProcurePlanNotice>().First(it => it.Id == Id && it.IsDel == "0" && it.Status == "2");
                if (planOrd == null)
                {
                    throw new Exception("未查询到采购单据总单信息");
                }
                //开启事务
                Db.BeginTran();
 
                planOrd.Status = "3";
                planOrd.UpdateUser = userId;
                planOrd.UpdateTime = DateTime.Now;
                //更新采购单状态
                Db.Updateable(planOrd).ExecuteCommand();
 
                //添加操作日志
                new OperationASNServer().AddLogOperationAsn("入库作业", "入库单据", planOrd.OrderCode, "关单", $"关闭了采购单据号为{planOrd.OrderCode}的采购单据信息", userId);
 
                //提交事务
                Db.CommitTran();
            }
            catch (Exception ex)
            {
                Db.RollbackTran();
                throw new Exception(ex.Message);
            }
        }
 
        #region 接口方法
        /// <summary>
        /// 上游系统下发创建采购单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ErpModel CreateProcurePlan(ProcurePlanInfo model)
        {
            try
            {
                var resultModel = new ErpModel() { Success = -1, Message = "" };
               
                if (string.IsNullOrEmpty(model.OrderCode))
                {
                    resultModel.Message = "上游系统单号不可为空!";
                    return resultModel;
                }
                if (model.ProcurePlanDetails.Count <= 0)
                {
                    resultModel.Message = "采购单明细不可为空!";
                    return resultModel;
                }
 
                //开启事务
                Db.BeginTran();
 
                // 采购单总表信息
                var planModel = new BllProcurePlanNotice()
                {
                    Status = "0",                   
                    OrderCode = model.OrderCode,
                };
                // 插入采购单总表
                int parentId = Db.Insertable(planModel).ExecuteReturnIdentity();
 
                // 采购单明细表信息
                List<BllProcurePlanNoticeDetail> detailModels = new List<BllProcurePlanNoticeDetail>();
                foreach (ProcurePlanDetail asnDetailModel in model.ProcurePlanDetails)
                {
                    if (string.IsNullOrEmpty(asnDetailModel.SkuNo))
                    {
                        resultModel.Message = "物料编码不可为空!";
                        return resultModel;
                    }
                    if (asnDetailModel.Qty <= 0)
                    {
                        resultModel.Message = "数量不能为空!";
                        return resultModel;
                    }
                    if (string.IsNullOrEmpty(asnDetailModel.Customer))
                    {
                        resultModel.Message = "供应商不可为空!";
                        return resultModel;
                    }
 
                    // 获取物料详细信息
                    var skuModel = Db.Queryable<SysMaterials>().First(it => it.SkuNo == asnDetailModel.SkuNo && it.IsDel == "0");
                    if (skuModel == null)
                    {
                        resultModel.Message = "不存在当前物料信息!";
                        return resultModel;
                    }
 
                    // 根据客户编号获取客户名称
                    var CustomerModel = Db.Queryable<SysCustomer>().First(it => it.CustomerNo == asnDetailModel.Customer && it.IsDel == "0");
                    if (CustomerModel == null)
                    {
                        resultModel.Message = "供应商编号不存在!";
                        return resultModel;
                    }
 
                    //采购单明细信息
                    var detailModel = new BllProcurePlanNoticeDetail()
                    {
                        ParentId = parentId,
                        OrderDetailCode= asnDetailModel.OrderDetailCode,
                        SkuNo = asnDetailModel.SkuNo,
                        SkuName = skuModel.SkuName,
                        Standard = skuModel.Standard,
                        Qty = (decimal)asnDetailModel.Qty,
                        CompleteQty=0,
                        PackagNo = skuModel.PackagNo,
                        Status = "0",
                        CustomerNo = asnDetailModel.Customer,
                        CustomerName = CustomerModel.CustomerName,
                        CreateUser = 0
                    };
                    detailModels.Add(detailModel);
 
                    #region 入库单
                    // 入库总表信息
                    string asnNo = new Common().GetMaxNo("ASN");
                    var asnModel = new BllArrivalNotice()
                    {
                        ASNNo = asnNo,
                        Status = "0",//执行状态,0:等待执行
                        Type = "1",//单据类型,1:采购入库
                        Origin = "采购单",
                        CustomerNo = CustomerModel.CustomerNo,
                        CustomerName = CustomerModel.CustomerName,
                        OrderCode = model.OrderCode,
                        CreateUser = 0
                    };
                    // 插入入库总表信息
                    Db.Insertable(asnModel).ExecuteCommand();
                    //入库单明细
                    var arrDetailModel = new BllArrivalNoticeDetail()
                    {
                        ASNNo = asnNo,
                        SkuNo = skuModel.SkuNo,
                        SkuName = skuModel.SkuName,
                        Standard = skuModel.Standard,
                        LotNo = "",
                        LotText = "",
                        Qty = 0,
                        FactQty=0,
                        CompleteQty=0,
                        PackagNo = skuModel.PackagNo,
                        SupplierLot = "",
                        Status = "0",
                        IsSampling = "0",
                        InspectStatus = "0",
                        OrderDetailCode = asnDetailModel.OrderDetailCode,
                        CreateUser = 0
                    };
                    // 插入入库总表信息
                    Db.Insertable(arrDetailModel).ExecuteCommand();
                    #endregion
 
                }
                // 插入采购单明细表
                Db.Insertable(detailModels).ExecuteCommand();
                Db.CommitTran();
 
                resultModel.Success = 0;
                resultModel.Message = "成功";
                return resultModel;
            }
            catch (Exception ex)
            {
                Db.RollbackTran();
                throw ex;
            }
        }
 
        #endregion
    }
}