using AutoMapper;
using Newtonsoft.Json;
using OASystem.Domain;
using OASystem.Domain.Entities.Groups;
using OASystem.Domain.ViewModels.JuHeExchangeRate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OASystem.Infrastructure.Repositories.Groups
{
///
/// 团组流程总览表仓储
///
public class ProcessOverviewRepository : BaseRepository
{
private readonly IMapper _mapper;
private readonly DelegationInfoRepository _groupRep;
public ProcessOverviewRepository(SqlSugarClient sqlSugar, IMapper mapper, DelegationInfoRepository groupRep) : base(sqlSugar)
{
_mapper = mapper;
_groupRep = groupRep;
}
///
/// 团组流程初始化
///
/// 创建流程请求参数
/// 创建的流程信息
public async Task ProcessInitAsync(int groupId,int currUserId)
{
//团组验证
var groupInfo = await _sqlSugar.Queryable().FirstAsync(g => g.Id == groupId);
if (groupInfo == null)
{
return new Result { Code = 400, Msg = "团组不存在" };
}
// 检查是否已存在流程
var existingProcesses = await _sqlSugar.Queryable().Where(p => p.GroupId == groupId).ToListAsync();
if (existingProcesses.Any())
{
return new Result { Code = 400, Msg = "该团组的流程已存在" };
}
//处理签证国家
var visaCountries = _groupRep.GroupSplitCountry(groupInfo.VisitCountry);
// 定义默认的流程节点
var processs = Grp_ProcessOverview.ProcessInit(groupId,currUserId,visaCountries);
_sqlSugar.BeginTran();
foreach (var item in processs)
{
var processId = await _sqlSugar.Insertable(item).ExecuteReturnIdentityAsync();
if (processId < 1 )
{
_sqlSugar.RollbackTran();
return new Result { Code = 400, Msg = "团组流程进度总览表添加失败!" };
}
var nodes = item.Nodes.Select((nodeDto, index) => new Grp_ProcessNode
{
ProcessId = processId,
NodeName = nodeDto.NodeName,
NodeOrder = nodeDto.NodeOrder,
OverallStatus = ProcessStatus.UnStarted,
//Country = nodeDto.Country,
IsCurrent = nodeDto.IsCurrent,
Remark =nodeDto.Remark
}).ToList();
var nodeIds = await _sqlSugar.Insertable(nodes).ExecuteCommandAsync();
if (nodeIds < 1)
{
_sqlSugar.RollbackTran();
return new Result { Code = 400, Msg = "团组流程进度流程表添加失败!" };
}
}
_sqlSugar.CommitTran();
return new Result { Code = 200, Msg = "添加成功!" }; ;
}
///
/// 获取团组的所有流程及流程详情
///
/// 创建流程请求参数
/// 创建的流程信息
public async Task ProcessesDetailsAsync(int groupId)
{
//团组验证
var groupInfo = await _sqlSugar.Queryable().FirstAsync(g => g.Id == groupId);
if (groupInfo == null)
{
return new Result { Code = 400, Msg = "团组不存在" };
}
// 检查是否已存在流程
var existingProcesses = await _sqlSugar.Queryable().Where(p => p.GroupId == groupId).ToListAsync();
if (!existingProcesses.Any())
{
return new Result { Code = 400, Msg = "该团组的流程不存在" };
}
var users = await _sqlSugar.Queryable().ToListAsync();
var processData = await _sqlSugar.Queryable()
.Where(p => p.GroupId == groupId && p.IsDel == 0)
.Mapper(p => p.Nodes, p => p.Nodes.First().ProcessId)
.ToListAsync();
var processes = processData.Select(p => new
{
p.Id,
p.GroupId,
p.ProcessType,
ProcessName = p.ProcessType.GetEnumDescription(),
//p.OverallStatus,
//StatusText = p.OverallStatus.GetDescription(),
Nodes = p.Nodes.Select(n => {
//单独处理签证板块
var visaSubNodes = new List();
string remark = string.Empty;
if (p.ProcessType == GroupProcessType.Visa)
{
visaSubNodes = JsonConvert.DeserializeObject>(n.Remark);
}
return new
{
n.Id,
n.ProcessId,
n.NodeOrder,
n.NodeName,
n.OverallStatus,
StatusText = p.OverallStatus.GetEnumDescription(),
Operator = users.FirstOrDefault(u => u.Id == n.Operator)?.CnName ?? "-",
OpeateTime = n.OperationTime.HasValue ? n.OperationTime.Value.ToString("yyyy-MM-dd HH:mm:ss") : "-",
//节点类型为签证时使用
visaSubNodes
};
}).OrderBy(n => n.NodeOrder).ToList()
}).ToList();
return new Result { Code = 200,Data = processes, Msg = "查询成功!" };
}
}
}