Blame view

netcore/src/Modularity/Extend/NCC.Extend/ChangelogService.cs 10.5 KB
a8965d32   “wangming”   ```
1
2
3
4
5
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using Microsoft.AspNetCore.Mvc;
e1dcb3a0   “wangming”   1
6
7
  using Microsoft.Extensions.Logging;
  using Newtonsoft.Json;
a8965d32   “wangming”   ```
8
9
  using NCC.Common.Core.Manager;
  using NCC.Common.Filter;
e1dcb3a0   “wangming”   1
10
  using NCC.Dependency;
a8965d32   “wangming”   ```
11
12
13
  using NCC.DynamicApiController;
  using NCC.Extend.Entitys.Dto.Changelog;
  using NCC.Extend.Entitys.lq_changelog;
e1dcb3a0   “wangming”   1
14
15
  using NCC.Extend.Helpers;
  using NCC.Extend.Interfaces.BusinessOperationLog;
a8965d32   “wangming”   ```
16
17
18
19
20
21
22
23
24
25
26
27
  using SqlSugar;
  using Yitter.IdGenerator;
  
  namespace NCC.Extend
  {
      /// <summary>
      /// 系统更新日志服务
      /// </summary>
      [ApiDescriptionSettings(Tag = "Common", Name = "Changelog", Order = 170)]
      [Route("api/Extend/[controller]")]
      public class ChangelogService : IDynamicApiController, ITransient
      {
e1dcb3a0   “wangming”   1
28
29
          private const string BizLogModule = "系统更新";
  
a8965d32   “wangming”   ```
30
31
          private readonly ISqlSugarClient _db;
          private readonly IUserManager _userManager;
e1dcb3a0   “wangming”   1
32
33
          private readonly IBusinessOperationLogService _businessOperationLogService;
          private readonly ILogger<ChangelogService> _logger;
a8965d32   “wangming”   ```
34
  
e1dcb3a0   “wangming”   1
35
36
37
38
39
          public ChangelogService(
              ISqlSugarClient db,
              IUserManager userManager,
              IBusinessOperationLogService businessOperationLogService,
              ILogger<ChangelogService> logger)
a8965d32   “wangming”   ```
40
41
42
          {
              _db = db;
              _userManager = userManager;
e1dcb3a0   “wangming”   1
43
44
              _businessOperationLogService = businessOperationLogService;
              _logger = logger;
a8965d32   “wangming”   ```
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
              _db.CodeFirst.InitTables(typeof(LqChangelogEntity));
          }
  
          /// <summary>
          /// 获取更新日志列表(分页)
          /// </summary>
          [HttpGet("GetList")]
          public async Task<dynamic> GetList([FromQuery] ChangelogListQueryInput input)
          {
              var data = await _db.Queryable<LqChangelogEntity>()
                  .WhereIF(input.isPublished.HasValue, x => x.IsPublished == input.isPublished.Value)
                  .WhereIF(input.type.HasValue, x => x.Type == input.type.Value)
                  .WhereIF(!string.IsNullOrWhiteSpace(input.keyword),
                      x => x.Title.Contains(input.keyword) || x.Version.Contains(input.keyword))
                  .OrderBy(x => x.PublishedTime, OrderByType.Desc)
                  .OrderBy(x => x.CreateTime, OrderByType.Desc)
                  .Select(x => new ChangelogListOutput
                  {
                      id = x.Id,
                      version = x.Version,
                      title = x.Title,
                      content = x.Content,
                      type = x.Type,
                      isPublished = x.IsPublished,
                      publishedTime = x.PublishedTime,
                      createTime = x.CreateTime,
                      modifyTime = x.ModifyTime
                  })
                  .ToPagedListAsync(input.currentPage, input.pageSize);
  
              return PageResult<ChangelogListOutput>.SqlSugarPageResult(data);
          }
  
          /// <summary>
          /// 获取已发布的更新日志列表(前端用)
          /// </summary>
          [HttpGet("GetPublishedList")]
          public async Task<dynamic> GetPublishedList([FromQuery] int currentPage = 1, int pageSize = 20)
          {
              var data = await _db.Queryable<LqChangelogEntity>()
                  .Where(x => x.IsPublished)
                  .OrderBy(x => x.PublishedTime, OrderByType.Desc)
                  .Select(x => new ChangelogListOutput
                  {
                      id = x.Id,
                      version = x.Version,
                      title = x.Title,
                      content = x.Content,
                      type = x.Type,
                      isPublished = x.IsPublished,
                      publishedTime = x.PublishedTime,
                      createTime = x.CreateTime,
                      modifyTime = x.ModifyTime
                  })
                  .ToPagedListAsync(currentPage, pageSize);
  
              return PageResult<ChangelogListOutput>.SqlSugarPageResult(data);
          }
  
          /// <summary>
          /// 获取最新一条已发布更新日志(右上角红点提示用)
          /// </summary>
          [HttpGet("GetLatest")]
          public async Task<dynamic> GetLatest()
          {
              var latest = await _db.Queryable<LqChangelogEntity>()
                  .Where(x => x.IsPublished)
                  .OrderBy(x => x.PublishedTime, OrderByType.Desc)
                  .FirstAsync();
  
              return latest;
          }
  
          /// <summary>
          /// 获取更新日志详情
          /// </summary>
          [HttpGet("GetDetail/{id}")]
          public async Task<dynamic> GetDetail(string id)
          {
              var entity = await _db.Queryable<LqChangelogEntity>()
                  .Where(x => x.Id == id)
                  .FirstAsync();
  
              if (entity == null)
                  throw new Exception("更新日志不存在");
  
              return entity;
          }
  
          /// <summary>
          /// 添加更新日志
          /// </summary>
          [HttpPost("Add")]
          public async Task<dynamic> Add([FromBody] ChangelogAddInput input)
          {
              var entity = new LqChangelogEntity
              {
                  Id = YitIdHelper.NextId().ToString(),
                  Version = input.Version,
                  Title = input.Title,
                  Content = input.Content,
                  Type = input.Type,
                  IsPublished = input.IsPublished,
                  PublishedTime = input.IsPublished ? DateTime.Now : null,
                  CreateUserId = _userManager?.UserId,
                  CreateTime = DateTime.Now
              };
  
              await _db.Insertable(entity).ExecuteCommandAsync();
  
e1dcb3a0   “wangming”   1
155
156
157
158
159
160
161
162
163
164
165
              var snapshot = BuildSnapshot(entity);
              await BusinessOperationLogHelper.SafeWriteAsync(
                  _businessOperationLogService,
                  _logger,
                  BizLogModule,
                  "新增更新日志",
                  "lq_changelog",
                  entity.Id,
                  $"新增更新日志 {entity.Version}:{entity.Title}",
                  JsonConvert.SerializeObject(new { after = snapshot }));
  
a8965d32   “wangming”   ```
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
              return new { success = true, id = entity.Id };
          }
  
          /// <summary>
          /// 修改更新日志
          /// </summary>
          [HttpPost("Update")]
          public async Task<dynamic> Update([FromBody] ChangelogUpdateInput input)
          {
              var entity = await _db.Queryable<LqChangelogEntity>()
                  .Where(x => x.Id == input.Id)
                  .FirstAsync();
  
              if (entity == null)
                  throw new Exception("更新日志不存在");
  
e1dcb3a0   “wangming”   1
182
              var beforeSnapshot = BuildSnapshot(entity);
a8965d32   “wangming”   ```
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
              entity.Version = input.Version ?? entity.Version;
              entity.Title = input.Title ?? entity.Title;
              entity.Content = input.Content ?? entity.Content;
              entity.Type = input.Type;
              entity.ModifyTime = DateTime.Now;
  
              if (input.IsPublished && !entity.IsPublished)
              {
                  entity.IsPublished = true;
                  entity.PublishedTime = DateTime.Now;
              }
              else if (!input.IsPublished)
              {
                  entity.IsPublished = false;
                  entity.PublishedTime = null;
              }
  
              await _db.Updateable(entity).ExecuteCommandAsync();
  
e1dcb3a0   “wangming”   1
202
203
204
205
206
207
208
209
210
211
              await BusinessOperationLogHelper.SafeWriteAsync(
                  _businessOperationLogService,
                  _logger,
                  BizLogModule,
                  "修改更新日志",
                  "lq_changelog",
                  entity.Id,
                  $"修改更新日志 {entity.Version}:{entity.Title}",
                  JsonConvert.SerializeObject(new { before = beforeSnapshot, after = BuildSnapshot(entity) }));
  
a8965d32   “wangming”   ```
212
213
214
215
216
217
218
219
220
              return new { success = true };
          }
  
          /// <summary>
          /// 删除更新日志
          /// </summary>
          [HttpPost("Delete/{id}")]
          public async Task<dynamic> Delete(string id)
          {
e1dcb3a0   “wangming”   1
221
222
223
224
225
226
227
228
229
              var entity = await _db.Queryable<LqChangelogEntity>()
                  .Where(x => x.Id == id)
                  .FirstAsync();
              if (entity == null)
              {
                  throw new Exception("更新日志不存在");
              }
  
              var beforeSnapshot = BuildSnapshot(entity);
a8965d32   “wangming”   ```
230
231
              await _db.Deleteable<LqChangelogEntity>(id).ExecuteCommandAsync();
  
e1dcb3a0   “wangming”   1
232
233
234
235
236
237
238
239
240
241
              await BusinessOperationLogHelper.SafeWriteAsync(
                  _businessOperationLogService,
                  _logger,
                  BizLogModule,
                  "删除更新日志",
                  "lq_changelog",
                  id,
                  $"删除更新日志 {entity.Version}:{entity.Title}",
                  JsonConvert.SerializeObject(new { before = beforeSnapshot }));
  
a8965d32   “wangming”   ```
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
              return new { success = true };
          }
  
          /// <summary>
          /// 切换发布状态
          /// </summary>
          [HttpPost("TogglePublish/{id}")]
          public async Task<dynamic> TogglePublish(string id)
          {
              var entity = await _db.Queryable<LqChangelogEntity>()
                  .Where(x => x.Id == id)
                  .FirstAsync();
  
              if (entity == null)
                  throw new Exception("更新日志不存在");
  
e1dcb3a0   “wangming”   1
258
              var beforePublished = entity.IsPublished;
a8965d32   “wangming”   ```
259
260
261
262
263
264
265
266
              entity.IsPublished = !entity.IsPublished;
              entity.PublishedTime = entity.IsPublished ? DateTime.Now : null;
              entity.ModifyTime = DateTime.Now;
  
              await _db.Updateable(entity)
                  .UpdateColumns(x => new { x.IsPublished, x.PublishedTime, x.ModifyTime })
                  .ExecuteCommandAsync();
  
e1dcb3a0   “wangming”   1
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
              var action = entity.IsPublished ? "发布更新日志" : "取消发布更新日志";
              await BusinessOperationLogHelper.SafeWriteAsync(
                  _businessOperationLogService,
                  _logger,
                  BizLogModule,
                  action,
                  "lq_changelog",
                  entity.Id,
                  $"{action} {entity.Version}:{entity.Title}",
                  JsonConvert.SerializeObject(new
                  {
                      before = new { isPublished = beforePublished },
                      after = new { isPublished = entity.IsPublished }
                  }));
  
a8965d32   “wangming”   ```
282
283
              return new { success = true, isPublished = entity.IsPublished };
          }
e1dcb3a0   “wangming”   1
284
285
286
287
288
289
290
291
292
  
          private static object BuildSnapshot(LqChangelogEntity entity) => new
          {
              version = entity.Version,
              title = entity.Title,
              type = entity.Type,
              isPublished = entity.IsPublished,
              publishedTime = entity.PublishedTime
          };
a8965d32   “wangming”   ```
293
294
      }
  }