wxw
2025-05-12 c7c2f7aa20427204944ba80a2704232b2f281582
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
using Model.ModelDto;
using Model.ModelVm;
using SqlSugar;
using System;
using System.Collections.Generic;
using WMS.BLL.LogServer;
using WMS.DAL;
using WMS.Entity.BllAsnEntity;
using WMS.Entity.Context;
using WMS.IBLL.IBllAsnServer;
 
namespace WMS.BLL.BllAsnServer
{
    public class BllBoxInfoServer:DbHelper<BllBoxInfo>,IBllBoxInfoServer
    {
        private static readonly SqlSugarScope Db = DataContext.Db;
 
        public BllBoxInfoServer():base(Db)
        {
        }
 
        //添加箱码信息
        public string AddBoxInfo(BoxInfoVm model)
        {
            string strMessage = "";
            string sqlString = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(model.BoxNo))
                {
                    strMessage = "-1:箱码不可为空!";
                    return strMessage;
                }
                //if (string.IsNullOrEmpty(model.BoxNo2))
                //{
                //    strMessage = "-1:盒码不可为空!";
                //    return strMessage;
                //}
                if (string.IsNullOrEmpty(model.BoxNo3))
                {
                    strMessage = "-1:支码不可为空!";
                    return strMessage;
                }
                if (string.IsNullOrEmpty(model.SkuNo)) 
                {
                    strMessage = "-1:物料编码不可为空!";
                    return strMessage;
                }
                if (string.IsNullOrEmpty(model.LotNo))
                {
                    strMessage = "-1:批次号不可为空!";
                    return strMessage;
                }
                if (model.Qty == null || model.Qty <= 0)
                {
                    strMessage = "-1:实际数量必须大于0!";
                    return strMessage;
                }
                if (string.IsNullOrEmpty(model.ProductionTime))
                {
                    strMessage = "-1:生产日期不可为空!";
                    return strMessage;
                }
                
                // 验证是否重复 支/袋码
                sqlString += $@"select count(id) from BllBoxInfo where BoxNo3 = '{model.BoxNo3}' and IsDel = 0 ";
                int rowCount = Db.Ado.GetInt(sqlString);
                if (rowCount > 0) 
                {
                    strMessage = "-1:重复数据!";
                    return strMessage;
                }
                // 获取物料信息
                sqlString = "select tb1.SkuNo,tb1.SkuName,tb1.Warranty, ";
                sqlString += "case tb2.level when 5 then tb2.L4Num when 4 then tb2.L3Num when 3 then tb2.L2Num else 0 end as FullQty ";
                sqlString += "from SysMaterials as tb1 left join SysPackag as tb2 on tb1.PackagNo = tb2.PackagNo ";
                sqlString += $"where SkuNo = '{model.SkuNo}' and tb1.IsDel = '0';";
                var materialModel = Db.Ado.SqlQuery<BoxInfoVm>(sqlString);
                if (materialModel.Count <= 0) 
                {
                    strMessage = "-1:该物料信息不存在!";
                    return strMessage;
                }
                DateTime Ptime = DateTime.Parse(model.ProductionTime);
                double dayNum = (int)materialModel[0].Warranty;
                model.ExpirationTime = Ptime.AddDays(dayNum);
                if (string.IsNullOrEmpty(model.InspectMark)) 
                {
                    model.InspectMark = "0";
                }
                if (string.IsNullOrEmpty(model.BitBoxMark))
                {
                    model.BitBoxMark = "0";
                }
 
                // 插入信息
                sqlString = "Insert into BllBoxInfo (BoxNo,BoxNo2,BoxNo3,SkuNo,SkuName,LotNo,LotText,";
                sqlString += "Qty,FullQty,ProductionTime,ExpirationTime,InspectMark,InspectStatus,BitBoxMark,Origin,Status,CreateUser) values ( ";
                sqlString += $"'{model.BoxNo}','{model.BoxNo2}','{model.BoxNo3}','{model.SkuNo}', ";
                sqlString += $"'{materialModel[0].SkuName}','{model.LotNo}','{model.LotText}','{model.Qty}','{materialModel[0].FullQty}',";
                sqlString += $"'{model.ProductionTime}','{model.ExpirationTime}','{model.InspectMark}','1','{model.BitBoxMark}','{model.Origin}','0','{model.CreateUser}');";
 
                rowCount = Db.Ado.ExecuteCommand(sqlString);
                if (rowCount <= 0)
                {
                    strMessage = "-1:添加失败!";
                }
                else
                {
                    if (model.Origin == "录入")
                    {
                        new OperationASNServer().AddLogOperationAsn("入库作业", "物料信息录入", model.BoxNo3, "添加", $"添加了箱码:{model.BoxNo}、追溯码:{model.BoxNo3}的箱支物料信息", Convert.ToInt32(model.CreateUser));
                    }
                }
 
                return strMessage;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
 
