ProcessOverviewRepository.cs 92 KB

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