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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model.ModelDto.SysDto;
using SqlSugar;
using WMS.Entity.SysEntity;
using WMS.IBLL.ILogServer;
using WMS.IBLL.ISysServer;
using WMS.IDAL.ISysInterface;
 
namespace WMS.BLL.SysServer
{
    public class UnitServer : IUnitServer
    {
        public IUnitRepository UnitRst { get; set; }
        private readonly IOperationSysServer _operation;
 
        public UnitServer(IUnitRepository unitRst, IOperationSysServer operation)
        {
            UnitRst = unitRst;
            _operation = operation;
        }
 
        /// <summary>
        /// 查询计量单位信息
        /// </summary>
        /// <param name="unitNo">单位编号</param>
        /// <param name="unitName">单位名称</param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<UnitDto> GetUnitList(string unitNo, string unitName, int page, int limit, out int count)
        {
            try
            {
                Expression<Func<SysUnit, bool>> item = Expressionable.Create<SysUnit>() //创建表达式
                    .AndIF(!string.IsNullOrWhiteSpace(unitNo), it => it.UnitNo.Contains(unitNo.Trim()))
                    .AndIF(!string.IsNullOrWhiteSpace(unitName), it => it.UnitName.Contains(unitName.Trim()))
                    .ToExpression();//注意 这一句 不能少
 
 
                var data = UnitRst.GetAllByOrderPageAsync(item, limit, page, out int counts)
                    .Includes(x => x.CreateUserInfo)
                    .Includes(x => x.UpdateUserInfo).ToList();
                count = counts;
                return data.Select(m => new UnitDto()
                {
                    Id = m.Id,
                    UnitNo = m.UnitNo,
                    UnitName = m.UnitName,
                    Abbrev = m.Abbrev,
                    CreateTime = m.CreateTime,
                    CreateUserName = m.CreateUserInfo == null ? "" : m.CreateUserInfo.RealName,
                    UpdateTime = m.UpdateTime,
                    UpdateUserName = m.UpdateUserInfo == null ? "" : m.UpdateUserInfo.RealName
                }).ToList();
 
 
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// 获取单条单位信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public SysUnit GetUnit(int id)
        {
            try
            {
                var data = UnitRst.GetOneById(id);
                return data;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// 添加单位信息
        /// </summary>
        /// <param name="unitName">名称</param>
        /// <param name="abbrev">缩写</param>
        /// <param name="userId">操作人</param>
        /// <returns></returns>
        public bool AddUnit(string unitName, string abbrev, int userId)
        {
            try
            {
                if (string.IsNullOrEmpty(abbrev))
                {
                    var date = UnitRst.GetAllWhereAsync(m => m.UnitName == unitName).Count();
                    if (date > 0)
                    {
                        throw new Exception("单位名称重复");
                    }
                }
                else
                {
                    var date = UnitRst.GetAllWhereAsync(m => m.UnitName == unitName || m.Abbrev == abbrev).Count();
                    if (date > 0)
                    {
                        throw new Exception("单位名称或英文缩写重复");
                    }
                }
 
                //自动UnitCode处理
                var unitList = UnitRst.GetAllAsync().OrderByDescending(m => m.UnitNo);
                var code = "01";
                if (unitList != null && unitList.Count()>0)
                {
                    var unitCode = unitList.First().UnitNo;
                    var str = unitCode.Substring(0, 1);
                    var str2 = unitCode.Substring(1, 1);
                    code = str == "0" ? (str2 == "9" ? (int.Parse(str2) + 1).ToString() : "0" + (int.Parse(str2) + 1)) : (int.Parse(unitCode) + 1).ToString();
                }
 
                var num = UnitRst.Add(new SysUnit()
                {
                    UnitNo = code,
                    UnitName = unitName,
                    Abbrev = abbrev,
                    CreateUser = userId
                });
                if (num > 0)
                {
                    _operation.InsertOperation("基础信息", "计量单位", code, "添加", "添加计量单位 单位号:" + code, Convert.ToInt32(userId));
                }
                return num > 0;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// 编辑单位信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="unitName">名称</param>
        /// <param name="abbrev">缩写</param>
        /// <param name="userId">操作人</param>
        /// <returns></returns>
        public bool EditUnit(int id, string unitName, string abbrev, int userId)
        {
            try
            {
                if (string.IsNullOrEmpty(abbrev))
                {
                    var date = UnitRst.GetAllWhereAsync(m => m.Id != id && m.UnitName == unitName).Count();
                    if (date > 0)
                    {
                        throw new Exception("单位名称重复");
                    }
                }
                else
                {
                    var date = UnitRst.GetAllWhereAsync(m => m.Id != id && (m.UnitName == unitName || m.Abbrev == abbrev)).Count();
                    if (date > 0)
                    {
                        throw new Exception("单位名称或英文缩写重复");
                    }
                }
 
                //自动UnitCode处理
                var unit = UnitRst.GetOneById(id);
                if (unit == null)
                {
                    throw new Exception("未查询到单位信息");
                }
                unit.UnitName = unitName;
                unit.Abbrev = abbrev;
                unit.UpdateUser = userId;
                unit.UpdateTime = DateTime.Now;
 
                var num = UnitRst.Edit(unit);
                return num > 0;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
        public bool DelUnit(int id, int userId)
        {
            try
            {
                var unit = UnitRst.GetOneById(id);
                if (unit == null)
                {
                    throw new Exception("未查询到单位信息");
                }
                int num = UnitRst.Remove(id, userId);
                if (num > 0)
                {
                    _operation.InsertOperation("基础信息", "计量单位", unit.UnitNo, "删除", "删除计量单位 单位号:" + unit.UnitNo, Convert.ToInt32(userId));
                }
 
                return num > 0;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
        public bool DelsUnit(List<int> ids, int userId)
        {
            try
            {
                var list = UnitRst.GetAllWhereAsync(m => ids.Contains(m.Id)).ToList();
                if (list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        _operation.InsertOperation("基础信息", "计量单位", list[i].UnitNo, "删除", "删除计量单位 单位号:" + list[i].UnitNo, Convert.ToInt32(userId));
                    }
                }
                var num = UnitRst.RemoveAll(list, userId);
                return num > 0;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
    }
}