ProcessOverviewRepository.cs 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. using AutoMapper;
  2. using Newtonsoft.Json;
  3. using OASystem.Domain;
  4. using OASystem.Domain.Dtos.Groups;
  5. using OASystem.Domain.Entities.Groups;
  6. using OASystem.Domain.Entities.Resource;
  7. using System.Reflection;
  8. namespace OASystem.Infrastructure.Repositories.Groups
  9. {
  10. /// <summary>
  11. /// 团组流程总览表仓储
  12. /// </summary>
  13. public class ProcessOverviewRepository : BaseRepository<Grp_ProcessOverview, Grp_ProcessOverview>
  14. {
  15. private readonly IMapper _mapper;
  16. private readonly DelegationInfoRepository _groupRep;
  17. public ProcessOverviewRepository(SqlSugarClient sqlSugar, IMapper mapper, DelegationInfoRepository groupRep) : base(sqlSugar)
  18. {
  19. _mapper = mapper;
  20. _groupRep = groupRep;
  21. }
  22. /// <summary>
  23. /// 基础数据初始化-团组流程
  24. /// </summary>
  25. /// <param name="groupId"></param>
  26. /// <param name="currUserId"></param>
  27. /// <returns></returns>
  28. public async Task<List<Grp_ProcessOverview>> ProcessDataInitAsync(int groupId, int currUserId, List<string> visaCountries)
  29. {
  30. var processs = new List<Grp_ProcessOverview>();
  31. //团组验证
  32. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
  33. if (groupInfo == null) return processs;
  34. // 检查是否已存在流程
  35. var existingProcesses = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  36. .Where(p => p.IsDel == 0 && p.GroupId == groupId)
  37. .ToListAsync();
  38. if (existingProcesses.Any()) return processs;
  39. #region 商邀报批流程
  40. var custInfo = await _sqlSugar.Queryable<Grp_TourClientList>()
  41. .Where(c => c.DiId == groupId && c.IsDel == 0)
  42. .OrderByDescending(c => c.CreateTime)
  43. .FirstAsync();
  44. string oaNode2Tips = "客户提供完整名单后,2周内取得邀请函(翻译件)。";
  45. if (custInfo != null)
  46. {
  47. oaNode2Tips = $"请于{custInfo.CreateTime.AddDays(14):yyyy年MM月dd日}内完成该项工作(客户提供完整名单后,2周内取得邀请函(翻译件))";
  48. }
  49. var oaNode4Tips = $"请于{groupInfo.VisitDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(按进度实际公务活动落实情况,出发前5日落实公务)";
  50. var oaNode7Tips = $"请于{groupInfo.VisitEndDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(团组结束前完成)";
  51. processs.Add(
  52. Grp_ProcessOverview.Create(groupId, 1, GroupProcessType.Invitation, ProcessStatus.InProgress, currUserId,
  53. new List<Grp_ProcessNode>()
  54. {
  55. Grp_ProcessNode.Create(1, "报批基础资料准备","更新报批行程和请示,提供其他报批所需材料,4个工作日内完成。",ProcessStatus.InProgress, true,false,false,false,currUserId),
  56. Grp_ProcessNode.Create(2, "报批邀请函资料准备",oaNode2Tips, ProcessStatus.InProgress, false,false,false,false,currUserId),
  57. Grp_ProcessNode.Create(3, "获得批件","提供完整的报批全套资源。",ProcessStatus.InProgress, false,false,false,false, currUserId ),
  58. Grp_ProcessNode.Create(4, "对接公务",oaNode4Tips,ProcessStatus.InProgress, false,false,false,false, currUserId),
  59. Grp_ProcessNode.Create(5, "参与翻译对接","",ProcessStatus.InProgress, false,false,true,true, currUserId),
  60. Grp_ProcessNode.Create(6, "商邀文案配合","",ProcessStatus.InProgress, false,false,false,false, currUserId),
  61. Grp_ProcessNode.Create(7, "票据上传(相关票据)",oaNode7Tips,ProcessStatus.InProgress, false,false,true,false, currUserId),
  62. }));
  63. #endregion
  64. #region 签证流程
  65. //单独处理签证流程节点
  66. var visaNodes = new List<Grp_ProcessNode>();
  67. if (visaCountries != null && visaCountries.Count > 0)
  68. {
  69. var visaDefualtNodes = new List<VisaProcessNode>();
  70. for (int i = 1; i < visaCountries.Count + 1; i++)
  71. {
  72. visaDefualtNodes.Add(VisaProcessNode.Info(i, visaCountries[i - 1].ToString()));
  73. }
  74. var visaNode2Tips = $"请于{groupInfo.VisitDate:yyyy年MM月dd日}内完成该项工作(按进度实际签证办理落实情况,团组出发前上传票据。)";
  75. visaNodes.Add(Grp_ProcessNode.Create(1, "签证信息", "", ProcessStatus.InProgress, true, false, false, false, currUserId, JsonConvert.SerializeObject(visaDefualtNodes)));
  76. visaNodes.Add(Grp_ProcessNode.Create(2, "票据上传(明细表、费用票据、保单及超支费用账单)", visaNode2Tips, ProcessStatus.InProgress, false, false, true, false, currUserId));
  77. }
  78. processs.Add(Grp_ProcessOverview.Create(groupId, 2, GroupProcessType.Visa, ProcessStatus.UnStarted, currUserId, visaNodes));
  79. #endregion
  80. #region 机票流程
  81. string airNode1Tips = "建团后打勾确认出团的时候开始24小时内。";
  82. if (groupInfo.Step == 1 || groupInfo.Step == 2)
  83. {
  84. if (groupInfo.StepOperationTime.HasValue)
  85. {
  86. airNode1Tips = $"请于{groupInfo.StepOperationTime.Value.AddDays(1):yyyy年MM月dd日}内完成该项工作(建团后打勾确认出团的时候开始24小时内)";
  87. }
  88. }
  89. var airNode5Tips = $"请于{groupInfo.VisitDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(团组出发前5日)";
  90. var airNode7Tips = $"请于{groupInfo.VisitEndDate.AddDays(5):yyyy年MM月dd日}内完成该项工作(团组归国后5个工作日内)";
  91. var airNode8Tips = $"请于{groupInfo.VisitEndDate.AddDays(10):yyyy年MM月dd日}内完成该项工作(团组归国后10个工作日内) *按机票报价*0.999折扣出具机票报销蓝联、行程单及机票说明";
  92. processs.Add(
  93. Grp_ProcessOverview.Create(groupId, 3, GroupProcessType.AirTicket, ProcessStatus.InProgress, currUserId,
  94. new List<Grp_ProcessNode>()
  95. {
  96. Grp_ProcessNode.Create(1, "初步拟定航程方案及价格", airNode1Tips, ProcessStatus.InProgress, true,false,false,false, currUserId ),
  97. Grp_ProcessNode.Create(2, "机票占位、续位", "", ProcessStatus.UnStarted, false,false,false,false,currUserId ),
  98. Grp_ProcessNode.Create(3, "完成机票采购确认(含预算核对、出票确认等)", "", ProcessStatus.UnStarted,false,false,false,false, currUserId),
  99. Grp_ProcessNode.Create(4, "进行出票操作并核查信息", "", ProcessStatus.UnStarted, false,false,false,false, currUserId),
  100. Grp_ProcessNode.Create(5, "机票已出", airNode5Tips, ProcessStatus.UnStarted, false,false,false,false, currUserId),
  101. Grp_ProcessNode.Create(6, "完成机票选座", "", ProcessStatus.UnStarted, false,false,false,false,currUserId),
  102. Grp_ProcessNode.Create(7, "票据上传(机票超支费用账单)", airNode7Tips, ProcessStatus.UnStarted, false,false,true,false, currUserId),
  103. Grp_ProcessNode.Create(8, "票据上传(机票报销蓝联、行程单及机票说明)", airNode8Tips, ProcessStatus.UnStarted, false,false,true,false, currUserId)
  104. }
  105. )
  106. );
  107. #endregion
  108. #region 酒店流程
  109. string hotelNode1Tips = "建团后打勾确认出团的时候开始2个工作日。";
  110. if (groupInfo.Step == 1 || groupInfo.Step == 2)
  111. {
  112. if (groupInfo.StepOperationTime.HasValue)
  113. {
  114. hotelNode1Tips = $"请于{groupInfo.StepOperationTime.Value.AddDays(2):yyyy年MM月dd日}内完成该项工作(建团后打勾确认出团的时候开始2个工作日)";
  115. }
  116. }
  117. var hotelNode4Tips = $"请于{groupInfo.VisitDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(团组出发前5天)";
  118. var hotelNode5Tips = $"请于{groupInfo.VisitEndDate.AddDays(5):yyyy年MM月dd日}内完成该项工作(团组结束后5天内)";
  119. processs.Add(
  120. Grp_ProcessOverview.Create(groupId, 4, GroupProcessType.Hotel, ProcessStatus.InProgress, currUserId,
  121. new List<Grp_ProcessNode>()
  122. {
  123. Grp_ProcessNode.Create(1, "筛选并按照预算标准,对目标酒店进行询价、比价、谈价", hotelNode1Tips, ProcessStatus.InProgress, true, false, false, false, currUserId),
  124. Grp_ProcessNode.Create(2, "获取酒店确认函与入住名单核对", "", ProcessStatus.UnStarted, false, false, false,false, currUserId ),
  125. Grp_ProcessNode.Create(3, "预订酒店并录入OA", "", ProcessStatus.UnStarted,false, false, false,false,currUserId ),
  126. Grp_ProcessNode.Create(4, "行前再次确认酒店订单、付款状态及入住安排", hotelNode4Tips,ProcessStatus.UnStarted, false, false, false,false,currUserId ),
  127. Grp_ProcessNode.Create(5, "行程结束后整理酒店发票(含超支费用发票)与结算", hotelNode5Tips, ProcessStatus.UnStarted, false, false, true,false, currUserId ),
  128. }
  129. )
  130. );
  131. #endregion
  132. #region 地接流程
  133. var airTripCodeInfo = await _sqlSugar.Queryable<Air_TicketBlackCode>()
  134. .Where(x => x.IsDel == 0 && x.DiId == groupId)
  135. .OrderByDescending(x => x.CreateTime)
  136. .FirstAsync();
  137. string opNode1Tips = $"机票行程代码最后一段录入后1个工作日内。";
  138. if (airTripCodeInfo != null)
  139. {
  140. opNode1Tips = $"请于{airTripCodeInfo.CreateTime.AddDays(1):yyyy年MM月dd日}内完成该项工作(机票行程代码最后一段录入后1个工作日内)";
  141. }
  142. string opNode2Tips = $"请于{groupInfo.CreateTime.AddDays(7):yyyy年MM月dd日}内完成该项工作(建团完成后7个工作日内)";
  143. string opNode3Tips = $"请于{groupInfo.CreateTime.AddDays(10):yyyy年MM月dd日}内完成该项工作(上一步往后3个工作日内)";
  144. string opNode4Tips = $"请于{groupInfo.CreateTime.AddDays(12):yyyy年MM月dd日}内完成该项工作(上一步往后2个工作日内)";
  145. var backListInfo = await _sqlSugar.Queryable<Grp_InvertedList>().Where(x => x.DiId == groupId && x.IsDel == 0).FirstAsync();
  146. string opNode5Tips = $"倒推表里开行前会 -3天。";
  147. if (backListInfo != null) {
  148. if (DateTime.TryParse(backListInfo.PreTripMeetingDt,out DateTime dateTime))
  149. {
  150. opNode5Tips = $"请于{dateTime.AddDays(-3):yyyy年MM月dd日}内完成该项工作(倒推表里开行前会 -3天)";
  151. }
  152. }
  153. string opNode7Tips = $"请于{groupInfo.VisitEndDate.AddDays(5):yyyy年MM月dd日}内完成该项工作(团组归国后5个工作日内) *上传最终报批行程,确定城市间交通最终版报价分配;地接账单(清楚标注超时及其他项超支费用)、地接交通费用原始票据、城市间交通明细表;";
  154. processs.Add(
  155. Grp_ProcessOverview.Create(groupId, 5, GroupProcessType.LocalGuide, ProcessStatus.InProgress, currUserId,
  156. new List<Grp_ProcessNode>()
  157. {
  158. Grp_ProcessNode.Create(1,"根据机票方案出框架行程", opNode1Tips,ProcessStatus.InProgress, true, false, false,false,currUserId ),
  159. Grp_ProcessNode.Create(2,"联系并询价地接、餐厅、用车、景点等供应商", opNode2Tips,ProcessStatus.UnStarted, false, false, false,false, currUserId ),
  160. Grp_ProcessNode.Create(3,"提交供应商报价及比价表", opNode3Tips, ProcessStatus.UnStarted, false, false, false, false,currUserId),
  161. Grp_ProcessNode.Create(4,"执行采购流程", opNode4Tips, ProcessStatus.UnStarted, false, false, false,false, currUserId),
  162. Grp_ProcessNode.Create(5,"制定最终《行程表》及《出行手册》", opNode5Tips, ProcessStatus.UnStarted, false, false, false,false, currUserId ),
  163. Grp_ProcessNode.Create(6,"送机", "", ProcessStatus.UnStarted, false, false, false,false, currUserId ),
  164. Grp_ProcessNode.Create(7,"最终版报批行程、票据上传", opNode7Tips, ProcessStatus.UnStarted, false, false, true, false,currUserId )
  165. }
  166. )
  167. );
  168. #endregion
  169. #region 费用结算流程
  170. var feeNode3Tips = $"请于{groupInfo.VisitEndDate.AddDays(12):yyyy年MM月dd日}内完成该项工作(团组归国后12个工作日内)";
  171. processs.Add(
  172. Grp_ProcessOverview.Create(groupId, 6, GroupProcessType.FeeSettle, ProcessStatus.InProgress, currUserId,
  173. new List<Grp_ProcessNode>()
  174. {
  175. Grp_ProcessNode.Create(1, "城市间交通报批金额核定", "团组报批前", ProcessStatus.InProgress, true, true, false,false,currUserId ),
  176. Grp_ProcessNode.Create(2, "团组全程各段机票打票金额的核定", "团组报批后、订票前", ProcessStatus.UnStarted, false, false, false,false,currUserId ),
  177. Grp_ProcessNode.Create(3, "整理统计团组超支费用、三公报销资料给到各单位", feeNode3Tips, ProcessStatus.UnStarted, false, false, false,false,currUserId ),
  178. Grp_ProcessNode.Create(4, "费用结算完毕", "", ProcessStatus.UnStarted, false, false, false,false, currUserId ),
  179. }
  180. )
  181. );
  182. #endregion
  183. return processs;
  184. }
  185. /// <summary>
  186. /// 团组流程初始化
  187. /// </summary>
  188. /// <param name="request">创建流程请求参数</param>
  189. /// <returns>创建的流程信息</returns>
  190. public async Task<Result> ProcessInitAsync(int groupId, int currUserId)
  191. {
  192. //团组验证
  193. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
  194. if (groupInfo == null)
  195. {
  196. return new Result { Code = 400, Msg = "团组不存在" };
  197. }
  198. // 检查是否已存在流程
  199. var existingProcesses = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  200. .Where(p => p.IsDel == 0 && p.GroupId == groupId)
  201. .ToListAsync();
  202. if (existingProcesses.Any())
  203. {
  204. return new Result { Code = 400, Msg = "该团组的流程已存在" };
  205. }
  206. //处理签证国家
  207. var visaCountries = _groupRep.GroupSplitCountry(groupInfo.VisitCountry);
  208. // 定义默认的流程节点
  209. var processs = await ProcessDataInitAsync(groupId, currUserId, visaCountries);
  210. _sqlSugar.BeginTran();
  211. foreach (var item in processs)
  212. {
  213. var processId = await _sqlSugar.Insertable(item).ExecuteReturnIdentityAsync();
  214. if (processId < 1)
  215. {
  216. _sqlSugar.RollbackTran();
  217. return new Result { Code = 400, Msg = "团组流程进度总览表添加失败!" };
  218. }
  219. item.Id = processId;
  220. // 记录流程日志
  221. await LogProcessOpAsync(null, item, "Create", currUserId);
  222. var nodes = item.Nodes.Select((nodeDto, index) => new Grp_ProcessNode
  223. {
  224. ProcessId = processId,
  225. NodeName = nodeDto.NodeName,
  226. NodeOrder = nodeDto.NodeOrder,
  227. OverallStatus = nodeDto.OverallStatus,
  228. NodeDescTips = nodeDto.NodeDescTips,
  229. //Country = nodeDto.Country,
  230. IsCurrent = nodeDto.IsCurrent,
  231. IsAssist = nodeDto.IsAssist,
  232. IsFileUp = nodeDto.IsFileUp,
  233. Remark = nodeDto.Remark
  234. }).ToList();
  235. var nodeIds = await _sqlSugar.Insertable(nodes).ExecuteCommandAsync();
  236. if (nodeIds < 1)
  237. {
  238. _sqlSugar.RollbackTran();
  239. return new Result { Code = 400, Msg = "团组流程进度流程节点添加失败!" };
  240. }
  241. //设置节点ID
  242. nodes = await _sqlSugar.Queryable<Grp_ProcessNode>().Where(x => x.IsDel == 0 && x.ProcessId == processId).ToListAsync();
  243. //记录节点日志
  244. foreach (var node in nodes)
  245. {
  246. await LogNodeOpAsync(null, node, "Create", currUserId);
  247. }
  248. }
  249. _sqlSugar.CommitTran();
  250. return new Result { Code = 200, Msg = "添加成功!" }; ;
  251. }
  252. /// <summary>
  253. /// 获取团组的所有流程及流程详情
  254. /// </summary>
  255. /// <param name="request">创建流程请求参数</param>
  256. /// <returns>创建的流程信息</returns>
  257. public async Task<Result> ProcessesDetailsAsync(int groupId)
  258. {
  259. //团组验证
  260. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
  261. if (groupInfo == null)
  262. {
  263. return new Result { Code = 400, Msg = "团组不存在" };
  264. }
  265. // 检查是否已存在流程
  266. var existingProcesses = await _sqlSugar.Queryable<Grp_ProcessOverview>().Where(p => p.IsDel == 0 && p.GroupId == groupId).ToListAsync();
  267. if (!existingProcesses.Any())
  268. {
  269. //新建团组流程
  270. var res = await ProcessInitAsync(groupId, 4);
  271. if (res.Code != 200)
  272. {
  273. return res;
  274. }
  275. }
  276. var users = await _sqlSugar.Queryable<Sys_Users>().Select(x => new {x.Id,x.CnName }).ToListAsync();
  277. var processData = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  278. .Where(p => p.GroupId == groupId && p.IsDel == 0)
  279. .Mapper(p => p.Nodes, p => p.Nodes.First().ProcessId)
  280. .ToListAsync();
  281. // 预先构建用户字典,提升查询性能
  282. var userDict = users.ToDictionary(u => u.Id, u => u.CnName);
  283. var processes = processData.Select(p =>
  284. {
  285. var orderedNodes = p.Nodes.OrderBy(n => n.NodeOrder).ToList();
  286. var totalNodes = orderedNodes.Count;
  287. return new
  288. {
  289. p.Id,
  290. p.GroupId,
  291. p.ProcessType,
  292. ProcessName = p.ProcessType.GetEnumDescription(),
  293. Nodes = orderedNodes.Select((n, index) =>
  294. {
  295. var isLastNode = index == totalNodes - 1;
  296. var isSecondLastNode = index == totalNodes - 2;
  297. var isFifthStep = index == 4;
  298. // 计算按钮状态
  299. bool isEnaAssistBtn = p.ProcessType == GroupProcessType.FeeSettle && n.NodeOrder == 1;
  300. // 文件上传按钮启用规则
  301. bool isEnaFileUpBtn = false;
  302. // 是否参与按钮启用
  303. bool isEnaPartBtn = false;
  304. // 规则1:商邀流程第五步启用参与按钮
  305. if (p.ProcessType == GroupProcessType.Invitation && isFifthStep)
  306. {
  307. isEnaPartBtn = true;
  308. }
  309. // 规则2:机票流程倒数第二步启用上传按钮
  310. else if (p.ProcessType == GroupProcessType.AirTicket && isSecondLastNode)
  311. {
  312. isEnaFileUpBtn = true;
  313. }
  314. // 规则3:默认流程节点最后一步启用上传按钮
  315. else if (isLastNode && p.ProcessType != GroupProcessType.FeeSettle)
  316. {
  317. isEnaFileUpBtn = true;
  318. }
  319. // 处理签证子节点
  320. List<VisaProcessNode> visaSubNodes = new();
  321. if (p.ProcessType == GroupProcessType.Visa && n.NodeOrder == 1)
  322. {
  323. visaSubNodes = JsonConvert.DeserializeObject<List<VisaProcessNode>>(n.Remark ?? "[]")
  324. ?? new List<VisaProcessNode>();
  325. }
  326. // 获取操作人姓名(使用字典提升性能)
  327. string operatorName = "-";
  328. if (n.Operator.HasValue && userDict.TryGetValue(n.Operator.Value, out var name))
  329. {
  330. operatorName = name;
  331. }
  332. return new
  333. {
  334. n.Id,
  335. n.ProcessId,
  336. n.NodeOrder,
  337. n.NodeName,
  338. n.OverallStatus,
  339. StatusText = n.OverallStatus.GetEnumDescription(),
  340. Operator = operatorName,
  341. OpeateTime = n.OperationTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? "-",
  342. ActualDone = n.ActualDone?.ToString("yyyy-MM-dd HH:mm:ss") ?? "",
  343. n.NodeDescTips,
  344. isEnaAssistBtn, // 是否启用财务流程首节点协助按钮
  345. n.IsAssist, // 财务流程首节点 存储值
  346. isEnaFileUpBtn, // 是否启用上传文件按钮
  347. n.IsFileUp, // 票据上传节点 存储值
  348. isEnaPartBtn, // 是否启用参与按钮
  349. n.IsPart, // 参与按钮 存储值
  350. visaSubNodes // 签证节点类型使用
  351. };
  352. }).ToList()
  353. };
  354. }).ToList();
  355. return new Result { Code = 200, Data = processes, Msg = "查询成功!" };
  356. }
  357. /// <summary>
  358. /// 更新节点状态
  359. /// </summary>
  360. /// <param name="nodeId">节点ID</param>
  361. /// <param name="currUserId">当前用户ID</param>
  362. /// <param name="processStatus">流程状态,默认为已完成</param>
  363. /// <returns>操作结果</returns>
  364. public async Task<Result> UpdateNodeStatusAsync(int nodeId, int currUserId, ProcessStatus processStatus = ProcessStatus.Completed)
  365. {
  366. try
  367. {
  368. // 使用事务确保数据一致性
  369. var result = await _sqlSugar.Ado.UseTranAsync(async () =>
  370. {
  371. // 1. 获取并验证节点
  372. var node = await _sqlSugar.Queryable<Grp_ProcessNode>()
  373. .FirstAsync(n => n.Id == nodeId && n.IsDel == 0) ?? throw new BusinessException("当前节点不存在或已被删除。");
  374. // 2. 获取流程信息,检查ProcessType
  375. var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  376. .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0) ?? throw new BusinessException("关联的流程不存在。");
  377. // 3. 节点操作验证
  378. ValidateNodeOperation(node, processStatus);
  379. // 4. 存储更新前的值
  380. var before = new Grp_ProcessNode()
  381. {
  382. Id = node.Id,
  383. ProcessId = node.ProcessId,
  384. NodeName = node.NodeName,
  385. NodeOrder = node.NodeOrder,
  386. OverallStatus = node.OverallStatus,
  387. Operator = node.Operator,
  388. OperationTime = node.OperationTime,
  389. IsCurrent = node.IsCurrent,
  390. };
  391. // 5. 更新节点状态
  392. node.OverallStatus = processStatus;
  393. node.Operator = currUserId;
  394. node.OperationTime = DateTime.Now;
  395. var updateCount = await _sqlSugar.Updateable(node)
  396. .UpdateColumns(n => new
  397. {
  398. n.OverallStatus,
  399. n.Operator,
  400. n.OperationTime
  401. })
  402. .ExecuteCommandAsync();
  403. if (updateCount == 0)
  404. {
  405. throw new BusinessException("节点状态更新失败。");
  406. }
  407. // 6. 记录节点日志
  408. await LogNodeOpAsync(before, node, "Update", currUserId);
  409. // 7. 如果是完成当前节点,处理流程流转
  410. // 当前节点或者流程类型为商邀可进入状态流转
  411. if (processStatus == ProcessStatus.Completed && (node.IsCurrent || process.ProcessType == GroupProcessType.Invitation))
  412. {
  413. await ProcessCurrentNodeCompletionAsync(node, currUserId);
  414. }
  415. return new Result { Code = StatusCodes.Status200OK, Msg = "操作成功。" };
  416. });
  417. return result.IsSuccess ? result.Data : new Result
  418. {
  419. Code = StatusCodes.Status500InternalServerError,
  420. Msg = result.ErrorMessage
  421. };
  422. }
  423. catch (BusinessException ex)
  424. {
  425. // 业务异常
  426. return new Result { Code = StatusCodes.Status400BadRequest, Msg = ex.Message };
  427. }
  428. catch (Exception ex)
  429. {
  430. // 系统异常
  431. return new Result { Code = StatusCodes.Status500InternalServerError, Msg = "系统错误,请稍后重试" };
  432. }
  433. }
  434. /// <summary>
  435. /// 验证节点操作权限
  436. /// </summary>
  437. /// <param name="node">流程节点</param>
  438. /// <param name="targetStatus">目标状态</param>
  439. private static void ValidateNodeOperation(Grp_ProcessNode node, ProcessStatus targetStatus)
  440. {
  441. // 验证节点是否已完成
  442. if (node.OverallStatus == ProcessStatus.Completed)
  443. {
  444. throw new BusinessException("当前节点已完成,不可重复操作。");
  445. }
  446. // 验证状态流转是否合法(可选)
  447. //if (targetStatus != ProcessStatus.Completed)
  448. //{
  449. // throw new BusinessException("未开始或者进行中的节点只能重新完成,不可进行其他操作。");
  450. //}
  451. // 验证是否尝试将已完成节点改为其他状态
  452. if (node.OverallStatus == ProcessStatus.Completed && targetStatus != ProcessStatus.Completed)
  453. {
  454. throw new BusinessException("已完成节点不可修改状态。");
  455. }
  456. }
  457. /// <summary>
  458. /// 处理当前节点完成后的流程流转
  459. /// </summary>
  460. private async Task ProcessCurrentNodeCompletionAsync(Grp_ProcessNode currentNode, int currUserId)
  461. {
  462. // 1. 获取流程信息
  463. var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  464. .FirstAsync(p => p.Id == currentNode.ProcessId && p.IsDel == 0);
  465. if (process == null)
  466. {
  467. throw new BusinessException("关联的流程不存在。");
  468. }
  469. var processBefore = new Grp_ProcessOverview()
  470. {
  471. Id = process.Id,
  472. GroupId = process.GroupId,
  473. ProcessOrder = process.ProcessOrder,
  474. ProcessType = process.ProcessType,
  475. OverallStatus = process.OverallStatus,
  476. StartTime = process.StartTime,
  477. EndTime = process.EndTime,
  478. UpdatedUserId = process.UpdatedUserId,
  479. UpdatedTime = process.UpdatedTime
  480. };
  481. // 2. 取消当前节点的当前状态
  482. var before = new Grp_ProcessNode()
  483. {
  484. Id = currentNode.Id,
  485. ProcessId = currentNode.ProcessId,
  486. NodeName = currentNode.NodeName,
  487. NodeOrder = currentNode.NodeOrder,
  488. OverallStatus = currentNode.OverallStatus,
  489. Operator = currentNode.Operator,
  490. OperationTime = currentNode.OperationTime,
  491. IsCurrent = currentNode.IsCurrent,
  492. };
  493. currentNode.IsCurrent = false;
  494. await _sqlSugar.Updateable(currentNode)
  495. .UpdateColumns(n => new { n.IsCurrent })
  496. .ExecuteCommandAsync();
  497. // 2.1 记录节点日志 取消当前节点状态
  498. await LogNodeOpAsync(before, currentNode, "Update", currUserId);
  499. // 3. 查找并激活下一个节点 商邀节点单独处理
  500. if (process.ProcessType == GroupProcessType.Invitation)
  501. {
  502. var invitaNodeStatus = await _sqlSugar.Queryable<Grp_ProcessNode>()
  503. .Where(x => x.IsDel == 0 && x.ProcessId == currentNode.ProcessId)
  504. .ToListAsync();
  505. int completedCount = invitaNodeStatus.Count(n => n.OverallStatus == ProcessStatus.Completed);
  506. int nodeCount = invitaNodeStatus.Count;
  507. if (completedCount == nodeCount) //全部子节点完成,该流程完成
  508. {
  509. process.OverallStatus = ProcessStatus.Completed;
  510. process.EndTime = DateTime.Now;
  511. }
  512. }
  513. else
  514. {
  515. var nextNode = await _sqlSugar.Queryable<Grp_ProcessNode>()
  516. .Where(n => n.ProcessId == currentNode.ProcessId
  517. && n.NodeOrder == currentNode.NodeOrder + 1
  518. && n.IsDel == 0)
  519. .FirstAsync();
  520. if (nextNode != null)
  521. {
  522. var nextNodeBefore = new Grp_ProcessNode()
  523. {
  524. Id = nextNode.Id,
  525. ProcessId = nextNode.ProcessId,
  526. NodeName = nextNode.NodeName,
  527. NodeOrder = nextNode.NodeOrder,
  528. OverallStatus = nextNode.OverallStatus,
  529. Operator = nextNode.Operator,
  530. OperationTime = nextNode.OperationTime,
  531. IsCurrent = nextNode.IsCurrent,
  532. };
  533. // 激活下一个节点
  534. nextNode.IsCurrent = true;
  535. nextNode.OverallStatus = ProcessStatus.InProgress;
  536. //nextNode.Operator = currUserId;
  537. //nextNode.OperationTime = DateTime.Now;
  538. var updateCount = await _sqlSugar.Updateable(nextNode)
  539. .UpdateColumns(n => new
  540. {
  541. n.IsCurrent,
  542. n.OverallStatus,
  543. n.Operator,
  544. n.OperationTime
  545. })
  546. .ExecuteCommandAsync();
  547. if (updateCount == 0)
  548. {
  549. throw new BusinessException("激活下一节点失败");
  550. }
  551. // 1.1 记录节点日志 激活下一节点当前节点状态
  552. await LogNodeOpAsync(nextNodeBefore, nextNode, "Start", currUserId);
  553. // 更新流程状态为进行中
  554. process.OverallStatus = ProcessStatus.InProgress;
  555. }
  556. else
  557. {
  558. // 下一节点不存在,整个流程完成
  559. process.OverallStatus = ProcessStatus.Completed;
  560. process.EndTime = DateTime.Now;
  561. }
  562. }
  563. // 4. 更新流程信息
  564. process.UpdatedUserId = currUserId;
  565. process.UpdatedTime = DateTime.Now;
  566. var processUpdateCount = await _sqlSugar.Updateable(process)
  567. .UpdateColumns(p => new
  568. {
  569. p.OverallStatus,
  570. p.EndTime,
  571. p.UpdatedUserId,
  572. p.UpdatedTime
  573. })
  574. .ExecuteCommandAsync();
  575. if (processUpdateCount == 0)
  576. {
  577. throw new BusinessException("流程状态更新失败。");
  578. }
  579. //记录流程日志
  580. await LogProcessOpAsync(processBefore, process, "Update", currUserId);
  581. }
  582. /// <summary>
  583. /// 更新签证节点信息及状态
  584. /// </summary>
  585. /// <param name="dto">签证节点更新数据传输对象</param>
  586. /// <returns>操作结果</returns>
  587. public async Task<Result> UpdateVisaNodeDetailsAsync(GroupProcessUpdateVisaNodeDetailsDto dto)
  588. {
  589. // 1. 获取并验证节点和流程
  590. var node = await _sqlSugar.Queryable<Grp_ProcessNode>()
  591. .FirstAsync(n => n.Id == dto.NodeId && n.IsDel == 0)
  592. ?? throw new BusinessException("当前节点不存在或已被删除。");
  593. var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  594. .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0)
  595. ?? throw new BusinessException("当前流程不存在或已被删除。");
  596. if (process.ProcessType != GroupProcessType.Visa)
  597. {
  598. throw new BusinessException("当前流程节点不为签证流程,不可编辑。");
  599. }
  600. // 2. 检查签证子节点 字段信息是否全部填写
  601. var allSubNodesCompleted = dto.VisaSubNodes?.All(subNode => EntityExtensions.IsCompleted(subNode)) ?? false;
  602. // 2.1 存储更新前流程及节点信息
  603. var nodeBefore = new Grp_ProcessNode()
  604. {
  605. Id = node.Id,
  606. ProcessId = node.ProcessId,
  607. NodeName = node.NodeName,
  608. NodeOrder = node.NodeOrder,
  609. OverallStatus = node.OverallStatus,
  610. Operator = node.Operator,
  611. OperationTime = node.OperationTime,
  612. IsCurrent = node.IsCurrent,
  613. };
  614. var processBefore = new Grp_ProcessOverview()
  615. {
  616. Id = process.Id,
  617. GroupId = process.GroupId,
  618. ProcessOrder = process.ProcessOrder,
  619. ProcessType = process.ProcessType,
  620. OverallStatus = process.OverallStatus,
  621. StartTime = process.StartTime,
  622. EndTime = process.EndTime,
  623. UpdatedUserId = process.UpdatedUserId,
  624. UpdatedTime = process.UpdatedTime
  625. };
  626. // 3. 更新节点信息
  627. node.Remark = JsonConvert.SerializeObject(dto.VisaSubNodes);
  628. node.Operator = dto.CurrUserId;
  629. node.OperationTime = DateTime.Now;
  630. if (allSubNodesCompleted)
  631. {
  632. node.OverallStatus = ProcessStatus.Completed;
  633. process.OverallStatus = ProcessStatus.Completed;
  634. process.EndTime = DateTime.Now;
  635. process.UpdatedUserId = dto.CurrUserId;
  636. process.UpdatedTime = DateTime.Now;
  637. // 更新流程状态
  638. await _sqlSugar.Updateable(process)
  639. .UpdateColumns(p => new
  640. {
  641. p.OverallStatus,
  642. p.EndTime,
  643. p.UpdatedUserId,
  644. p.UpdatedTime
  645. })
  646. .ExecuteCommandAsync();
  647. //记录流程日志
  648. await LogProcessOpAsync(processBefore, process, "Update", dto.CurrUserId);
  649. }
  650. // 4. 保存节点更新
  651. await _sqlSugar.Updateable(node)
  652. .UpdateColumns(n => new
  653. {
  654. n.Remark,
  655. n.Operator,
  656. n.OperationTime,
  657. n.OverallStatus
  658. })
  659. .ExecuteCommandAsync();
  660. //记录节点日志
  661. await LogNodeOpAsync(nodeBefore, node, "Update", dto.CurrUserId);
  662. return new Result { Code = 200, Msg = "节点信息更新成功。" };
  663. }
  664. /// <summary>
  665. /// 更新签证节点信息及状态
  666. /// </summary>
  667. /// <param name="dto">签证节点更新数据传输对象</param>
  668. /// <returns>操作结果</returns>
  669. public async Task<Result> SetActualDoneAsync(GroupProcessSetActualDoneDto dto )
  670. {
  671. int nodeId = dto.NodeId;
  672. DateTime dt = dto.ActualDone;
  673. int currUserId = dto.CurrUserId;
  674. bool isAssist = dto.IsAssist;
  675. bool isFileUp = dto.IsFileUp;
  676. bool isPart = dto.IsPart;
  677. // 1. 获取并验证节点和流程
  678. var node = await _sqlSugar.Queryable<Grp_ProcessNode>()
  679. .FirstAsync(n => n.Id == nodeId && n.IsDel == 0)
  680. ?? throw new BusinessException("当前节点不存在或已被删除。");
  681. var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  682. .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0)
  683. ?? throw new BusinessException("当前流程不存在或已被删除。");
  684. // 2.1 存储更新前流程及节点信息
  685. var nodeBefore = new Grp_ProcessNode()
  686. {
  687. Id = node.Id,
  688. ProcessId = node.ProcessId,
  689. NodeName = node.NodeName,
  690. NodeOrder = node.NodeOrder,
  691. OverallStatus = node.OverallStatus,
  692. Operator = node.Operator,
  693. OperationTime = node.OperationTime,
  694. IsCurrent = node.IsCurrent
  695. };
  696. node.ActualDone = dt;
  697. node.IsAssist = isAssist;
  698. node.IsFileUp = isFileUp;
  699. node.IsPart = isPart;
  700. // 3. 保存节点更新
  701. await _sqlSugar.Updateable(node)
  702. .UpdateColumns(n => new
  703. {
  704. n.ActualDone,
  705. n.IsAssist,
  706. n.IsFileUp,
  707. n.IsPart,
  708. })
  709. .ExecuteCommandAsync();
  710. //记录节点日志
  711. await LogNodeOpAsync(nodeBefore, node, "Update", currUserId);
  712. return new Result { Code = 200, Msg = "实际操作时间设置成功。" };
  713. }
  714. #region 操作日志
  715. /// <summary>
  716. /// 记录流程操作日志
  717. /// </summary>
  718. /// <param name="before">操作前</param>
  719. /// <param name="after">操作后</param>
  720. /// <param name="opType">操作类型(Create - 创建、Update - 更新、Complete - 完成)</param>
  721. /// <param name="operId">操作人ID</param>
  722. /// <returns>异步任务</returns>
  723. public async Task LogProcessOpAsync(Grp_ProcessOverview before, Grp_ProcessOverview after,string opType, int operId)
  724. {
  725. var chgDetails = GetProcessChgDetails(before, after);
  726. var log = new Grp_ProcessLog
  727. {
  728. ProcessId = after?.Id ?? before?.Id,
  729. GroupId = after?.GroupId ?? before?.GroupId ?? 0,
  730. OpType = opType,
  731. OpDesc = GenerateProcessOpDesc(opType, before, after, chgDetails),
  732. BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  733. AfterData = after != null ? JsonConvert.SerializeObject(after, GetJsonSettings()) : null,
  734. ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
  735. CreateUserId = operId
  736. };
  737. await _sqlSugar.Insertable(log).ExecuteCommandAsync();
  738. }
  739. /// <summary>
  740. /// 记录节点操作日志
  741. /// </summary>
  742. /// <param name="before">操作前</param>
  743. /// <param name="after">操作后</param>
  744. /// <param name="opType">操作类型(Create - 创建、Update - 更新、Start - 启动、Complete - 完成)</param>
  745. /// <param name="operId">操作人ID</param>
  746. /// <returns>异步任务</returns>
  747. public async Task LogNodeOpAsync(Grp_ProcessNode before, Grp_ProcessNode after,string opType, int operId)
  748. {
  749. var chgDetails = GetNodeChgDetails(before, after);
  750. var log = new Grp_ProcessLog
  751. {
  752. NodeId = after?.Id ?? before?.Id,
  753. ProcessId = after?.ProcessId ?? before?.ProcessId,
  754. GroupId = 0, // 通过流程ID关联获取
  755. OpType = opType,
  756. OpDesc = GenerateNodeOpDesc(opType, before, after, chgDetails),
  757. BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  758. AfterData = after != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  759. ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
  760. CreateUserId = operId
  761. };
  762. await _sqlSugar.Insertable(log).ExecuteCommandAsync();
  763. }
  764. /// <summary>
  765. /// 获取流程变更详情
  766. /// </summary>
  767. /// <param name="before">变更前</param>
  768. /// <param name="after">变更后</param>
  769. /// <returns>变更详情</returns>
  770. private List<FieldChgDetail> GetProcessChgDetails(Grp_ProcessOverview before, Grp_ProcessOverview after)
  771. {
  772. var chgDetails = new List<FieldChgDetail>();
  773. if (before == null || after == null) return chgDetails;
  774. var props = typeof(Grp_ProcessOverview).GetProperties(BindingFlags.Public | BindingFlags.Instance)
  775. .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
  776. foreach (var prop in props)
  777. {
  778. var beforeVal = prop.GetValue(before);
  779. var afterVal = prop.GetValue(after);
  780. if (!Equals(beforeVal, afterVal))
  781. {
  782. chgDetails.Add(new FieldChgDetail
  783. {
  784. FieldName = prop.Name,
  785. BeforeValue = FormatVal(beforeVal),
  786. AfterValue = FormatVal(afterVal)
  787. });
  788. }
  789. }
  790. return chgDetails;
  791. }
  792. /// <summary>
  793. /// 获取节点变更详情
  794. /// </summary>
  795. /// <param name="before">变更前</param>
  796. /// <param name="after">变更后</param>
  797. /// <returns>变更详情</returns>
  798. private List<FieldChgDetail> GetNodeChgDetails(Grp_ProcessNode before, Grp_ProcessNode after)
  799. {
  800. var chgDetails = new List<FieldChgDetail>();
  801. if (before == null || after == null) return chgDetails;
  802. var props = typeof(Grp_ProcessNode).GetProperties(BindingFlags.Public | BindingFlags.Instance)
  803. .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
  804. foreach (var prop in props)
  805. {
  806. var beforeVal = prop.GetValue(before);
  807. var afterVal = prop.GetValue(after);
  808. if (!Equals(beforeVal, afterVal))
  809. {
  810. chgDetails.Add(new FieldChgDetail
  811. {
  812. FieldName = prop.Name,
  813. BeforeValue = FormatVal(beforeVal),
  814. AfterValue = FormatVal(afterVal)
  815. });
  816. }
  817. }
  818. return chgDetails;
  819. }
  820. /// <summary>
  821. /// 生成流程操作描述
  822. /// </summary>
  823. /// <param name="opType">操作类型</param>
  824. /// <param name="before">操作前</param>
  825. /// <param name="after">操作后</param>
  826. /// <param name="chgDetails">变更详情</param>
  827. /// <returns>操作描述</returns>
  828. private string GenerateProcessOpDesc(string opType, Grp_ProcessOverview before,
  829. Grp_ProcessOverview after, List<FieldChgDetail> chgDetails)
  830. {
  831. var processType = after?.ProcessType ?? before?.ProcessType;
  832. var processName = GetProcessTypeName(processType);
  833. if (!chgDetails.Any())
  834. {
  835. return opType switch
  836. {
  837. "Create" => $"创建流程:{processName}",
  838. "Update" => $"更新流程:{processName} - 无变更",
  839. //"Start" => $"启动流程:{processName}",
  840. "Complete" => $"完成流程:{processName}",
  841. //"Delete" => $"删除流程:{processName}",
  842. _ => $"{opType}:{processName}"
  843. };
  844. }
  845. var chgDesc = string.Join("; ", chgDetails.Select(x =>
  846. $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
  847. return $"{GetOpTypeDisplay(opType)}:{processName} - {chgDesc}";
  848. }
  849. /// <summary>
  850. /// 获取JSON序列化设置
  851. /// </summary>
  852. /// <returns>JSON设置</returns>
  853. private static JsonSerializerSettings GetJsonSettings()
  854. {
  855. return new JsonSerializerSettings
  856. {
  857. ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
  858. NullValueHandling = NullValueHandling.Ignore,
  859. DateFormatString = "yyyy-MM-dd HH:mm:ss",
  860. Formatting = Formatting.None
  861. };
  862. }
  863. /// <summary>
  864. /// 生成节点操作描述
  865. /// </summary>
  866. /// <param name="opType">操作类型</param>
  867. /// <param name="before">操作前</param>
  868. /// <param name="after">操作后</param>
  869. /// <param name="chgDetails">变更详情</param>
  870. /// <returns>操作描述</returns>
  871. private string GenerateNodeOpDesc(string opType, Grp_ProcessNode before,
  872. Grp_ProcessNode after, List<FieldChgDetail> chgDetails)
  873. {
  874. var nodeName = after?.NodeName ?? before?.NodeName;
  875. if (!chgDetails.Any())
  876. {
  877. return opType switch
  878. {
  879. "Create" => $"创建节点:{nodeName}",
  880. "Update" => $"更新节点:{nodeName} - 无变更",
  881. "Start" => $"启动节点:{nodeName}",
  882. "Complete" => $"完成节点:{nodeName}",
  883. //"Delete" => $"删除节点:{nodeName}",
  884. _ => $"{opType}:{nodeName}"
  885. };
  886. }
  887. var chgDesc = string.Join("; ", chgDetails.Select(x =>
  888. $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
  889. return $"{GetOpTypeDisplay(opType)}:{nodeName} - {chgDesc}";
  890. }
  891. /// <summary>
  892. /// 获取流程类型名称
  893. /// </summary>
  894. /// <param name="processType">流程类型</param>
  895. /// <returns>流程名称</returns>
  896. private static string GetProcessTypeName(GroupProcessType? processType)
  897. {
  898. return processType switch
  899. {
  900. GroupProcessType.Invitation => "商邀报批",
  901. GroupProcessType.Visa => "签证",
  902. GroupProcessType.AirTicket => "机票",
  903. GroupProcessType.Hotel => "酒店",
  904. GroupProcessType.LocalGuide => "地接",
  905. GroupProcessType.FeeSettle => "费用结算",
  906. _ => "未知流程"
  907. };
  908. }
  909. /// <summary>
  910. /// 获取操作类型显示
  911. /// </summary>
  912. /// <param name="opType">操作类型</param>
  913. /// <returns>显示名称</returns>
  914. private static string GetOpTypeDisplay(string opType)
  915. {
  916. return opType switch
  917. {
  918. "Create" => "创建",
  919. "Update" => "更新",
  920. "Start" => "启动",
  921. "Complete" => "完成",
  922. "Delete" => "删除",
  923. "StatusChg" => "状态变更",
  924. _ => opType
  925. };
  926. }
  927. /// <summary>
  928. /// 获取字段显示名称
  929. /// </summary>
  930. /// <param name="fieldName">字段名</param>
  931. /// <returns>显示名称</returns>
  932. private string GetFieldDisplayName(string fieldName)
  933. {
  934. return fieldName switch
  935. {
  936. "OverallStatus" => "状态",
  937. "ProcessOrder" => "流程顺序",
  938. "StartTime" => "开始时间",
  939. "EndTime" => "结束时间",
  940. "NodeOrder" => "节点顺序",
  941. "NodeName" => "节点名称",
  942. "IsCurrent" => "当前节点",
  943. "Operator" => "操作人",
  944. "OperationTime" => "操作时间",
  945. _ => fieldName
  946. };
  947. }
  948. /// <summary>
  949. /// 格式化值显示
  950. /// </summary>
  951. /// <param name="value">值</param>
  952. /// <returns>格式化值</returns>
  953. private string FormatVal(object value)
  954. {
  955. if (value == null) return "空";
  956. if (value is ProcessStatus status)
  957. {
  958. return status switch
  959. {
  960. ProcessStatus.UnStarted => "未开始",
  961. ProcessStatus.InProgress => "进行中",
  962. ProcessStatus.Completed => "已完成",
  963. _ => status.ToString()
  964. };
  965. }
  966. if (value is bool boolVal) return boolVal ? "是" : "否";
  967. if (value is DateTime dateVal) return dateVal.ToString("yyyy-MM-dd HH:mm");
  968. var strVal = value.ToString();
  969. return string.IsNullOrEmpty(strVal) ? "空" : strVal;
  970. }
  971. /// <summary>
  972. /// 检查是否排除字段
  973. /// </summary>
  974. /// <param name="fieldName">字段名</param>
  975. /// <returns>是否排除</returns>
  976. private bool IsExclField(string fieldName)
  977. {
  978. var exclFields = new List<string>
  979. {
  980. "Id", "CreateTime", "CreateUserId", "UpdatedTime", "UpdatedUserId",
  981. "Nodes", "Process" // 导航属性
  982. };
  983. return exclFields.Contains(fieldName);
  984. }
  985. /// <summary>
  986. /// 获取流程日志
  987. /// </summary>
  988. /// <param name="processId">流程ID</param>
  989. /// <returns>日志列表</returns>
  990. public async Task<List<Grp_ProcessLog>> GetProcessLogsAsync(int processId)
  991. {
  992. return await _sqlSugar.Queryable<Grp_ProcessLog>()
  993. .Where(x => x.ProcessId == processId)
  994. .OrderByDescending(x => x.CreateTime)
  995. .ToListAsync();
  996. }
  997. /// <summary>
  998. /// 获取团组流程日志
  999. /// </summary>
  1000. /// <param name="groupId">团组ID</param>
  1001. /// <returns>日志列表</returns>
  1002. public async Task<List<Grp_ProcessLog>> GetGroupLogsAsync(int groupId)
  1003. {
  1004. return await _sqlSugar.Queryable<Grp_ProcessLog>()
  1005. .Where(x => x.GroupId == groupId)
  1006. .OrderByDescending(x => x.CreateTime)
  1007. .ToListAsync();
  1008. }
  1009. #endregion
  1010. #region 节点按钮策略
  1011. // 定义按钮状态计算策略接口
  1012. public interface IButtonStateStrategy
  1013. {
  1014. (bool IsEnaAssistBtn, bool IsEnaFileUpBtn) Calculate(Grp_ProcessOverview process, Grp_ProcessNode node, int index);
  1015. }
  1016. // 通用策略
  1017. public class DefaultButtonStateStrategy : IButtonStateStrategy
  1018. {
  1019. public (bool IsEnaAssistBtn, bool IsEnaFileUpBtn) Calculate(Grp_ProcessOverview process, Grp_ProcessNode node, int index)
  1020. {
  1021. var totalNodes = process.Nodes.Count();
  1022. var isLastNode = index == totalNodes - 1;
  1023. return (false, isLastNode); // 默认只有最后一步启用文件上传
  1024. }
  1025. }
  1026. // 财务流程策略
  1027. public class FeeSettleButtonStateStrategy : IButtonStateStrategy
  1028. {
  1029. public (bool IsEnaAssistBtn, bool IsEnaFileUpBtn) Calculate(Grp_ProcessOverview process, Grp_ProcessNode node, int index)
  1030. {
  1031. var totalNodes = process.Nodes.Count();
  1032. var isLastNode = index == totalNodes - 1;
  1033. bool isEnaAssistBtn = node.NodeOrder == 1; // 首节点启用协助
  1034. bool isEnaFileUpBtn = isLastNode; // 最后一步启用文件上传
  1035. return (isEnaAssistBtn, isEnaFileUpBtn);
  1036. }
  1037. }
  1038. // 机票流程策略
  1039. public class FlightButtonStateStrategy : IButtonStateStrategy
  1040. {
  1041. public (bool IsEnaAssistBtn, bool IsEnaFileUpBtn) Calculate(Grp_ProcessOverview process, Grp_ProcessNode node, int index)
  1042. {
  1043. var totalNodes = process.Nodes.Count();
  1044. var isLastNode = index == totalNodes - 1;
  1045. var isSecondLastNode = index == totalNodes - 2;
  1046. bool isEnaAssistBtn = false;
  1047. bool isEnaFileUpBtn = isLastNode || isSecondLastNode; // 倒数两步都启用文件上传
  1048. return (isEnaAssistBtn, isEnaFileUpBtn);
  1049. }
  1050. }
  1051. // 策略工厂
  1052. public static class ButtonStateStrategyFactory
  1053. {
  1054. private static readonly Dictionary<GroupProcessType, IButtonStateStrategy> _strategies = new()
  1055. {
  1056. [GroupProcessType.Invitation] = new DefaultButtonStateStrategy(),
  1057. [GroupProcessType.Visa] = new DefaultButtonStateStrategy(),
  1058. [GroupProcessType.AirTicket] = new FlightButtonStateStrategy(),
  1059. [GroupProcessType.Hotel] = new DefaultButtonStateStrategy(),
  1060. [GroupProcessType.LocalGuide] = new DefaultButtonStateStrategy(),
  1061. [GroupProcessType.FeeSettle] = new FeeSettleButtonStateStrategy()
  1062. };
  1063. public static IButtonStateStrategy GetStrategy(GroupProcessType processType)
  1064. {
  1065. return _strategies.TryGetValue(processType, out var strategy)
  1066. ? strategy
  1067. : new DefaultButtonStateStrategy();
  1068. }
  1069. }
  1070. #endregion
  1071. }
  1072. }