ProcessOverviewRepository.cs 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038
  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 OASystem.Domain.ViewModels.Groups;
  8. using Org.BouncyCastle.Asn1.X500;
  9. using System.Reflection;
  10. using System.Runtime.Intrinsics.Arm;
  11. using System.Text.RegularExpressions;
  12. namespace OASystem.Infrastructure.Repositories.Groups
  13. {
  14. /// <summary>
  15. /// 团组流程总览表仓储
  16. /// </summary>
  17. public class ProcessOverviewRepository : BaseRepository<Grp_ProcessOverview, Grp_ProcessOverview>
  18. {
  19. private readonly IMapper _mapper;
  20. private readonly DelegationInfoRepository _groupRep;
  21. public ProcessOverviewRepository(SqlSugarClient sqlSugar, IMapper mapper, DelegationInfoRepository groupRep) : base(sqlSugar)
  22. {
  23. _mapper = mapper;
  24. _groupRep = groupRep;
  25. }
  26. #region 团组流程
  27. /// <summary>
  28. /// 基础数据初始化-团组流程
  29. /// </summary>
  30. /// <param name="groupId"></param>
  31. /// <param name="currUserId"></param>
  32. /// <returns></returns>
  33. public async Task<List<Grp_ProcessOverview>> ProcessDataInitAsync(int groupId, int currUserId, List<string> visaCountries)
  34. {
  35. var processs = new List<Grp_ProcessOverview>();
  36. //团组验证
  37. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
  38. if (groupInfo == null) return processs;
  39. // 检查是否已存在流程
  40. var existingProcesses = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  41. .Where(p => p.IsDel == 0 && p.GroupId == groupId)
  42. .ToListAsync();
  43. if (existingProcesses.Any()) return processs;
  44. #region 商邀报批流程
  45. var custInfo = await _sqlSugar.Queryable<Grp_TourClientList>()
  46. .Where(c => c.DiId == groupId && c.IsDel == 0)
  47. .OrderByDescending(c => c.CreateTime)
  48. .FirstAsync();
  49. string oaNode2Tips = "客户提供完整名单后,2周内取得邀请函(翻译件)。";
  50. if (custInfo != null)
  51. {
  52. oaNode2Tips = $"请于{custInfo.CreateTime.AddDays(14):yyyy年MM月dd日}内完成该项工作(客户提供完整名单后,2周内取得邀请函(翻译件))";
  53. }
  54. var oaNode4Tips = $"请于{groupInfo.VisitDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(按进度实际公务活动落实情况,出发前5日落实公务)";
  55. var oaNode7Tips = $"请于{groupInfo.VisitEndDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(团组结束前完成)";
  56. processs.Add(
  57. Grp_ProcessOverview.Create(groupId, 1, GroupProcessType.Invitation, ProcessStatus.InProgress, currUserId,
  58. new List<Grp_ProcessNode>()
  59. {
  60. Grp_ProcessNode.Create(1, "报批基础资料准备","更新报批行程和请示,提供其他报批所需材料,4个工作日内完成。",ProcessStatus.InProgress, true,false,false,false,currUserId),
  61. Grp_ProcessNode.Create(2, "报批邀请函资料准备",oaNode2Tips, ProcessStatus.InProgress, false,false,false,false,currUserId),
  62. Grp_ProcessNode.Create(3, "获得批件","提供完整的报批全套资源。",ProcessStatus.InProgress, false,false,false,false, currUserId ),
  63. Grp_ProcessNode.Create(4, "对接公务",oaNode4Tips,ProcessStatus.InProgress, false,false,false,false, currUserId),
  64. Grp_ProcessNode.Create(5, "参与翻译对接","",ProcessStatus.InProgress, false,false,false,true, currUserId),
  65. Grp_ProcessNode.Create(6, "商邀文案配合","",ProcessStatus.InProgress, false,false,false,false, currUserId),
  66. Grp_ProcessNode.Create(7, "票据上传(相关票据)",oaNode7Tips,ProcessStatus.InProgress, false,false,true,false, currUserId),
  67. }));
  68. #endregion
  69. #region 签证流程
  70. //单独处理签证流程节点
  71. var visaNodes = new List<Grp_ProcessNode>();
  72. if (visaCountries != null && visaCountries.Count > 0)
  73. {
  74. var visaDefualtNodes = new List<VisaProcessNode>();
  75. for (int i = 1; i < visaCountries.Count + 1; i++)
  76. {
  77. visaDefualtNodes.Add(VisaProcessNode.Info(i, visaCountries[i - 1].ToString()));
  78. }
  79. var visaNode2Tips = $"请于{groupInfo.VisitDate:yyyy年MM月dd日}内完成该项工作(按进度实际签证办理落实情况,团组出发前上传票据。)";
  80. visaNodes.Add(Grp_ProcessNode.Create(1, "签证信息", "", ProcessStatus.InProgress, true, false, false, false, currUserId, JsonConvert.SerializeObject(visaDefualtNodes)));
  81. visaNodes.Add(Grp_ProcessNode.Create(2, "票据上传(明细表、费用票据、保单及超支费用账单)", visaNode2Tips, ProcessStatus.InProgress, false, false, true, false, currUserId));
  82. }
  83. processs.Add(Grp_ProcessOverview.Create(groupId, 2, GroupProcessType.Visa, ProcessStatus.UnStarted, currUserId, visaNodes));
  84. #endregion
  85. #region 机票流程
  86. string airNode1Tips = "建团后打勾确认出团的时候开始24小时内。";
  87. if (groupInfo.Step == 1 || groupInfo.Step == 2)
  88. {
  89. if (groupInfo.StepOperationTime.HasValue)
  90. {
  91. airNode1Tips = $"请于{groupInfo.StepOperationTime.Value.AddDays(1):yyyy年MM月dd日}内完成该项工作(建团后打勾确认出团的时候开始24小时内)";
  92. }
  93. }
  94. var airNode3Tips = $"完成机票采购确认(含预算核对、出票确认等)";
  95. var airNode5Tips = $"请于{groupInfo.VisitDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(团组出发前5日)";
  96. var airNode7Tips = $"请于{groupInfo.VisitEndDate.AddDays(5):yyyy年MM月dd日}内完成该项工作(团组归国后5个工作日内)";
  97. var airNode8Tips = $"1. 票据上传(机票报销蓝联、行程单及机票说明) \r\n 2. 请于{groupInfo.VisitEndDate.AddDays(10):yyyy年MM月dd日}内完成该项工作(团组归国后10个工作日内) *按机票报价*0.999折扣出具机票报销蓝联、行程单及机票说明";
  98. processs.Add(
  99. Grp_ProcessOverview.Create(groupId, 3, GroupProcessType.AirTicket, ProcessStatus.InProgress, currUserId,
  100. new List<Grp_ProcessNode>()
  101. {
  102. Grp_ProcessNode.Create(1, "初步拟定航程方案及价格", airNode1Tips, ProcessStatus.InProgress, true,false,false,false, currUserId ),
  103. Grp_ProcessNode.Create(2, "机票占位、续位", "", ProcessStatus.UnStarted, false,false,false,false,currUserId ),
  104. Grp_ProcessNode.Create(3, "完成机票采购确认", airNode3Tips, ProcessStatus.UnStarted,false,false,false,false, currUserId),
  105. Grp_ProcessNode.Create(4, "进行出票操作并核查信息", "", ProcessStatus.UnStarted, false,false,false,false, currUserId),
  106. Grp_ProcessNode.Create(5, "机票已出", airNode5Tips, ProcessStatus.UnStarted, false,false,false,false, currUserId),
  107. Grp_ProcessNode.Create(6, "完成机票选座", "", ProcessStatus.UnStarted, false,false,false,false,currUserId),
  108. Grp_ProcessNode.Create(7, "票据上传(机票超支费用账单)", airNode7Tips, ProcessStatus.UnStarted, false,false,true,false, currUserId),
  109. Grp_ProcessNode.Create(8, "票据上传", airNode8Tips, ProcessStatus.UnStarted, false,false,true,false, currUserId)
  110. }
  111. )
  112. );
  113. #endregion
  114. #region 酒店流程
  115. string hotelNode1Tips = "1. 筛选并按照预算标准,对目标酒店进行询价、比价、谈价 \r\n2. 建团后打勾确认出团的时候开始2个工作日。";
  116. if (groupInfo.Step == 1 || groupInfo.Step == 2)
  117. {
  118. if (groupInfo.StepOperationTime.HasValue)
  119. {
  120. hotelNode1Tips = $"请于{groupInfo.StepOperationTime.Value.AddDays(2):yyyy年MM月dd日}内完成该项工作(建团后打勾确认出团的时候开始2个工作日)";
  121. }
  122. }
  123. var hotelNode4Tips = $"1.行前再次确认酒店订单、付款状态及入住安排 \r\n 2.请于{groupInfo.VisitDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(团组出发前5天)";
  124. var hotelNode5Tips = $"1.行程结束后整理酒店发票(含超支费用发票)与结算 \r\n 2.请于{groupInfo.VisitEndDate.AddDays(5):yyyy年MM月dd日}内完成该项工作(团组结束后5天内)";
  125. processs.Add(
  126. Grp_ProcessOverview.Create(groupId, 4, GroupProcessType.Hotel, ProcessStatus.InProgress, currUserId,
  127. new List<Grp_ProcessNode>()
  128. {
  129. Grp_ProcessNode.Create(1, "按照预算,询价、比价、谈价", hotelNode1Tips, ProcessStatus.InProgress, true, false, false, false, currUserId),
  130. Grp_ProcessNode.Create(2, "获取酒店确认函与入住名单核对", "", ProcessStatus.UnStarted, false, false, false,false, currUserId ),
  131. Grp_ProcessNode.Create(3, "预订酒店并录入OA", "", ProcessStatus.UnStarted,false, false, false,false,currUserId ),
  132. Grp_ProcessNode.Create(4, "行前再次确认酒店相关情况", hotelNode4Tips,ProcessStatus.UnStarted, false, false, false,false,currUserId ),
  133. Grp_ProcessNode.Create(5, "行程结束后整理酒店发票与结算", hotelNode5Tips, ProcessStatus.UnStarted, false, false, true,false, currUserId ),
  134. }
  135. )
  136. );
  137. #endregion
  138. #region 地接流程
  139. var airTripCodeInfo = await _sqlSugar.Queryable<Air_TicketBlackCode>()
  140. .Where(x => x.IsDel == 0 && x.DiId == groupId)
  141. .OrderByDescending(x => x.CreateTime)
  142. .FirstAsync();
  143. string opNode1Tips = $"机票行程代码最后一段录入后1个工作日内。";
  144. if (airTripCodeInfo != null)
  145. {
  146. opNode1Tips = $"请于{airTripCodeInfo.CreateTime.AddDays(1):yyyy年MM月dd日}内完成该项工作(机票行程代码最后一段录入后1个工作日内)";
  147. }
  148. string opNode2Tips = $"1.联系并询价地接、餐厅、用车、景点等供应商 \r\n 2. 请于{groupInfo.CreateTime.AddDays(7):yyyy年MM月dd日}内完成该项工作(建团完成后7个工作日内)";
  149. string opNode3Tips = $"请于{groupInfo.CreateTime.AddDays(10):yyyy年MM月dd日}内完成该项工作(上一步往后3个工作日内)";
  150. string opNode4Tips = $"请于{groupInfo.CreateTime.AddDays(12):yyyy年MM月dd日}内完成该项工作(上一步往后2个工作日内)";
  151. var backListInfo = await _sqlSugar.Queryable<Grp_InvertedList>().Where(x => x.DiId == groupId && x.IsDel == 0).FirstAsync();
  152. string opNode5Tips = $"1.制定最终《行程单》及《出行手册》 \r\n2. 倒推表里开行前会 -3天。";
  153. if (backListInfo != null)
  154. {
  155. if (DateTime.TryParse(backListInfo.PreTripMeetingDt, out DateTime dateTime))
  156. {
  157. opNode5Tips = $"请于{dateTime.AddDays(-3):yyyy年MM月dd日}内完成该项工作(倒推表里开行前会 -3天)";
  158. }
  159. }
  160. string opNode7Tips = $"请于{groupInfo.VisitEndDate.AddDays(5):yyyy年MM月dd日}内完成该项工作(团组归国后5个工作日内) *上传最终报批行程,确定城市间交通最终版报价分配;地接账单(清楚标注超时及其他项超支费用)、地接交通费用原始票据、城市间交通明细表;";
  161. processs.Add(
  162. Grp_ProcessOverview.Create(groupId, 5, GroupProcessType.LocalGuide, ProcessStatus.InProgress, currUserId,
  163. new List<Grp_ProcessNode>()
  164. {
  165. Grp_ProcessNode.Create(1,"根据机票方案出框架行程", opNode1Tips,ProcessStatus.InProgress, true, false, false,false,currUserId ),
  166. Grp_ProcessNode.Create(2,"联系并询价地接相关的供应商", opNode2Tips,ProcessStatus.UnStarted, false, false, false,false, currUserId ),
  167. Grp_ProcessNode.Create(3,"提交供应商报价及比价表", opNode3Tips, ProcessStatus.UnStarted, false, false, false, false,currUserId),
  168. Grp_ProcessNode.Create(4,"执行采购流程", opNode4Tips, ProcessStatus.UnStarted, false, false, false,false, currUserId),
  169. Grp_ProcessNode.Create(5,"制定最终行程单及出行手册", opNode5Tips, ProcessStatus.UnStarted, false, false, false,false, currUserId ),
  170. Grp_ProcessNode.Create(6,"送机", "", ProcessStatus.UnStarted, false, false, false,false, currUserId ),
  171. Grp_ProcessNode.Create(7,"最终版报批行程、票据上传", opNode7Tips, ProcessStatus.UnStarted, false, false, true, false,currUserId )
  172. }
  173. )
  174. );
  175. #endregion
  176. #region 费用结算流程
  177. var feeNode3Tips = $"1.整理统计团组超支费用、三公报销资料给到各单位 \r\n 2. 请于{groupInfo.VisitEndDate.AddDays(12):yyyy年MM月dd日}内完成该项工作(团组归国后12个工作日内)";
  178. processs.Add(
  179. Grp_ProcessOverview.Create(groupId, 6, GroupProcessType.FeeSettle, ProcessStatus.InProgress, currUserId,
  180. new List<Grp_ProcessNode>()
  181. {
  182. Grp_ProcessNode.Create(1, "城市间交通报批金额核定", "团组报批前", ProcessStatus.InProgress, true, true, false,false,currUserId ),
  183. Grp_ProcessNode.Create(2, "团组全程各段机票打票金额的核定", "团组报批后、订票前", ProcessStatus.UnStarted, false, false, false,false,currUserId ),
  184. Grp_ProcessNode.Create(3, "整理统计相关财务资料给到各单位", feeNode3Tips, ProcessStatus.UnStarted, false, false, false,false,currUserId ),
  185. Grp_ProcessNode.Create(4, "费用结算完毕", "", ProcessStatus.UnStarted, false, false, false,false, currUserId ),
  186. }
  187. )
  188. );
  189. #endregion
  190. return processs;
  191. }
  192. /// <summary>
  193. /// 团组流程初始化
  194. /// </summary>
  195. /// <param name="request">创建流程请求参数</param>
  196. /// <returns>创建的流程信息</returns>
  197. public async Task<Result> ProcessInitAsync(int groupId, int currUserId)
  198. {
  199. //团组验证
  200. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
  201. if (groupInfo == null)
  202. {
  203. return new Result { Code = 400, Msg = "团组不存在" };
  204. }
  205. // 检查是否已存在流程
  206. var existingProcesses = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  207. .Where(p => p.IsDel == 0 && p.GroupId == groupId)
  208. .ToListAsync();
  209. if (existingProcesses.Any())
  210. {
  211. return new Result { Code = 400, Msg = "该团组的流程已存在" };
  212. }
  213. //处理签证国家
  214. var visaCountries = _groupRep.GroupSplitCountry(groupInfo.VisitCountry);
  215. // 定义默认的流程节点
  216. var processs = await ProcessDataInitAsync(groupId, currUserId, visaCountries);
  217. _sqlSugar.BeginTran();
  218. foreach (var item in processs)
  219. {
  220. var processId = await _sqlSugar.Insertable(item).ExecuteReturnIdentityAsync();
  221. if (processId < 1)
  222. {
  223. _sqlSugar.RollbackTran();
  224. return new Result { Code = 400, Msg = "团组流程进度总览表添加失败!" };
  225. }
  226. item.Id = processId;
  227. // 记录流程日志
  228. await LogProcessOpAsync(null, item, "Create", currUserId);
  229. var nodes = item.Nodes.Select((nodeDto, index) => new Grp_ProcessNode
  230. {
  231. ProcessId = processId,
  232. NodeName = nodeDto.NodeName,
  233. NodeOrder = nodeDto.NodeOrder,
  234. OverallStatus = nodeDto.OverallStatus,
  235. NodeDescTips = nodeDto.NodeDescTips,
  236. //Country = nodeDto.Country,
  237. IsCurrent = nodeDto.IsCurrent,
  238. IsAssist = nodeDto.IsAssist,
  239. IsPart = nodeDto.IsPart,
  240. IsFileUp = nodeDto.IsFileUp,
  241. Remark = nodeDto.Remark
  242. }).ToList();
  243. var nodeIds = await _sqlSugar.Insertable(nodes).ExecuteCommandAsync();
  244. if (nodeIds < 1)
  245. {
  246. _sqlSugar.RollbackTran();
  247. return new Result { Code = 400, Msg = "团组流程进度流程节点添加失败!" };
  248. }
  249. //设置节点ID
  250. nodes = await _sqlSugar.Queryable<Grp_ProcessNode>().Where(x => x.IsDel == 0 && x.ProcessId == processId).ToListAsync();
  251. //记录节点日志
  252. foreach (var node in nodes)
  253. {
  254. await LogNodeOpAsync(null, node, "Create", currUserId);
  255. }
  256. }
  257. _sqlSugar.CommitTran();
  258. return new Result { Code = 200, Msg = "添加成功!" }; ;
  259. }
  260. /// <summary>
  261. /// 获取团组的所有流程及节点详情
  262. /// </summary>
  263. /// <param name="request">创建流程请求参数</param>
  264. /// <returns>创建的流程信息</returns>
  265. public async Task<Result> ProcessesDetailsAsync(int groupId)
  266. {
  267. //团组验证
  268. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
  269. if (groupInfo == null)
  270. {
  271. return new Result { Code = 400, Msg = "团组不存在" };
  272. }
  273. // 检查是否已存在流程
  274. var existingProcesses = await _sqlSugar.Queryable<Grp_ProcessOverview>().Where(p => p.IsDel == 0 && p.GroupId == groupId).ToListAsync();
  275. if (!existingProcesses.Any())
  276. {
  277. //新建团组流程
  278. var res = await ProcessInitAsync(groupId, 4);
  279. if (res.Code != 200)
  280. {
  281. return res;
  282. }
  283. }
  284. var users = await _sqlSugar.Queryable<Sys_Users>().Select(x => new { x.Id, x.CnName }).ToListAsync();
  285. var processData = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  286. .Where(p => p.GroupId == groupId && p.IsDel == 0)
  287. .Mapper(p => p.Nodes, p => p.Nodes.First().ProcessId)
  288. .ToListAsync();
  289. // 预先构建用户字典,提升查询性能
  290. var userDict = users.ToDictionary(u => u.Id, u => u.CnName);
  291. var processes = processData.Select(p =>
  292. {
  293. var orderedNodes = p.Nodes.OrderBy(n => n.NodeOrder).ToList();
  294. var totalNodes = orderedNodes.Count;
  295. return new
  296. {
  297. p.Id,
  298. p.GroupId,
  299. p.ProcessType,
  300. ProcessName = p.ProcessType.GetEnumDescription(),
  301. Nodes = orderedNodes.Select((n, index) =>
  302. {
  303. var isLastNode = index == totalNodes - 1;
  304. var isSecondLastNode = index == totalNodes - 2;
  305. var isFifthStep = index == 4;
  306. // 计算按钮状态
  307. bool isEnaAssistBtn = p.ProcessType == GroupProcessType.FeeSettle && n.NodeOrder == 1;
  308. // 文件上传按钮启用规则
  309. bool isEnaFileUpBtn = false;
  310. // 是否参与按钮启用
  311. bool isEnaPartBtn = false;
  312. // 规则1:商邀流程第五步启用参与按钮
  313. if (p.ProcessType == GroupProcessType.Invitation && isFifthStep)
  314. {
  315. isEnaPartBtn = true;
  316. }
  317. // 规则2:机票流程倒数第二步启用上传按钮
  318. else if (p.ProcessType == GroupProcessType.AirTicket && isSecondLastNode)
  319. {
  320. isEnaFileUpBtn = true;
  321. }
  322. // 规则3:默认流程节点最后一步启用上传按钮
  323. else if (isLastNode && p.ProcessType != GroupProcessType.FeeSettle)
  324. {
  325. isEnaFileUpBtn = true;
  326. }
  327. // 处理签证子节点
  328. List<VisaProcessNode> visaSubNodes = new();
  329. if (p.ProcessType == GroupProcessType.Visa && n.NodeOrder == 1)
  330. {
  331. visaSubNodes = JsonConvert.DeserializeObject<List<VisaProcessNode>>(n.Remark ?? "[]")
  332. ?? new List<VisaProcessNode>();
  333. }
  334. // 获取操作人姓名(使用字典提升性能)
  335. string operatorName = "-";
  336. if (n.Operator.HasValue && userDict.TryGetValue(n.Operator.Value, out var name))
  337. {
  338. operatorName = name;
  339. }
  340. return new
  341. {
  342. n.Id,
  343. n.ProcessId,
  344. n.NodeOrder,
  345. n.NodeName,
  346. n.OverallStatus,
  347. StatusText = n.OverallStatus.GetEnumDescription(),
  348. Operator = operatorName,
  349. OpeateTime = n.OperationTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? "-",
  350. ActualDone = n.ActualDone?.ToString("yyyy-MM-dd HH:mm:ss") ?? "",
  351. n.NodeDescTips,
  352. isEnaAssistBtn, // 是否启用财务流程首节点协助按钮
  353. n.IsAssist, // 财务流程首节点 存储值
  354. isEnaFileUpBtn, // 是否启用上传文件按钮
  355. n.IsFileUp, // 票据上传节点 存储值
  356. isEnaPartBtn, // 是否启用参与按钮
  357. n.IsPart, // 参与按钮 存储值
  358. visaSubNodes // 签证节点类型使用
  359. };
  360. }).ToList()
  361. };
  362. }).ToList();
  363. return new Result { Code = 200, Data = processes, Msg = "查询成功!" };
  364. }
  365. /// <summary>
  366. /// 更新节点状态
  367. /// </summary>
  368. /// <param name="nodeId">节点ID</param>
  369. /// <param name="currUserId">当前用户ID</param>
  370. /// <param name="processStatus">流程状态,默认为已完成</param>
  371. /// <returns>操作结果</returns>
  372. public async Task<Result> UpdateNodeStatusAsync(int nodeId, int currUserId, ProcessStatus processStatus = ProcessStatus.Completed)
  373. {
  374. try
  375. {
  376. // 使用事务确保数据一致性
  377. var result = await _sqlSugar.Ado.UseTranAsync(async () =>
  378. {
  379. // 1. 获取并验证节点
  380. var node = await _sqlSugar.Queryable<Grp_ProcessNode>()
  381. .FirstAsync(n => n.Id == nodeId && n.IsDel == 0) ?? throw new BusinessException("当前节点不存在或已被删除。");
  382. // 2. 获取流程信息,检查ProcessType
  383. var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  384. .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0) ?? throw new BusinessException("关联的流程不存在。");
  385. // 3. 节点操作验证
  386. ValidateNodeOperation(node, processStatus);
  387. // 4. 存储更新前的值
  388. var before = new Grp_ProcessNode()
  389. {
  390. Id = node.Id,
  391. ProcessId = node.ProcessId,
  392. NodeName = node.NodeName,
  393. NodeOrder = node.NodeOrder,
  394. OverallStatus = node.OverallStatus,
  395. Operator = node.Operator,
  396. OperationTime = node.OperationTime,
  397. IsCurrent = node.IsCurrent,
  398. };
  399. // 5. 更新节点状态
  400. node.OverallStatus = processStatus;
  401. node.Operator = currUserId;
  402. node.OperationTime = DateTime.Now;
  403. var updateCount = await _sqlSugar.Updateable(node)
  404. .UpdateColumns(n => new
  405. {
  406. n.OverallStatus,
  407. n.Operator,
  408. n.OperationTime
  409. })
  410. .ExecuteCommandAsync();
  411. if (updateCount == 0)
  412. {
  413. throw new BusinessException("节点状态更新失败。");
  414. }
  415. // 6. 记录节点日志
  416. await LogNodeOpAsync(before, node, "Update", currUserId);
  417. // 7. 如果是完成当前节点,处理流程流转
  418. // 当前节点或者流程类型为商邀可进入状态流转
  419. if (processStatus == ProcessStatus.Completed && (node.IsCurrent || process.ProcessType == GroupProcessType.Invitation))
  420. {
  421. await ProcessCurrentNodeCompletionAsync(node, currUserId);
  422. }
  423. return new Result { Code = StatusCodes.Status200OK, Msg = "操作成功。" };
  424. });
  425. return result.IsSuccess ? result.Data : new Result
  426. {
  427. Code = StatusCodes.Status500InternalServerError,
  428. Msg = result.ErrorMessage
  429. };
  430. }
  431. catch (BusinessException ex)
  432. {
  433. // 业务异常
  434. return new Result { Code = StatusCodes.Status400BadRequest, Msg = ex.Message };
  435. }
  436. catch (Exception ex)
  437. {
  438. // 系统异常
  439. return new Result { Code = StatusCodes.Status500InternalServerError, Msg = "系统错误,请稍后重试" };
  440. }
  441. }
  442. /// <summary>
  443. /// 验证节点操作权限
  444. /// </summary>
  445. /// <param name="node">流程节点</param>
  446. /// <param name="targetStatus">目标状态</param>
  447. private static void ValidateNodeOperation(Grp_ProcessNode node, ProcessStatus targetStatus)
  448. {
  449. // 验证节点是否已完成
  450. if (node.OverallStatus == ProcessStatus.Completed)
  451. {
  452. throw new BusinessException("当前节点已完成,不可重复操作。");
  453. }
  454. // 验证状态流转是否合法(可选)
  455. //if (targetStatus != ProcessStatus.Completed)
  456. //{
  457. // throw new BusinessException("未开始或者进行中的节点只能重新完成,不可进行其他操作。");
  458. //}
  459. // 验证是否尝试将已完成节点改为其他状态
  460. if (node.OverallStatus == ProcessStatus.Completed && targetStatus != ProcessStatus.Completed)
  461. {
  462. throw new BusinessException("已完成节点不可修改状态。");
  463. }
  464. }
  465. /// <summary>
  466. /// 处理当前节点完成后的流程流转
  467. /// </summary>
  468. private async Task ProcessCurrentNodeCompletionAsync(Grp_ProcessNode currentNode, int currUserId)
  469. {
  470. // 1. 获取流程信息
  471. var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  472. .FirstAsync(p => p.Id == currentNode.ProcessId && p.IsDel == 0);
  473. if (process == null)
  474. {
  475. throw new BusinessException("关联的流程不存在。");
  476. }
  477. var processBefore = new Grp_ProcessOverview()
  478. {
  479. Id = process.Id,
  480. GroupId = process.GroupId,
  481. ProcessOrder = process.ProcessOrder,
  482. ProcessType = process.ProcessType,
  483. OverallStatus = process.OverallStatus,
  484. StartTime = process.StartTime,
  485. EndTime = process.EndTime,
  486. UpdatedUserId = process.UpdatedUserId,
  487. UpdatedTime = process.UpdatedTime
  488. };
  489. // 2. 取消当前节点的当前状态
  490. var before = new Grp_ProcessNode()
  491. {
  492. Id = currentNode.Id,
  493. ProcessId = currentNode.ProcessId,
  494. NodeName = currentNode.NodeName,
  495. NodeOrder = currentNode.NodeOrder,
  496. OverallStatus = currentNode.OverallStatus,
  497. Operator = currentNode.Operator,
  498. OperationTime = currentNode.OperationTime,
  499. IsCurrent = currentNode.IsCurrent,
  500. };
  501. currentNode.IsCurrent = false;
  502. await _sqlSugar.Updateable(currentNode)
  503. .UpdateColumns(n => new { n.IsCurrent })
  504. .ExecuteCommandAsync();
  505. // 2.1 记录节点日志 取消当前节点状态
  506. await LogNodeOpAsync(before, currentNode, "Update", currUserId);
  507. // 3. 查找并激活下一个节点 商邀节点单独处理
  508. if (process.ProcessType == GroupProcessType.Invitation)
  509. {
  510. var invitaNodeStatus = await _sqlSugar.Queryable<Grp_ProcessNode>()
  511. .Where(x => x.IsDel == 0 && x.ProcessId == currentNode.ProcessId)
  512. .ToListAsync();
  513. int completedCount = invitaNodeStatus.Count(n => n.OverallStatus == ProcessStatus.Completed);
  514. int nodeCount = invitaNodeStatus.Count;
  515. if (completedCount == nodeCount) //全部子节点完成,该流程完成
  516. {
  517. process.OverallStatus = ProcessStatus.Completed;
  518. process.EndTime = DateTime.Now;
  519. }
  520. }
  521. else
  522. {
  523. var nextNode = await _sqlSugar.Queryable<Grp_ProcessNode>()
  524. .Where(n => n.ProcessId == currentNode.ProcessId
  525. && n.NodeOrder == currentNode.NodeOrder + 1
  526. && n.IsDel == 0)
  527. .FirstAsync();
  528. if (nextNode != null)
  529. {
  530. var nextNodeBefore = new Grp_ProcessNode()
  531. {
  532. Id = nextNode.Id,
  533. ProcessId = nextNode.ProcessId,
  534. NodeName = nextNode.NodeName,
  535. NodeOrder = nextNode.NodeOrder,
  536. OverallStatus = nextNode.OverallStatus,
  537. Operator = nextNode.Operator,
  538. OperationTime = nextNode.OperationTime,
  539. IsCurrent = nextNode.IsCurrent,
  540. };
  541. // 激活下一个节点
  542. nextNode.IsCurrent = true;
  543. nextNode.OverallStatus = ProcessStatus.InProgress;
  544. //nextNode.Operator = currUserId;
  545. //nextNode.OperationTime = DateTime.Now;
  546. var updateCount = await _sqlSugar.Updateable(nextNode)
  547. .UpdateColumns(n => new
  548. {
  549. n.IsCurrent,
  550. n.OverallStatus,
  551. n.Operator,
  552. n.OperationTime
  553. })
  554. .ExecuteCommandAsync();
  555. if (updateCount == 0)
  556. {
  557. throw new BusinessException("激活下一节点失败");
  558. }
  559. // 1.1 记录节点日志 激活下一节点当前节点状态
  560. await LogNodeOpAsync(nextNodeBefore, nextNode, "Start", currUserId);
  561. // 更新流程状态为进行中
  562. process.OverallStatus = ProcessStatus.InProgress;
  563. }
  564. else
  565. {
  566. // 下一节点不存在,整个流程完成
  567. process.OverallStatus = ProcessStatus.Completed;
  568. process.EndTime = DateTime.Now;
  569. }
  570. }
  571. // 4. 更新流程信息
  572. process.UpdatedUserId = currUserId;
  573. process.UpdatedTime = DateTime.Now;
  574. var processUpdateCount = await _sqlSugar.Updateable(process)
  575. .UpdateColumns(p => new
  576. {
  577. p.OverallStatus,
  578. p.EndTime,
  579. p.UpdatedUserId,
  580. p.UpdatedTime
  581. })
  582. .ExecuteCommandAsync();
  583. if (processUpdateCount == 0)
  584. {
  585. throw new BusinessException("流程状态更新失败。");
  586. }
  587. //记录流程日志
  588. await LogProcessOpAsync(processBefore, process, "Update", currUserId);
  589. }
  590. /// <summary>
  591. /// 更新签证节点信息及状态
  592. /// </summary>
  593. /// <param name="dto">签证节点更新数据传输对象</param>
  594. /// <returns>操作结果</returns>
  595. public async Task<Result> UpdateVisaNodeDetailsAsync(GroupProcessUpdateVisaNodeDetailsDto dto)
  596. {
  597. // 1. 获取并验证节点和流程
  598. var node = await _sqlSugar.Queryable<Grp_ProcessNode>()
  599. .FirstAsync(n => n.Id == dto.NodeId && n.IsDel == 0)
  600. ?? throw new BusinessException("当前节点不存在或已被删除。");
  601. var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  602. .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0)
  603. ?? throw new BusinessException("当前流程不存在或已被删除。");
  604. if (process.ProcessType != GroupProcessType.Visa)
  605. {
  606. throw new BusinessException("当前流程节点不为签证流程,不可编辑。");
  607. }
  608. // 2. 检查签证子节点 字段信息是否全部填写
  609. var allSubNodesCompleted = dto.VisaSubNodes?.All(subNode => EntityExtensions.IsCompleted(subNode)) ?? false;
  610. // 2.1 存储更新前流程及节点信息
  611. var nodeBefore = new Grp_ProcessNode()
  612. {
  613. Id = node.Id,
  614. ProcessId = node.ProcessId,
  615. NodeName = node.NodeName,
  616. NodeOrder = node.NodeOrder,
  617. OverallStatus = node.OverallStatus,
  618. Operator = node.Operator,
  619. OperationTime = node.OperationTime,
  620. IsCurrent = node.IsCurrent,
  621. };
  622. var processBefore = new Grp_ProcessOverview()
  623. {
  624. Id = process.Id,
  625. GroupId = process.GroupId,
  626. ProcessOrder = process.ProcessOrder,
  627. ProcessType = process.ProcessType,
  628. OverallStatus = process.OverallStatus,
  629. StartTime = process.StartTime,
  630. EndTime = process.EndTime,
  631. UpdatedUserId = process.UpdatedUserId,
  632. UpdatedTime = process.UpdatedTime
  633. };
  634. // 3. 更新节点信息
  635. node.Remark = JsonConvert.SerializeObject(dto.VisaSubNodes);
  636. node.Operator = dto.CurrUserId;
  637. node.OperationTime = DateTime.Now;
  638. if (allSubNodesCompleted)
  639. {
  640. node.OverallStatus = ProcessStatus.Completed;
  641. process.OverallStatus = ProcessStatus.Completed;
  642. process.EndTime = DateTime.Now;
  643. process.UpdatedUserId = dto.CurrUserId;
  644. process.UpdatedTime = DateTime.Now;
  645. // 更新流程状态
  646. await _sqlSugar.Updateable(process)
  647. .UpdateColumns(p => new
  648. {
  649. p.OverallStatus,
  650. p.EndTime,
  651. p.UpdatedUserId,
  652. p.UpdatedTime
  653. })
  654. .ExecuteCommandAsync();
  655. //记录流程日志
  656. await LogProcessOpAsync(processBefore, process, "Update", dto.CurrUserId);
  657. }
  658. // 4. 保存节点更新
  659. await _sqlSugar.Updateable(node)
  660. .UpdateColumns(n => new
  661. {
  662. n.Remark,
  663. n.Operator,
  664. n.OperationTime,
  665. n.OverallStatus
  666. })
  667. .ExecuteCommandAsync();
  668. //记录节点日志
  669. await LogNodeOpAsync(nodeBefore, node, "Update", dto.CurrUserId);
  670. return new Result { Code = 200, Msg = "节点信息更新成功。" };
  671. }
  672. /// <summary>
  673. /// 更新节点信息及状态
  674. /// </summary>
  675. /// <param name="dto">签证节点更新数据传输对象</param>
  676. /// <returns>操作结果</returns>
  677. public async Task<Result> SetActualDoneAsync(GroupProcessSetActualDoneDto dto)
  678. {
  679. int nodeId = dto.NodeId;
  680. var isDtNul = DateTime.TryParse(dto.ActualDone, out DateTime actualDone);
  681. int currUserId = dto.CurrUserId;
  682. bool isAssist = dto.IsAssist;
  683. bool isFileUp = dto.IsFileUp;
  684. bool isPart = dto.IsPart;
  685. // 1. 获取并验证节点和流程
  686. var node = await _sqlSugar.Queryable<Grp_ProcessNode>()
  687. .FirstAsync(n => n.Id == nodeId && n.IsDel == 0)
  688. ?? throw new BusinessException("当前节点不存在或已被删除。");
  689. var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  690. .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0)
  691. ?? throw new BusinessException("当前流程不存在或已被删除。");
  692. // 2.1 存储更新前流程及节点信息
  693. var nodeBefore = new Grp_ProcessNode()
  694. {
  695. Id = node.Id,
  696. ProcessId = node.ProcessId,
  697. NodeName = node.NodeName,
  698. NodeOrder = node.NodeOrder,
  699. OverallStatus = node.OverallStatus,
  700. Operator = node.Operator,
  701. OperationTime = node.OperationTime,
  702. IsCurrent = node.IsCurrent
  703. };
  704. if (isDtNul)
  705. {
  706. node.ActualDone = actualDone;
  707. }
  708. node.IsAssist = isAssist;
  709. node.IsFileUp = isFileUp;
  710. node.IsPart = isPart;
  711. // 3. 保存节点更新
  712. await _sqlSugar.Updateable(node)
  713. .UpdateColumns(n => new
  714. {
  715. ActualDone = isDtNul ? node.ActualDone : null,
  716. n.IsAssist,
  717. n.IsFileUp,
  718. n.IsPart,
  719. })
  720. .ExecuteCommandAsync();
  721. //记录节点日志
  722. await LogNodeOpAsync(nodeBefore, node, "Update", currUserId);
  723. return new Result { Code = 200, Msg = "实际操作时间设置成功。" };
  724. }
  725. #endregion
  726. #region 会务流程
  727. /// <summary>
  728. /// 设置节点流程模板
  729. /// </summary>
  730. /// <param name="groupId"></param>
  731. /// <param name="currUserId"></param>
  732. /// <returns></returns>
  733. public async Task<List<Grp_ConfProcessOverview>> DefaultConfProcessTemps(int groupId, int currUserId)
  734. {
  735. var temps = new List<Grp_ConfProcessOverview>();
  736. //团组验证
  737. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
  738. if (groupInfo == null) return temps;
  739. //// 检查是否已存在流程
  740. //var existingProcesses = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
  741. // .Where(p => p.IsDel == 0 && p.GroupId == groupId)
  742. // .ToListAsync();
  743. //if (existingProcesses.Any()) return temps;
  744. #region 会务流程
  745. //参与人 participators
  746. var defaultParticipators = new List<ParticipatorInfo>() { new() { UserId = 213, UserName = "李新江" } };
  747. var defaultPorc1 = new List<Grp_ConfProcessNode>() {
  748. Grp_ConfProcessNode.Create(1,"方案/报价(含成本)","", ProcessStatus.InProgress,true,false, currUserId,defaultParticipators),
  749. Grp_ConfProcessNode.Create(2,"项目前期比选/招投标相关文件","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
  750. Grp_ConfProcessNode.Create(3,"参与投标","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
  751. Grp_ConfProcessNode.Create(4,"拟定/签订合同","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
  752. Grp_ConfProcessNode.Create(5,"场地预订/物料设计/对接活动所需的供应商/嘉宾邀约","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
  753. Grp_ConfProcessNode.Create(6,"现场执行","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
  754. Grp_ConfProcessNode.Create(7,"验收报告/决算表","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
  755. Grp_ConfProcessNode.Create(8,"跟进项目收款","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
  756. };
  757. temps.Add(Grp_ConfProcessOverview.Create(groupId, 1, ProcessStatus.InProgress, currUserId, defaultPorc1));
  758. var defaultPorc2 = new List<Grp_ConfProcessNode>() {
  759. Grp_ConfProcessNode.Create(1,"方案/报价(含成本)","", ProcessStatus.InProgress,true,false, currUserId,defaultParticipators),
  760. Grp_ConfProcessNode.Create(2,"拟定/签订合同","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
  761. Grp_ConfProcessNode.Create(3,"场地预订/物料设计/对接活动所需的供应商/嘉宾邀约","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
  762. Grp_ConfProcessNode.Create(4,"现场执行","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
  763. Grp_ConfProcessNode.Create(5,"验收报告/决算表","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
  764. Grp_ConfProcessNode.Create(6,"跟进项目收款","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
  765. };
  766. temps.Add(Grp_ConfProcessOverview.Create(groupId, 2, ProcessStatus.InProgress, currUserId, defaultPorc1));
  767. #endregion
  768. return temps;
  769. }
  770. /// <summary>
  771. /// 团组会务流程初始化
  772. /// </summary>
  773. /// <param name="groupId">团组Id</param>
  774. /// <param name="currUserId">当前用户Id</param>
  775. /// <param name="nodeTempId">节点模板Id</param>
  776. /// <returns>创建的流程信息</returns>
  777. public async Task<Result> ConfProcessInitAsync(int groupId, int currUserId,int nodeTempId = 1)
  778. {
  779. //团组验证
  780. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
  781. if (groupInfo == null)
  782. {
  783. return new Result { Code = 400, Msg = "团组不存在" };
  784. }
  785. // 检查是否已存在流程
  786. var existingProcesses = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
  787. .Where(p => p.IsDel == 0 && p.GroupId == groupId)
  788. .ToListAsync();
  789. if (existingProcesses.Any())
  790. {
  791. return new Result { Code = 400, Msg = "团组会务流程已存在" };
  792. }
  793. // 定义默认的流程节点
  794. var temps = await DefaultConfProcessTemps(groupId, currUserId);
  795. if (temps == null || temps.Count < 1)
  796. return new Result { Code = 400, Msg = "团组会务默认流程不存在" };
  797. var process = temps.FirstOrDefault(x => x.ProcessOrder == nodeTempId);
  798. process.CreateUserId = currUserId;
  799. _sqlSugar.BeginTran();
  800. try
  801. {
  802. var processId = await _sqlSugar.Insertable(process).ExecuteReturnIdentityAsync();
  803. if (processId < 1)
  804. {
  805. _sqlSugar.RollbackTran();
  806. return new Result { Code = 400, Msg = "团组会务流程进度总览添加失败!" };
  807. }
  808. process.Id = processId;
  809. // 记录流程日志
  810. await LogConfProcessOpAsync(null, process, "Create", currUserId);
  811. var nodes = process.Nodes.Select((nodeDto, index) => new Grp_ConfProcessNode
  812. {
  813. ProcessId = processId,
  814. NodeName = nodeDto.NodeName,
  815. NodeOrder = nodeDto.NodeOrder,
  816. Participator = JsonConvert.SerializeObject(nodeDto.Participators.Select(p => p.UserId).ToList()),
  817. OverallStatus = nodeDto.OverallStatus,
  818. NodeDescTips = nodeDto.NodeDescTips,
  819. //Country = nodeDto.Country,
  820. IsCurrent = nodeDto.IsCurrent,
  821. IsFileUp = nodeDto.IsFileUp,
  822. Remark = nodeDto.Remark,
  823. CreateUserId = currUserId,
  824. }).ToList();
  825. var nodeIds = await _sqlSugar.Insertable(nodes).ExecuteCommandAsync();
  826. if (nodeIds < 1)
  827. {
  828. _sqlSugar.RollbackTran();
  829. return new Result { Code = 400, Msg = "团组流程进度流程节点添加失败!" };
  830. }
  831. //设置节点ID
  832. nodes = await _sqlSugar.Queryable<Grp_ConfProcessNode>().Where(x => x.IsDel == 0 && x.ProcessId == processId).ToListAsync();
  833. //记录节点日志
  834. foreach (var node in nodes)
  835. {
  836. await LogConfNodeOpAsync(null, node, "Create", currUserId);
  837. }
  838. _sqlSugar.CommitTran();
  839. return new Result { Code = 200, Msg = "添加成功!" };
  840. }
  841. catch (Exception ex)
  842. {
  843. _sqlSugar.RollbackTran();
  844. return new Result { Code = 500, Msg = $"操作失败!msg:{ex.Message}" };
  845. }
  846. }
  847. /// <summary>
  848. /// 获取团组会务流程及节点详情
  849. /// </summary>
  850. /// <param name="groupId">团组Id</param>
  851. /// <param name="currUserId">当前用户Id</param>
  852. /// <returns></returns>
  853. public async Task<Result> ConfProcessesDetailsAsync(int groupId, int currUserId = 4)
  854. {
  855. //团组验证
  856. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
  857. if (groupInfo == null)
  858. {
  859. return new Result { Code = 400, Msg = "团组不存在" };
  860. }
  861. // 检查是否已存在流程
  862. var existingProcesses = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
  863. .Where(p => p.IsDel == 0 && p.GroupId == groupId)
  864. .ToListAsync();
  865. if (!existingProcesses.Any())
  866. {
  867. //新建团组流程
  868. var res = await ConfProcessInitAsync(groupId, currUserId);
  869. if (res.Code != 200)
  870. {
  871. return res;
  872. }
  873. }
  874. var users = await _sqlSugar.Queryable<Sys_Users>().Select(x => new { x.Id, x.CnName }).ToListAsync();
  875. var processData = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
  876. .Where(p => p.GroupId == groupId && p.IsDel == 0)
  877. .Mapper(p => p.Nodes, p => p.Nodes.First().ProcessId)
  878. .ToListAsync();
  879. // 预先构建用户字典,提升查询性能
  880. var userDict = users.ToDictionary(u => u.Id, u => u.CnName);
  881. var processes = processData.Select(p =>
  882. {
  883. var orderedNodes = p.Nodes.OrderBy(n => n.NodeOrder).ToList();
  884. var totalNodes = orderedNodes.Count;
  885. return new ConfProcessOverInfoView()
  886. {
  887. Id = p.Id,
  888. GroupId = p.GroupId,
  889. ProcessType = p.ProcessType,
  890. ProcessName = p.ProcessType.GetEnumDescription(),
  891. Nodes = orderedNodes.Select((n, index) =>
  892. {
  893. //var isLastNode = index == totalNodes - 1;
  894. //// 文件上传按钮启用规则
  895. //bool isEnaFileUpBtn = false;
  896. //if (isLastNode )
  897. //{
  898. // isEnaFileUpBtn = true;
  899. //}
  900. //获取参与人姓名
  901. var participators = new List<ParticipatorInfo>();
  902. var participatorArray = JsonConvert.DeserializeObject<List<int>>(n.Participator);
  903. if (participatorArray?.Count > 0)
  904. {
  905. foreach (var item in participatorArray)
  906. {
  907. if (userDict.TryGetValue(item, out var userName))
  908. {
  909. participators.Add(new ParticipatorInfo
  910. {
  911. UserId = item,
  912. UserName = userName
  913. });
  914. }
  915. }
  916. }
  917. // 获取操作人姓名(使用字典提升性能)
  918. string operatorName = "-";
  919. if (n.Operator.HasValue && userDict.TryGetValue(n.Operator.Value, out var name))
  920. {
  921. operatorName = name;
  922. }
  923. return new ConfProcessNodeInfoView()
  924. {
  925. Id = n.Id,
  926. ProcessId = n.ProcessId,
  927. NodeOrder = n.NodeOrder,
  928. NodeName = n.NodeName,
  929. NodeDescTips = n.NodeDescTips,
  930. OverallStatus = n.OverallStatus,
  931. Participators = participators,
  932. StatusText = n.OverallStatus.GetEnumDescription(),
  933. Operator = operatorName,
  934. OpeateTime = n.OperationTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? "-",
  935. ActualDone = n.ActualDone?.ToString("yyyy-MM-dd HH:mm:ss") ?? "",
  936. IsFileUp = n.IsFileUp, // 票据上传节点 存储值
  937. };
  938. }).ToList()
  939. };
  940. }).ToList();
  941. return new Result { Code = 200, Data = processes, Msg = "查询成功!" };
  942. }
  943. /// <summary>
  944. /// 更新节点模板
  945. /// </summary>
  946. /// <param name="groupId">团组Id</param>
  947. /// <param name="nodeTempId">节点模板Id</param>
  948. /// <param name="currUserId">当前用户Id</param>
  949. /// <returns></returns>
  950. public async Task<Result> ConfProcessChangeNodeTempSaveAsync(int groupId, int nodeTempId, int currUserId)
  951. {
  952. //节点模板id验证
  953. var nodeTempIds = new List<int>() { 1, 2 };
  954. if (!nodeTempIds.Contains(nodeTempId))
  955. {
  956. return new Result { Code = 400, Msg = "请传入有效的节点模板Id" };
  957. }
  958. //团组验证
  959. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
  960. if (groupInfo == null)
  961. {
  962. return new Result { Code = 400, Msg = "团组不存在" };
  963. }
  964. _sqlSugar.BeginTran();
  965. try
  966. {
  967. // 检查是否已存在流程
  968. var existingProcesses = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
  969. .Where(p => p.IsDel == 0 && p.GroupId == groupId)
  970. .ToListAsync();
  971. if (existingProcesses.Any())
  972. {
  973. //删除 原有的节点模板
  974. var parentIds = existingProcesses.Select(x => x.Id).ToList();
  975. var updProcesses = await _sqlSugar.Updateable<Grp_ConfProcessOverview>()
  976. .SetColumns(x => x.DeleteUserId == currUserId)
  977. .SetColumns(x => x.DeleteTime == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  978. .SetColumns(x => x.IsDel == 1)
  979. .Where(x => parentIds.Contains(x.Id))
  980. .ExecuteCommandAsync();
  981. var updProcessNodes = await _sqlSugar.Updateable<Grp_ConfProcessNode>()
  982. .SetColumns(x => x.DeleteUserId == currUserId)
  983. .SetColumns(x => x.DeleteTime == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  984. .SetColumns(x => x.IsDel == 1)
  985. .Where(x => parentIds.Contains(x.ProcessId))
  986. .ExecuteCommandAsync();
  987. }
  988. //更新模板节点信息
  989. var result = await ConfProcessInitAsync(groupId,currUserId,nodeTempId);
  990. if (result.Code == 200)
  991. {
  992. var infoResult = await ConfProcessesDetailsAsync(groupId, currUserId);
  993. if (infoResult.Code == 200)
  994. {
  995. _sqlSugar.CommitTran();
  996. return infoResult;
  997. }
  998. }
  999. _sqlSugar.RollbackTran();
  1000. return new Result { Code = 500, Msg = $"操作失败!Msg:{result.Msg}" };
  1001. }
  1002. catch (Exception ex)
  1003. {
  1004. _sqlSugar.RollbackTran();
  1005. return new Result { Code = 500, Msg = $"操作失败!Msg:{ex.Message}" };
  1006. }
  1007. }
  1008. /// <summary>
  1009. /// 更新团组会务流程节点状态
  1010. /// </summary>
  1011. /// <param name="nodeId">节点ID</param>
  1012. /// <param name="currUserId">当前用户ID</param>
  1013. /// <param name="processStatus">流程状态,默认为已完成</param>
  1014. /// <returns>操作结果</returns>
  1015. public async Task<Result> UpdateConfNodeStatusAsync(int nodeId, int currUserId, ProcessStatus processStatus = ProcessStatus.Completed)
  1016. {
  1017. try
  1018. {
  1019. // 使用事务确保数据一致性
  1020. var result = await _sqlSugar.Ado.UseTranAsync(async () =>
  1021. {
  1022. // 1. 获取并验证节点
  1023. var node = await _sqlSugar.Queryable<Grp_ConfProcessNode>()
  1024. .FirstAsync(n => n.Id == nodeId && n.IsDel == 0) ?? throw new BusinessException("当前节点不存在或已被删除。");
  1025. // 2. 获取流程信息,检查ProcessType
  1026. var process = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
  1027. .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0) ?? throw new BusinessException("关联的流程不存在。");
  1028. // 3. 节点操作验证
  1029. ValidateConfNodeOperation(node, processStatus);
  1030. // 4. 存储更新前的值
  1031. var before = new Grp_ConfProcessNode()
  1032. {
  1033. Id = node.Id,
  1034. ProcessId = node.ProcessId,
  1035. NodeName = node.NodeName,
  1036. NodeDescTips = node.NodeDescTips,
  1037. NodeOrder = node.NodeOrder,
  1038. Participator = node.Participator,
  1039. OverallStatus = node.OverallStatus,
  1040. Operator = node.Operator,
  1041. OperationTime = node.OperationTime,
  1042. IsCurrent = node.IsCurrent,
  1043. ActualDone = node.ActualDone,
  1044. IsFileUp = node.IsFileUp,
  1045. };
  1046. // 5. 更新节点状态
  1047. node.OverallStatus = processStatus;
  1048. node.Operator = currUserId;
  1049. node.OperationTime = DateTime.Now;
  1050. var updateCount = await _sqlSugar.Updateable(node)
  1051. .UpdateColumns(n => new
  1052. {
  1053. n.OverallStatus,
  1054. n.Operator,
  1055. n.OperationTime
  1056. })
  1057. .ExecuteCommandAsync();
  1058. if (updateCount == 0)
  1059. {
  1060. throw new BusinessException("节点状态更新失败。");
  1061. }
  1062. // 6. 记录节点日志
  1063. await LogConfNodeOpAsync(before, node, "Update", currUserId);
  1064. // 7. 如果是完成当前节点,处理流程流转
  1065. if (processStatus == ProcessStatus.Completed)
  1066. {
  1067. await ConfProcessCurrentNodeCompletionAsync(node, currUserId);
  1068. }
  1069. return new Result { Code = StatusCodes.Status200OK, Msg = "操作成功。" };
  1070. });
  1071. return result.IsSuccess ? result.Data : new Result
  1072. {
  1073. Code = StatusCodes.Status500InternalServerError,
  1074. Msg = result.ErrorMessage
  1075. };
  1076. }
  1077. catch (BusinessException ex)
  1078. {
  1079. // 业务异常
  1080. return new Result { Code = StatusCodes.Status400BadRequest, Msg = ex.Message };
  1081. }
  1082. catch (Exception ex)
  1083. {
  1084. // 系统异常
  1085. return new Result { Code = StatusCodes.Status500InternalServerError, Msg = "系统错误,请稍后重试" };
  1086. }
  1087. }
  1088. /// <summary>
  1089. /// 验证会务流程节点操作权限
  1090. /// </summary>
  1091. /// <param name="node">流程节点</param>
  1092. /// <param name="targetStatus">目标状态</param>
  1093. private static void ValidateConfNodeOperation(Grp_ConfProcessNode node, ProcessStatus targetStatus)
  1094. {
  1095. // 验证节点是否已完成
  1096. if (node.OverallStatus == ProcessStatus.Completed)
  1097. {
  1098. throw new BusinessException("当前节点已完成,不可重复操作。");
  1099. }
  1100. // 验证状态流转是否合法(可选)
  1101. //if (targetStatus != ProcessStatus.Completed)
  1102. //{
  1103. // throw new BusinessException("未开始或者进行中的节点只能重新完成,不可进行其他操作。");
  1104. //}
  1105. // 验证是否尝试将已完成节点改为其他状态
  1106. if (node.OverallStatus == ProcessStatus.Completed && targetStatus != ProcessStatus.Completed)
  1107. {
  1108. throw new BusinessException("已完成节点不可修改状态。");
  1109. }
  1110. }
  1111. /// <summary>
  1112. /// 处理当前会务流程节点完成后的流程流转
  1113. /// </summary>
  1114. private async Task ConfProcessCurrentNodeCompletionAsync(Grp_ConfProcessNode currentNode, int currUserId)
  1115. {
  1116. // 1. 获取流程信息
  1117. var process = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
  1118. .FirstAsync(p => p.Id == currentNode.ProcessId && p.IsDel == 0) ?? throw new BusinessException("关联的流程不存在。");
  1119. var processBefore = new Grp_ConfProcessOverview()
  1120. {
  1121. Id = process.Id,
  1122. GroupId = process.GroupId,
  1123. ProcessOrder = process.ProcessOrder,
  1124. ProcessType = process.ProcessType,
  1125. OverallStatus = process.OverallStatus,
  1126. StartTime = process.StartTime,
  1127. EndTime = process.EndTime,
  1128. UpdatedUserId = process.UpdatedUserId,
  1129. UpdatedTime = process.UpdatedTime
  1130. };
  1131. // 2. 取消当前节点的当前状态
  1132. var before = new Grp_ConfProcessNode()
  1133. {
  1134. Id = currentNode.Id,
  1135. ProcessId = currentNode.ProcessId,
  1136. NodeName = currentNode.NodeName,
  1137. NodeOrder = currentNode.NodeOrder,
  1138. OverallStatus = currentNode.OverallStatus,
  1139. Operator = currentNode.Operator,
  1140. OperationTime = currentNode.OperationTime,
  1141. IsCurrent = currentNode.IsCurrent,
  1142. };
  1143. currentNode.IsCurrent = false;
  1144. await _sqlSugar.Updateable(currentNode)
  1145. .UpdateColumns(n => new { n.IsCurrent })
  1146. .ExecuteCommandAsync();
  1147. // 2.1 记录节点日志 取消当前节点状态
  1148. await LogConfNodeOpAsync(before, currentNode, "Update", currUserId);
  1149. // 3. 查找并激活下一个节点
  1150. var nextNode = await _sqlSugar.Queryable<Grp_ConfProcessNode>()
  1151. .Where(n => n.ProcessId == currentNode.ProcessId
  1152. && n.NodeOrder == currentNode.NodeOrder + 1
  1153. && n.IsDel == 0)
  1154. .FirstAsync();
  1155. if (nextNode != null)
  1156. {
  1157. var nextNodeBefore = new Grp_ConfProcessNode()
  1158. {
  1159. Id = nextNode.Id,
  1160. ProcessId = nextNode.ProcessId,
  1161. NodeName = nextNode.NodeName,
  1162. NodeOrder = nextNode.NodeOrder,
  1163. OverallStatus = nextNode.OverallStatus,
  1164. Operator = nextNode.Operator,
  1165. OperationTime = nextNode.OperationTime,
  1166. IsCurrent = nextNode.IsCurrent,
  1167. };
  1168. // 激活下一个节点
  1169. nextNode.IsCurrent = true;
  1170. nextNode.OverallStatus = ProcessStatus.InProgress;
  1171. var updateCount = await _sqlSugar.Updateable(nextNode)
  1172. .UpdateColumns(n => new
  1173. {
  1174. n.IsCurrent,
  1175. n.OverallStatus,
  1176. n.Operator,
  1177. n.OperationTime
  1178. })
  1179. .ExecuteCommandAsync();
  1180. if (updateCount == 0)
  1181. {
  1182. throw new BusinessException("激活下一节点失败");
  1183. }
  1184. // 1.1 记录节点日志 激活下一节点当前节点状态
  1185. await LogConfNodeOpAsync(nextNodeBefore, nextNode, "Start", currUserId);
  1186. // 更新流程状态为进行中
  1187. process.OverallStatus = ProcessStatus.InProgress;
  1188. }
  1189. else
  1190. {
  1191. // 下一节点不存在,整个流程完成
  1192. process.OverallStatus = ProcessStatus.Completed;
  1193. process.EndTime = DateTime.Now;
  1194. }
  1195. // 4. 更新流程信息
  1196. process.UpdatedUserId = currUserId;
  1197. process.UpdatedTime = DateTime.Now;
  1198. var processUpdateCount = await _sqlSugar.Updateable(process)
  1199. .UpdateColumns(p => new
  1200. {
  1201. p.OverallStatus,
  1202. p.EndTime,
  1203. p.UpdatedUserId,
  1204. p.UpdatedTime
  1205. })
  1206. .ExecuteCommandAsync();
  1207. if (processUpdateCount == 0)
  1208. {
  1209. throw new BusinessException("流程状态更新失败。");
  1210. }
  1211. //记录流程日志
  1212. await LogConfProcessOpAsync(processBefore, process, "Update", currUserId);
  1213. }
  1214. /// <summary>
  1215. /// 更新节点信息
  1216. /// </summary>
  1217. /// <param name="dto">签证节点更新数据传输对象</param>
  1218. /// <returns>操作结果</returns>
  1219. public async Task<Result> SetNodeInfoAsync(ConfProcessSetActualDoneDto dto)
  1220. {
  1221. //参与人验证
  1222. if (dto.Participators?.Count < 1)
  1223. {
  1224. throw new BusinessException("参与人不能为空。");
  1225. }
  1226. int nodeId = dto.NodeId;
  1227. var isDtNul = DateTime.TryParse(dto.ActualDone, out DateTime actualDone);
  1228. int currUserId = dto.CurrUserId;
  1229. bool isFileUp = dto.IsFileUp;
  1230. // 1. 获取并验证节点和流程
  1231. var node = await _sqlSugar.Queryable<Grp_ConfProcessNode>()
  1232. .FirstAsync(n => n.Id == nodeId && n.IsDel == 0)
  1233. ?? throw new BusinessException("当前节点不存在或已被删除。");
  1234. var process = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
  1235. .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0)
  1236. ?? throw new BusinessException("当前流程不存在或已被删除。");
  1237. // 2.1 存储更新前流程及节点信息
  1238. var nodeBefore = new Grp_ConfProcessNode()
  1239. {
  1240. Id = node.Id,
  1241. ProcessId = node.ProcessId,
  1242. NodeName = node.NodeName,
  1243. NodeOrder = node.NodeOrder,
  1244. OverallStatus = node.OverallStatus,
  1245. Operator = node.Operator,
  1246. OperationTime = node.OperationTime,
  1247. IsCurrent = node.IsCurrent
  1248. };
  1249. if (isDtNul)
  1250. {
  1251. node.ActualDone = actualDone;
  1252. }
  1253. node.IsFileUp = isFileUp;
  1254. node.Participator = JsonConvert.SerializeObject(node.Participators.Select(x => x.UserId).ToList());
  1255. // 3. 保存节点更新
  1256. await _sqlSugar.Updateable(node)
  1257. .UpdateColumns(n => new
  1258. {
  1259. ActualDone = isDtNul ? node.ActualDone : null,
  1260. n.Participator,
  1261. n.IsFileUp,
  1262. })
  1263. .ExecuteCommandAsync();
  1264. //记录节点日志
  1265. await LogConfNodeOpAsync(nodeBefore, node, "Update", currUserId);
  1266. return new Result { Code = 200, Msg = "实际操作时间设置成功。" };
  1267. }
  1268. #endregion
  1269. #region 操作日志
  1270. #region 团组流程
  1271. /// <summary>
  1272. /// 记录流程操作日志
  1273. /// </summary>
  1274. /// <param name="before">操作前</param>
  1275. /// <param name="after">操作后</param>
  1276. /// <param name="opType">操作类型(Create - 创建、Update - 更新、Complete - 完成)</param>
  1277. /// <param name="operId">操作人ID</param>
  1278. /// <returns>异步任务</returns>
  1279. public async Task LogProcessOpAsync(Grp_ProcessOverview before, Grp_ProcessOverview after, string opType, int operId)
  1280. {
  1281. var chgDetails = GetProcessChgDetails(before, after);
  1282. var log = new Grp_ProcessLog
  1283. {
  1284. ProcessId = after?.Id ?? before?.Id,
  1285. GroupId = after?.GroupId ?? before?.GroupId ?? 0,
  1286. OpType = opType,
  1287. OpDesc = GenerateProcessOpDesc(opType, before, after, chgDetails),
  1288. BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  1289. AfterData = after != null ? JsonConvert.SerializeObject(after, GetJsonSettings()) : null,
  1290. ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
  1291. CreateUserId = operId
  1292. };
  1293. await _sqlSugar.Insertable(log).ExecuteCommandAsync();
  1294. }
  1295. /// <summary>
  1296. /// 记录节点操作日志
  1297. /// </summary>
  1298. /// <param name="before">操作前</param>
  1299. /// <param name="after">操作后</param>
  1300. /// <param name="opType">操作类型(Create - 创建、Update - 更新、Start - 启动、Complete - 完成)</param>
  1301. /// <param name="operId">操作人ID</param>
  1302. /// <returns>异步任务</returns>
  1303. public async Task LogNodeOpAsync(Grp_ProcessNode before, Grp_ProcessNode after, string opType, int operId)
  1304. {
  1305. var chgDetails = GetNodeChgDetails(before, after);
  1306. var log = new Grp_ProcessLog
  1307. {
  1308. NodeId = after?.Id ?? before?.Id,
  1309. ProcessId = after?.ProcessId ?? before?.ProcessId,
  1310. GroupId = 0, // 通过流程ID关联获取
  1311. OpType = opType,
  1312. OpDesc = GenerateNodeOpDesc(opType, before, after, chgDetails),
  1313. BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  1314. AfterData = after != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  1315. ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
  1316. CreateUserId = operId
  1317. };
  1318. await _sqlSugar.Insertable(log).ExecuteCommandAsync();
  1319. }
  1320. /// <summary>
  1321. /// 获取流程变更详情
  1322. /// </summary>
  1323. /// <param name="before">变更前</param>
  1324. /// <param name="after">变更后</param>
  1325. /// <returns>变更详情</returns>
  1326. private List<FieldChgDetail> GetProcessChgDetails(Grp_ProcessOverview before, Grp_ProcessOverview after)
  1327. {
  1328. var chgDetails = new List<FieldChgDetail>();
  1329. if (before == null || after == null) return chgDetails;
  1330. var props = typeof(Grp_ProcessOverview).GetProperties(BindingFlags.Public | BindingFlags.Instance)
  1331. .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
  1332. foreach (var prop in props)
  1333. {
  1334. var beforeVal = prop.GetValue(before);
  1335. var afterVal = prop.GetValue(after);
  1336. if (!Equals(beforeVal, afterVal))
  1337. {
  1338. chgDetails.Add(new FieldChgDetail
  1339. {
  1340. FieldName = prop.Name,
  1341. BeforeValue = FormatVal(beforeVal),
  1342. AfterValue = FormatVal(afterVal)
  1343. });
  1344. }
  1345. }
  1346. return chgDetails;
  1347. }
  1348. /// <summary>
  1349. /// 获取节点变更详情
  1350. /// </summary>
  1351. /// <param name="before">变更前</param>
  1352. /// <param name="after">变更后</param>
  1353. /// <returns>变更详情</returns>
  1354. private List<FieldChgDetail> GetNodeChgDetails(Grp_ProcessNode before, Grp_ProcessNode after)
  1355. {
  1356. var chgDetails = new List<FieldChgDetail>();
  1357. if (before == null || after == null) return chgDetails;
  1358. var props = typeof(Grp_ProcessNode).GetProperties(BindingFlags.Public | BindingFlags.Instance)
  1359. .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
  1360. foreach (var prop in props)
  1361. {
  1362. var beforeVal = prop.GetValue(before);
  1363. var afterVal = prop.GetValue(after);
  1364. if (!Equals(beforeVal, afterVal))
  1365. {
  1366. chgDetails.Add(new FieldChgDetail
  1367. {
  1368. FieldName = prop.Name,
  1369. BeforeValue = FormatVal(beforeVal),
  1370. AfterValue = FormatVal(afterVal)
  1371. });
  1372. }
  1373. }
  1374. return chgDetails;
  1375. }
  1376. /// <summary>
  1377. /// 生成流程操作描述
  1378. /// </summary>
  1379. /// <param name="opType">操作类型</param>
  1380. /// <param name="before">操作前</param>
  1381. /// <param name="after">操作后</param>
  1382. /// <param name="chgDetails">变更详情</param>
  1383. /// <returns>操作描述</returns>
  1384. private string GenerateProcessOpDesc(string opType, Grp_ProcessOverview before,
  1385. Grp_ProcessOverview after, List<FieldChgDetail> chgDetails)
  1386. {
  1387. var processType = after?.ProcessType ?? before?.ProcessType;
  1388. var processName = GetProcessTypeName(processType);
  1389. if (!chgDetails.Any())
  1390. {
  1391. return opType switch
  1392. {
  1393. "Create" => $"创建流程:{processName}",
  1394. "Update" => $"更新流程:{processName} - 无变更",
  1395. "Start" => $"启动流程:{processName}",
  1396. "Complete" => $"完成流程:{processName}",
  1397. "Delete" => $"删除流程:{processName}",
  1398. _ => $"{opType}:{processName}"
  1399. };
  1400. }
  1401. var chgDesc = string.Join("; ", chgDetails.Select(x =>
  1402. $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
  1403. return $"{GetOpTypeDisplay(opType)}:{processName} - {chgDesc}";
  1404. }
  1405. /// <summary>
  1406. /// 生成节点操作描述
  1407. /// </summary>
  1408. /// <param name="opType">操作类型</param>
  1409. /// <param name="before">操作前</param>
  1410. /// <param name="after">操作后</param>
  1411. /// <param name="chgDetails">变更详情</param>
  1412. /// <returns>操作描述</returns>
  1413. private string GenerateNodeOpDesc(string opType, Grp_ProcessNode before,
  1414. Grp_ProcessNode after, List<FieldChgDetail> chgDetails)
  1415. {
  1416. var nodeName = after?.NodeName ?? before?.NodeName;
  1417. if (!chgDetails.Any())
  1418. {
  1419. return opType switch
  1420. {
  1421. "Create" => $"创建节点:{nodeName}",
  1422. "Update" => $"更新节点:{nodeName} - 无变更",
  1423. "Start" => $"启动节点:{nodeName}",
  1424. "Complete" => $"完成节点:{nodeName}",
  1425. //"Delete" => $"删除节点:{nodeName}",
  1426. _ => $"{opType}:{nodeName}"
  1427. };
  1428. }
  1429. var chgDesc = string.Join("; ", chgDetails.Select(x =>
  1430. $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
  1431. return $"{GetOpTypeDisplay(opType)}:{nodeName} - {chgDesc}";
  1432. }
  1433. /// <summary>
  1434. /// 获取流程类型名称
  1435. /// </summary>
  1436. /// <param name="processType">流程类型</param>
  1437. /// <returns>流程名称</returns>
  1438. private static string GetProcessTypeName(GroupProcessType? processType)
  1439. {
  1440. return processType switch
  1441. {
  1442. GroupProcessType.Invitation => "商邀报批",
  1443. GroupProcessType.Visa => "签证",
  1444. GroupProcessType.AirTicket => "机票",
  1445. GroupProcessType.Hotel => "酒店",
  1446. GroupProcessType.LocalGuide => "地接",
  1447. GroupProcessType.FeeSettle => "费用结算",
  1448. _ => "未知流程"
  1449. };
  1450. }
  1451. /// <summary>
  1452. /// 获取流程日志
  1453. /// </summary>
  1454. /// <param name="processId">流程ID</param>
  1455. /// <returns>日志列表</returns>
  1456. public async Task<List<Grp_ProcessLog>> GetProcessLogsAsync(int processId)
  1457. {
  1458. return await _sqlSugar.Queryable<Grp_ProcessLog>()
  1459. .Where(x => x.ProcessId == processId)
  1460. .OrderByDescending(x => x.CreateTime)
  1461. .ToListAsync();
  1462. }
  1463. /// <summary>
  1464. /// 获取团组流程日志
  1465. /// </summary>
  1466. /// <param name="groupId">团组ID</param>
  1467. /// <returns>日志列表</returns>
  1468. public async Task<List<Grp_ProcessLog>> GetGroupLogsAsync(int groupId)
  1469. {
  1470. return await _sqlSugar.Queryable<Grp_ProcessLog>()
  1471. .Where(x => x.GroupId == groupId)
  1472. .OrderByDescending(x => x.CreateTime)
  1473. .ToListAsync();
  1474. }
  1475. #endregion
  1476. #region 会务流程
  1477. /// <summary>
  1478. /// 记录会务流程操作日志
  1479. /// </summary>
  1480. /// <param name="before">操作前</param>
  1481. /// <param name="after">操作后</param>
  1482. /// <param name="opType">操作类型(Create - 创建、Update - 更新、Complete - 完成)</param>
  1483. /// <param name="operId">操作人ID</param>
  1484. /// <returns>异步任务</returns>
  1485. public async Task LogConfProcessOpAsync(Grp_ConfProcessOverview before, Grp_ConfProcessOverview after, string opType, int operId)
  1486. {
  1487. var chgDetails = GetConfProcessChgDetails(before, after);
  1488. var log = new Grp_ConfProcessLog
  1489. {
  1490. ProcessId = after?.Id ?? before?.Id,
  1491. GroupId = after?.GroupId ?? before?.GroupId ?? 0,
  1492. OpType = opType,
  1493. OpDesc = GenerateConfProcessOpDesc(opType, before, after, chgDetails),
  1494. BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  1495. AfterData = after != null ? JsonConvert.SerializeObject(after, GetJsonSettings()) : null,
  1496. ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
  1497. CreateUserId = operId
  1498. };
  1499. await _sqlSugar.Insertable(log).ExecuteCommandAsync();
  1500. }
  1501. /// <summary>
  1502. /// 记录会务节点操作日志
  1503. /// </summary>
  1504. /// <param name="before">操作前</param>
  1505. /// <param name="after">操作后</param>
  1506. /// <param name="opType">操作类型(Create - 创建、Update - 更新、Start - 启动、Complete - 完成)</param>
  1507. /// <param name="operId">操作人ID</param>
  1508. /// <returns>异步任务</returns>
  1509. public async Task LogConfNodeOpAsync(Grp_ConfProcessNode before, Grp_ConfProcessNode after, string opType, int operId)
  1510. {
  1511. var chgDetails = GetConfNodeChgDetails(before, after);
  1512. var log = new Grp_ConfProcessLog
  1513. {
  1514. NodeId = after?.Id ?? before?.Id,
  1515. ProcessId = after?.ProcessId ?? before?.ProcessId,
  1516. GroupId = 0, // 通过流程ID关联获取
  1517. OpType = opType,
  1518. OpDesc = GenerateConfNodeOpDesc(opType, before, after, chgDetails),
  1519. BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  1520. AfterData = after != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  1521. ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
  1522. CreateUserId = operId
  1523. };
  1524. await _sqlSugar.Insertable(log).ExecuteCommandAsync();
  1525. }
  1526. /// <summary>
  1527. /// 获取流程变更详情
  1528. /// </summary>
  1529. /// <param name="before">变更前</param>
  1530. /// <param name="after">变更后</param>
  1531. /// <returns>变更详情</returns>
  1532. private static List<FieldChgDetail> GetConfProcessChgDetails(Grp_ConfProcessOverview before, Grp_ConfProcessOverview after)
  1533. {
  1534. var chgDetails = new List<FieldChgDetail>();
  1535. if (before == null || after == null) return chgDetails;
  1536. var props = typeof(Grp_ConfProcessOverview).GetProperties(BindingFlags.Public | BindingFlags.Instance)
  1537. .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
  1538. foreach (var prop in props)
  1539. {
  1540. var beforeVal = prop.GetValue(before);
  1541. var afterVal = prop.GetValue(after);
  1542. if (!Equals(beforeVal, afterVal))
  1543. {
  1544. chgDetails.Add(new FieldChgDetail
  1545. {
  1546. FieldName = prop.Name,
  1547. BeforeValue = FormatVal(beforeVal),
  1548. AfterValue = FormatVal(afterVal)
  1549. });
  1550. }
  1551. }
  1552. return chgDetails;
  1553. }
  1554. /// <summary>
  1555. /// 获取节点变更详情
  1556. /// </summary>
  1557. /// <param name="before">变更前</param>
  1558. /// <param name="after">变更后</param>
  1559. /// <returns>变更详情</returns>
  1560. private static List<FieldChgDetail> GetConfNodeChgDetails(Grp_ConfProcessNode before, Grp_ConfProcessNode after)
  1561. {
  1562. var chgDetails = new List<FieldChgDetail>();
  1563. if (before == null || after == null) return chgDetails;
  1564. var props = typeof(Grp_ConfProcessNode).GetProperties(BindingFlags.Public | BindingFlags.Instance)
  1565. .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
  1566. foreach (var prop in props)
  1567. {
  1568. var beforeVal = prop.GetValue(before);
  1569. var afterVal = prop.GetValue(after);
  1570. if (!Equals(beforeVal, afterVal))
  1571. {
  1572. chgDetails.Add(new FieldChgDetail
  1573. {
  1574. FieldName = prop.Name,
  1575. BeforeValue = FormatVal(beforeVal),
  1576. AfterValue = FormatVal(afterVal)
  1577. });
  1578. }
  1579. }
  1580. return chgDetails;
  1581. }
  1582. /// <summary>
  1583. /// 生成流程操作描述
  1584. /// </summary>
  1585. /// <param name="opType">操作类型</param>
  1586. /// <param name="before">操作前</param>
  1587. /// <param name="after">操作后</param>
  1588. /// <param name="chgDetails">变更详情</param>
  1589. /// <returns>操作描述</returns>
  1590. private static string GenerateConfProcessOpDesc(string opType, Grp_ConfProcessOverview before,
  1591. Grp_ConfProcessOverview after, List<FieldChgDetail> chgDetails)
  1592. {
  1593. var processType = after?.ProcessType ?? before?.ProcessType;
  1594. var processName = GetConfProcessTypeName(processType);
  1595. if (!chgDetails.Any())
  1596. {
  1597. return opType switch
  1598. {
  1599. "Create" => $"创建流程:{processName}",
  1600. "Update" => $"更新流程:{processName} - 无变更",
  1601. "Start" => $"启动流程:{processName}",
  1602. "Complete" => $"完成流程:{processName}",
  1603. "Delete" => $"删除流程:{processName}",
  1604. _ => $"{opType}:{processName}"
  1605. };
  1606. }
  1607. var chgDesc = string.Join("; ", chgDetails.Select(x =>
  1608. $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
  1609. return $"{GetOpTypeDisplay(opType)}:{processName} - {chgDesc}";
  1610. }
  1611. /// <summary>
  1612. /// 生成节点操作描述
  1613. /// </summary>
  1614. /// <param name="opType">操作类型</param>
  1615. /// <param name="before">操作前</param>
  1616. /// <param name="after">操作后</param>
  1617. /// <param name="chgDetails">变更详情</param>
  1618. /// <returns>操作描述</returns>
  1619. private static string GenerateConfNodeOpDesc(string opType, Grp_ConfProcessNode before,
  1620. Grp_ConfProcessNode after, List<FieldChgDetail> chgDetails)
  1621. {
  1622. var nodeName = after?.NodeName ?? before?.NodeName;
  1623. if (!chgDetails.Any())
  1624. {
  1625. return opType switch
  1626. {
  1627. "Create" => $"创建节点:{nodeName}",
  1628. "Update" => $"更新节点:{nodeName} - 无变更",
  1629. "Start" => $"启动节点:{nodeName}",
  1630. "Complete" => $"完成节点:{nodeName}",
  1631. //"Delete" => $"删除节点:{nodeName}",
  1632. _ => $"{opType}:{nodeName}"
  1633. };
  1634. }
  1635. var chgDesc = string.Join("; ", chgDetails.Select(x =>
  1636. $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
  1637. return $"{GetOpTypeDisplay(opType)}:{nodeName} - {chgDesc}";
  1638. }
  1639. /// <summary>
  1640. /// 获取流程类型名称
  1641. /// </summary>
  1642. /// <param name="processType">流程类型</param>
  1643. /// <returns>流程名称</returns>
  1644. private static string GetConfProcessTypeName(ConfProcessType? processType)
  1645. {
  1646. return processType switch
  1647. {
  1648. ConfProcessType.Conference => "会务",
  1649. _ => "未知流程"
  1650. };
  1651. }
  1652. /// <summary>
  1653. /// 获取会务流程日志
  1654. /// </summary>
  1655. /// <param name="processId">流程ID</param>
  1656. /// <returns>日志列表</returns>
  1657. public async Task<List<Grp_ConfProcessLog>> GetConfProcessLogsAsync(int processId)
  1658. {
  1659. return await _sqlSugar.Queryable<Grp_ConfProcessLog>()
  1660. .Where(x => x.ProcessId == processId)
  1661. .OrderByDescending(x => x.CreateTime)
  1662. .ToListAsync();
  1663. }
  1664. /// <summary>
  1665. /// 获取团组会务流程日志
  1666. /// </summary>
  1667. /// <param name="groupId">团组ID</param>
  1668. /// <returns>日志列表</returns>
  1669. public async Task<List<Grp_ConfProcessLog>> GetGroupConfLogsAsync(int groupId)
  1670. {
  1671. return await _sqlSugar.Queryable<Grp_ConfProcessLog>()
  1672. .Where(x => x.GroupId == groupId)
  1673. .OrderByDescending(x => x.CreateTime)
  1674. .ToListAsync();
  1675. }
  1676. #endregion
  1677. #region 日志私有方法
  1678. /// <summary>
  1679. /// 获取JSON序列化设置
  1680. /// </summary>
  1681. /// <returns>JSON设置</returns>
  1682. private static JsonSerializerSettings GetJsonSettings()
  1683. {
  1684. return new JsonSerializerSettings
  1685. {
  1686. ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
  1687. NullValueHandling = NullValueHandling.Ignore,
  1688. DateFormatString = "yyyy-MM-dd HH:mm:ss",
  1689. Formatting = Formatting.None
  1690. };
  1691. }
  1692. /// <summary>
  1693. /// 获取操作类型显示
  1694. /// </summary>
  1695. /// <param name="opType">操作类型</param>
  1696. /// <returns>显示名称</returns>
  1697. private static string GetOpTypeDisplay(string opType)
  1698. {
  1699. return opType switch
  1700. {
  1701. "Create" => "创建",
  1702. "Update" => "更新",
  1703. "Start" => "启动",
  1704. "Complete" => "完成",
  1705. "Delete" => "删除",
  1706. "StatusChg" => "状态变更",
  1707. _ => opType
  1708. };
  1709. }
  1710. /// <summary>
  1711. /// 获取字段显示名称
  1712. /// </summary>
  1713. /// <param name="fieldName">字段名</param>
  1714. /// <returns>显示名称</returns>
  1715. private static string GetFieldDisplayName(string fieldName)
  1716. {
  1717. return fieldName switch
  1718. {
  1719. "OverallStatus" => "状态",
  1720. "ProcessOrder" => "流程顺序",
  1721. "StartTime" => "开始时间",
  1722. "EndTime" => "结束时间",
  1723. "UpdatedUserId" => "更新人",
  1724. "UpdatedTime" => "更新时间",
  1725. "NodeOrder" => "节点顺序",
  1726. "NodeName" => "节点名称",
  1727. "NodeDescTips" => "节点描述提示",
  1728. "IsCurrent" => "当前节点",
  1729. "Participator" => "参与人",
  1730. "Operator" => "操作人",
  1731. "OperationTime" => "操作时间",
  1732. "ActualDone" => "实际完成时间",
  1733. "IsFileUp" => "是否上传文件(签证、机票、酒店、地接 流程结尾节点使用)",
  1734. "IsAssist" => "是否协助(财务流程首节点使用)",
  1735. "IsPart" => "是否参与(商邀 第五步使用)",
  1736. _ => fieldName
  1737. };
  1738. }
  1739. /// <summary>
  1740. /// 格式化值显示
  1741. /// </summary>
  1742. /// <param name="value">值</param>
  1743. /// <returns>格式化值</returns>
  1744. private static string FormatVal(object value)
  1745. {
  1746. if (value == null) return "空";
  1747. if (value is ProcessStatus status)
  1748. {
  1749. return status switch
  1750. {
  1751. ProcessStatus.UnStarted => "未开始",
  1752. ProcessStatus.InProgress => "进行中",
  1753. ProcessStatus.Completed => "已完成",
  1754. _ => status.ToString()
  1755. };
  1756. }
  1757. if (value is bool boolVal) return boolVal ? "是" : "否";
  1758. if (value is DateTime dateVal) return dateVal.ToString("yyyy-MM-dd HH:mm");
  1759. var strVal = value.ToString();
  1760. return string.IsNullOrEmpty(strVal) ? "空" : strVal;
  1761. }
  1762. /// <summary>
  1763. /// 检查是否排除字段
  1764. /// </summary>
  1765. /// <param name="fieldName">字段名</param>
  1766. /// <returns>是否排除</returns>
  1767. private static bool IsExclField(string fieldName)
  1768. {
  1769. var exclFields = new List<string>
  1770. {
  1771. "Id", "CreateTime", "CreateUserId", "Nodes", "Process" // 导航属性
  1772. };
  1773. return exclFields.Contains(fieldName);
  1774. }
  1775. #endregion
  1776. #endregion
  1777. }
  1778. }