LqLaundrySupplierService.cs
13.8 KB
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
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NCC.Common.Core.Manager;
using NCC.Common.Enum;
using NCC.Common.Filter;
using NCC.Dependency;
using NCC.DynamicApiController;
using NCC.Extend.Entitys.Dto.LqLaundrySupplier;
using NCC.Extend.Entitys.Enum;
using NCC.Extend.Entitys.lq_laundry_supplier;
using NCC.Extend.Interfaces.LqLaundrySupplier;
using NCC.FriendlyException;
using NCC.System.Entitys.Permission;
using SqlSugar;
using Yitter.IdGenerator;
namespace NCC.Extend
{
/// <summary>
/// 清洗商服务
/// </summary>
[ApiDescriptionSettings(Tag = "绿纤清洗商管理", Name = "LqLaundrySupplier", Order = 200)]
[Route("api/Extend/LqLaundrySupplier")]
public class LqLaundrySupplierService : IDynamicApiController, ITransient, ILqLaundrySupplierService
{
private readonly IUserManager _userManager;
private readonly ILogger<LqLaundrySupplierService> _logger;
private readonly ISqlSugarClient _db;
/// <summary>
/// 构造函数
/// </summary>
public LqLaundrySupplierService(IUserManager userManager, ILogger<LqLaundrySupplierService> logger, ISqlSugarClient db)
{
_userManager = userManager;
_logger = logger;
_db = db;
}
#region 创建清洗商
/// <summary>
/// 创建清洗商
/// </summary>
/// <remarks>
/// 创建清洗商记录,一个清洗商对应一个产品类型一条记录
///
/// 示例请求:
/// ```json
/// {
/// "supplierName": "清洗商A",
/// "productType": "毛巾",
/// "laundryPrice": 5.00,
/// "remark": "备注信息"
/// }
/// ```
/// </remarks>
/// <param name="input">创建输入</param>
/// <returns>创建结果</returns>
/// <response code="200">创建成功</response>
/// <response code="400">清洗商已存在或参数错误</response>
/// <response code="500">服务器错误</response>
[HttpPost("Create")]
public async Task CreateAsync([FromBody] LqLaundrySupplierCrInput input)
{
try
{
// 检查是否已存在相同清洗商和产品类型的记录
var existing = await _db.Queryable<LqLaundrySupplierEntity>()
.Where(x => x.SupplierName == input.SupplierName
&& x.ProductType == input.ProductType
&& x.IsEffective == StatusEnum.有效.GetHashCode())
.FirstAsync();
if (existing != null)
{
throw NCCException.Oh($"清洗商【{input.SupplierName}】已存在{input.ProductType}的记录");
}
// 创建清洗商记录
var entity = new LqLaundrySupplierEntity
{
Id = YitIdHelper.NextId().ToString(),
SupplierName = input.SupplierName,
ProductType = input.ProductType,
LaundryPrice = input.LaundryPrice,
Remark = input.Remark,
IsEffective = StatusEnum.有效.GetHashCode(),
CreateUser = _userManager.UserId,
CreateTime = DateTime.Now
};
var isOk = await _db.Insertable(entity).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000);
}
catch (Exception ex)
{
_logger.LogError(ex, "创建清洗商失败");
throw NCCException.Oh($"创建失败:{ex.Message}");
}
}
#endregion
#region 更新清洗商
/// <summary>
/// 更新清洗商
/// </summary>
/// <remarks>
/// 更新清洗商信息,包括价格(更新价格不影响历史流水记录的价格)
/// </remarks>
/// <param name="input">更新输入</param>
/// <returns>更新结果</returns>
/// <response code="200">更新成功</response>
/// <response code="400">记录不存在或参数错误</response>
/// <response code="500">服务器错误</response>
[HttpPut("Update")]
public async Task UpdateAsync([FromBody] LqLaundrySupplierUpInput input)
{
try
{
var existing = await _db.Queryable<LqLaundrySupplierEntity>()
.Where(x => x.Id == input.Id && x.IsEffective == StatusEnum.有效.GetHashCode())
.FirstAsync();
if (existing == null)
{
throw NCCException.Oh("清洗商记录不存在或已失效");
}
// 如果清洗商名称或产品类型改变,检查是否冲突
if (existing.SupplierName != input.SupplierName || existing.ProductType != input.ProductType)
{
var conflict = await _db.Queryable<LqLaundrySupplierEntity>()
.Where(x => x.Id != input.Id
&& x.SupplierName == input.SupplierName
&& x.ProductType == input.ProductType
&& x.IsEffective == StatusEnum.有效.GetHashCode())
.FirstAsync();
if (conflict != null)
{
throw NCCException.Oh($"清洗商【{input.SupplierName}】已存在{input.ProductType}的记录");
}
}
// 更新记录
existing.SupplierName = input.SupplierName;
existing.ProductType = input.ProductType;
existing.LaundryPrice = input.LaundryPrice;
existing.Remark = input.Remark;
existing.UpdateUser = _userManager.UserId;
existing.UpdateTime = DateTime.Now;
var isOk = await _db.Updateable(existing).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000);
}
catch (Exception ex)
{
_logger.LogError(ex, "更新清洗商失败");
throw NCCException.Oh($"更新失败:{ex.Message}");
}
}
#endregion
#region 获取清洗商列表
/// <summary>
/// 获取清洗商列表
/// </summary>
/// <remarks>
/// 分页查询清洗商列表,支持按清洗商名称、产品类型筛选
/// </remarks>
/// <param name="input">查询输入</param>
/// <returns>清洗商列表</returns>
/// <response code="200">查询成功</response>
/// <response code="500">服务器错误</response>
[HttpGet("GetList")]
public async Task<dynamic> GetListAsync([FromQuery] LqLaundrySupplierListQueryInput input)
{
try
{
var sidx = string.IsNullOrEmpty(input.sidx) ? "createTime" : input.sidx;
var sort = string.IsNullOrEmpty(input.sort) ? "desc" : input.sort;
var data = await _db.Queryable<LqLaundrySupplierEntity>()
.WhereIF(!string.IsNullOrWhiteSpace(input.SupplierName), x => x.SupplierName.Contains(input.SupplierName))
.WhereIF(!string.IsNullOrWhiteSpace(input.ProductType), x => x.ProductType == input.ProductType)
.WhereIF(input.IsEffective.HasValue, x => x.IsEffective == input.IsEffective.Value)
.Select(x => new LqLaundrySupplierListOutput
{
id = x.Id,
supplierName = x.SupplierName,
productType = x.ProductType,
laundryPrice = x.LaundryPrice,
remark = x.Remark,
isEffective = x.IsEffective,
createUser = x.CreateUser,
createUserName = "",
createTime = x.CreateTime,
updateUser = x.UpdateUser,
updateUserName = "",
updateTime = x.UpdateTime
})
.MergeTable()
.OrderBy(sidx + " " + sort)
.ToPagedListAsync(input.currentPage, input.pageSize);
// 补充用户名称信息
var userIds = data.list.SelectMany(x => new[] { x.createUser, x.updateUser })
.Where(x => !string.IsNullOrEmpty(x))
.Distinct()
.ToList();
if (userIds.Any())
{
var userList = await _db.Queryable<UserEntity>()
.Where(x => userIds.Contains(x.Id))
.Select(x => new { x.Id, x.RealName })
.ToListAsync();
var userDict = userList.ToDictionary(k => k.Id, v => v.RealName);
foreach (var item in data.list)
{
item.createUserName = userDict.ContainsKey(item.createUser) ? userDict[item.createUser] : "";
item.updateUserName = !string.IsNullOrEmpty(item.updateUser) && userDict.ContainsKey(item.updateUser) ? userDict[item.updateUser] : "";
}
}
return PageResult<LqLaundrySupplierListOutput>.SqlSugarPageResult(data);
}
catch (Exception ex)
{
_logger.LogError(ex, "获取清洗商列表失败");
throw NCCException.Oh($"查询失败:{ex.Message}");
}
}
#endregion
#region 获取清洗商详情
/// <summary>
/// 获取清洗商详情
/// </summary>
/// <remarks>
/// 根据ID获取清洗商的详细信息
/// </remarks>
/// <param name="id">清洗商ID</param>
/// <returns>清洗商详情</returns>
/// <response code="200">查询成功</response>
/// <response code="400">记录不存在</response>
/// <response code="500">服务器错误</response>
[HttpGet("{id}")]
public async Task<LqLaundrySupplierInfoOutput> GetInfoAsync(string id)
{
try
{
var entity = await _db.Queryable<LqLaundrySupplierEntity>()
.Where(x => x.Id == id)
.Select(x => new LqLaundrySupplierInfoOutput
{
id = x.Id,
supplierName = x.SupplierName,
productType = x.ProductType,
laundryPrice = x.LaundryPrice,
remark = x.Remark,
isEffective = x.IsEffective,
createUser = x.CreateUser,
createUserName = "",
createTime = x.CreateTime,
updateUser = x.UpdateUser,
updateUserName = "",
updateTime = x.UpdateTime
})
.FirstAsync();
if (entity == null)
{
throw NCCException.Oh("清洗商记录不存在");
}
// 补充用户名称
if (!string.IsNullOrEmpty(entity.createUser))
{
var createUser = await _db.Queryable<UserEntity>()
.Where(x => x.Id == entity.createUser)
.Select(x => x.RealName)
.FirstAsync();
entity.createUserName = createUser ?? "";
}
if (!string.IsNullOrEmpty(entity.updateUser))
{
var updateUser = await _db.Queryable<UserEntity>()
.Where(x => x.Id == entity.updateUser)
.Select(x => x.RealName)
.FirstAsync();
entity.updateUserName = updateUser ?? "";
}
return entity;
}
catch (Exception ex)
{
_logger.LogError(ex, "获取清洗商详情失败");
throw NCCException.Oh($"查询失败:{ex.Message}");
}
}
#endregion
#region 删除/作废清洗商
/// <summary>
/// 删除/作废清洗商
/// </summary>
/// <remarks>
/// 将清洗商记录标记为无效
/// </remarks>
/// <param name="id">清洗商ID</param>
/// <returns>操作结果</returns>
/// <response code="200">操作成功</response>
/// <response code="400">记录不存在</response>
/// <response code="500">服务器错误</response>
[HttpDelete("{id}")]
public async Task DeleteAsync(string id)
{
try
{
var entity = await _db.Queryable<LqLaundrySupplierEntity>()
.Where(x => x.Id == id)
.FirstAsync();
if (entity == null)
{
throw NCCException.Oh("清洗商记录不存在");
}
entity.IsEffective = StatusEnum.无效.GetHashCode();
entity.UpdateUser = _userManager.UserId;
entity.UpdateTime = DateTime.Now;
var isOk = await _db.Updateable(entity).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000);
}
catch (Exception ex)
{
_logger.LogError(ex, "删除清洗商失败");
throw NCCException.Oh($"删除失败:{ex.Message}");
}
}
#endregion
}
}