ProcessOverviewRepository.cs 55 KB

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