ProcessOverviewRepository.cs 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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. var isDtNul = DateTime.TryParse(dto.ActualDone,out DateTime 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. if (isDtNul)
  698. {
  699. node.ActualDone = actualDone;
  700. }
  701. node.IsAssist = isAssist;
  702. node.IsFileUp = isFileUp;
  703. node.IsPart = isPart;
  704. // 3. 保存节点更新
  705. await _sqlSugar.Updateable(node)
  706. .UpdateColumns(n => new
  707. {
  708. ActualDone = isDtNul ? node.ActualDone : null,
  709. n.IsAssist,
  710. n.IsFileUp,
  711. n.IsPart,
  712. })
  713. .ExecuteCommandAsync();
  714. //记录节点日志
  715. await LogNodeOpAsync(nodeBefore, node, "Update", currUserId);
  716. return new Result { Code = 200, Msg = "实际操作时间设置成功。" };
  717. }
  718. #region 操作日志
  719. /// <summary>
  720. /// 记录流程操作日志
  721. /// </summary>
  722. /// <param name="before">操作前</param>
  723. /// <param name="after">操作后</param>
  724. /// <param name="opType">操作类型(Create - 创建、Update - 更新、Complete - 完成)</param>
  725. /// <param name="operId">操作人ID</param>
  726. /// <returns>异步任务</returns>
  727. public async Task LogProcessOpAsync(Grp_ProcessOverview before, Grp_ProcessOverview after,string opType, int operId)
  728. {
  729. var chgDetails = GetProcessChgDetails(before, after);
  730. var log = new Grp_ProcessLog
  731. {
  732. ProcessId = after?.Id ?? before?.Id,
  733. GroupId = after?.GroupId ?? before?.GroupId ?? 0,
  734. OpType = opType,
  735. OpDesc = GenerateProcessOpDesc(opType, before, after, chgDetails),
  736. BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  737. AfterData = after != null ? JsonConvert.SerializeObject(after, GetJsonSettings()) : null,
  738. ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
  739. CreateUserId = operId
  740. };
  741. await _sqlSugar.Insertable(log).ExecuteCommandAsync();
  742. }
  743. /// <summary>
  744. /// 记录节点操作日志
  745. /// </summary>
  746. /// <param name="before">操作前</param>
  747. /// <param name="after">操作后</param>
  748. /// <param name="opType">操作类型(Create - 创建、Update - 更新、Start - 启动、Complete - 完成)</param>
  749. /// <param name="operId">操作人ID</param>
  750. /// <returns>异步任务</returns>
  751. public async Task LogNodeOpAsync(Grp_ProcessNode before, Grp_ProcessNode after,string opType, int operId)
  752. {
  753. var chgDetails = GetNodeChgDetails(before, after);
  754. var log = new Grp_ProcessLog
  755. {
  756. NodeId = after?.Id ?? before?.Id,
  757. ProcessId = after?.ProcessId ?? before?.ProcessId,
  758. GroupId = 0, // 通过流程ID关联获取
  759. OpType = opType,
  760. OpDesc = GenerateNodeOpDesc(opType, before, after, chgDetails),
  761. BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  762. AfterData = after != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  763. ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
  764. CreateUserId = operId
  765. };
  766. await _sqlSugar.Insertable(log).ExecuteCommandAsync();
  767. }
  768. /// <summary>
  769. /// 获取流程变更详情
  770. /// </summary>
  771. /// <param name="before">变更前</param>
  772. /// <param name="after">变更后</param>
  773. /// <returns>变更详情</returns>
  774. private List<FieldChgDetail> GetProcessChgDetails(Grp_ProcessOverview before, Grp_ProcessOverview after)
  775. {
  776. var chgDetails = new List<FieldChgDetail>();
  777. if (before == null || after == null) return chgDetails;
  778. var props = typeof(Grp_ProcessOverview).GetProperties(BindingFlags.Public | BindingFlags.Instance)
  779. .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
  780. foreach (var prop in props)
  781. {
  782. var beforeVal = prop.GetValue(before);
  783. var afterVal = prop.GetValue(after);
  784. if (!Equals(beforeVal, afterVal))
  785. {
  786. chgDetails.Add(new FieldChgDetail
  787. {
  788. FieldName = prop.Name,
  789. BeforeValue = FormatVal(beforeVal),
  790. AfterValue = FormatVal(afterVal)
  791. });
  792. }
  793. }
  794. return chgDetails;
  795. }
  796. /// <summary>
  797. /// 获取节点变更详情
  798. /// </summary>
  799. /// <param name="before">变更前</param>
  800. /// <param name="after">变更后</param>
  801. /// <returns>变更详情</returns>
  802. private List<FieldChgDetail> GetNodeChgDetails(Grp_ProcessNode before, Grp_ProcessNode after)
  803. {
  804. var chgDetails = new List<FieldChgDetail>();
  805. if (before == null || after == null) return chgDetails;
  806. var props = typeof(Grp_ProcessNode).GetProperties(BindingFlags.Public | BindingFlags.Instance)
  807. .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
  808. foreach (var prop in props)
  809. {
  810. var beforeVal = prop.GetValue(before);
  811. var afterVal = prop.GetValue(after);
  812. if (!Equals(beforeVal, afterVal))
  813. {
  814. chgDetails.Add(new FieldChgDetail
  815. {
  816. FieldName = prop.Name,
  817. BeforeValue = FormatVal(beforeVal),
  818. AfterValue = FormatVal(afterVal)
  819. });
  820. }
  821. }
  822. return chgDetails;
  823. }
  824. /// <summary>
  825. /// 生成流程操作描述
  826. /// </summary>
  827. /// <param name="opType">操作类型</param>
  828. /// <param name="before">操作前</param>
  829. /// <param name="after">操作后</param>
  830. /// <param name="chgDetails">变更详情</param>
  831. /// <returns>操作描述</returns>
  832. private string GenerateProcessOpDesc(string opType, Grp_ProcessOverview before,
  833. Grp_ProcessOverview after, List<FieldChgDetail> chgDetails)
  834. {
  835. var processType = after?.ProcessType ?? before?.ProcessType;
  836. var processName = GetProcessTypeName(processType);
  837. if (!chgDetails.Any())
  838. {
  839. return opType switch
  840. {
  841. "Create" => $"创建流程:{processName}",
  842. "Update" => $"更新流程:{processName} - 无变更",
  843. //"Start" => $"启动流程:{processName}",
  844. "Complete" => $"完成流程:{processName}",
  845. //"Delete" => $"删除流程:{processName}",
  846. _ => $"{opType}:{processName}"
  847. };
  848. }
  849. var chgDesc = string.Join("; ", chgDetails.Select(x =>
  850. $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
  851. return $"{GetOpTypeDisplay(opType)}:{processName} - {chgDesc}";
  852. }
  853. /// <summary>
  854. /// 获取JSON序列化设置
  855. /// </summary>
  856. /// <returns>JSON设置</returns>
  857. private static JsonSerializerSettings GetJsonSettings()
  858. {
  859. return new JsonSerializerSettings
  860. {
  861. ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
  862. NullValueHandling = NullValueHandling.Ignore,
  863. DateFormatString = "yyyy-MM-dd HH:mm:ss",
  864. Formatting = Formatting.None
  865. };
  866. }
  867. /// <summary>
  868. /// 生成节点操作描述
  869. /// </summary>
  870. /// <param name="opType">操作类型</param>
  871. /// <param name="before">操作前</param>
  872. /// <param name="after">操作后</param>
  873. /// <param name="chgDetails">变更详情</param>
  874. /// <returns>操作描述</returns>
  875. private string GenerateNodeOpDesc(string opType, Grp_ProcessNode before,
  876. Grp_ProcessNode after, List<FieldChgDetail> chgDetails)
  877. {
  878. var nodeName = after?.NodeName ?? before?.NodeName;
  879. if (!chgDetails.Any())
  880. {
  881. return opType switch
  882. {
  883. "Create" => $"创建节点:{nodeName}",
  884. "Update" => $"更新节点:{nodeName} - 无变更",
  885. "Start" => $"启动节点:{nodeName}",
  886. "Complete" => $"完成节点:{nodeName}",
  887. //"Delete" => $"删除节点:{nodeName}",
  888. _ => $"{opType}:{nodeName}"
  889. };
  890. }
  891. var chgDesc = string.Join("; ", chgDetails.Select(x =>
  892. $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
  893. return $"{GetOpTypeDisplay(opType)}:{nodeName} - {chgDesc}";
  894. }
  895. /// <summary>
  896. /// 获取流程类型名称
  897. /// </summary>
  898. /// <param name="processType">流程类型</param>
  899. /// <returns>流程名称</returns>
  900. private static string GetProcessTypeName(GroupProcessType? processType)
  901. {
  902. return processType switch
  903. {
  904. GroupProcessType.Invitation => "商邀报批",
  905. GroupProcessType.Visa => "签证",
  906. GroupProcessType.AirTicket => "机票",
  907. GroupProcessType.Hotel => "酒店",
  908. GroupProcessType.LocalGuide => "地接",
  909. GroupProcessType.FeeSettle => "费用结算",
  910. _ => "未知流程"
  911. };
  912. }
  913. /// <summary>
  914. /// 获取操作类型显示
  915. /// </summary>
  916. /// <param name="opType">操作类型</param>
  917. /// <returns>显示名称</returns>
  918. private static string GetOpTypeDisplay(string opType)
  919. {
  920. return opType switch
  921. {
  922. "Create" => "创建",
  923. "Update" => "更新",
  924. "Start" => "启动",
  925. "Complete" => "完成",
  926. "Delete" => "删除",
  927. "StatusChg" => "状态变更",
  928. _ => opType
  929. };
  930. }
  931. /// <summary>
  932. /// 获取字段显示名称
  933. /// </summary>
  934. /// <param name="fieldName">字段名</param>
  935. /// <returns>显示名称</returns>
  936. private string GetFieldDisplayName(string fieldName)
  937. {
  938. return fieldName switch
  939. {
  940. "OverallStatus" => "状态",
  941. "ProcessOrder" => "流程顺序",
  942. "StartTime" => "开始时间",
  943. "EndTime" => "结束时间",
  944. "NodeOrder" => "节点顺序",
  945. "NodeName" => "节点名称",
  946. "IsCurrent" => "当前节点",
  947. "Operator" => "操作人",
  948. "OperationTime" => "操作时间",
  949. _ => fieldName
  950. };
  951. }
  952. /// <summary>
  953. /// 格式化值显示
  954. /// </summary>
  955. /// <param name="value">值</param>
  956. /// <returns>格式化值</returns>
  957. private string FormatVal(object value)
  958. {
  959. if (value == null) return "空";
  960. if (value is ProcessStatus status)
  961. {
  962. return status switch
  963. {
  964. ProcessStatus.UnStarted => "未开始",
  965. ProcessStatus.InProgress => "进行中",
  966. ProcessStatus.Completed => "已完成",
  967. _ => status.ToString()
  968. };
  969. }
  970. if (value is bool boolVal) return boolVal ? "是" : "否";
  971. if (value is DateTime dateVal) return dateVal.ToString("yyyy-MM-dd HH:mm");
  972. var strVal = value.ToString();
  973. return string.IsNullOrEmpty(strVal) ? "空" : strVal;
  974. }
  975. /// <summary>
  976. /// 检查是否排除字段
  977. /// </summary>
  978. /// <param name="fieldName">字段名</param>
  979. /// <returns>是否排除</returns>
  980. private bool IsExclField(string fieldName)
  981. {
  982. var exclFields = new List<string>
  983. {
  984. "Id", "CreateTime", "CreateUserId", "UpdatedTime", "UpdatedUserId",
  985. "Nodes", "Process" // 导航属性
  986. };
  987. return exclFields.Contains(fieldName);
  988. }
  989. /// <summary>
  990. /// 获取流程日志
  991. /// </summary>
  992. /// <param name="processId">流程ID</param>
  993. /// <returns>日志列表</returns>
  994. public async Task<List<Grp_ProcessLog>> GetProcessLogsAsync(int processId)
  995. {
  996. return await _sqlSugar.Queryable<Grp_ProcessLog>()
  997. .Where(x => x.ProcessId == processId)
  998. .OrderByDescending(x => x.CreateTime)
  999. .ToListAsync();
  1000. }
  1001. /// <summary>
  1002. /// 获取团组流程日志
  1003. /// </summary>
  1004. /// <param name="groupId">团组ID</param>
  1005. /// <returns>日志列表</returns>
  1006. public async Task<List<Grp_ProcessLog>> GetGroupLogsAsync(int groupId)
  1007. {
  1008. return await _sqlSugar.Queryable<Grp_ProcessLog>()
  1009. .Where(x => x.GroupId == groupId)
  1010. .OrderByDescending(x => x.CreateTime)
  1011. .ToListAsync();
  1012. }
  1013. #endregion
  1014. #region 节点按钮策略
  1015. // 定义按钮状态计算策略接口
  1016. public interface IButtonStateStrategy
  1017. {
  1018. (bool IsEnaAssistBtn, bool IsEnaFileUpBtn) Calculate(Grp_ProcessOverview process, Grp_ProcessNode node, int index);
  1019. }
  1020. // 通用策略
  1021. public class DefaultButtonStateStrategy : IButtonStateStrategy
  1022. {
  1023. public (bool IsEnaAssistBtn, bool IsEnaFileUpBtn) Calculate(Grp_ProcessOverview process, Grp_ProcessNode node, int index)
  1024. {
  1025. var totalNodes = process.Nodes.Count();
  1026. var isLastNode = index == totalNodes - 1;
  1027. return (false, isLastNode); // 默认只有最后一步启用文件上传
  1028. }
  1029. }
  1030. // 财务流程策略
  1031. public class FeeSettleButtonStateStrategy : IButtonStateStrategy
  1032. {
  1033. public (bool IsEnaAssistBtn, bool IsEnaFileUpBtn) Calculate(Grp_ProcessOverview process, Grp_ProcessNode node, int index)
  1034. {
  1035. var totalNodes = process.Nodes.Count();
  1036. var isLastNode = index == totalNodes - 1;
  1037. bool isEnaAssistBtn = node.NodeOrder == 1; // 首节点启用协助
  1038. bool isEnaFileUpBtn = isLastNode; // 最后一步启用文件上传
  1039. return (isEnaAssistBtn, isEnaFileUpBtn);
  1040. }
  1041. }
  1042. // 机票流程策略
  1043. public class FlightButtonStateStrategy : IButtonStateStrategy
  1044. {
  1045. public (bool IsEnaAssistBtn, bool IsEnaFileUpBtn) Calculate(Grp_ProcessOverview process, Grp_ProcessNode node, int index)
  1046. {
  1047. var totalNodes = process.Nodes.Count();
  1048. var isLastNode = index == totalNodes - 1;
  1049. var isSecondLastNode = index == totalNodes - 2;
  1050. bool isEnaAssistBtn = false;
  1051. bool isEnaFileUpBtn = isLastNode || isSecondLastNode; // 倒数两步都启用文件上传
  1052. return (isEnaAssistBtn, isEnaFileUpBtn);
  1053. }
  1054. }
  1055. // 策略工厂
  1056. public static class ButtonStateStrategyFactory
  1057. {
  1058. private static readonly Dictionary<GroupProcessType, IButtonStateStrategy> _strategies = new()
  1059. {
  1060. [GroupProcessType.Invitation] = new DefaultButtonStateStrategy(),
  1061. [GroupProcessType.Visa] = new DefaultButtonStateStrategy(),
  1062. [GroupProcessType.AirTicket] = new FlightButtonStateStrategy(),
  1063. [GroupProcessType.Hotel] = new DefaultButtonStateStrategy(),
  1064. [GroupProcessType.LocalGuide] = new DefaultButtonStateStrategy(),
  1065. [GroupProcessType.FeeSettle] = new FeeSettleButtonStateStrategy()
  1066. };
  1067. public static IButtonStateStrategy GetStrategy(GroupProcessType processType)
  1068. {
  1069. return _strategies.TryGetValue(processType, out var strategy)
  1070. ? strategy
  1071. : new DefaultButtonStateStrategy();
  1072. }
  1073. }
  1074. #endregion
  1075. }
  1076. }