Blame view

netcore/src/Modularity/Extend/NCC.Extend/LqJdqdService.cs 10.9 KB
96009bc9   hexiaodong   hxd
1
2
3
4
5
6
7
  using NCC.Common.Core.Manager;
  using NCC.Common.Enum;
  using NCC.Common.Extension;
  using NCC.Common.Filter;
  using NCC.Dependency;
  using NCC.DynamicApiController;
  using NCC.FriendlyException;
e1dcb3a0   “wangming”   1
8
  using NCC.Extend.Interfaces.BusinessOperationLog;
96009bc9   hexiaodong   hxd
9
10
11
12
13
14
15
16
  using NCC.Extend.Interfaces.LqJdqd;
  using Mapster;
  using Microsoft.AspNetCore.Mvc;
  using SqlSugar;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
efde7d04   “wangming”   更新多个文件,删除不再使用的CSS...
17
  using NCC.Extend.Entitys.lq_jdqd;
96009bc9   hexiaodong   hxd
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  using NCC.Extend.Entitys.Dto.LqJdqd;
  using Yitter.IdGenerator;
  using NCC.Common.Helper;
  using NCC.JsonSerialization;
  using NCC.Common.Model.NPOI;
  using NCC.Common.Configuration;
  using NCC.DataEncryption;
  using NCC.ClayObject;
  
  namespace NCC.Extend.LqJdqd
  {
      /// <summary>
      /// 进店渠道服务
      /// </summary>
278810cb   “wangming”   refactor: 统一代码风格和...
32
      [ApiDescriptionSettings(Tag = "Extend", Name = "LqJdqd", Order = 200)]
96009bc9   hexiaodong   hxd
33
34
35
36
37
38
      [Route("api/Extend/[controller]")]
      public class LqJdqdService : ILqJdqdService, IDynamicApiController, ITransient
      {
          private readonly ISqlSugarRepository<LqJdqdEntity> _lqJdqdRepository;
          private readonly SqlSugarScope _db;
          private readonly IUserManager _userManager;
e1dcb3a0   “wangming”   1
39
          private readonly IBusinessOperationLogService _businessOperationLogService;
96009bc9   hexiaodong   hxd
40
41
42
43
44
45
  
          /// <summary>
          /// 初始化一个<see cref="LqJdqdService"/>类型的新实例
          /// </summary>
          public LqJdqdService(
              ISqlSugarRepository<LqJdqdEntity> lqJdqdRepository,
e1dcb3a0   “wangming”   1
46
47
              IUserManager userManager,
              IBusinessOperationLogService businessOperationLogService)
96009bc9   hexiaodong   hxd
48
          {
278810cb   “wangming”   refactor: 统一代码风格和...
49
              _lqJdqdRepository = lqJdqdRepository;
96009bc9   hexiaodong   hxd
50
51
              _db = _lqJdqdRepository.Context;
              _userManager = userManager;
e1dcb3a0   “wangming”   1
52
              _businessOperationLogService = businessOperationLogService;
96009bc9   hexiaodong   hxd
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
          }
  
          /// <summary>
          /// 获取进店渠道
          /// </summary>
          /// <param name="id">参数</param>
          /// <returns></returns>
          [HttpGet("{id}")]
          public async Task<dynamic> GetInfo(string id)
          {
              var entity = await _db.Queryable<LqJdqdEntity>().FirstAsync(p => p.Id == id);
              var output = entity.Adapt<LqJdqdInfoOutput>();
              return output;
          }
  
          /// <summary>
  		/// 获取进店渠道列表
  		/// </summary>
  		/// <param name="input">请求参数</param>
  		/// <returns></returns>
          [HttpGet("")]
          public async Task<dynamic> GetList([FromQuery] LqJdqdListQueryInput input)
          {
              var sidx = input.sidx == null ? "id" : input.sidx;
              var data = await _db.Queryable<LqJdqdEntity>()
                  .WhereIF(!string.IsNullOrEmpty(input.id), p => p.Id.Contains(input.id))
                  .WhereIF(!string.IsNullOrEmpty(input.qdmc), p => p.Qdmc.Contains(input.qdmc))
278810cb   “wangming”   refactor: 统一代码风格和...
80
                  .Select(it => new LqJdqdListOutput
96009bc9   hexiaodong   hxd
81
82
                  {
                      id = it.Id,
278810cb   “wangming”   refactor: 统一代码风格和...
83
84
85
                      qdmc = it.Qdmc,
                  }).MergeTable().OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
              return PageResult<LqJdqdListOutput>.SqlSugarPageResult(data);
96009bc9   hexiaodong   hxd
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
          }
  
          /// <summary>
          /// 新建进店渠道
          /// </summary>
          /// <param name="input">参数</param>
          /// <returns></returns>
          [HttpPost("")]
          public async Task Create([FromBody] LqJdqdCrInput input)
          {
              var userInfo = await _userManager.GetUserInfo();
              var entity = input.Adapt<LqJdqdEntity>();
              entity.Id = YitIdHelper.NextId().ToString();
              var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
              if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000);
e1dcb3a0   “wangming”   1
101
102
103
104
105
106
  
              await TryWriteLogAsync(
                  "新增进店渠道",
                  entity.Id,
                  $"新增进店渠道:{entity.Qdmc ?? entity.Id}",
                  new { after = new { entity.Id, entity.Qdmc } }.Serialize());
96009bc9   hexiaodong   hxd
107
108
109
110
111
112
113
114
115
116
117
118
119
120
          }
  
          /// <summary>
  		/// 获取进店渠道无分页列表
  		/// </summary>
  		/// <param name="input">请求参数</param>
  		/// <returns></returns>
          [NonAction]
          public async Task<dynamic> GetNoPagingList([FromQuery] LqJdqdListQueryInput input)
          {
              var sidx = input.sidx == null ? "id" : input.sidx;
              var data = await _db.Queryable<LqJdqdEntity>()
                  .WhereIF(!string.IsNullOrEmpty(input.id), p => p.Id.Contains(input.id))
                  .WhereIF(!string.IsNullOrEmpty(input.qdmc), p => p.Qdmc.Contains(input.qdmc))
278810cb   “wangming”   refactor: 统一代码风格和...
121
                  .Select(it => new LqJdqdListOutput
96009bc9   hexiaodong   hxd
122
123
                  {
                      id = it.Id,
278810cb   “wangming”   refactor: 统一代码风格和...
124
125
126
                      qdmc = it.Qdmc,
                  }).MergeTable().OrderBy(sidx + " " + input.sort).ToListAsync();
              return data;
96009bc9   hexiaodong   hxd
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
          }
  
          /// <summary>
  		/// 导出进店渠道
  		/// </summary>
  		/// <param name="input">请求参数</param>
  		/// <returns></returns>
          [HttpGet("Actions/Export")]
          public async Task<dynamic> Export([FromQuery] LqJdqdListQueryInput input)
          {
              var userInfo = await _userManager.GetUserInfo();
              var exportData = new List<LqJdqdListOutput>();
              if (input.dataType == 0)
              {
                  var data = Clay.Object(await this.GetList(input));
                  exportData = data.Solidify<PageResult<LqJdqdListOutput>>().list;
              }
              else
              {
                  exportData = await this.GetNoPagingList(input);
              }
278810cb   “wangming”   refactor: 统一代码风格和...
148
              List<ParamsModel> paramList = "[{\"value\":\"渠道编号\",\"field\":\"id\"},{\"value\":\"渠道名称\",\"field\":\"qdmc\"},]".ToList<ParamsModel>();
96009bc9   hexiaodong   hxd
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
              ExcelConfig excelconfig = new ExcelConfig();
              excelconfig.FileName = "进店渠道.xls";
              excelconfig.HeadFont = "微软雅黑";
              excelconfig.HeadPoint = 10;
              excelconfig.IsAllSizeColumn = true;
              excelconfig.ColumnModel = new List<ExcelColumnModel>();
              List<string> selectKeyList = input.selectKey.Split(',').ToList();
              foreach (var item in selectKeyList)
              {
                  var isExist = paramList.Find(p => p.field == item);
                  if (isExist != null)
                  {
                      excelconfig.ColumnModel.Add(new ExcelColumnModel() { Column = isExist.field, ExcelColumn = isExist.value });
                  }
              }
              var addPath = FileVariable.TemporaryFilePath + excelconfig.FileName;
              ExcelExportHelper<LqJdqdListOutput>.Export(exportData, excelconfig, addPath);
              var fileName = _userManager.UserId + "|" + addPath + "|xls";
              var output = new
              {
                  name = excelconfig.FileName,
                  url = "/api/File/Download?encryption=" + DESCEncryption.Encrypt(fileName, "NCC")
              };
e1dcb3a0   “wangming”   1
172
173
174
175
176
177
178
179
180
181
182
183
  
              await TryWriteLogAsync(
                  "导出进店渠道",
                  null,
                  $"导出进店渠道,共 {exportData.Count} 条",
                  new
                  {
                      filters = new { input.id, input.qdmc, input.dataType },
                      rowCount = exportData.Count,
                      fileName = excelconfig.FileName
                  }.Serialize());
  
96009bc9   hexiaodong   hxd
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
              return output;
          }
  
          /// <summary>
          /// 批量删除进店渠道
          /// </summary>
          /// <param name="ids">主键数组</param>
          /// <returns></returns>
          [HttpPost("batchRemove")]
          public async Task BatchRemove([FromBody] List<string> ids)
          {
              var entitys = await _db.Queryable<LqJdqdEntity>().In(it => it.Id, ids).ToListAsync();
              if (entitys.Count > 0)
              {
                  try
                  {
                      //开启事务
                      _db.BeginTran();
                      //批量删除进店渠道
278810cb   “wangming”   refactor: 统一代码风格和...
203
                      await _db.Deleteable<LqJdqdEntity>().In(d => d.Id, ids).ExecuteCommandAsync();
96009bc9   hexiaodong   hxd
204
205
206
207
208
209
210
211
212
                      //关闭事务
                      _db.CommitTran();
                  }
                  catch (Exception)
                  {
                      //回滚事务
                      _db.RollbackTran();
                      throw NCCException.Oh(ErrorCode.COM1002);
                  }
e1dcb3a0   “wangming”   1
213
214
215
216
217
218
219
220
221
  
                  foreach (var entity in entitys)
                  {
                      await TryWriteLogAsync(
                          "批量删除进店渠道",
                          entity.Id,
                          $"批量删除进店渠道:{entity.Qdmc ?? entity.Id}(ID:{entity.Id})",
                          new { before = new { entity.Id, entity.Qdmc } }.Serialize());
                  }
96009bc9   hexiaodong   hxd
222
223
224
225
226
227
228
229
230
231
232
233
              }
          }
  
          /// <summary>
          /// 更新进店渠道
          /// </summary>
          /// <param name="id">主键</param>
          /// <param name="input">参数</param>
          /// <returns></returns>
          [HttpPut("{id}")]
          public async Task Update(string id, [FromBody] LqJdqdUpInput input)
          {
e1dcb3a0   “wangming”   1
234
235
236
              var before = await _db.Queryable<LqJdqdEntity>().FirstAsync(p => p.Id == id);
              _ = before ?? throw NCCException.Oh(ErrorCode.COM1005);
  
96009bc9   hexiaodong   hxd
237
238
239
              var entity = input.Adapt<LqJdqdEntity>();
              var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
              if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001);
e1dcb3a0   “wangming”   1
240
241
242
243
244
245
246
247
248
249
  
              await TryWriteLogAsync(
                  "修改进店渠道",
                  id,
                  $"修改进店渠道:{entity.Qdmc ?? id}(ID:{id})",
                  new
                  {
                      before = new { before.Id, before.Qdmc },
                      after = new { entity.Id, entity.Qdmc }
                  }.Serialize());
96009bc9   hexiaodong   hxd
250
251
252
253
254
255
256
257
258
259
260
261
262
          }
  
          /// <summary>
          /// 删除进店渠道
          /// </summary>
          /// <returns></returns>
          [HttpDelete("{id}")]
          public async Task Delete(string id)
          {
              var entity = await _db.Queryable<LqJdqdEntity>().FirstAsync(p => p.Id == id);
              _ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
              var isOk = await _db.Deleteable<LqJdqdEntity>().Where(d => d.Id == id).ExecuteCommandAsync();
              if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1002);
e1dcb3a0   “wangming”   1
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
  
              await TryWriteLogAsync(
                  "删除进店渠道",
                  id,
                  $"删除进店渠道:{entity.Qdmc ?? id}(ID:{id})",
                  new { before = new { entity.Id, entity.Qdmc } }.Serialize());
          }
  
          private async Task TryWriteLogAsync(string actionCode, string bizId, string summary, string detailJson)
          {
              try
              {
                  await _businessOperationLogService.WriteAsync(
                      "进店渠道",
                      actionCode,
                      "lq_jdqd",
                      bizId,
                      summary,
                      detailJson);
              }
              catch
              {
                  // 写日志失败不拖垮主业务
              }
96009bc9   hexiaodong   hxd
287
288
289
          }
      }
  }