ProcessOverviewRepository.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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 System.Reflection;
  7. namespace OASystem.Infrastructure.Repositories.Groups
  8. {
  9. /// <summary>
  10. /// 团组流程总览表仓储
  11. /// </summary>
  12. public class ProcessOverviewRepository : BaseRepository<Grp_ProcessOverview, Grp_ProcessOverview>
  13. {
  14. private readonly IMapper _mapper;
  15. private readonly DelegationInfoRepository _groupRep;
  16. public ProcessOverviewRepository(SqlSugarClient sqlSugar, IMapper mapper, DelegationInfoRepository groupRep) : base(sqlSugar)
  17. {
  18. _mapper = mapper;
  19. _groupRep = groupRep;
  20. }
  21. /// <summary>
  22. /// 团组流程初始化
  23. /// </summary>
  24. /// <param name="request">创建流程请求参数</param>
  25. /// <returns>创建的流程信息</returns>
  26. public async Task<Result> ProcessInitAsync(int groupId, int currUserId)
  27. {
  28. //团组验证
  29. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
  30. if (groupInfo == null)
  31. {
  32. return new Result { Code = 400, Msg = "团组不存在" };
  33. }
  34. // 检查是否已存在流程
  35. var existingProcesses = await _sqlSugar.Queryable<Grp_ProcessOverview>().Where(p => p.GroupId == groupId).ToListAsync();
  36. if (existingProcesses.Any())
  37. {
  38. return new Result { Code = 400, Msg = "该团组的流程已存在" };
  39. }
  40. //处理签证国家
  41. var visaCountries = _groupRep.GroupSplitCountry(groupInfo.VisitCountry);
  42. // 定义默认的流程节点
  43. var processs = Grp_ProcessOverview.ProcessInit(groupId, currUserId, visaCountries);
  44. _sqlSugar.BeginTran();
  45. foreach (var item in processs)
  46. {
  47. var processId = await _sqlSugar.Insertable(item).ExecuteReturnIdentityAsync();
  48. if (processId < 1)
  49. {
  50. _sqlSugar.RollbackTran();
  51. return new Result { Code = 400, Msg = "团组流程进度总览表添加失败!" };
  52. }
  53. // 记录流程日志
  54. await LogProcessOpAsync(null, item, "Create", currUserId);
  55. var nodes = item.Nodes.Select((nodeDto, index) => new Grp_ProcessNode
  56. {
  57. ProcessId = processId,
  58. NodeName = nodeDto.NodeName,
  59. NodeOrder = nodeDto.NodeOrder,
  60. OverallStatus = nodeDto.OverallStatus,
  61. //Country = nodeDto.Country,
  62. IsCurrent = nodeDto.IsCurrent,
  63. Remark = nodeDto.Remark
  64. }).ToList();
  65. var nodeIds = await _sqlSugar.Insertable(nodes).ExecuteCommandAsync();
  66. if (nodeIds < 1)
  67. {
  68. _sqlSugar.RollbackTran();
  69. return new Result { Code = 400, Msg = "团组流程进度流程节点添加失败!" };
  70. }
  71. //记录节点日志
  72. foreach (var node in nodes)
  73. {
  74. await LogNodeOpAsync(null, node, "Create", currUserId);
  75. }
  76. }
  77. _sqlSugar.CommitTran();
  78. return new Result { Code = 200, Msg = "添加成功!" }; ;
  79. }
  80. /// <summary>
  81. /// 获取团组的所有流程及流程详情
  82. /// </summary>
  83. /// <param name="request">创建流程请求参数</param>
  84. /// <returns>创建的流程信息</returns>
  85. public async Task<Result> ProcessesDetailsAsync(int groupId)
  86. {
  87. //团组验证
  88. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
  89. if (groupInfo == null)
  90. {
  91. return new Result { Code = 400, Msg = "团组不存在" };
  92. }
  93. // 检查是否已存在流程
  94. var existingProcesses = await _sqlSugar.Queryable<Grp_ProcessOverview>().Where(p => p.GroupId == groupId).ToListAsync();
  95. if (!existingProcesses.Any())
  96. {
  97. return new Result { Code = 400, Msg = "该团组的流程不存在" };
  98. }
  99. var users = await _sqlSugar.Queryable<Sys_Users>().ToListAsync();
  100. var processData = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  101. .Where(p => p.GroupId == groupId && p.IsDel == 0)
  102. .Mapper(p => p.Nodes, p => p.Nodes.First().ProcessId)
  103. .ToListAsync();
  104. var processes = processData.Select(p => new
  105. {
  106. p.Id,
  107. p.GroupId,
  108. p.ProcessType,
  109. ProcessName = p.ProcessType.GetEnumDescription(),
  110. //p.OverallStatus,
  111. //StatusText = p.OverallStatus.GetDescription(),
  112. Nodes = p.Nodes.Select(n =>
  113. {
  114. //单独处理签证板块
  115. var visaSubNodes = new List<VisaProcessNode>();
  116. string remark = string.Empty;
  117. if (p.ProcessType == GroupProcessType.Visa)
  118. {
  119. visaSubNodes = JsonConvert.DeserializeObject<List<VisaProcessNode>>(n.Remark);
  120. }
  121. return new
  122. {
  123. n.Id,
  124. n.ProcessId,
  125. n.NodeOrder,
  126. n.NodeName,
  127. n.OverallStatus,
  128. StatusText = n.OverallStatus.GetEnumDescription(),
  129. Operator = users.FirstOrDefault(u => u.Id == n.Operator)?.CnName ?? "-",
  130. OpeateTime = n.OperationTime.HasValue ? n.OperationTime.Value.ToString("yyyy-MM-dd HH:mm:ss") : "-",
  131. //节点类型为签证时使用
  132. visaSubNodes
  133. };
  134. }).OrderBy(n => n.NodeOrder).ToList()
  135. }).ToList();
  136. return new Result { Code = 200, Data = processes, Msg = "查询成功!" };
  137. }
  138. /// <summary>
  139. /// 更新节点状态
  140. /// </summary>
  141. /// <param name="nodeId">节点ID</param>
  142. /// <param name="currUserId">当前用户ID</param>
  143. /// <param name="processStatus">流程状态,默认为已完成</param>
  144. /// <returns>操作结果</returns>
  145. public async Task<Result> UpdateNodeStatusAsync(int nodeId, int currUserId, ProcessStatus processStatus = ProcessStatus.Completed)
  146. {
  147. try
  148. {
  149. // 使用事务确保数据一致性
  150. var result = await _sqlSugar.Ado.UseTranAsync(async () =>
  151. {
  152. // 1. 获取并验证节点
  153. var node = await _sqlSugar.Queryable<Grp_ProcessNode>()
  154. .FirstAsync(n => n.Id == nodeId && n.IsDel == 0);
  155. if (node == null)
  156. {
  157. throw new BusinessException("当前节点不存在或已被删除。");
  158. }
  159. // 新增验证:当前节点已完成,不可操作
  160. ValidateNodeOperation(node, processStatus);
  161. //存储更新前的值
  162. var before = new Grp_ProcessNode() {
  163. Id = node.Id,
  164. ProcessId = node.ProcessId,
  165. NodeName = node.NodeName,
  166. NodeOrder = node.NodeOrder,
  167. OverallStatus = node.OverallStatus,
  168. Operator = node.Operator,
  169. OperationTime = node.OperationTime,
  170. IsCurrent = node.IsCurrent,
  171. };
  172. // 2. 更新节点状态
  173. node.OverallStatus = processStatus;
  174. node.Operator = currUserId;
  175. node.OperationTime = DateTime.Now;
  176. var updateCount = await _sqlSugar.Updateable(node)
  177. .UpdateColumns(n => new
  178. {
  179. n.OverallStatus,
  180. n.Operator,
  181. n.OperationTime
  182. })
  183. .ExecuteCommandAsync();
  184. if (updateCount == 0)
  185. {
  186. throw new BusinessException("节点状态更新失败。");
  187. }
  188. //记录节点日志
  189. await LogNodeOpAsync(before, node, "Update", currUserId);
  190. // 3. 如果是完成当前节点,处理流程流转
  191. if (processStatus == ProcessStatus.Completed && node.IsCurrent)
  192. {
  193. await ProcessCurrentNodeCompletionAsync(node, currUserId);
  194. }
  195. return new Result { Code = StatusCodes.Status200OK, Msg = "操作成功。" };
  196. });
  197. return result.IsSuccess ? result.Data : new Result
  198. {
  199. Code = StatusCodes.Status500InternalServerError,
  200. Msg = result.ErrorMessage
  201. };
  202. }
  203. catch (BusinessException ex)
  204. {
  205. // 业务异常
  206. return new Result { Code = StatusCodes.Status400BadRequest, Msg = ex.Message };
  207. }
  208. catch (Exception ex)
  209. {
  210. // 系统异常
  211. return new Result { Code = StatusCodes.Status500InternalServerError, Msg = "系统错误,请稍后重试" };
  212. }
  213. }
  214. /// <summary>
  215. /// 验证节点操作权限
  216. /// </summary>
  217. /// <param name="node">流程节点</param>
  218. /// <param name="targetStatus">目标状态</param>
  219. private static void ValidateNodeOperation(Grp_ProcessNode node, ProcessStatus targetStatus)
  220. {
  221. // 验证节点是否已完成
  222. if (node.OverallStatus == ProcessStatus.Completed)
  223. {
  224. throw new BusinessException("当前节点已完成,不可重复操作。");
  225. }
  226. // 验证状态流转是否合法(可选)
  227. if (targetStatus != ProcessStatus.Completed)
  228. {
  229. throw new BusinessException("未开始或者进行中的节点只能重新完成,不可进行其他操作。");
  230. }
  231. // 验证是否尝试将已完成节点改为其他状态
  232. if (node.OverallStatus == ProcessStatus.Completed && targetStatus != ProcessStatus.Completed)
  233. {
  234. throw new BusinessException("已完成节点不可修改状态。");
  235. }
  236. }
  237. /// <summary>
  238. /// 处理当前节点完成后的流程流转
  239. /// </summary>
  240. private async Task ProcessCurrentNodeCompletionAsync(Grp_ProcessNode currentNode, int currUserId)
  241. {
  242. // 1. 获取流程信息
  243. var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  244. .FirstAsync(p => p.Id == currentNode.ProcessId && p.IsDel == 0);
  245. if (process == null)
  246. {
  247. throw new BusinessException("关联的流程不存在。");
  248. }
  249. var processBefore = new Grp_ProcessOverview()
  250. {
  251. Id = process.Id,
  252. GroupId = process.GroupId,
  253. ProcessOrder = process.ProcessOrder,
  254. ProcessType = process.ProcessType,
  255. OverallStatus = process.OverallStatus,
  256. StartTime = process.StartTime,
  257. EndTime = process.EndTime,
  258. UpdatedUserId = process.UpdatedUserId,
  259. UpdatedTime = process.UpdatedTime
  260. };
  261. // 2. 取消当前节点的当前状态
  262. var before = new Grp_ProcessNode()
  263. {
  264. Id = currentNode.Id,
  265. ProcessId = currentNode.ProcessId,
  266. NodeName = currentNode.NodeName,
  267. NodeOrder = currentNode.NodeOrder,
  268. OverallStatus = currentNode.OverallStatus,
  269. Operator = currentNode.Operator,
  270. OperationTime = currentNode.OperationTime,
  271. IsCurrent = currentNode.IsCurrent,
  272. };
  273. currentNode.IsCurrent = false;
  274. await _sqlSugar.Updateable(currentNode)
  275. .UpdateColumns(n => new { n.IsCurrent })
  276. .ExecuteCommandAsync();
  277. // 2.1 记录节点日志 取消当前节点状态
  278. await LogNodeOpAsync(before, currentNode, "Update", currUserId);
  279. // 3. 查找并激活下一个节点
  280. var nextNode = await _sqlSugar.Queryable<Grp_ProcessNode>()
  281. .Where(n => n.ProcessId == currentNode.ProcessId
  282. && n.NodeOrder == currentNode.NodeOrder + 1
  283. && n.IsDel == 0)
  284. .FirstAsync();
  285. if (nextNode != null)
  286. {
  287. var nextNodeBefore = new Grp_ProcessNode()
  288. {
  289. Id = nextNode.Id,
  290. ProcessId = nextNode.ProcessId,
  291. NodeName = nextNode.NodeName,
  292. NodeOrder = nextNode.NodeOrder,
  293. OverallStatus = nextNode.OverallStatus,
  294. Operator = nextNode.Operator,
  295. OperationTime = nextNode.OperationTime,
  296. IsCurrent = nextNode.IsCurrent,
  297. };
  298. // 激活下一个节点
  299. nextNode.IsCurrent = true;
  300. nextNode.OverallStatus = ProcessStatus.InProgress;
  301. //nextNode.Operator = currUserId;
  302. //nextNode.OperationTime = DateTime.Now;
  303. var updateCount = await _sqlSugar.Updateable(nextNode)
  304. .UpdateColumns(n => new
  305. {
  306. n.IsCurrent,
  307. n.OverallStatus,
  308. n.Operator,
  309. n.OperationTime
  310. })
  311. .ExecuteCommandAsync();
  312. if (updateCount == 0)
  313. {
  314. throw new BusinessException("激活下一节点失败");
  315. }
  316. // 1.1 记录节点日志 激活下一节点当前节点状态
  317. await LogNodeOpAsync(nextNodeBefore, nextNode, "Start", currUserId);
  318. // 更新流程状态为进行中
  319. process.OverallStatus = ProcessStatus.InProgress;
  320. }
  321. else
  322. {
  323. // 下一节点不存在,整个流程完成
  324. process.OverallStatus = ProcessStatus.Completed;
  325. process.EndTime = DateTime.Now;
  326. }
  327. // 4. 更新流程信息
  328. process.UpdatedUserId = currUserId;
  329. process.UpdatedTime = DateTime.Now;
  330. var processUpdateCount = await _sqlSugar.Updateable(process)
  331. .UpdateColumns(p => new
  332. {
  333. p.OverallStatus,
  334. p.EndTime,
  335. p.UpdatedUserId,
  336. p.UpdatedTime
  337. })
  338. .ExecuteCommandAsync();
  339. if (processUpdateCount == 0)
  340. {
  341. throw new BusinessException("流程状态更新失败。");
  342. }
  343. //记录流程日志
  344. await LogProcessOpAsync(processBefore, process, "Update", currUserId);
  345. }
  346. /// <summary>
  347. /// 更新签证节点信息及状态
  348. /// </summary>
  349. /// <param name="dto">签证节点更新数据传输对象</param>
  350. /// <returns>操作结果</returns>
  351. public async Task<Result> UpdateVisaNodeDetailsAsync(GroupProcessUpdateVisaNodeDetailsDto dto)
  352. {
  353. // 1. 获取并验证节点和流程
  354. var node = await _sqlSugar.Queryable<Grp_ProcessNode>()
  355. .FirstAsync(n => n.Id == dto.NodeId && n.IsDel == 0)
  356. ?? throw new BusinessException("当前节点不存在或已被删除。");
  357. var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  358. .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0)
  359. ?? throw new BusinessException("当前流程不存在或已被删除。");
  360. if (process.ProcessType != GroupProcessType.Visa)
  361. {
  362. throw new BusinessException("当前流程节点不为签证流程,不可编辑。");
  363. }
  364. // 2. 检查签证子节点 字段信息是否全部填写
  365. var allSubNodesCompleted = dto.VisaSubNodes?.All(subNode => EntityExtensions.IsCompleted(subNode)) ?? false;
  366. // 2.1 存储更新前流程及节点信息
  367. var nodeBefore = new Grp_ProcessNode()
  368. {
  369. Id = node.Id,
  370. ProcessId = node.ProcessId,
  371. NodeName = node.NodeName,
  372. NodeOrder = node.NodeOrder,
  373. OverallStatus = node.OverallStatus,
  374. Operator = node.Operator,
  375. OperationTime = node.OperationTime,
  376. IsCurrent = node.IsCurrent,
  377. };
  378. var processBefore = new Grp_ProcessOverview()
  379. {
  380. Id = process.Id,
  381. GroupId = process.GroupId,
  382. ProcessOrder = process.ProcessOrder,
  383. ProcessType = process.ProcessType,
  384. OverallStatus = process.OverallStatus,
  385. StartTime = process.StartTime,
  386. EndTime = process.EndTime,
  387. UpdatedUserId = process.UpdatedUserId,
  388. UpdatedTime = process.UpdatedTime
  389. };
  390. // 3. 更新节点信息
  391. node.Remark = JsonConvert.SerializeObject(dto.VisaSubNodes);
  392. node.Operator = dto.CurrUserId;
  393. node.OperationTime = DateTime.Now;
  394. if (allSubNodesCompleted)
  395. {
  396. node.OverallStatus = ProcessStatus.Completed;
  397. process.OverallStatus = ProcessStatus.Completed;
  398. process.EndTime = DateTime.Now;
  399. process.UpdatedUserId = dto.CurrUserId;
  400. process.UpdatedTime = DateTime.Now;
  401. // 更新流程状态
  402. await _sqlSugar.Updateable(process)
  403. .UpdateColumns(p => new
  404. {
  405. p.OverallStatus,
  406. p.EndTime,
  407. p.UpdatedUserId,
  408. p.UpdatedTime
  409. })
  410. .ExecuteCommandAsync();
  411. //记录流程日志
  412. await LogProcessOpAsync(processBefore, process, "Update", dto.CurrUserId);
  413. }
  414. // 4. 保存节点更新
  415. await _sqlSugar.Updateable(node)
  416. .UpdateColumns(n => new
  417. {
  418. n.Remark,
  419. n.Operator,
  420. n.OperationTime,
  421. n.OverallStatus
  422. })
  423. .ExecuteCommandAsync();
  424. //记录节点日志
  425. await LogNodeOpAsync(nodeBefore, node, "Update", dto.CurrUserId);
  426. return new Result { Code = 200, Msg = "节点信息更新成功。" };
  427. }
  428. #region 操作日志
  429. /// <summary>
  430. /// 记录流程操作日志
  431. /// </summary>
  432. /// <param name="before">操作前</param>
  433. /// <param name="after">操作后</param>
  434. /// <param name="opType">操作类型(Create - 创建、Update - 更新、Complete - 完成)</param>
  435. /// <param name="operId">操作人ID</param>
  436. /// <returns>异步任务</returns>
  437. public async Task LogProcessOpAsync(Grp_ProcessOverview before, Grp_ProcessOverview after,string opType, int operId)
  438. {
  439. var chgDetails = GetProcessChgDetails(before, after);
  440. var log = new Grp_ProcessLog
  441. {
  442. ProcessId = after?.Id ?? before?.Id,
  443. GroupId = after?.GroupId ?? before?.GroupId ?? 0,
  444. OpType = opType,
  445. OpDesc = GenerateProcessOpDesc(opType, before, after, chgDetails),
  446. BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  447. AfterData = after != null ? JsonConvert.SerializeObject(after, GetJsonSettings()) : null,
  448. ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
  449. CreateUserId = operId
  450. };
  451. await _sqlSugar.Insertable(log).ExecuteCommandAsync();
  452. }
  453. /// <summary>
  454. /// 记录节点操作日志
  455. /// </summary>
  456. /// <param name="before">操作前</param>
  457. /// <param name="after">操作后</param>
  458. /// <param name="opType">操作类型(Create - 创建、Update - 更新、Start - 启动、Complete - 完成)</param>
  459. /// <param name="operId">操作人ID</param>
  460. /// <returns>异步任务</returns>
  461. public async Task LogNodeOpAsync(Grp_ProcessNode before, Grp_ProcessNode after,string opType, int operId)
  462. {
  463. var chgDetails = GetNodeChgDetails(before, after);
  464. var log = new Grp_ProcessLog
  465. {
  466. NodeId = after?.Id ?? before?.Id,
  467. ProcessId = after?.ProcessId ?? before?.ProcessId,
  468. GroupId = 0, // 通过流程ID关联获取
  469. OpType = opType,
  470. OpDesc = GenerateNodeOpDesc(opType, before, after, chgDetails),
  471. BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  472. AfterData = after != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  473. ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
  474. CreateUserId = operId
  475. };
  476. await _sqlSugar.Insertable(log).ExecuteCommandAsync();
  477. }
  478. /// <summary>
  479. /// 获取流程变更详情
  480. /// </summary>
  481. /// <param name="before">变更前</param>
  482. /// <param name="after">变更后</param>
  483. /// <returns>变更详情</returns>
  484. private List<FieldChgDetail> GetProcessChgDetails(Grp_ProcessOverview before, Grp_ProcessOverview after)
  485. {
  486. var chgDetails = new List<FieldChgDetail>();
  487. if (before == null || after == null) return chgDetails;
  488. var props = typeof(Grp_ProcessOverview).GetProperties(BindingFlags.Public | BindingFlags.Instance)
  489. .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
  490. foreach (var prop in props)
  491. {
  492. var beforeVal = prop.GetValue(before);
  493. var afterVal = prop.GetValue(after);
  494. if (!Equals(beforeVal, afterVal))
  495. {
  496. chgDetails.Add(new FieldChgDetail
  497. {
  498. FieldName = prop.Name,
  499. BeforeValue = FormatVal(beforeVal),
  500. AfterValue = FormatVal(afterVal)
  501. });
  502. }
  503. }
  504. return chgDetails;
  505. }
  506. /// <summary>
  507. /// 获取节点变更详情
  508. /// </summary>
  509. /// <param name="before">变更前</param>
  510. /// <param name="after">变更后</param>
  511. /// <returns>变更详情</returns>
  512. private List<FieldChgDetail> GetNodeChgDetails(Grp_ProcessNode before, Grp_ProcessNode after)
  513. {
  514. var chgDetails = new List<FieldChgDetail>();
  515. if (before == null || after == null) return chgDetails;
  516. var props = typeof(Grp_ProcessNode).GetProperties(BindingFlags.Public | BindingFlags.Instance)
  517. .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
  518. foreach (var prop in props)
  519. {
  520. var beforeVal = prop.GetValue(before);
  521. var afterVal = prop.GetValue(after);
  522. if (!Equals(beforeVal, afterVal))
  523. {
  524. chgDetails.Add(new FieldChgDetail
  525. {
  526. FieldName = prop.Name,
  527. BeforeValue = FormatVal(beforeVal),
  528. AfterValue = FormatVal(afterVal)
  529. });
  530. }
  531. }
  532. return chgDetails;
  533. }
  534. /// <summary>
  535. /// 生成流程操作描述
  536. /// </summary>
  537. /// <param name="opType">操作类型</param>
  538. /// <param name="before">操作前</param>
  539. /// <param name="after">操作后</param>
  540. /// <param name="chgDetails">变更详情</param>
  541. /// <returns>操作描述</returns>
  542. private string GenerateProcessOpDesc(string opType, Grp_ProcessOverview before,
  543. Grp_ProcessOverview after, List<FieldChgDetail> chgDetails)
  544. {
  545. var processType = after?.ProcessType ?? before?.ProcessType;
  546. var processName = GetProcessTypeName(processType);
  547. if (!chgDetails.Any())
  548. {
  549. return opType switch
  550. {
  551. "Create" => $"创建流程:{processName}",
  552. "Update" => $"更新流程:{processName} - 无变更",
  553. //"Start" => $"启动流程:{processName}",
  554. "Complete" => $"完成流程:{processName}",
  555. //"Delete" => $"删除流程:{processName}",
  556. _ => $"{opType}:{processName}"
  557. };
  558. }
  559. var chgDesc = string.Join("; ", chgDetails.Select(x =>
  560. $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
  561. return $"{GetOpTypeDisplay(opType)}:{processName} - {chgDesc}";
  562. }
  563. /// <summary>
  564. /// 获取JSON序列化设置
  565. /// </summary>
  566. /// <returns>JSON设置</returns>
  567. private static JsonSerializerSettings GetJsonSettings()
  568. {
  569. return new JsonSerializerSettings
  570. {
  571. ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
  572. NullValueHandling = NullValueHandling.Ignore,
  573. DateFormatString = "yyyy-MM-dd HH:mm:ss",
  574. Formatting = Formatting.None
  575. };
  576. }
  577. /// <summary>
  578. /// 生成节点操作描述
  579. /// </summary>
  580. /// <param name="opType">操作类型</param>
  581. /// <param name="before">操作前</param>
  582. /// <param name="after">操作后</param>
  583. /// <param name="chgDetails">变更详情</param>
  584. /// <returns>操作描述</returns>
  585. private string GenerateNodeOpDesc(string opType, Grp_ProcessNode before,
  586. Grp_ProcessNode after, List<FieldChgDetail> chgDetails)
  587. {
  588. var nodeName = after?.NodeName ?? before?.NodeName;
  589. if (!chgDetails.Any())
  590. {
  591. return opType switch
  592. {
  593. "Create" => $"创建节点:{nodeName}",
  594. "Update" => $"更新节点:{nodeName} - 无变更",
  595. "Start" => $"启动节点:{nodeName}",
  596. "Complete" => $"完成节点:{nodeName}",
  597. //"Delete" => $"删除节点:{nodeName}",
  598. _ => $"{opType}:{nodeName}"
  599. };
  600. }
  601. var chgDesc = string.Join("; ", chgDetails.Select(x =>
  602. $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
  603. return $"{GetOpTypeDisplay(opType)}:{nodeName} - {chgDesc}";
  604. }
  605. /// <summary>
  606. /// 获取流程类型名称
  607. /// </summary>
  608. /// <param name="processType">流程类型</param>
  609. /// <returns>流程名称</returns>
  610. private static string GetProcessTypeName(GroupProcessType? processType)
  611. {
  612. return processType switch
  613. {
  614. GroupProcessType.Invitation => "商邀报批",
  615. GroupProcessType.Visa => "签证",
  616. GroupProcessType.AirTicket => "机票",
  617. GroupProcessType.Hotel => "酒店",
  618. GroupProcessType.LocalGuide => "地接",
  619. GroupProcessType.FeeSettle => "费用结算",
  620. _ => "未知流程"
  621. };
  622. }
  623. /// <summary>
  624. /// 获取操作类型显示
  625. /// </summary>
  626. /// <param name="opType">操作类型</param>
  627. /// <returns>显示名称</returns>
  628. private static string GetOpTypeDisplay(string opType)
  629. {
  630. return opType switch
  631. {
  632. "Create" => "创建",
  633. "Update" => "更新",
  634. "Start" => "启动",
  635. "Complete" => "完成",
  636. "Delete" => "删除",
  637. "StatusChg" => "状态变更",
  638. _ => opType
  639. };
  640. }
  641. /// <summary>
  642. /// 获取字段显示名称
  643. /// </summary>
  644. /// <param name="fieldName">字段名</param>
  645. /// <returns>显示名称</returns>
  646. private string GetFieldDisplayName(string fieldName)
  647. {
  648. return fieldName switch
  649. {
  650. "OverallStatus" => "状态",
  651. "ProcessOrder" => "流程顺序",
  652. "StartTime" => "开始时间",
  653. "EndTime" => "结束时间",
  654. "NodeOrder" => "节点顺序",
  655. "NodeName" => "节点名称",
  656. "IsCurrent" => "当前节点",
  657. "Operator" => "操作人",
  658. "OperationTime" => "操作时间",
  659. _ => fieldName
  660. };
  661. }
  662. /// <summary>
  663. /// 格式化值显示
  664. /// </summary>
  665. /// <param name="value">值</param>
  666. /// <returns>格式化值</returns>
  667. private string FormatVal(object value)
  668. {
  669. if (value == null) return "空";
  670. if (value is ProcessStatus status)
  671. {
  672. return status switch
  673. {
  674. ProcessStatus.UnStarted => "未开始",
  675. ProcessStatus.InProgress => "进行中",
  676. ProcessStatus.Completed => "已完成",
  677. _ => status.ToString()
  678. };
  679. }
  680. if (value is bool boolVal) return boolVal ? "是" : "否";
  681. if (value is DateTime dateVal) return dateVal.ToString("yyyy-MM-dd HH:mm");
  682. var strVal = value.ToString();
  683. return string.IsNullOrEmpty(strVal) ? "空" : strVal;
  684. }
  685. /// <summary>
  686. /// 检查是否排除字段
  687. /// </summary>
  688. /// <param name="fieldName">字段名</param>
  689. /// <returns>是否排除</returns>
  690. private bool IsExclField(string fieldName)
  691. {
  692. var exclFields = new List<string>
  693. {
  694. "Id", "CreateTime", "CreateUserId", "UpdatedTime", "UpdatedUserId",
  695. "Nodes", "Process" // 导航属性
  696. };
  697. return exclFields.Contains(fieldName);
  698. }
  699. /// <summary>
  700. /// 获取流程日志
  701. /// </summary>
  702. /// <param name="processId">流程ID</param>
  703. /// <returns>日志列表</returns>
  704. public async Task<List<Grp_ProcessLog>> GetProcessLogsAsync(int processId)
  705. {
  706. return await _sqlSugar.Queryable<Grp_ProcessLog>()
  707. .Where(x => x.ProcessId == processId)
  708. .OrderByDescending(x => x.CreateTime)
  709. .ToListAsync();
  710. }
  711. /// <summary>
  712. /// 获取团组流程日志
  713. /// </summary>
  714. /// <param name="groupId">团组ID</param>
  715. /// <returns>日志列表</returns>
  716. public async Task<List<Grp_ProcessLog>> GetGroupLogsAsync(int groupId)
  717. {
  718. return await _sqlSugar.Queryable<Grp_ProcessLog>()
  719. .Where(x => x.GroupId == groupId)
  720. .OrderByDescending(x => x.CreateTime)
  721. .ToListAsync();
  722. }
  723. #endregion
  724. }
  725. }