| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082 |
- using AutoMapper;
- using Newtonsoft.Json;
- using OASystem.Domain;
- using OASystem.Domain.Dtos.Groups;
- using OASystem.Domain.Entities.Groups;
- using OASystem.Domain.Entities.Resource;
- using System.Reflection;
- namespace OASystem.Infrastructure.Repositories.Groups
- {
- /// <summary>
- /// 团组流程总览表仓储
- /// </summary>
- public class ProcessOverviewRepository : BaseRepository<Grp_ProcessOverview, Grp_ProcessOverview>
- {
- private readonly IMapper _mapper;
- private readonly DelegationInfoRepository _groupRep;
- public ProcessOverviewRepository(SqlSugarClient sqlSugar, IMapper mapper, DelegationInfoRepository groupRep) : base(sqlSugar)
- {
- _mapper = mapper;
- _groupRep = groupRep;
- }
- /// <summary>
- /// 基础数据初始化-团组流程
- /// </summary>
- /// <param name="groupId"></param>
- /// <param name="currUserId"></param>
- /// <returns></returns>
- public async Task<List<Grp_ProcessOverview>> ProcessDataInitAsync(int groupId, int currUserId, List<string> visaCountries)
- {
- var processs = new List<Grp_ProcessOverview>();
- //团组验证
- var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
- if (groupInfo == null) return processs;
- // 检查是否已存在流程
- var existingProcesses = await _sqlSugar.Queryable<Grp_ProcessOverview>()
- .Where(p => p.IsDel == 0 && p.GroupId == groupId)
- .ToListAsync();
- if (existingProcesses.Any()) return processs;
- #region 商邀报批流程
- var custInfo = await _sqlSugar.Queryable<Grp_TourClientList>()
- .Where(c => c.DiId == groupId && c.IsDel == 0)
- .OrderByDescending(c => c.CreateTime)
- .FirstAsync();
- string oaNode2Tips = "客户提供完整名单后,2周内取得邀请函(翻译件)。";
- if (custInfo != null)
- {
- oaNode2Tips = $"请于{custInfo.CreateTime.AddDays(14):yyyy年MM月dd日}内完成该项工作(客户提供完整名单后,2周内取得邀请函(翻译件)。)";
- }
- var oaNode4Tips = $"请于{groupInfo.VisitDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(按进度实际公务活动落实情况,出发前5日落实公务。)";
- processs.Add(
- Grp_ProcessOverview.Create(groupId, 1, GroupProcessType.Invitation, ProcessStatus.InProgress, currUserId,
- new List<Grp_ProcessNode>()
- {
- Grp_ProcessNode.Create(1, "报批基础资料准备","更新报批行程和请示,提供其他报批所需材料,4个工作日内完成。",ProcessStatus.InProgress, true,currUserId),
- Grp_ProcessNode.Create(2, "报批邀请函资料准备",oaNode2Tips, ProcessStatus.InProgress, false,currUserId),
- Grp_ProcessNode.Create(3, "获得批件","提供完整的报批全套资源。",ProcessStatus.InProgress, false, currUserId ),
- Grp_ProcessNode.Create(4, "对接公务",oaNode4Tips,ProcessStatus.InProgress, false, currUserId),
- Grp_ProcessNode.Create(5, "参与翻译对接","",ProcessStatus.InProgress, false, currUserId),
- Grp_ProcessNode.Create(6, "商邀文案配合","",ProcessStatus.InProgress, false, currUserId),
- }));
- #endregion
- #region 签证流程
- //单独处理签证流程节点
- var visaNodes = new List<Grp_ProcessNode>();
- if (visaCountries != null && visaCountries.Count > 0)
- {
- var visaDefualtNodes = new List<VisaProcessNode>();
- for (int i = 1; i < visaCountries.Count + 1; i++)
- {
- visaDefualtNodes.Add(VisaProcessNode.Info(i, visaCountries[i - 1].ToString()));
- }
- visaNodes.Add(Grp_ProcessNode.Create(1, "签证", "", ProcessStatus.InProgress, true, currUserId, JsonConvert.SerializeObject(visaDefualtNodes)));
- }
- processs.Add(Grp_ProcessOverview.Create(groupId, 2, GroupProcessType.Visa, ProcessStatus.InProgress, currUserId, visaNodes));
- #endregion
- #region 机票流程
- string airNode1Tips = "建团后打勾确认出团的时候开始24小时内。";
- if (groupInfo.Step == 1 || groupInfo.Step == 2)
- {
- if (groupInfo.StepOperationTime.HasValue)
- {
- airNode1Tips = $"请于{groupInfo.StepOperationTime.Value.AddDays(1):yyyy年MM月dd日}内完成该项工作(建团后打勾确认出团的时候开始24小时内)";
- }
- }
-
- var airNode5Tips = $"请于{groupInfo.VisitDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(团组出发前5日)";
- processs.Add(
- Grp_ProcessOverview.Create(groupId, 3, GroupProcessType.AirTicket, ProcessStatus.InProgress, currUserId,
- new List<Grp_ProcessNode>()
- {
- Grp_ProcessNode.Create(1, "初步拟定航程方案及价格", airNode1Tips, ProcessStatus.InProgress, true,currUserId ),
- Grp_ProcessNode.Create(2, "机票占位、续位", "", ProcessStatus.UnStarted, false,currUserId ),
- Grp_ProcessNode.Create(3, "完成机票采购确认(含预算核对、出票确认等)", "", ProcessStatus.UnStarted,false,currUserId),
- Grp_ProcessNode.Create(4, "进行出票操作并核查信息", "", ProcessStatus.UnStarted, false, currUserId),
- Grp_ProcessNode.Create(5, "机票已出", airNode5Tips, ProcessStatus.UnStarted, false, currUserId),
- Grp_ProcessNode.Create(6, "完成机票选座", "", ProcessStatus.UnStarted, false,currUserId)
- }
- )
- );
- #endregion
- #region 酒店流程
- string hotelNode1Tips = "建团后打勾确认出团的时候开始2个工作日。";
- if (groupInfo.Step == 1 || groupInfo.Step == 2)
- {
- if (groupInfo.StepOperationTime.HasValue)
- {
- hotelNode1Tips = $"请于{groupInfo.StepOperationTime.Value.AddDays(2):yyyy年MM月dd日}内完成该项工作(建团后打勾确认出团的时候开始2个工作日)";
- }
- }
- var hotelNode4Tips = $"请于{groupInfo.VisitDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(团组出发前5天)";
- var hotelNode5Tips = $"请于{groupInfo.VisitEndDate.AddDays(5):yyyy年MM月dd日}内完成该项工作(团组结束后5天内)";
- processs.Add(
- Grp_ProcessOverview.Create(groupId, 4, GroupProcessType.Hotel, ProcessStatus.InProgress, currUserId,
- new List<Grp_ProcessNode>()
- {
- Grp_ProcessNode.Create(1, "筛选并按照预算标准,对目标酒店进行询价、比价、谈价", hotelNode1Tips, ProcessStatus.InProgress, true, currUserId),
- Grp_ProcessNode.Create(2, "获取酒店确认函与入住名单核对", "", ProcessStatus.UnStarted, false, currUserId ),
- Grp_ProcessNode.Create(3, "预订酒店并录入OA", "", ProcessStatus.UnStarted,false,currUserId ),
- Grp_ProcessNode.Create(4, "行前再次确认酒店订单、付款状态及入住安排", hotelNode4Tips,ProcessStatus.UnStarted, false,currUserId ),
- Grp_ProcessNode.Create(5, "行程结束后整理酒店发票与结算", hotelNode5Tips, ProcessStatus.UnStarted, false, currUserId ),
- }
- )
- );
- #endregion
- #region 地接流程
- var airTripCodeInfo = await _sqlSugar.Queryable<Air_TicketBlackCode>()
- .Where(x => x.IsDel == 0 && x.DiId == groupId)
- .OrderByDescending(x => x.CreateTime)
- .FirstAsync();
- string opNode1Tips = $"机票行程代码最后一段录入后1个工作日内。";
- if (airTripCodeInfo != null)
- {
- opNode1Tips = $"请于{airTripCodeInfo.CreateTime.AddDays(1):yyyy年MM月dd日}内完成该项工作(机票行程代码最后一段录入后1个工作日内)";
- }
- string opNode2Tips = $"请于{groupInfo.CreateTime.AddDays(7):yyyy年MM月dd日}内完成该项工作(建团完成后7个工作日内)";
- string opNode3Tips = $"请于{groupInfo.CreateTime.AddDays(10):yyyy年MM月dd日}内完成该项工作(上一步往后3个工作日内)";
- string opNode4Tips = $"请于{groupInfo.CreateTime.AddDays(12):yyyy年MM月dd日}内完成该项工作(上一步往后2个工作日内)";
- var backListInfo = await _sqlSugar.Queryable<Grp_InvertedList>().Where(x => x.DiId == groupId && x.IsDel == 0).FirstAsync();
- string opNode5Tips = $"倒推表里开行前会 -3天。";
- if (backListInfo != null) {
- if (DateTime.TryParse(backListInfo.PreTripMeetingDt,out DateTime dateTime))
- {
- opNode5Tips = $"请于{dateTime.AddDays(-3):yyyy年MM月dd日}内完成该项工作(倒推表里开行前会 -3天)";
- }
- }
- processs.Add(
- Grp_ProcessOverview.Create(groupId, 5, GroupProcessType.LocalGuide, ProcessStatus.InProgress, currUserId,
- new List<Grp_ProcessNode>()
- {
- Grp_ProcessNode.Create(1,"根据机票方案出框架行程", opNode1Tips,ProcessStatus.InProgress, true, currUserId ),
- Grp_ProcessNode.Create(2,"联系并询价地接、餐厅、用车、景点等供应商", opNode2Tips,ProcessStatus.UnStarted, false, currUserId ),
- Grp_ProcessNode.Create(3,"提交供应商报价及比价表", opNode3Tips, ProcessStatus.UnStarted, false, currUserId),
- Grp_ProcessNode.Create(4,"执行采购流程", opNode4Tips, ProcessStatus.UnStarted, false, currUserId),
- Grp_ProcessNode.Create(5,"制定最终《行程表》及《出行手册》", opNode5Tips, ProcessStatus.UnStarted, false, currUserId ),
- Grp_ProcessNode.Create(6,"送机", "", ProcessStatus.UnStarted, false, currUserId ),
- }
- )
- );
- #endregion
- #region 费用结算流程
- processs.Add(
- Grp_ProcessOverview.Create(groupId, 6, GroupProcessType.FeeSettle, ProcessStatus.InProgress, currUserId,
- new List<Grp_ProcessNode>()
- {
- Grp_ProcessNode.Create(1, "费用结算中", "", ProcessStatus.InProgress, true,currUserId ),
- Grp_ProcessNode.Create(2, "费用结算完毕", "", ProcessStatus.UnStarted, false,currUserId ),
- }
- )
- );
- #endregion
- return processs;
- }
- /// <summary>
- /// 团组流程初始化
- /// </summary>
- /// <param name="request">创建流程请求参数</param>
- /// <returns>创建的流程信息</returns>
- public async Task<Result> ProcessInitAsync(int groupId, int currUserId)
- {
- //团组验证
- var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
- if (groupInfo == null)
- {
- return new Result { Code = 400, Msg = "团组不存在" };
- }
- // 检查是否已存在流程
- var existingProcesses = await _sqlSugar.Queryable<Grp_ProcessOverview>()
- .Where(p => p.IsDel == 0 && p.GroupId == groupId)
- .ToListAsync();
- if (existingProcesses.Any())
- {
- return new Result { Code = 400, Msg = "该团组的流程已存在" };
- }
- //处理签证国家
- var visaCountries = _groupRep.GroupSplitCountry(groupInfo.VisitCountry);
- // 定义默认的流程节点
- var processs = await ProcessDataInitAsync(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 = "团组流程进度总览表添加失败!" };
- }
- // 记录流程日志
- await LogProcessOpAsync(null, item, "Create", currUserId);
- var nodes = item.Nodes.Select((nodeDto, index) => new Grp_ProcessNode
- {
- ProcessId = processId,
- NodeName = nodeDto.NodeName,
- NodeOrder = nodeDto.NodeOrder,
- OverallStatus = nodeDto.OverallStatus,
- NodeDescTips = nodeDto.NodeDescTips,
- //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 = "团组流程进度流程节点添加失败!" };
- }
- //记录节点日志
- foreach (var node in nodes)
- {
- await LogNodeOpAsync(null, node, "Create", currUserId);
- }
-
- }
- _sqlSugar.CommitTran();
- return new Result { Code = 200, Msg = "添加成功!" }; ;
- }
- /// <summary>
- /// 获取团组的所有流程及流程详情
- /// </summary>
- /// <param name="request">创建流程请求参数</param>
- /// <returns>创建的流程信息</returns>
- public async Task<Result> ProcessesDetailsAsync(int groupId)
- {
- //团组验证
- var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
- if (groupInfo == null)
- {
- return new Result { Code = 400, Msg = "团组不存在" };
- }
- // 检查是否已存在流程
- var existingProcesses = await _sqlSugar.Queryable<Grp_ProcessOverview>().Where(p => p.IsDel == 0 && p.GroupId == groupId).ToListAsync();
- if (!existingProcesses.Any())
- {
- //新建团组流程
- var res = await ProcessInitAsync(groupId, 4);
- if (res.Code != 200)
- {
- return res;
- }
- }
- var users = await _sqlSugar.Queryable<Sys_Users>().Select(x => new {x.Id,x.CnName }).ToListAsync();
- var processData = await _sqlSugar.Queryable<Grp_ProcessOverview>()
- .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<VisaProcessNode>();
- string remark = string.Empty;
- if (p.ProcessType == GroupProcessType.Visa)
- {
- visaSubNodes = JsonConvert.DeserializeObject<List<VisaProcessNode>>(n.Remark);
- }
- return new
- {
- n.Id,
- n.ProcessId,
- n.NodeOrder,
- n.NodeName,
- n.OverallStatus,
- StatusText = n.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") : "-",
- ActualDone = n.ActualDone?.ToString("yyyy-MM-dd HH:mm:ss") ?? "",
- n.NodeDescTips,
- //节点类型为签证时使用
- visaSubNodes
- };
- }).OrderBy(n => n.NodeOrder).ToList()
- }).ToList();
- return new Result { Code = 200, Data = processes, Msg = "查询成功!" };
- }
- /// <summary>
- /// 更新节点状态
- /// </summary>
- /// <param name="nodeId">节点ID</param>
- /// <param name="currUserId">当前用户ID</param>
- /// <param name="processStatus">流程状态,默认为已完成</param>
- /// <returns>操作结果</returns>
- public async Task<Result> UpdateNodeStatusAsync(int nodeId, int currUserId, ProcessStatus processStatus = ProcessStatus.Completed)
- {
- try
- {
- // 使用事务确保数据一致性
- var result = await _sqlSugar.Ado.UseTranAsync(async () =>
- {
- // 1. 获取并验证节点
- var node = await _sqlSugar.Queryable<Grp_ProcessNode>()
- .FirstAsync(n => n.Id == nodeId && n.IsDel == 0);
- if (node == null)
- {
- throw new BusinessException("当前节点不存在或已被删除。");
- }
- // 2. 获取流程信息,检查ProcessType
- var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
- .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0);
- if (process == null)
- {
- throw new BusinessException("关联的流程不存在。");
- }
- // 3. 节点操作验证
- ValidateNodeOperation(node, processStatus);
- // 4. 存储更新前的值
- var before = new Grp_ProcessNode()
- {
- Id = node.Id,
- ProcessId = node.ProcessId,
- NodeName = node.NodeName,
- NodeOrder = node.NodeOrder,
- OverallStatus = node.OverallStatus,
- Operator = node.Operator,
- OperationTime = node.OperationTime,
- IsCurrent = node.IsCurrent,
- };
- // 5. 更新节点状态
- node.OverallStatus = processStatus;
- node.Operator = currUserId;
- node.OperationTime = DateTime.Now;
- var updateCount = await _sqlSugar.Updateable(node)
- .UpdateColumns(n => new
- {
- n.OverallStatus,
- n.Operator,
- n.OperationTime
- })
- .ExecuteCommandAsync();
- if (updateCount == 0)
- {
- throw new BusinessException("节点状态更新失败。");
- }
- // 6. 记录节点日志
- await LogNodeOpAsync(before, node, "Update", currUserId);
- // 7. 如果是完成当前节点,处理流程流转
- // 当前节点或者流程类型为商邀可进入状态流转
- if (processStatus == ProcessStatus.Completed && (node.IsCurrent || process.ProcessType == GroupProcessType.Invitation))
- {
- await ProcessCurrentNodeCompletionAsync(node, currUserId);
- }
- return new Result { Code = StatusCodes.Status200OK, Msg = "操作成功。" };
- });
- return result.IsSuccess ? result.Data : new Result
- {
- Code = StatusCodes.Status500InternalServerError,
- Msg = result.ErrorMessage
- };
- }
- catch (BusinessException ex)
- {
- // 业务异常
- return new Result { Code = StatusCodes.Status400BadRequest, Msg = ex.Message };
- }
- catch (Exception ex)
- {
- // 系统异常
- return new Result { Code = StatusCodes.Status500InternalServerError, Msg = "系统错误,请稍后重试" };
- }
- }
- /// <summary>
- /// 验证节点操作权限
- /// </summary>
- /// <param name="node">流程节点</param>
- /// <param name="targetStatus">目标状态</param>
- private static void ValidateNodeOperation(Grp_ProcessNode node, ProcessStatus targetStatus)
- {
- // 验证节点是否已完成
- if (node.OverallStatus == ProcessStatus.Completed)
- {
- throw new BusinessException("当前节点已完成,不可重复操作。");
- }
- // 验证状态流转是否合法(可选)
- //if (targetStatus != ProcessStatus.Completed)
- //{
- // throw new BusinessException("未开始或者进行中的节点只能重新完成,不可进行其他操作。");
- //}
- // 验证是否尝试将已完成节点改为其他状态
- if (node.OverallStatus == ProcessStatus.Completed && targetStatus != ProcessStatus.Completed)
- {
- throw new BusinessException("已完成节点不可修改状态。");
- }
- }
- /// <summary>
- /// 处理当前节点完成后的流程流转
- /// </summary>
- private async Task ProcessCurrentNodeCompletionAsync(Grp_ProcessNode currentNode, int currUserId)
- {
- // 1. 获取流程信息
- var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
- .FirstAsync(p => p.Id == currentNode.ProcessId && p.IsDel == 0);
- if (process == null)
- {
- throw new BusinessException("关联的流程不存在。");
- }
- var processBefore = new Grp_ProcessOverview()
- {
- Id = process.Id,
- GroupId = process.GroupId,
- ProcessOrder = process.ProcessOrder,
- ProcessType = process.ProcessType,
- OverallStatus = process.OverallStatus,
- StartTime = process.StartTime,
- EndTime = process.EndTime,
- UpdatedUserId = process.UpdatedUserId,
- UpdatedTime = process.UpdatedTime
- };
- // 2. 取消当前节点的当前状态
- var before = new Grp_ProcessNode()
- {
- Id = currentNode.Id,
- ProcessId = currentNode.ProcessId,
- NodeName = currentNode.NodeName,
- NodeOrder = currentNode.NodeOrder,
- OverallStatus = currentNode.OverallStatus,
- Operator = currentNode.Operator,
- OperationTime = currentNode.OperationTime,
- IsCurrent = currentNode.IsCurrent,
- };
- currentNode.IsCurrent = false;
- await _sqlSugar.Updateable(currentNode)
- .UpdateColumns(n => new { n.IsCurrent })
- .ExecuteCommandAsync();
- // 2.1 记录节点日志 取消当前节点状态
- await LogNodeOpAsync(before, currentNode, "Update", currUserId);
- // 3. 查找并激活下一个节点 商邀节点单独处理
- if (process.ProcessType == GroupProcessType.Invitation)
- {
- var invitaNodeStatus = await _sqlSugar.Queryable<Grp_ProcessNode>()
- .Where(x => x.IsDel == 0 && x.ProcessId == currentNode.ProcessId)
- .ToListAsync();
- int completedCount = invitaNodeStatus.Count(n => n.OverallStatus == ProcessStatus.Completed);
- int nodeCount = invitaNodeStatus.Count;
- if (completedCount == nodeCount) //全部子节点完成,该流程完成
- {
- process.OverallStatus = ProcessStatus.Completed;
- process.EndTime = DateTime.Now;
- }
- }
- else
- {
- var nextNode = await _sqlSugar.Queryable<Grp_ProcessNode>()
- .Where(n => n.ProcessId == currentNode.ProcessId
- && n.NodeOrder == currentNode.NodeOrder + 1
- && n.IsDel == 0)
- .FirstAsync();
- if (nextNode != null)
- {
- var nextNodeBefore = new Grp_ProcessNode()
- {
- Id = nextNode.Id,
- ProcessId = nextNode.ProcessId,
- NodeName = nextNode.NodeName,
- NodeOrder = nextNode.NodeOrder,
- OverallStatus = nextNode.OverallStatus,
- Operator = nextNode.Operator,
- OperationTime = nextNode.OperationTime,
- IsCurrent = nextNode.IsCurrent,
- };
- // 激活下一个节点
- nextNode.IsCurrent = true;
- nextNode.OverallStatus = ProcessStatus.InProgress;
- //nextNode.Operator = currUserId;
- //nextNode.OperationTime = DateTime.Now;
- var updateCount = await _sqlSugar.Updateable(nextNode)
- .UpdateColumns(n => new
- {
- n.IsCurrent,
- n.OverallStatus,
- n.Operator,
- n.OperationTime
- })
- .ExecuteCommandAsync();
- if (updateCount == 0)
- {
- throw new BusinessException("激活下一节点失败");
- }
- // 1.1 记录节点日志 激活下一节点当前节点状态
- await LogNodeOpAsync(nextNodeBefore, nextNode, "Start", currUserId);
- // 更新流程状态为进行中
- process.OverallStatus = ProcessStatus.InProgress;
- }
- else
- {
- // 下一节点不存在,整个流程完成
- process.OverallStatus = ProcessStatus.Completed;
- process.EndTime = DateTime.Now;
- }
- }
- // 4. 更新流程信息
- process.UpdatedUserId = currUserId;
- process.UpdatedTime = DateTime.Now;
- var processUpdateCount = await _sqlSugar.Updateable(process)
- .UpdateColumns(p => new
- {
- p.OverallStatus,
- p.EndTime,
- p.UpdatedUserId,
- p.UpdatedTime
- })
- .ExecuteCommandAsync();
- if (processUpdateCount == 0)
- {
- throw new BusinessException("流程状态更新失败。");
- }
- //记录流程日志
- await LogProcessOpAsync(processBefore, process, "Update", currUserId);
- }
- /// <summary>
- /// 更新签证节点信息及状态
- /// </summary>
- /// <param name="dto">签证节点更新数据传输对象</param>
- /// <returns>操作结果</returns>
- public async Task<Result> UpdateVisaNodeDetailsAsync(GroupProcessUpdateVisaNodeDetailsDto dto)
- {
- // 1. 获取并验证节点和流程
- var node = await _sqlSugar.Queryable<Grp_ProcessNode>()
- .FirstAsync(n => n.Id == dto.NodeId && n.IsDel == 0)
- ?? throw new BusinessException("当前节点不存在或已被删除。");
- var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
- .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0)
- ?? throw new BusinessException("当前流程不存在或已被删除。");
- if (process.ProcessType != GroupProcessType.Visa)
- {
- throw new BusinessException("当前流程节点不为签证流程,不可编辑。");
- }
- // 2. 检查签证子节点 字段信息是否全部填写
- var allSubNodesCompleted = dto.VisaSubNodes?.All(subNode => EntityExtensions.IsCompleted(subNode)) ?? false;
- // 2.1 存储更新前流程及节点信息
- var nodeBefore = new Grp_ProcessNode()
- {
- Id = node.Id,
- ProcessId = node.ProcessId,
- NodeName = node.NodeName,
- NodeOrder = node.NodeOrder,
- OverallStatus = node.OverallStatus,
- Operator = node.Operator,
- OperationTime = node.OperationTime,
- IsCurrent = node.IsCurrent,
- };
- var processBefore = new Grp_ProcessOverview()
- {
- Id = process.Id,
- GroupId = process.GroupId,
- ProcessOrder = process.ProcessOrder,
- ProcessType = process.ProcessType,
- OverallStatus = process.OverallStatus,
- StartTime = process.StartTime,
- EndTime = process.EndTime,
- UpdatedUserId = process.UpdatedUserId,
- UpdatedTime = process.UpdatedTime
- };
- // 3. 更新节点信息
- node.Remark = JsonConvert.SerializeObject(dto.VisaSubNodes);
- node.Operator = dto.CurrUserId;
- node.OperationTime = DateTime.Now;
- if (allSubNodesCompleted)
- {
- node.OverallStatus = ProcessStatus.Completed;
- process.OverallStatus = ProcessStatus.Completed;
- process.EndTime = DateTime.Now;
- process.UpdatedUserId = dto.CurrUserId;
- process.UpdatedTime = DateTime.Now;
- // 更新流程状态
- await _sqlSugar.Updateable(process)
- .UpdateColumns(p => new
- {
- p.OverallStatus,
- p.EndTime,
- p.UpdatedUserId,
- p.UpdatedTime
- })
- .ExecuteCommandAsync();
- //记录流程日志
- await LogProcessOpAsync(processBefore, process, "Update", dto.CurrUserId);
- }
- // 4. 保存节点更新
- await _sqlSugar.Updateable(node)
- .UpdateColumns(n => new
- {
- n.Remark,
- n.Operator,
- n.OperationTime,
- n.OverallStatus
- })
- .ExecuteCommandAsync();
- //记录节点日志
- await LogNodeOpAsync(nodeBefore, node, "Update", dto.CurrUserId);
- return new Result { Code = 200, Msg = "节点信息更新成功。" };
- }
- /// <summary>
- /// 更新签证节点信息及状态
- /// </summary>
- /// <param name="dto">签证节点更新数据传输对象</param>
- /// <returns>操作结果</returns>
- public async Task<Result> SetActualDoneAsync(int nodeId, DateTime dt, int currUserId)
- {
- // 1. 获取并验证节点和流程
- var node = await _sqlSugar.Queryable<Grp_ProcessNode>()
- .FirstAsync(n => n.Id == nodeId && n.IsDel == 0)
- ?? throw new BusinessException("当前节点不存在或已被删除。");
- var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
- .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0)
- ?? throw new BusinessException("当前流程不存在或已被删除。");
- // 2.1 存储更新前流程及节点信息
- var nodeBefore = new Grp_ProcessNode()
- {
- Id = node.Id,
- ProcessId = node.ProcessId,
- NodeName = node.NodeName,
- NodeOrder = node.NodeOrder,
- OverallStatus = node.OverallStatus,
- Operator = node.Operator,
- OperationTime = node.OperationTime,
- IsCurrent = node.IsCurrent,
- };
- node.ActualDone = dt;
- // 3. 保存节点更新
- await _sqlSugar.Updateable(node)
- .UpdateColumns(n => new
- {
- n.ActualDone
- })
- .ExecuteCommandAsync();
- //记录节点日志
- await LogNodeOpAsync(nodeBefore, node, "Update", currUserId);
- return new Result { Code = 200, Msg = "实际操作时间设置成功。" };
- }
- #region 操作日志
- /// <summary>
- /// 记录流程操作日志
- /// </summary>
- /// <param name="before">操作前</param>
- /// <param name="after">操作后</param>
- /// <param name="opType">操作类型(Create - 创建、Update - 更新、Complete - 完成)</param>
- /// <param name="operId">操作人ID</param>
- /// <returns>异步任务</returns>
- public async Task LogProcessOpAsync(Grp_ProcessOverview before, Grp_ProcessOverview after,string opType, int operId)
- {
- var chgDetails = GetProcessChgDetails(before, after);
- var log = new Grp_ProcessLog
- {
- ProcessId = after?.Id ?? before?.Id,
- GroupId = after?.GroupId ?? before?.GroupId ?? 0,
- OpType = opType,
- OpDesc = GenerateProcessOpDesc(opType, before, after, chgDetails),
- BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
- AfterData = after != null ? JsonConvert.SerializeObject(after, GetJsonSettings()) : null,
- ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
- CreateUserId = operId
- };
- await _sqlSugar.Insertable(log).ExecuteCommandAsync();
- }
- /// <summary>
- /// 记录节点操作日志
- /// </summary>
- /// <param name="before">操作前</param>
- /// <param name="after">操作后</param>
- /// <param name="opType">操作类型(Create - 创建、Update - 更新、Start - 启动、Complete - 完成)</param>
- /// <param name="operId">操作人ID</param>
- /// <returns>异步任务</returns>
- public async Task LogNodeOpAsync(Grp_ProcessNode before, Grp_ProcessNode after,string opType, int operId)
- {
- var chgDetails = GetNodeChgDetails(before, after);
- var log = new Grp_ProcessLog
- {
- NodeId = after?.Id ?? before?.Id,
- ProcessId = after?.ProcessId ?? before?.ProcessId,
- GroupId = 0, // 通过流程ID关联获取
- OpType = opType,
- OpDesc = GenerateNodeOpDesc(opType, before, after, chgDetails),
- BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
- AfterData = after != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
- ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
- CreateUserId = operId
- };
- await _sqlSugar.Insertable(log).ExecuteCommandAsync();
- }
- /// <summary>
- /// 获取流程变更详情
- /// </summary>
- /// <param name="before">变更前</param>
- /// <param name="after">变更后</param>
- /// <returns>变更详情</returns>
- private List<FieldChgDetail> GetProcessChgDetails(Grp_ProcessOverview before, Grp_ProcessOverview after)
- {
- var chgDetails = new List<FieldChgDetail>();
- if (before == null || after == null) return chgDetails;
- var props = typeof(Grp_ProcessOverview).GetProperties(BindingFlags.Public | BindingFlags.Instance)
- .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
- foreach (var prop in props)
- {
- var beforeVal = prop.GetValue(before);
- var afterVal = prop.GetValue(after);
- if (!Equals(beforeVal, afterVal))
- {
- chgDetails.Add(new FieldChgDetail
- {
- FieldName = prop.Name,
- BeforeValue = FormatVal(beforeVal),
- AfterValue = FormatVal(afterVal)
- });
- }
- }
- return chgDetails;
- }
- /// <summary>
- /// 获取节点变更详情
- /// </summary>
- /// <param name="before">变更前</param>
- /// <param name="after">变更后</param>
- /// <returns>变更详情</returns>
- private List<FieldChgDetail> GetNodeChgDetails(Grp_ProcessNode before, Grp_ProcessNode after)
- {
- var chgDetails = new List<FieldChgDetail>();
- if (before == null || after == null) return chgDetails;
- var props = typeof(Grp_ProcessNode).GetProperties(BindingFlags.Public | BindingFlags.Instance)
- .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
- foreach (var prop in props)
- {
- var beforeVal = prop.GetValue(before);
- var afterVal = prop.GetValue(after);
- if (!Equals(beforeVal, afterVal))
- {
- chgDetails.Add(new FieldChgDetail
- {
- FieldName = prop.Name,
- BeforeValue = FormatVal(beforeVal),
- AfterValue = FormatVal(afterVal)
- });
- }
- }
- return chgDetails;
- }
- /// <summary>
- /// 生成流程操作描述
- /// </summary>
- /// <param name="opType">操作类型</param>
- /// <param name="before">操作前</param>
- /// <param name="after">操作后</param>
- /// <param name="chgDetails">变更详情</param>
- /// <returns>操作描述</returns>
- private string GenerateProcessOpDesc(string opType, Grp_ProcessOverview before,
- Grp_ProcessOverview after, List<FieldChgDetail> chgDetails)
- {
- var processType = after?.ProcessType ?? before?.ProcessType;
- var processName = GetProcessTypeName(processType);
- if (!chgDetails.Any())
- {
- return opType switch
- {
- "Create" => $"创建流程:{processName}",
- "Update" => $"更新流程:{processName} - 无变更",
- //"Start" => $"启动流程:{processName}",
- "Complete" => $"完成流程:{processName}",
- //"Delete" => $"删除流程:{processName}",
- _ => $"{opType}:{processName}"
- };
- }
- var chgDesc = string.Join("; ", chgDetails.Select(x =>
- $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
- return $"{GetOpTypeDisplay(opType)}:{processName} - {chgDesc}";
- }
- /// <summary>
- /// 获取JSON序列化设置
- /// </summary>
- /// <returns>JSON设置</returns>
- private static JsonSerializerSettings GetJsonSettings()
- {
- return new JsonSerializerSettings
- {
- ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
- NullValueHandling = NullValueHandling.Ignore,
- DateFormatString = "yyyy-MM-dd HH:mm:ss",
- Formatting = Formatting.None
- };
- }
- /// <summary>
- /// 生成节点操作描述
- /// </summary>
- /// <param name="opType">操作类型</param>
- /// <param name="before">操作前</param>
- /// <param name="after">操作后</param>
- /// <param name="chgDetails">变更详情</param>
- /// <returns>操作描述</returns>
- private string GenerateNodeOpDesc(string opType, Grp_ProcessNode before,
- Grp_ProcessNode after, List<FieldChgDetail> chgDetails)
- {
- var nodeName = after?.NodeName ?? before?.NodeName;
- if (!chgDetails.Any())
- {
- return opType switch
- {
- "Create" => $"创建节点:{nodeName}",
- "Update" => $"更新节点:{nodeName} - 无变更",
- "Start" => $"启动节点:{nodeName}",
- "Complete" => $"完成节点:{nodeName}",
- //"Delete" => $"删除节点:{nodeName}",
- _ => $"{opType}:{nodeName}"
- };
- }
- var chgDesc = string.Join("; ", chgDetails.Select(x =>
- $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
- return $"{GetOpTypeDisplay(opType)}:{nodeName} - {chgDesc}";
- }
- /// <summary>
- /// 获取流程类型名称
- /// </summary>
- /// <param name="processType">流程类型</param>
- /// <returns>流程名称</returns>
- private static string GetProcessTypeName(GroupProcessType? processType)
- {
- return processType switch
- {
- GroupProcessType.Invitation => "商邀报批",
- GroupProcessType.Visa => "签证",
- GroupProcessType.AirTicket => "机票",
- GroupProcessType.Hotel => "酒店",
- GroupProcessType.LocalGuide => "地接",
- GroupProcessType.FeeSettle => "费用结算",
- _ => "未知流程"
- };
- }
- /// <summary>
- /// 获取操作类型显示
- /// </summary>
- /// <param name="opType">操作类型</param>
- /// <returns>显示名称</returns>
- private static string GetOpTypeDisplay(string opType)
- {
- return opType switch
- {
- "Create" => "创建",
- "Update" => "更新",
- "Start" => "启动",
- "Complete" => "完成",
- "Delete" => "删除",
- "StatusChg" => "状态变更",
- _ => opType
- };
- }
- /// <summary>
- /// 获取字段显示名称
- /// </summary>
- /// <param name="fieldName">字段名</param>
- /// <returns>显示名称</returns>
- private string GetFieldDisplayName(string fieldName)
- {
- return fieldName switch
- {
- "OverallStatus" => "状态",
- "ProcessOrder" => "流程顺序",
- "StartTime" => "开始时间",
- "EndTime" => "结束时间",
- "NodeOrder" => "节点顺序",
- "NodeName" => "节点名称",
- "IsCurrent" => "当前节点",
- "Operator" => "操作人",
- "OperationTime" => "操作时间",
- _ => fieldName
- };
- }
- /// <summary>
- /// 格式化值显示
- /// </summary>
- /// <param name="value">值</param>
- /// <returns>格式化值</returns>
- private string FormatVal(object value)
- {
- if (value == null) return "空";
- if (value is ProcessStatus status)
- {
- return status switch
- {
- ProcessStatus.UnStarted => "未开始",
- ProcessStatus.InProgress => "进行中",
- ProcessStatus.Completed => "已完成",
- _ => status.ToString()
- };
- }
- if (value is bool boolVal) return boolVal ? "是" : "否";
- if (value is DateTime dateVal) return dateVal.ToString("yyyy-MM-dd HH:mm");
- var strVal = value.ToString();
- return string.IsNullOrEmpty(strVal) ? "空" : strVal;
- }
- /// <summary>
- /// 检查是否排除字段
- /// </summary>
- /// <param name="fieldName">字段名</param>
- /// <returns>是否排除</returns>
- private bool IsExclField(string fieldName)
- {
- var exclFields = new List<string>
- {
- "Id", "CreateTime", "CreateUserId", "UpdatedTime", "UpdatedUserId",
- "Nodes", "Process" // 导航属性
- };
- return exclFields.Contains(fieldName);
- }
- /// <summary>
- /// 获取流程日志
- /// </summary>
- /// <param name="processId">流程ID</param>
- /// <returns>日志列表</returns>
- public async Task<List<Grp_ProcessLog>> GetProcessLogsAsync(int processId)
- {
- return await _sqlSugar.Queryable<Grp_ProcessLog>()
- .Where(x => x.ProcessId == processId)
- .OrderByDescending(x => x.CreateTime)
- .ToListAsync();
- }
- /// <summary>
- /// 获取团组流程日志
- /// </summary>
- /// <param name="groupId">团组ID</param>
- /// <returns>日志列表</returns>
- public async Task<List<Grp_ProcessLog>> GetGroupLogsAsync(int groupId)
- {
- return await _sqlSugar.Queryable<Grp_ProcessLog>()
- .Where(x => x.GroupId == groupId)
- .OrderByDescending(x => x.CreateTime)
- .ToListAsync();
- }
- #endregion
- }
- }
|