bklLiudl
2025-04-08 a82e815f97b789dc4ca208df09dd96534a023f4a
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
using Model.ModelDto;
using Model.ModelDto.BllSoDto;
using Model.ModelDto.SysDto;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using WMS.DAL;
using WMS.Entity.BllSoEntity;
using WMS.Entity.Context;
using WMS.Entity.SysEntity;
using WMS.IBLL.ISysServer;
 
namespace WMS.BLL.SysServer
{
    /// <summary>
    ///  物料类别管理方法
    /// </summary>
    public class MaterialCategoryServer : DbHelper<SysMaterialCategory>, IMaterialCategoryServer
    {
 
        private static readonly SqlSugarScope Db = DataContext.Db;
        public MaterialCategoryServer() : base(Db)
        {
        }
 
        /// <summary>
        /// 获取物料类别信息
        /// </summary>
        /// <param name="categoryName">类别名称</param>
        /// <param name="areaNo">区域编码</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public List<MaterialCategoryDto> GetMaterialCategories(string categoryName, string areaNo)
        {
            try
            {
                //var list = Db.Queryable<MaterialCategoryDto>()
                //    .WhereIF(!string.IsNullOrWhiteSpace(categoryName), a => a.CategoryName == categoryName)
                //    .WhereIF(!string.IsNullOrWhiteSpace(areaNo), a => a.AreaNo == areaNo)
                //    .Where(a => a.IsDel == "0").ToList();
 
 
                Expression<Func<SysMaterialCategory, bool>> item = Expressionable.Create<SysMaterialCategory>()
                   .AndIF(!string.IsNullOrWhiteSpace(categoryName), it => it.CategoryName.Contains(categoryName))
                   .AndIF(!string.IsNullOrWhiteSpace(areaNo), it => it.AreaNo.Contains(areaNo))
                   .And(it => it.IsDel == "0")
                   .ToExpression();//注意 这一句 不能少
 
                var data = GetAllWhereAsync(item)
                    .Includes(x => x.CreateUserInfo)
                    .Includes(x => x.UpdateUserInfo).ToList();
                //.LeftJoin<SysWareHouse>((a, b) => a.WareHouseNo == b.WareHouseNo)
                //.LeftJoin<SysUserInfor>((a, b, c) => a.CreateUser == c.Id)
                //.LeftJoin<SysUserInfor>((a, b, c, d) => a.UpdateUser == d.Id)
                var data2 = data.Select(a => new MaterialCategoryDto()
                {
                    Id = a.Id,
 
                    CategoryNo = a.CategoryNo, //类别编码
                    CategoryName = a.CategoryName, //类别名称
 
                    AreaNo = a.AreaNo, //区域号
                    AreaName = GetStorageArea(a.AreaNo), //区域名称
 
                    //WareHouseNo = a.WareHouseNo, //仓库号
                    //WareHouseName = a.WareHouseInfo.WareHouseName, //仓库名称
 
                    Demo = a.Demo, //备注
                    IsDel = a.IsDel,
 
                    CreateName = a.CreateUserInfo.RealName,
                    UpdateName = a.UpdateUserInfo == null ? "" : a.UpdateUserInfo.RealName,
 
                    CreateTime = a.CreateTime,
                    UpdateTime = a.UpdateTime,
                }).ToList();
 
 
 
                return data2;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
 
        /// <summary>
        /// 获取物料类别下拉菜单信息
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public List<SysMaterialCategory> GetMaterialCategories()
        {
            try
            {
                //var list = Db.Queryable<MaterialCategoryDto>()
                //    .WhereIF(!string.IsNullOrWhiteSpace(categoryName), a => a.CategoryName == categoryName)
                //    .WhereIF(!string.IsNullOrWhiteSpace(areaNo), a => a.AreaNo == areaNo)
                //    .Where(a => a.IsDel == "0").ToList();
 
                var list = Db.Queryable<SysMaterialCategory>().Where(a => a.IsDel == "0").ToList();
                return list;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
 
        /// <summary>
        /// 根据Id获取物料类别信息
        /// </summary>
        /// <param name="Id">Id</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public SysMaterialCategory GetMaterialCategoriesById(int Id)
        {
            try
            {
                var list = Db.Queryable<SysMaterialCategory>().Where(a => a.IsDel == "0" && a.Id == Id).First();
                return list;
            }
            catch (Exception ex)
            {
 
                throw new Exception(ex.Message);
            }
        }
 
        /// <summary>
        /// 获取巷道信息
        /// </summary>
        /// <param name="AreaNo">巷道号</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public string GetStorageArea(string AreaNo)
        {
            try
            {
                string str = "";
                if (string.IsNullOrWhiteSpace(AreaNo))
                {
                    return str;
                }
                else
                {
                    //判断是否需要进行分割字符串
                    var arr = AreaNo.Split(',');
                    //获取区域信息
                    var list = Db.Queryable<SysStorageArea>().Where(a => a.IsDel == "0" && arr.Contains(a.AreaNo)).ToList();
                    //拼接字符串
 
                    foreach (var item in list)
                    {
                        str += item.AreaName + ";";
                    }
                    return str;
                }
 
            }
            catch (Exception ex)
            {
 
                throw new Exception("获取巷道信息异常" + ex.Message);
            }
        }
 
        /// <summary>
        /// 获取库区域信息
        /// </summary>
        /// <returns></returns>
        public List<SysStorageArea> GetStorageAreaList()
        {
            try
            {
                var list = Db.Queryable<SysStorageArea>().Where(a => a.IsDel == "0").ToList();
                return list;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
        /// <summary>
        /// 新增类别信息
        /// </summary>
        /// <param name="category">物料类别实体</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public string InsertMaterialCategories(SysMaterialCategory category)
        {
            try
            {
                string msg = "";
                //获取类别信息
                var categoryInfo = Db.Queryable<SysMaterialCategory>().First(a => a.IsDel == "0" && a.CategoryNo == category.CategoryNo);
                //获取区域信息
                var area = Db.Queryable<SysStorageArea>().Where(a => category.AreaNo.Contains(a.AreaNo) && a.IsDel == "0").ToList();
                if (categoryInfo != null)
                {
                    throw new Exception("当前类别信息已被创建,请重新填写信息!");
                }
                if (area.Count == 0)
                {
                    throw new Exception("当前选择区域信息异常,请重新选择或联系管理员!");
                }
 
                Db.BeginTran();
                SysMaterialCategory list = new SysMaterialCategory()
                {
                    CategoryNo = category.CategoryNo, //类别编码
                    CategoryName = category.CategoryName, //类别名称
 
                    AreaNo = category.AreaNo, //区域编码
                    Demo = category.Demo, //备注
 
                    IsDel = "0", //是否删除
                    CreateUser = category.CreateUser, //创建人
                    CreateTime = Db.GetDate(), //创建时间
                };
 
                Db.Insertable(list).ExecuteCommand();
 
 
                Db.CommitTran();
 
                msg = "新增类别信息成功!";
                return msg;
 
 
            }
            catch (Exception ex)
            {
                Db.RollbackTran();
                throw new Exception(ex.Message);
            }
        }
 
        /// <summary>
        /// 编辑类别信息
        /// </summary>
        /// <param name="category">物料类别实体</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public string ExitMaterialCategories(SysMaterialCategory category)
        {
            try
            {
                string msg = "";
                //获取类别信息
                var categoryInfo = Db.Queryable<SysMaterialCategory>().First(a => a.IsDel == "0" && a.Id == category.Id);
                //获取类别对应物料信息
                var matelist = Db.Queryable<SysMaterials>().Where(a=>a.IsDel == "0" && a.CategoryNo == categoryInfo.CategoryNo).ToList();
                //获取区域信息
                var area = Db.Queryable<SysStorageArea>().First(a => category.AreaNo.Contains(a.AreaNo) && a.IsDel == "0");
 
                if (area == null)
                {
                    throw new Exception("当前选择区域信息异常,请重新选择或联系管理员!");
                }
 
                Db.BeginTran();
                categoryInfo.CategoryNo = category.CategoryNo; //类别编码
                categoryInfo.CategoryName = category.CategoryName; //类别名称
                categoryInfo.AreaNo = category.AreaNo; //区域编码
                categoryInfo.Demo = category.Demo; //备注
                categoryInfo.UpdateUser = category.CreateUser; //更改人
                categoryInfo.UpdateTime = Db.GetDate(); //更改时间
 
                Db.Updateable(categoryInfo).ExecuteCommand();
 
                foreach (var item in matelist)
                {
                    item.CategoryNo = categoryInfo.CategoryNo;
                }
                Db.Updateable(matelist).ExecuteCommand();
 
                Db.CommitTran();
 
                msg = "编辑类别信息成功!";
                return msg;
 
 
            }
            catch (Exception ex)
            {
                Db.RollbackTran();
                throw new Exception(ex.Message);
            }
        }
 
        /// <summary>
        /// 删除类别信息
        /// </summary>
        /// <param name="category">物料类别实体</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public string DeleteMaterialCategories(MaterialCategoryDto category)
        {
            try
            {
                string msg = "";
                //获取类别信息
                var categoryInfo = Db.Queryable<SysMaterialCategory>().First(a => a.IsDel == "0" && a.Id == category.Id);
                //获取区域信息
                var area = Db.Queryable<SysStorageArea>().First(a => categoryInfo.AreaNo.Contains(a.AreaNo) && a.IsDel == "0");
                //获取是否存在有物料已绑定当前类别
                var mate = Db.Queryable<SysMaterials>().Where(a => a.IsDel == "0" && a.CategoryNo == categoryInfo.CategoryNo).ToList();
 
                //判断是否存在有物料已绑定当前类别
                if (mate.Count > 0)
                {
                    throw new Exception("当前类别已有物料进行绑定,无法删除!");
                }
                if (categoryInfo == null)
                {
                    throw new Exception("当前类别信息已被删除,请重新选择!");
                }
                if (area == null)
                {
                    throw new Exception("当前选择区域信息异常,请重新选择或联系管理员!");
                }
 
                Db.BeginTran();
 
                categoryInfo.IsDel = "1";
                categoryInfo.UpdateUser = int.Parse(category.UpdateUser);
                categoryInfo.UpdateTime = Db.GetDate();
 
                Db.Updateable(categoryInfo).ExecuteCommand();
 
                Db.CommitTran();
 
                msg = "删除类别信息成功!";
                return msg;
 
 
            }
            catch (Exception ex)
            {
                Db.RollbackTran();
                throw new Exception(ex.Message);
            }
        }
    }
}