ProcessOverviewRepository.cs 54 KB

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