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.LqReimbursementApplication;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NCC.Extend.Entitys;
using NCC.Extend.Entitys.Dto.LqReimbursementApplication;
using NCC.Extend.Entitys.lq_reimbursement_application_node;
using NCC.Extend.Entitys.lq_reimbursement_application_node_user;
using NCC.Extend.Entitys.lq_reimbursement_approval_record;
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.LqReimbursementApplication
{
///
/// 报销申请表服务
///
[ApiDescriptionSettings(Tag = "Extend", Name = "LqReimbursementApplication", Order = 200)]
[Route("api/Extend/[controller]")]
public class LqReimbursementApplicationService : ILqReimbursementApplicationService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _lqReimbursementApplicationRepository;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
///
/// 初始化一个类型的新实例
///
public LqReimbursementApplicationService(
ISqlSugarRepository lqReimbursementApplicationRepository,
IUserManager userManager)
{
_lqReimbursementApplicationRepository = lqReimbursementApplicationRepository;
_db = _lqReimbursementApplicationRepository.Context;
_userManager = userManager;
}
///
/// 获取报销申请表详情(包含表单和流程信息)
///
/// 申请编号
///
[HttpGet("{id}")]
public async Task GetInfo(string id)
{
var entity = await _db.Queryable().FirstAsync(p => p.Id == id);
_ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
var output = entity.Adapt();
// 获取节点配置
var nodes = await _db.Queryable()
.Where(x => x.ApplicationId == id)
.OrderBy(x => x.NodeOrder)
.ToListAsync();
// 获取每个节点的审批人
var nodeUsers = await _db.Queryable()
.Where(x => x.ApplicationId == id)
.OrderBy(x => x.NodeOrder)
.OrderBy(x => x.SortOrder)
.ToListAsync();
// 获取审批历史
var approvalRecords = await _db.Queryable()
.Where(x => x.ApplicationId == id)
.OrderBy(x => x.NodeOrder)
.OrderBy(x => x.ApprovalTime)
.ToListAsync();
// 组装节点信息
var nodeList = nodes.Select(n => new
{
nodeId = n.Id,
nodeOrder = n.NodeOrder,
nodeName = n.NodeName,
approvalType = n.ApprovalType,
isRequired = n.IsRequired,
approvers = nodeUsers.Where(u => u.NodeId == n.Id).Select(u => new
{
userId = u.UserId,
userName = u.UserName,
sortOrder = u.SortOrder
}).ToList(),
approvalRecords = approvalRecords.Where(r => r.NodeId == n.Id).Select(r => new
{
approverName = r.ApproverName,
approvalResult = r.ApprovalResult,
approvalOpinion = r.ApprovalOpinion,
approvalTime = r.ApprovalTime
}).ToList()
}).ToList();
// 获取当前节点审批人
var currentApprovers = new List