        //删除箱码信息
        public string DelBoxInfo(BoxInfoVm model)
        {
            string sqlString = string.Empty;
            try
            {
                var boxInfo = Db.Queryable<BllBoxInfo>().First(m => m.IsDel == "0" && m.Id == model.Id);
                if (boxInfo == null)
                {
                    throw new Exception("未查询到信息");
                }
                // 删除明细单
                sqlString += $"UPDATE BllBoxInfo SET IsDel = '1',";
                sqlString += $"UpdateTime = GETDATE(),UpdateUser = '{model.CreateUser}' ";
                sqlString += $"WHERE Id = '{model.Id}' and Status = '0';";
 
                int rowCount = Db.Ado.ExecuteCommand(sqlString);
                if (rowCount <= 0)
                {
                    return "-1:状态已变更无法删除!";
                }
                new OperationASNServer().AddLogOperationAsn("入库作业", "物料信息录入", boxInfo.BoxNo3, "删除", $"删除了箱码:{boxInfo.BoxNo}、追溯码:{boxInfo.BoxNo3}的箱支物料信息", Convert.ToInt32(model.CreateUser));
                return "";
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
 
        //获取箱码信息(未绑定托盘的箱码)
        public List<BoxInfoDto> GetBoxInfoList(BoxInfoVm model, out int count)
        {
            string sqlString = string.Empty;
            string sqlCount = string.Empty;
            string sqlPub = string.Empty;
            try
            {
                if (model.Page == 0)
                {
                    model.Page = 1;
                }
                sqlCount += $"select tb1.* ,tb2.RealName as CreateUserName,tb3.RealName as UpdateUserName from BllBoxInfo  as tb1 ";
                sqlString += "select tb1.* ,tb2.RealName as CreateUserName,tb3.RealName as UpdateUserName from BllBoxInfo  as tb1  ";
                sqlPub += "left join SysUserInfor as tb2 on tb1.CreateUser = tb2.id ";
                sqlPub += "left join SysUserInfor as tb3 on tb1.CreateUser = tb3.id ";
                sqlPub += $"WHERE tb1.BoxNo LIKE '%{model.BoxNo}%' AND tb1.BoxNo2 LIKE '%{model.BoxNo2}%' ";
                sqlPub += $"AND tb1.BoxNo3  LIKE '%{model.BoxNo3}%' AND tb1.SkuNo  LIKE '%{model.SkuNo}%' ";
                sqlPub += $"AND tb1.LotNo  LIKE '%{model.LotNo}%' AND tb1.LotText  LIKE '%{model.LotText}%' ";
 
                if (!string.IsNullOrEmpty(model.ProductionTime))
                {
                    sqlPub += $"AND tb1.ProductionTime >= '{model.ProductionTime}' ";
                }
                if (!string.IsNullOrEmpty(model.InspectMark))
                {
                    sqlPub += $"AND tb1.InspectMark = '{model.InspectMark}' ";
                }
                if (!string.IsNullOrEmpty(model.BitBoxMark))
                {
                    sqlPub += $"AND tb1.BitBoxMark = '{model.BitBoxMark}' ";
                }
                //if (!string.IsNullOrEmpty(model.Origin))
                //{
                //    sqlPub += $"AND tb1.Origin = '{model.Origin}' ";
                //}
 
                sqlPub += $"AND tb1.IsDel = '0' and tb1.Status = '0' order by tb1.CreateTime desc ";
                sqlString += sqlPub + $"offset {((model.Page - 1) * model.Limit)} rows fetch next {model.Limit} rows only;";
 
                count = Db.Ado.GetInt(sqlCount + sqlPub);
                List<BoxInfoDto> modelList = new List<BoxInfoDto>();
                if (count != 0)
                {
                    modelList = Db.Ado.SqlQuery<BoxInfoDto>(sqlString);
                }
 
                return modelList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        //导入箱码信息
        public string ImportBoxInfo(BoxInfoVms models)
        {
            string strMessage = "";
            try
            {
                if (models.ListBoxInfo.Count <= 0) 
                {
                    strMessage = "-1:文件内无数据请核实!";
                }
 
                var num = 0;
                foreach (BoxInfoVm model in models.ListBoxInfo) 
                {
                    try
                    {
                        model.CreateUser = models.CreateUser;
                        model.Origin = "导入";
                        var msg = AddBoxInfo(model);
                        strMessage += msg;
                        if (msg == "")
                        {
                            num += 1;
                            new OperationASNServer().AddLogOperationAsn("入库作业", "物料信息录入", model.BoxNo3, "导入", $"导入了箱码:{model.BoxNo}、追溯码:{model.BoxNo3}箱支物料信息", Convert.ToInt32(model.CreateUser));
                        }
                    }
                    catch
                    {
                        // ignored
                    }
                }
 
                if (strMessage.Contains("-1") && num > 0)
                {
                    return "部分导入成功" + strMessage;
                }
                if (num>0)
                {
                    return "导入成功" + strMessage;
                }
 
                return strMessage;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}