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
{
///
/// 统一待办服务
///
[ApiDescriptionSettings(Tag = "绿纤统一待办服务", Name = "LqTodo", Order = 200)]
[Route("api/Extend/[controller]")]
public class LqTodoService : ILqTodoService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _repository;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
public LqTodoService(
ISqlSugarRepository repository,
IUserManager userManager)
{
_repository = repository;
_db = _repository.Context;
_userManager = userManager;
}
///
/// 获取我的待办列表(当前登录用户)
///
[HttpGet("MyTodoList")]
public async Task 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()
.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().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.SqlSugarPageResult(data);
}
///
/// 获取当前用户的待办数量统计
///
[HttpGet("TodoCount")]
public async Task GetTodoCount()
{
var userId = _userManager.UserId;
var pending = (int)TodoStatusEnum.Pending;
var allPending = await _db.Queryable()
.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())
};
}
///
/// 创建待办
///
[HttpPost("")]
public async Task CreateTodo([FromBody] LqTodoCrInput input)
{
var entity = input.Adapt();
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);
}
///
/// 处理待办(标记为已处理)
///
[HttpPost("Handle/{id}")]
public async Task HandleTodo(string id)
{
var entity = await _db.Queryable().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();
}
///
/// 忽略待办
///
[HttpPost("Dismiss/{id}")]
public async Task DismissTodo(string id)
{
var entity = await _db.Queryable().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();
}
///
/// 批量创建待办(供内部业务调用)
///
[NonAction]
public async Task BatchCreateTodos(List inputs)
{
if (inputs == null || inputs.Count == 0) return;
var entities = inputs.Select(input =>
{
var entity = input.Adapt();
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();
}
///
/// 根据来源和关联ID将待办标记为已处理(供内部业务调用)
///
[NonAction]
public async Task HandleByRef(string source, string refId)
{
await _db.Updateable()
.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();
}
}
}