Blame view

netcore/src/Modularity/Extend/NCC.Extend/LqZjlMdsmxszService.cs 13.2 KB
1e11e789   “wangming”   更新多个.DS_Store文件,优...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  using Mapster;
  using Microsoft.AspNetCore.Http;
  using Microsoft.AspNetCore.Mvc;
  using NCC.Common.Core.Manager;
  using NCC.Dependency;
  using NCC.DynamicApiController;
  using NCC.Extend.Entitys.Dto.LqZjlMdsmxsz;
  using NCC.Extend.Entitys.Entity.lq_zjl_mdsmxsz;
  using NCC.Extend.Interfaces.LqZjlMdsmxsz;
  using NCC.System.Entitys.Permission;
  using NCC.Extend.Entitys.lq_mdxx;
  using SqlSugar;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
2ce7c3cb   “wangming”   更新门店目标设定相关功能,新增字段...
17
18
19
20
  using Yitter.IdGenerator;
  using NCC.Common.Filter;
  using NCC.Common.Enum;
  using NCC.FriendlyException;
1e11e789   “wangming”   更新多个.DS_Store文件,优...
21
22
23
24
25
26
  
  namespace NCC.Extend
  {
      /// <summary>
      /// 事业部总经理门店生命线设置服务
      /// </summary>
3ab6f199   “wangming”   feat: 创建门店目标表和总经理...
27
      [ApiDescriptionSettings(Tag = "事业部总经理门店生命线设置服务(已废弃)", Name = "LqZjlMdsmxsz", Order = 200)]
1e11e789   “wangming”   更新多个.DS_Store文件,优...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
      [Route("api/Extend/[controller]")]
      public class LqZjlMdsmxszService : ILqZjlMdsmxszService, ITransient, IDynamicApiController
      {
          private readonly ISqlSugarClient _db;
          private readonly IUserManager _userManager;
          private readonly IHttpContextAccessor _httpContextAccessor;
  
          /// <summary>
          /// 构造函数
          /// </summary>
          /// <param name="db">数据库上下文</param>
          /// <param name="userManager">用户管理器</param>
          /// <param name="httpContextAccessor">HTTP上下文访问器</param>
          public LqZjlMdsmxszService(
              ISqlSugarClient db,
              IUserManager userManager,
              IHttpContextAccessor httpContextAccessor)
          {
              _db = db;
              _userManager = userManager;
              _httpContextAccessor = httpContextAccessor;
          }
2ce7c3cb   “wangming”   更新门店目标设定相关功能,新增字段...
50
  
1e11e789   “wangming”   更新多个.DS_Store文件,优...
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
          #region 根据总经理用户ID查询相关设置信息
          /// <summary>
          /// 根据总经理用户ID查询相关设置信息
          /// </summary>
          /// <remarks>
          /// 根据总经理用户ID查询其管理的所有门店的生命线设置信息
          /// 
          /// 示例请求:
          /// GET /api/Extend/LqZjlMdsmxsz/GetByZjlUserid?zjlUserid=zjl001
          /// 
          /// 业务逻辑:
          /// 1. 根据总经理用户ID查询生命线设置记录
          /// 2. 关联门店表获取门店详细信息
          /// 3. 关联用户表获取总经理姓名
          /// 4. 返回完整的设置信息列表
          /// </remarks>
          /// <param name="zjlUserid">总经理用户ID</param>
          /// <returns>总经理门店生命线设置列表</returns>
          /// <response code="200">成功返回设置列表</response>
          /// <response code="400">参数错误</response>
          /// <response code="500">服务器内部错误</response>
          [HttpGet("GetByZjlUserid")]
          public async Task<dynamic> GetByZjlUserid(string zjlUserid)
          {
              if (string.IsNullOrEmpty(zjlUserid))
              {
                  return new { list = new List<LqZjlMdsmxszListOutput>() };
              }
  
              var result = await _db.Queryable<LqZjlMdsmxszEntity>()
                  .LeftJoin<UserEntity>((smx, u) => smx.ZjlUserid == u.Id)
                  .LeftJoin<LqMdxxEntity>((smx, u, md) => smx.MdId == md.Id)
                  .Where((smx, u, md) => smx.ZjlUserid == zjlUserid && smx.Deletemark == 0)
                  .Select((smx, u, md) => new LqZjlMdsmxszListOutput
                  {
                      Id = smx.Id,
                      ZjlUserid = smx.ZjlUserid,
                      ZjlName = u.RealName,
                      MdId = smx.MdId,
                      Mdbm = md.Mdbm,
                      Dm = md.Dm,
                      Smx1 = smx.Smx1,
                      Tcbl1 = smx.Tcbl1,
                      Smx2 = smx.Smx2,
                      Tcbl2 = smx.Tcbl2,
                      Smx3 = smx.Smx3,
                      Tcbl3 = smx.Tcbl3,
                      Bz = smx.Bz,
                      Cjsj = smx.Cjsj,
                      Cjry = smx.Cjry,
                      Xgsj = smx.Xgsj,
                      Xgly = smx.Xgly
                  })
                  .ToListAsync();
  
              return new { list = result };
          }
          #endregion
  
          #region 批量创建事业部总经理门店生命线设置
          /// <summary>
          /// 批量创建事业部总经理门店生命线设置
          /// </summary>
          /// <remarks>
          /// 为指定总经理批量创建多个门店的生命线设置
          /// 
          /// 示例请求:
          /// ```json
          /// POST /api/Extend/LqZjlMdsmxsz/BatchCreate
          /// {
          ///   "zjlUserid": "zjl001",
          ///   "items": [
          ///     {
          ///       "mdId": "md001",
          ///       "smx1": 100000.00,
          ///       "tcbBl": 3.50,
          ///       "bz": "门店A生命线设置"
          ///     },
          ///     {
          ///       "mdId": "md002", 
          ///       "smx1": 150000.00,
          ///       "tcbBl": 4.00,
          ///       "bz": "门店B生命线设置"
          ///     }
          ///   ]
          /// }
          /// ```
          /// 
          /// 业务逻辑:
          /// 1. 验证总经理用户ID是否存在
          /// 2. 验证门店ID是否有效
          /// 3. 检查是否已存在相同的设置记录
          /// 4. 批量插入新的设置记录
          /// 5. 返回创建结果
          /// </remarks>
          /// <param name="input">批量创建输入参数</param>
          /// <returns>创建结果</returns>
          /// <response code="200">成功创建设置记录</response>
          /// <response code="400">参数验证失败</response>
          /// <response code="409">存在重复记录</response>
          /// <response code="500">服务器内部错误</response>
          [HttpPost("BatchCreate")]
          public async Task<dynamic> BatchCreate(LqZjlMdsmxszBatchCreateInput input)
          {
              // 验证总经理用户是否存在
              var userExists = await _db.Queryable<UserEntity>()
                  .Where(x => x.Id == input.ZjlUserid && x.DeleteMark == null)
                  .AnyAsync();
2ce7c3cb   “wangming”   更新门店目标设定相关功能,新增字段...
159
  
1e11e789   “wangming”   更新多个.DS_Store文件,优...
160
161
162
163
164
165
166
167
168
169
170
              if (!userExists)
              {
                  return new { success = false, message = "总经理用户不存在" };
              }
  
              // 验证门店ID是否有效
              var mdIds = input.Items.Select(x => x.MdId).ToList();
              var validMdIds = await _db.Queryable<LqMdxxEntity>()
                  .Where(x => mdIds.Contains(x.Id))
                  .Select(x => x.Id)
                  .ToListAsync();
2ce7c3cb   “wangming”   更新门店目标设定相关功能,新增字段...
171
  
1e11e789   “wangming”   更新多个.DS_Store文件,优...
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
              var invalidMdIds = mdIds.Except(validMdIds).ToList();
              if (invalidMdIds.Any())
              {
                  return new { success = false, message = $"以下门店ID不存在: {string.Join(", ", invalidMdIds)}" };
              }
  
              // 处理每个门店设置(支持新增和修改)
              var currentTime = DateTime.Now;
              var currentUser = _userManager.User?.Id ?? "system";
              var addCount = 0;
              var updateCount = 0;
  
              foreach (var item in input.Items)
              {
                  // 检查是否已存在
                  var existing = await _db.Queryable<LqZjlMdsmxszEntity>()
                      .Where(x => x.ZjlUserid == input.ZjlUserid && x.MdId == item.MdId && x.Deletemark == 0)
                      .FirstAsync();
  
                  if (existing != null)
                  {
                      // 更新现有记录
                      existing.Smx1 = item.Smx1;
                      existing.Tcbl1 = item.Tcbl1;
                      existing.Smx2 = item.Smx2;
                      existing.Tcbl2 = item.Tcbl2;
                      existing.Smx3 = item.Smx3;
                      existing.Tcbl3 = item.Tcbl3;
                      existing.Bz = item.Bz;
                      existing.Xgsj = currentTime;
                      existing.Xgly = currentUser;
  
                      await _db.Updateable(existing).ExecuteCommandAsync();
                      updateCount++;
                  }
                  else
                  {
                      // 新增记录
                      var entity = new LqZjlMdsmxszEntity
                      {
2ce7c3cb   “wangming”   更新门店目标设定相关功能,新增字段...
212
                          Id = YitIdHelper.NextId().ToString(),
1e11e789   “wangming”   更新多个.DS_Store文件,优...
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
                          ZjlUserid = input.ZjlUserid,
                          MdId = item.MdId,
                          Smx1 = item.Smx1,
                          Tcbl1 = item.Tcbl1,
                          Smx2 = item.Smx2,
                          Tcbl2 = item.Tcbl2,
                          Smx3 = item.Smx3,
                          Tcbl3 = item.Tcbl3,
                          Bz = item.Bz,
                          Cjsj = currentTime,
                          Cjry = currentUser,
                          Deletemark = 0
                      };
  
                      await _db.Insertable(entity).ExecuteCommandAsync();
                      addCount++;
                  }
              }
2ce7c3cb   “wangming”   更新门店目标设定相关功能,新增字段...
231
232
233
234
235
  
              return new
              {
                  success = true,
                  message = "设置成功",
1e11e789   “wangming”   更新多个.DS_Store文件,优...
236
237
238
239
240
241
242
243
                  addCount = addCount,
                  updateCount = updateCount,
                  totalCount = addCount + updateCount
              };
          }
          #endregion
  
          #region 获取总经理所在组织能管理的门店
2ce7c3cb   “wangming”   更新门店目标设定相关功能,新增字段...
244
          /// <summary>
1e11e789   “wangming”   更新多个.DS_Store文件,优...
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
          /// 获取总经理所在组织能管理的门店
          /// </summary>
          /// <remarks>
          /// 根据总经理的组织权限,获取其能管理的所有门店列表
          /// 
          /// 示例请求:
          /// GET /api/Extend/LqZjlTcsz/ManagedStores
          /// 
          /// 业务逻辑:
          /// 2. 根据用户组织ID获取下属组织列表
          /// 3. 根据组织权限过滤门店数据
          /// 4. 返回门店基本信息列表
          /// </remarks>
          /// <returns>总经理能管理的门店列表</returns>
          /// <response code="200">成功返回门店列表</response>
          /// <response code="401">未授权访问</response>
          /// <response code="500">服务器内部错误</response>
          [HttpGet("ManagedStores/{userid}")]
          public async Task<dynamic> GetManagedStores(string userid)
          {
              var userOrganizeId = await _db.Queryable<UserEntity>().Where(x => x.Id == userid && x.DeleteMark == null).Select(x => x.OrganizeId).FirstAsync();
              // 根据组织权限获取门店
              var stores = await _db.Queryable<LqMdxxEntity>()
2ce7c3cb   “wangming”   更新门店目标设定相关功能,新增字段...
268
                  .Where(s => s.Syb == userOrganizeId || s.Jyb == userOrganizeId || s.Kjb == userOrganizeId || s.Dxmb == userOrganizeId)
1e11e789   “wangming”   更新多个.DS_Store文件,优...
269
270
271
272
                  .ToListAsync();
              return new { list = stores };
          }
          #endregion
2ce7c3cb   “wangming”   更新门店目标设定相关功能,新增字段...
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
  
          #region 分页查询
          /// <summary>
          /// 获取所有总经理门店生命线设置数据(分页查询)
          /// </summary>
          /// <param name="input">查询输入参数</param>
          /// <returns>分页数据</returns>
          [HttpGet("GetPageList")]
          public async Task<dynamic> GetPageList([FromQuery] LqZjlMdsmxszListQueryInput input)
          {
              var sidx = input.sidx == null ? "id" : input.sidx;
              var data = await _db.Queryable<LqZjlMdsmxszEntity>()
              .WhereIF(!string.IsNullOrEmpty(input.ZjlUserid), p => p.ZjlUserid.Contains(input.ZjlUserid))
              .WhereIF(!string.IsNullOrEmpty(input.MdId), p => p.MdId.Contains(input.MdId))
                .Select(it => new LqZjlMdsmxszListOutput
                {
                    Id = it.Id,
                    ZjlUserid = it.ZjlUserid,
                    ZjlName = SqlFunc.Subqueryable<UserEntity>().Where(x => x.Id == it.ZjlUserid).Select(x => x.RealName),
                    MdId = it.MdId,
                    Mdbm = SqlFunc.Subqueryable<LqMdxxEntity>().Where(x => x.Id == it.MdId).Select(x => x.Mdbm),
                    Dm = SqlFunc.Subqueryable<LqMdxxEntity>().Where(x => x.Id == it.MdId).Select(x => x.Dm),
                    Smx1 = it.Smx1,
                    Tcbl1 = it.Tcbl1,
                    Smx2 = it.Smx2,
                    Tcbl2 = it.Tcbl2,
                    Smx3 = it.Smx3,
                    Tcbl3 = it.Tcbl3,
                    Bz = it.Bz,
                    Cjsj = it.Cjsj,
                    Cjry = it.Cjry,
                    Xgsj = it.Xgsj,
                    Xgly = it.Xgly
                }).MergeTable().OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
              return PageResult<LqZjlMdsmxszListOutput>.SqlSugarPageResult(data);
          }
          #endregion
  
          #region 更新总经理门店生命线设置
          /// <summary>
          /// 更新总经理门店生命线设置
          /// </summary>
          [HttpPut("{id}")]
          public async Task Update(string id, [FromBody] LqZjlMdsmxszUpInput input)
          {
              var entity = await _db.Queryable<LqZjlMdsmxszEntity>()
                  .Where(x => x.Id == id && x.Deletemark == 0)
                  .FirstAsync();
  
              if (entity == null)
              {
                  throw NCCException.Oh(ErrorCode.COM1001);
              }
  
              // 更新字段
              entity.Smx1 = input.Smx1;
              entity.Tcbl1 = input.Tcbl1;
              entity.Smx2 = input.Smx2;
              entity.Tcbl2 = input.Tcbl2;
              entity.Smx3 = input.Smx3;
              entity.Tcbl3 = input.Tcbl3;
              entity.Bz = input.Bz;
              entity.Xgsj = DateTime.Now;
              entity.Xgly = _userManager.User?.Id ?? "admin";
  
              var isOk = await _db.Updateable(entity).ExecuteCommandAsync();
              if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001);
          }
          #endregion
  
1e11e789   “wangming”   更新多个.DS_Store文件,优...
343
344
      }
  }