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.IO;
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 NCC.Extend.Entitys.lq_mdxx;
using NCC.Extend.Entitys.lq_reimbursement_workflow_config;
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;
}
///
/// 获取报销申请表详情(包含表单和流程信息)
///
///
/// 根据申请编号获取报销申请的详细信息,包括表单数据、节点配置、审批历史、购买记录等。
///
/// 示例请求:
/// ```
/// GET /api/Extend/LqReimbursementApplication/{id}
/// ```
///
/// 参数说明:
/// - id: 申请编号(必填)
///
/// 返回说明:
/// - form: 表单基本信息,包含申请编号、申请人信息、门店信息、审批状态、完成时间等
/// - nodes: 节点配置列表,包含每个节点的审批人、审批记录等
/// - currentApprovers: 当前节点审批人列表
/// - currentNodeOrder: 当前节点顺序
/// - approvalStatus: 审批状态
/// - returnedReason: 退回原因(如果有)
/// - purchaseRecords: 关联的购买记录列表
/// - form.completionTime: 完成时间(最后一个审批人通过的时间),如果申请未完成则为null
///
/// 申请编号
/// 报销申请详情
/// 查询成功
/// 申请不存在
/// 服务器错误
[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();
// 获取流程名称(关联流程配置时,单独查询避免 SqlSugar LeftJoin 列名映射问题)
if (!string.IsNullOrEmpty(entity.WorkflowConfigId))
{
var workflowConfig = await _db.Queryable()
.Where(x => x.Id == entity.WorkflowConfigId)
.Select(x => x.WorkflowName)
.FirstAsync();
output.workflowName = workflowConfig;
}
// 获取完成时间(优先使用实体类中的 CompletionTime,如果没有则查询审批记录)
if (entity.CompletionTime.HasValue)
{
output.completionTime = entity.CompletionTime;
}
else
{
// 查询最后一次审批通过的记录
var completionRecord = await _db.Queryable()
.Where(x => x.ApplicationId == id && x.ApprovalResult == "通过")
.OrderBy(x => x.ApprovalTime, OrderByType.Desc)
.FirstAsync();
output.completionTime = completionRecord?.ApprovalTime;
}
// 获取节点配置
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 purchaseRecords = await _db.Queryable()
.Where(x => x.ApplicationId == id)
.OrderBy(x => x.CreateTime)
.ToListAsync();
// 获取门店名称
string storeName = null;
if (!string.IsNullOrEmpty(entity.ApplicationStoreId))
{
var store = await _db.Queryable()
.Where(x => x.Id == entity.ApplicationStoreId)
.Select(x => x.Dm)
.FirstAsync();
storeName = store ?? null;
}
output.applicationStoreName = storeName;
// 组装节点信息
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