LqTodoService.cs
7.49 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
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;
using NCC.Extend.Interfaces.LqTodo;
using NCC.Extend.Entitys.lq_todo;
using NCC.Extend.Entitys.Dto.LqTodo;
using NCC.Extend.Entitys.Enum;
using NCC.Extend.Entitys.lq_mdxx;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yitter.IdGenerator;
namespace NCC.Extend.LqTodo
{
/// <summary>
/// 统一待办服务
/// </summary>
[ApiDescriptionSettings(Tag = "绿纤统一待办服务", Name = "LqTodo", Order = 200)]
[Route("api/Extend/[controller]")]
public class LqTodoService : ILqTodoService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository<LqTodoEntity> _repository;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
public LqTodoService(
ISqlSugarRepository<LqTodoEntity> repository,
IUserManager userManager)
{
_repository = repository;
_db = _repository.Context;
_userManager = userManager;
}
/// <summary>
/// 获取我的待办列表(当前登录用户)
/// </summary>
[HttpGet("MyTodoList")]
public async Task<dynamic> GetMyTodoList([FromQuery] LqTodoListQueryInput input)
{
var userId = _userManager.UserId;
var statusFilter = input.status ?? (int)TodoStatusEnum.Pending;
var sidx = input.sidx ?? "F_DueTime";
var data = await _db.Queryable<LqTodoEntity>()
.Where(p => p.UserId == userId)
.Where(p => p.Status == statusFilter)
.WhereIF(!string.IsNullOrEmpty(input.source), p => p.Source == input.source)
.WhereIF(!string.IsNullOrEmpty(input.storeId), p => p.StoreId == input.storeId)
.WhereIF(!string.IsNullOrEmpty(input.tag), p => p.Tag == input.tag)
.Select(it => new LqTodoListOutput
{
id = it.Id,
userId = it.UserId,
storeId = it.StoreId,
storeName = SqlFunc.Subqueryable<LqMdxxEntity>().Where(m => m.Id == it.StoreId).Select(m => m.Dm),
source = it.Source,
refId = it.RefId,
title = it.Title,
desc = it.Desc,
tag = it.Tag,
urgency = it.Urgency,
dueTime = it.DueTime,
handlePath = it.HandlePath,
handleType = it.HandleType,
handleParam = it.HandleParam,
extra = it.Extra,
status = it.Status,
createTime = it.CreateTime,
handleTime = it.HandleTime
})
.MergeTable()
.OrderBy("dueTime asc")
.ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult<LqTodoListOutput>.SqlSugarPageResult(data);
}
/// <summary>
/// 获取当前用户的待办数量统计
/// </summary>
[HttpGet("TodoCount")]
public async Task<dynamic> GetTodoCount()
{
var userId = _userManager.UserId;
var pending = (int)TodoStatusEnum.Pending;
var allPending = await _db.Queryable<LqTodoEntity>()
.Where(p => p.UserId == userId && p.Status == pending)
.ToListAsync();
return new LqTodoCountOutput
{
total = allPending.Count,
followUp = allPending.Count(x => x.Source == TodoSourceEnum.follow_up.ToString()),
approval = allPending.Count(x => x.Source == TodoSourceEnum.approval.ToString()),
system = allPending.Count(x => x.Source == TodoSourceEnum.system.ToString())
};
}
/// <summary>
/// 创建待办
/// </summary>
[HttpPost("")]
public async Task CreateTodo([FromBody] LqTodoCrInput input)
{
var entity = input.Adapt<LqTodoEntity>();
entity.Id = YitIdHelper.NextId().ToString();
entity.CreateTime = DateTime.Now;
entity.Status = (int)TodoStatusEnum.Pending;
if (string.IsNullOrEmpty(entity.Urgency))
entity.Urgency = TodoUrgencyEnum.normal.ToString();
if (string.IsNullOrEmpty(entity.HandleType))
entity.HandleType = TodoHandleTypeEnum.page.ToString();
var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000);
}
/// <summary>
/// 处理待办(标记为已处理)
/// </summary>
[HttpPost("Handle/{id}")]
public async Task HandleTodo(string id)
{
var entity = await _db.Queryable<LqTodoEntity>().FirstAsync(p => p.Id == id);
_ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
entity.Status = (int)TodoStatusEnum.Done;
entity.HandleTime = DateTime.Now;
await _db.Updateable(entity).UpdateColumns(it => new { it.Status, it.HandleTime }).ExecuteCommandAsync();
}
/// <summary>
/// 忽略待办
/// </summary>
[HttpPost("Dismiss/{id}")]
public async Task DismissTodo(string id)
{
var entity = await _db.Queryable<LqTodoEntity>().FirstAsync(p => p.Id == id);
_ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
entity.Status = (int)TodoStatusEnum.Dismissed;
entity.HandleTime = DateTime.Now;
await _db.Updateable(entity).UpdateColumns(it => new { it.Status, it.HandleTime }).ExecuteCommandAsync();
}
/// <summary>
/// 批量创建待办(供内部业务调用)
/// </summary>
[NonAction]
public async Task BatchCreateTodos(List<LqTodoCrInput> inputs)
{
if (inputs == null || inputs.Count == 0) return;
var entities = inputs.Select(input =>
{
var entity = input.Adapt<LqTodoEntity>();
entity.Id = YitIdHelper.NextId().ToString();
entity.CreateTime = DateTime.Now;
entity.Status = (int)TodoStatusEnum.Pending;
if (string.IsNullOrEmpty(entity.Urgency))
entity.Urgency = TodoUrgencyEnum.normal.ToString();
if (string.IsNullOrEmpty(entity.HandleType))
entity.HandleType = TodoHandleTypeEnum.page.ToString();
return entity;
}).ToList();
await _db.Insertable(entities).ExecuteCommandAsync();
}
/// <summary>
/// 根据来源和关联ID将待办标记为已处理(供内部业务调用)
/// </summary>
[NonAction]
public async Task HandleByRef(string source, string refId)
{
await _db.Updateable<LqTodoEntity>()
.SetColumns(it => it.Status == (int)TodoStatusEnum.Done)
.SetColumns(it => it.HandleTime == DateTime.Now)
.Where(it => it.Source == source && it.RefId == refId && it.Status == (int)TodoStatusEnum.Pending)
.ExecuteCommandAsync();
}
}
}