ProcessOverviewRepository.cs 46 KB

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