bklLiudl
2025-04-07 4e8f58cb41c7b6d570fd1979d80f74ab8a4d00c2
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Dm;
using Model.ModelDto.BllSoDto;
using SqlSugar;
using WMS.BLL.LogServer;
using WMS.DAL;
using WMS.Entity.BllSoEntity;
using WMS.Entity.Context;
using WMS.Entity.DataEntity;
using WMS.Entity.SysEntity;
using WMS.IBLL.IBllSoServer;
 
namespace WMS.BLL.BllSoServer
{
    public class ExportNoticeDetailServer: DbHelper<BllExportNoticeDetail>,IExportNoticeDetailServer
    {
        private static readonly SqlSugarScope Db = DataContext.Db;
        public ExportNoticeDetailServer():base(Db)
        {
        }
        public List<ExportNoticeDetailDto> GetExportNoticeDetailList(string soNo, int page, int limit, out int count)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(soNo))
                {
                    throw new Exception("出库单号为空");
                }
                var notice = DataContext.Db.Queryable<BllExportNotice>().Where(m => m.IsDel == "0" && m.SONo == soNo).ToList().FirstOrDefault();
                if (notice == null)
                {
                    throw new Exception("出库单号为空");
                }
                var total = 0;
                var data = GetAllWhereAsync(a=>a.SONo == soNo)
                    .LeftJoin<SysPackag>((a,b)=> a.PackagNo == b.PackagNo)
                    .LeftJoin<SysUserInfor>((a,b,c)=> a.CreateUser == c.Id)
                    .LeftJoin<SysUserInfor>((a,b,c,d)=> a.UpdateUser == d.Id)
                    .LeftJoin<BllExportNotice>((a, b, c, d, e) => a.SONo == e.SONo)
                    .Select((a, b, c, d, e) => new ExportNoticeDetailDto()
                    {
                        Id = a.Id,
                        Status = a.Status,
                        Origin = notice.Origin,
                        SONo = a.SONo,
                        Type = e.Type,
                        StatusZ = e.Status,
                        SkuNo = a.SkuNo,
                        SkuName = a.SkuName,
                        Standard = a.Standard,
                        LotNo = a.LotNo,
                        LotText = a.LotText,
                        Qty = a.Qty,
                        AllotQty = a.AllotQty,
                        FactQty = a.FactQty,
                        CompleteQty = a.CompleteQty,
                        PackagNo = a.PackagNo,
                        PackagName = b.PackagName,
                        Price = a.Price,
                        Money = a.Money,
                        IsBale = a.IsBale,
                        IsBelt = a.IsBelt,
                        SupplierLot = a.SupplierLot,
                        IsWave = a.IsWave,
                        WaveNo = a.WaveNo,
 
                        CreateUserName = c.RealName,
                        UpdateUserName = d.RealName,
                        CreateTime = a.CreateTime,
                        UpdateTime = a.UpdateTime
                    }).ToOffsetPage(page,limit,ref total);
                count = total;
                
                return data;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
        public bool DelExportNoticeDetail(int id, int userId)
        {
            try
            { 
                //出库单明细
                var noticeDetail = Db.Queryable<BllExportNoticeDetail>().Where(m => m.IsDel == "0" && m.Id == id).ToList().FirstOrDefault();
                //出库单明细                
                var notice = Db.Queryable<BllExportNotice>().Where(m => m.IsDel == "0" && m.SONo == noticeDetail.SONo).ToList().FirstOrDefault();
 
                if (noticeDetail == null)
                {
                    throw  new Exception("未查询到出库单明细信息");
                }
                if (notice == null)
                {
                    throw new Exception("未查询到出库单信息");
                }
                if (notice.Status != "0")
                {
                    throw new Exception("当前出库单状态不是等待执行,不能删除");
                }
                if (noticeDetail.AllotQty > 0)
                {
                    throw new Exception("当前出库单明细已被分配,不能删除");
                }
 
                var count = Db.Queryable<BllExportNoticeDetail>().Count(d => d.SONo == noticeDetail.SONo && d.IsDel == "0" && d.Id != noticeDetail.Id);
                Db.BeginTran();//开启事务
                try
                {
                    var stock = Db.Queryable<DataStock>().Where(s => s.SkuNo == noticeDetail.SkuNo).ToList();
                    if (!string.IsNullOrWhiteSpace(noticeDetail.LotNo))
                    {
                        stock = stock.Where(s => s.LotNo == noticeDetail.LotNo).ToList();
                    }
                    else
                    {
                        stock = stock.Where(s => string.IsNullOrWhiteSpace(s.LotNo)).ToList();
                    }
 
                    var sd = stock.FirstOrDefault();
                    if (sd == null)
                    {
                        throw new Exception($"未找到物料{noticeDetail.SkuNo}、批次{noticeDetail.LotNo} 的库存信息");
                    }
                    sd.LockQty -= noticeDetail.Qty;
                    noticeDetail.IsDel = "1";
                    noticeDetail.UpdateUser = userId;
                    noticeDetail.UpdateTime = DateTime.Now;
                    var n = 0;
                    var j = 0;
                    if (count < 1)
                    {
                        notice.IsDel = "1";
                        notice.UpdateUser = userId;
                        notice.UpdateTime = DateTime.Now;
 
                        n = Db.Updateable(notice).ExecuteCommand();
                        j = 1;
                    } 
                    var m = Db.Updateable(sd).UpdateColumns(it => new { it.LockQty }).ExecuteCommand();
 
                    var i = Db.Updateable(noticeDetail).ExecuteCommand();
 
                    //添加操作日志记录
                    var k = new OperationSOServer().AddLogOperationSo("出库作业", "出库单据", notice.SONo, "删除", $"删除了单据号为{notice.SONo}、物料为{noticeDetail.SkuNo}、批次号为{noticeDetail.LotNo}的单据明细信息", userId);
 
                    if (j>0)
                    {
                        if (n > 0 && m > 0 && i > 0 && k)
                        {
                            Db.CommitTran();
                            return true;
                        }
                    }
                    else
                    {
                        if ( m > 0 && i > 0 && k)
                        {
                            Db.CommitTran();
                            return true;
                        }
                    }
                    
                    Db.RollbackTran();
                    return false;
                }
                catch (Exception e)
                {
                    Db.RollbackTran();
                    throw new Exception(e.Message);
                }
 
            }
            catch (Exception e)
            {
                throw new Exception("删除出库单明细失败:"+e.Message);
            }
        }
    }
}