ProcessOverviewRepository.cs 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826
  1. using Aspose.Words;
  2. using AutoMapper;
  3. using EyeSoft.Extensions;
  4. using Microsoft.Extensions.Logging;
  5. using Newtonsoft.Json;
  6. using OASystem.Domain;
  7. using OASystem.Domain.Dtos.Groups;
  8. using OASystem.Domain.Entities.Groups;
  9. using OASystem.Domain.Entities.Resource;
  10. using OASystem.Domain.ViewModels.Groups;
  11. using Org.BouncyCastle.Asn1.X500;
  12. using System.Diagnostics;
  13. using System.Drawing;
  14. using System.Reflection;
  15. using System.Runtime.Intrinsics.Arm;
  16. using System.Text.RegularExpressions;
  17. namespace OASystem.Infrastructure.Repositories.Groups
  18. {
  19. /// <summary>
  20. /// 团组流程总览表仓储
  21. /// </summary>
  22. public class ProcessOverviewRepository : BaseRepository<Grp_ProcessOverview, Grp_ProcessOverview>
  23. {
  24. private readonly IMapper _mapper;
  25. private readonly DelegationInfoRepository _groupRep;
  26. private readonly ILogger<ProcessOverviewRepository> _logger;
  27. public ProcessOverviewRepository(SqlSugarClient sqlSugar, IMapper mapper, ILogger<ProcessOverviewRepository> logger, DelegationInfoRepository groupRep) : base(sqlSugar)
  28. {
  29. _mapper = mapper;
  30. _groupRep = groupRep;
  31. _logger = logger;
  32. }
  33. #region 团组流程
  34. /// <summary>
  35. /// 基础数据初始化-团组流程
  36. /// </summary>
  37. /// <param name="groupId"></param>
  38. /// <param name="currUserId"></param>
  39. /// <returns></returns>
  40. public async Task<List<Grp_ProcessOverview>> ProcessDataInitAsync(int groupId, int currUserId, List<string> visaCountries)
  41. {
  42. var processs = new List<Grp_ProcessOverview>();
  43. // 团组验证
  44. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
  45. if (groupInfo == null) return processs;
  46. // 流程验证
  47. var existingProcesses = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  48. .Where(p => p.IsDel == 0 && p.GroupId == groupId)
  49. .ToListAsync();
  50. if (existingProcesses.Any()) return processs;
  51. var users = await _sqlSugar.Queryable<Sys_Users>()
  52. .LeftJoin<Sys_Company>((u, c) => u.CompanyId == c.Id)
  53. .LeftJoin<Sys_Department>((u, c, d) => u.DepId == d.Id)
  54. .LeftJoin<Sys_JobPost>((u, c, d, jp) => u.JobPostId == jp.Id)
  55. .Where((u, c, d, jp) => u.IsDel == 0)
  56. .Select((u, c, d, jp) => new {
  57. u.Id,
  58. u.CnName,
  59. u.CompanyId,
  60. c.CompanyName,
  61. u.DepId,
  62. d.DepName,
  63. u.JobPostId,
  64. jp.JobName,
  65. })
  66. .ToListAsync();
  67. #region 商邀报批流程
  68. string oaNode1Tips = NodeTipsMsg(groupInfo, GroupProcessType.Invitation, 1);
  69. string oaNode2Tips = NodeTipsMsg(groupInfo, GroupProcessType.Invitation, 2);
  70. string oaNode3Tips = NodeTipsMsg(groupInfo, GroupProcessType.Invitation, 3);
  71. string oaNode4Tips = NodeTipsMsg(groupInfo, GroupProcessType.Invitation, 4);
  72. string oaNode5Tips = NodeTipsMsg(groupInfo, GroupProcessType.Invitation, 5);
  73. string oaNode6Tips = NodeTipsMsg(groupInfo, GroupProcessType.Invitation, 6);
  74. string oaNode7Tips = NodeTipsMsg(groupInfo, GroupProcessType.Invitation, 7);
  75. //节点可操作用户列表
  76. var oaNodeOpUsers = users.Where(u =>
  77. u.JobName != null && u.JobName.Contains("商邀主管")
  78. ).Select(u => u.Id).ToList();
  79. processs.Add(
  80. Grp_ProcessOverview.Create(groupId, 1, GroupProcessType.Invitation, ProcessStatus.InProgress, currUserId,
  81. new List<Grp_ProcessNode>()
  82. {
  83. Grp_ProcessNode.Create(1, "报批基础资料准备",oaNode1Tips,ProcessStatus.InProgress, true,false,false,false,currUserId,oaNodeOpUsers),
  84. Grp_ProcessNode.Create(2, "报批邀请函资料准备",oaNode2Tips, ProcessStatus.InProgress, false,false,false,false,currUserId,oaNodeOpUsers),
  85. Grp_ProcessNode.Create(3, "获得批件",oaNode3Tips,ProcessStatus.InProgress, false,false,false,false, currUserId ,oaNodeOpUsers),
  86. Grp_ProcessNode.Create(4, "对接公务",oaNode4Tips,ProcessStatus.InProgress, false,false,false,false, currUserId,oaNodeOpUsers),
  87. Grp_ProcessNode.Create(5, "参与翻译对接",oaNode5Tips,ProcessStatus.InProgress, false,false,false,true, currUserId,oaNodeOpUsers),
  88. Grp_ProcessNode.Create(6, "商邀文案配合",oaNode6Tips,ProcessStatus.InProgress, false,false,false,false, currUserId,oaNodeOpUsers),
  89. Grp_ProcessNode.Create(7, "票据上传(相关票据)",oaNode7Tips,ProcessStatus.InProgress, false,false,true,false, currUserId,oaNodeOpUsers),
  90. }));
  91. #endregion
  92. #region 签证流程
  93. //节点可操作用户列表
  94. var visaNodeOpUsers = users.Where(u =>
  95. u.JobName != null && u.JobName.Contains("签证")
  96. ).Select(u => u.Id).ToList();
  97. //单独处理签证流程节点
  98. var visaNodes = new List<Grp_ProcessNode>();
  99. if (visaCountries != null && visaCountries.Count > 0)
  100. {
  101. var visaDefualtNodes = new List<VisaProcessNode>();
  102. for (int i = 1; i < visaCountries.Count + 1; i++)
  103. {
  104. visaDefualtNodes.Add(VisaProcessNode.Info(i, visaCountries[i - 1].ToString()));
  105. }
  106. string visaNode1Tips = NodeTipsMsg(groupInfo, GroupProcessType.Visa, 1);
  107. string visaNode2Tips = NodeTipsMsg(groupInfo, GroupProcessType.Visa, 2);
  108. visaNodes.Add(Grp_ProcessNode.Create(1, "签证信息", visaNode1Tips, ProcessStatus.InProgress, true, false, false, false, currUserId, visaNodeOpUsers, JsonConvert.SerializeObject(visaDefualtNodes)));
  109. visaNodes.Add(Grp_ProcessNode.Create(2, "票据上传(明细表、费用票据、保单及超支费用账单)", visaNode2Tips, ProcessStatus.InProgress, false, false, true, false, currUserId, oaNodeOpUsers));
  110. }
  111. processs.Add(Grp_ProcessOverview.Create(groupId, 2, GroupProcessType.Visa, ProcessStatus.UnStarted, currUserId, visaNodes));
  112. #endregion
  113. #region 机票流程
  114. string airNode1Tips = NodeTipsMsg(groupInfo, GroupProcessType.AirTicket, 1);
  115. string airNode2Tips = NodeTipsMsg(groupInfo, GroupProcessType.AirTicket, 2);
  116. string airNode3Tips = NodeTipsMsg(groupInfo, GroupProcessType.AirTicket, 3);
  117. string airNode4Tips = NodeTipsMsg(groupInfo, GroupProcessType.AirTicket, 4);
  118. string airNode5Tips = NodeTipsMsg(groupInfo, GroupProcessType.AirTicket, 5);
  119. string airNode6Tips = NodeTipsMsg(groupInfo, GroupProcessType.AirTicket, 6);
  120. string airNode7Tips = NodeTipsMsg(groupInfo, GroupProcessType.AirTicket, 7);
  121. string airNode8Tips = NodeTipsMsg(groupInfo, GroupProcessType.AirTicket, 8);
  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, "机票占位、续位", airNode2Tips, ProcessStatus.InProgress, false,false,false,false,currUserId,airNodeOpUsers ),
  132. Grp_ProcessNode.Create(3, "完成机票采购确认", airNode3Tips, ProcessStatus.InProgress,false,false,false,false, currUserId, airNodeOpUsers),
  133. Grp_ProcessNode.Create(4, "进行出票操作并核查信息", airNode4Tips, ProcessStatus.InProgress, false,false,false,false, currUserId, airNodeOpUsers),
  134. Grp_ProcessNode.Create(5, "机票已出", airNode5Tips, ProcessStatus.InProgress, false,false,false,false, currUserId, airNodeOpUsers),
  135. Grp_ProcessNode.Create(6, "完成机票选座", airNode6Tips, ProcessStatus.InProgress, false,false,false,false,currUserId, airNodeOpUsers),
  136. Grp_ProcessNode.Create(7, "票据上传(机票超支费用账单)", airNode7Tips, ProcessStatus.InProgress, false,false,true,false, currUserId,airNodeOpUsers),
  137. Grp_ProcessNode.Create(8, "票据上传", airNode8Tips, ProcessStatus.InProgress, false,false,true,false, currUserId, airNodeOpUsers)
  138. }
  139. )
  140. );
  141. #endregion
  142. #region 酒店流程
  143. string hotelNode1Tips = NodeTipsMsg(groupInfo, GroupProcessType.Hotel, 1);
  144. string hotelNode2Tips = NodeTipsMsg(groupInfo, GroupProcessType.Hotel, 2);
  145. string hotelNode3Tips = NodeTipsMsg(groupInfo, GroupProcessType.Hotel, 3);
  146. string hotelNode4Tips = NodeTipsMsg(groupInfo, GroupProcessType.Hotel, 4);
  147. string hotelNode5Tips = NodeTipsMsg(groupInfo, GroupProcessType.Hotel, 5);
  148. //节点可操作用户列表
  149. var hotelNodeOpUsers = users.Where(u =>
  150. u.JobName != null && u.JobName.Contains("酒店")
  151. ).Select(u => u.Id).ToList();
  152. processs.Add(
  153. Grp_ProcessOverview.Create(groupId, 4, GroupProcessType.Hotel, ProcessStatus.InProgress, currUserId,
  154. new List<Grp_ProcessNode>()
  155. {
  156. Grp_ProcessNode.Create(1, "按照预算,询价、比价、谈价", hotelNode1Tips, ProcessStatus.InProgress, true, false, false, false, currUserId, hotelNodeOpUsers),
  157. Grp_ProcessNode.Create(2, "获取酒店确认函与入住名单核对", hotelNode2Tips, ProcessStatus.InProgress, false, false, false,false, currUserId , hotelNodeOpUsers),
  158. Grp_ProcessNode.Create(3, "预订酒店并录入OA", hotelNode3Tips, ProcessStatus.InProgress,false, false, false,false,currUserId , hotelNodeOpUsers),
  159. Grp_ProcessNode.Create(4, "行前再次确认酒店相关情况", hotelNode4Tips,ProcessStatus.InProgress, false, false, false,false,currUserId , hotelNodeOpUsers),
  160. Grp_ProcessNode.Create(5, "行程结束后整理酒店发票与结算", hotelNode5Tips, ProcessStatus.InProgress, false, false, true,false, currUserId ,hotelNodeOpUsers),
  161. }
  162. )
  163. );
  164. #endregion
  165. #region 地接流程
  166. string opNode1Tips = NodeTipsMsg(groupInfo, GroupProcessType.LocalGuide, 1);
  167. string opNode2Tips = NodeTipsMsg(groupInfo, GroupProcessType.LocalGuide, 2);
  168. string opNode3Tips = NodeTipsMsg(groupInfo, GroupProcessType.LocalGuide, 3);
  169. string opNode4Tips = NodeTipsMsg(groupInfo, GroupProcessType.LocalGuide, 4);
  170. string opNode5Tips = NodeTipsMsg(groupInfo, GroupProcessType.LocalGuide, 5);
  171. string opNode6Tips = NodeTipsMsg(groupInfo, GroupProcessType.LocalGuide, 6);
  172. string opNode7Tips = NodeTipsMsg(groupInfo, GroupProcessType.LocalGuide, 7);
  173. //节点可操作用户列表
  174. var opNodeOpUsers = users.Where(u =>
  175. u.JobName != null && u.JobName.Equals("OP")
  176. ).Select(u => u.Id).ToList();
  177. processs.Add(
  178. Grp_ProcessOverview.Create(groupId, 5, GroupProcessType.LocalGuide, ProcessStatus.InProgress, currUserId,
  179. new List<Grp_ProcessNode>()
  180. {
  181. Grp_ProcessNode.Create(1,"根据机票方案出框架行程", opNode1Tips,ProcessStatus.InProgress, true, false, false,false,currUserId,opNodeOpUsers),
  182. Grp_ProcessNode.Create(2,"联系并询价地接相关的供应商", opNode2Tips,ProcessStatus.InProgress, false, false, false,false, currUserId,opNodeOpUsers),
  183. Grp_ProcessNode.Create(3,"提交供应商报价及比价表", opNode3Tips, ProcessStatus.InProgress, false, false, false, false,currUserId,opNodeOpUsers),
  184. Grp_ProcessNode.Create(4,"执行采购流程", opNode4Tips, ProcessStatus.InProgress, false, false, false,false, currUserId,opNodeOpUsers),
  185. Grp_ProcessNode.Create(5,"制定最终行程单及出行手册", opNode5Tips, ProcessStatus.InProgress, false, false, false,false, currUserId ,opNodeOpUsers),
  186. Grp_ProcessNode.Create(6,"送机", opNode6Tips, ProcessStatus.InProgress, false, false, false,false, currUserId,opNodeOpUsers ),
  187. Grp_ProcessNode.Create(7,"最终版报批行程、票据上传", opNode7Tips, ProcessStatus.InProgress, false, false, true, false,currUserId,opNodeOpUsers )
  188. }
  189. )
  190. );
  191. #endregion
  192. #region 费用结算流程
  193. var feeNode1Tips = NodeTipsMsg(groupInfo, GroupProcessType.FeeSettle, 1);
  194. var feeNode2Tips = NodeTipsMsg(groupInfo, GroupProcessType.FeeSettle, 2);
  195. var feeNode3Tips = NodeTipsMsg(groupInfo, GroupProcessType.FeeSettle, 3);
  196. var feeNode4Tips = NodeTipsMsg(groupInfo, GroupProcessType.FeeSettle, 4);
  197. //节点可操作用户列表
  198. var feeNodeOpUsers = users.Where(u =>
  199. u.JobName != null && u.JobName.Contains("会计") && u.CnName.Equals("曾艳")
  200. ).Select(u => u.Id).ToList();
  201. processs.Add(
  202. Grp_ProcessOverview.Create(groupId, 6, GroupProcessType.FeeSettle, ProcessStatus.InProgress, currUserId,
  203. new List<Grp_ProcessNode>()
  204. {
  205. Grp_ProcessNode.Create(1, "城市间交通报批金额核定", feeNode1Tips, ProcessStatus.InProgress, true, true, false,false,currUserId,feeNodeOpUsers),
  206. Grp_ProcessNode.Create(2, "团组城市间交通及国际机票数据分配的合理性核对", feeNode2Tips, ProcessStatus.InProgress, false, false, false,false,currUserId,feeNodeOpUsers),
  207. Grp_ProcessNode.Create(3, "整理统计相关财务资料给到各单位", feeNode3Tips, ProcessStatus.InProgress, false, false, false,false,currUserId,feeNodeOpUsers),
  208. Grp_ProcessNode.Create(4, "费用结算完毕", feeNode4Tips, ProcessStatus.InProgress, false, false, false,false, currUserId , feeNodeOpUsers),
  209. }
  210. )
  211. );
  212. #endregion
  213. return processs;
  214. }
  215. /// <summary>
  216. /// 团组流程初始化
  217. /// </summary>
  218. /// <param name="request">创建流程请求参数</param>
  219. /// <returns>创建的流程信息</returns>
  220. public async Task<Result> ProcessInitAsync(int groupId, int currUserId)
  221. {
  222. //团组验证
  223. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
  224. if (groupInfo == null)
  225. {
  226. return new Result { Code = 400, Msg = "团组不存在" };
  227. }
  228. // 检查是否已存在流程
  229. var existingProcesses = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  230. .Where(p => p.IsDel == 0 && p.GroupId == groupId)
  231. .ToListAsync();
  232. if (existingProcesses.Any())
  233. {
  234. return new Result { Code = 400, Msg = "该团组的流程已存在" };
  235. }
  236. //处理签证国家
  237. var visaCountries = _groupRep.GroupSplitCountry(groupInfo.VisitCountry);
  238. // 定义默认的流程节点
  239. var processs = await ProcessDataInitAsync(groupId, currUserId, visaCountries);
  240. _sqlSugar.BeginTran();
  241. foreach (var item in processs)
  242. {
  243. var processId = await _sqlSugar.Insertable(item).ExecuteReturnIdentityAsync();
  244. if (processId < 1)
  245. {
  246. _sqlSugar.RollbackTran();
  247. return new Result { Code = 400, Msg = "团组流程进度总览表添加失败!" };
  248. }
  249. item.Id = processId;
  250. // 记录流程日志
  251. await LogProcessOpAsync(null, item, "Create", currUserId);
  252. var nodes = item.Nodes.Select((nodeDto, index) => new Grp_ProcessNode
  253. {
  254. ProcessId = processId,
  255. NodeName = nodeDto.NodeName,
  256. NodeOrder = nodeDto.NodeOrder,
  257. OverallStatus = nodeDto.OverallStatus,
  258. NodeDescTips = nodeDto.NodeDescTips,
  259. OpUserList = nodeDto.OpUserList,
  260. //Country = nodeDto.Country,
  261. IsCurrent = nodeDto.IsCurrent,
  262. IsAssist = nodeDto.IsAssist,
  263. IsPart = nodeDto.IsPart,
  264. IsFileUp = nodeDto.IsFileUp,
  265. Remark = nodeDto.Remark
  266. }).ToList();
  267. var nodeIds = await _sqlSugar.Insertable(nodes).ExecuteCommandAsync();
  268. if (nodeIds < 1)
  269. {
  270. _sqlSugar.RollbackTran();
  271. return new Result { Code = 400, Msg = "团组流程进度流程节点添加失败!" };
  272. }
  273. //设置节点ID
  274. nodes = await _sqlSugar.Queryable<Grp_ProcessNode>().Where(x => x.IsDel == 0 && x.ProcessId == processId).ToListAsync();
  275. //记录节点日志
  276. foreach (var node in nodes)
  277. {
  278. await LogNodeOpAsync(null, node, "Create", currUserId);
  279. }
  280. }
  281. _sqlSugar.CommitTran();
  282. return new Result { Code = 200, Msg = "添加成功!" }; ;
  283. }
  284. /// <summary>
  285. /// 获取团组的所有流程及节点详情
  286. /// </summary>
  287. /// <param name="request">创建流程请求参数</param>
  288. /// <returns>创建的流程信息</returns>
  289. public async Task<Result> ProcessesDetailsAsync(int groupId)
  290. {
  291. //团组验证
  292. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
  293. if (groupInfo == null)
  294. {
  295. return new Result { Code = 400, Msg = "团组不存在" };
  296. }
  297. // 检查是否已存在流程
  298. var existingProcesses = await _sqlSugar.Queryable<Grp_ProcessOverview>().Where(p => p.IsDel == 0 && p.GroupId == groupId).ToListAsync();
  299. if (!existingProcesses.Any())
  300. {
  301. //新建团组流程
  302. var res = await ProcessInitAsync(groupId, 4);
  303. if (res.Code != 200)
  304. {
  305. return res;
  306. }
  307. }
  308. var users = await _sqlSugar.Queryable<Sys_Users>().Select(x => new { x.Id, x.CnName }).ToListAsync();
  309. var processData = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  310. .Where(p => p.GroupId == groupId && p.IsDel == 0)
  311. .Mapper(p => p.Nodes, p => p.Nodes.First().ProcessId)
  312. .ToListAsync();
  313. // 预先构建用户字典,提升查询性能
  314. var userDict = users.ToDictionary(u => u.Id, u => u.CnName);
  315. var processes = processData.Select(p =>
  316. {
  317. var orderedNodes = p.Nodes.OrderBy(n => n.NodeOrder).ToList();
  318. var totalNodes = orderedNodes.Count;
  319. return new
  320. {
  321. p.Id,
  322. p.GroupId,
  323. p.ProcessType,
  324. ProcessName = p.ProcessType.GetEnumDescription(),
  325. Nodes = orderedNodes.Select((n, index) =>
  326. {
  327. var isLastNode = index == totalNodes - 1;
  328. var isSecondLastNode = index == totalNodes - 2;
  329. var isFifthStep = index == 4;
  330. // 计算按钮状态
  331. bool isEnaAssistBtn = p.ProcessType == GroupProcessType.FeeSettle && n.NodeOrder == 1;
  332. // 文件上传按钮启用规则
  333. bool isEnaFileUpBtn = false;
  334. // 是否参与按钮启用
  335. bool isEnaPartBtn = false;
  336. // 规则1:商邀流程第五步启用参与按钮
  337. if (p.ProcessType == GroupProcessType.Invitation && isFifthStep)
  338. {
  339. isEnaPartBtn = true;
  340. }
  341. // 规则2:机票流程倒数第二步启用上传按钮
  342. else if (p.ProcessType == GroupProcessType.AirTicket && isSecondLastNode)
  343. {
  344. isEnaFileUpBtn = true;
  345. }
  346. // 规则3:默认流程节点最后一步启用上传按钮
  347. else if (isLastNode && p.ProcessType != GroupProcessType.FeeSettle)
  348. {
  349. isEnaFileUpBtn = true;
  350. }
  351. // 处理签证子节点
  352. List<VisaProcessNode> visaSubNodes = new();
  353. if (p.ProcessType == GroupProcessType.Visa && n.NodeOrder == 1)
  354. {
  355. visaSubNodes = JsonConvert.DeserializeObject<List<VisaProcessNode>>(n.Remark ?? "[]")
  356. ?? new List<VisaProcessNode>();
  357. }
  358. // 获取操作人姓名(使用字典提升性能)
  359. string operatorName = "-";
  360. if (n.Operator.HasValue && userDict.TryGetValue(n.Operator.Value, out var name))
  361. {
  362. operatorName = name;
  363. }
  364. string nodeTipsMsg = NodeTipsMsg(groupInfo, p.ProcessType, n.NodeOrder);
  365. return new
  366. {
  367. n.Id,
  368. n.ProcessId,
  369. n.NodeOrder,
  370. n.NodeName,
  371. n.OverallStatus,
  372. StatusText = n.OverallStatus.GetEnumDescription(),
  373. Operator = operatorName,
  374. OpeateTime = n.OperationTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? "-",
  375. ActualDone = n.ActualDone?.ToString("yyyy-MM-dd HH:mm:ss") ?? "",
  376. n.OpUserList, //可操作用户列表
  377. NodeDescTips = nodeTipsMsg,
  378. isEnaAssistBtn, // 是否启用财务流程首节点协助按钮
  379. n.IsAssist, // 财务流程首节点 存储值
  380. isEnaFileUpBtn, // 是否启用上传文件按钮
  381. n.IsFileUp, // 票据上传节点 存储值
  382. isEnaPartBtn, // 是否启用参与按钮
  383. n.IsPart, // 参与按钮 存储值
  384. visaSubNodes // 签证节点类型使用
  385. };
  386. }).ToList()
  387. };
  388. }).ToList();
  389. return new Result { Code = 200, Data = processes, Msg = "查询成功!" };
  390. }
  391. /// <summary>
  392. /// 节点提示消息
  393. /// </summary>
  394. /// <param name="groupInfo"></param>
  395. /// <param name="procType"></param>
  396. /// <param name="nodeOrder"></param>
  397. /// <returns></returns>
  398. private string NodeTipsMsg(Grp_DelegationInfo groupInfo, GroupProcessType procType, int nodeOrder)
  399. {
  400. string msg = string.Empty;
  401. int groupId = groupInfo.Id;
  402. switch (procType)
  403. {
  404. case GroupProcessType.Invitation:
  405. switch (nodeOrder)
  406. {
  407. case 1:
  408. msg = "更新报批行程和请示,提供其他报批所需材料,4个工作日内完成。";
  409. break;
  410. case 2:
  411. var custInfo = _sqlSugar.Queryable<Grp_TourClientList>()
  412. .Where(c => c.DiId == groupId && c.IsDel == 0)
  413. .OrderByDescending(c => c.CreateTime)
  414. .First();
  415. msg = "客户提供完整名单后,2周内取得邀请函(翻译件)。";
  416. if (custInfo != null)
  417. {
  418. msg = $"请于{custInfo.CreateTime.AddDays(14):yyyy年MM月dd日}内完成该项工作(客户提供完整名单后,2周内取得邀请函(翻译件))";
  419. }
  420. break;
  421. case 3:
  422. msg = "提供完整的报批全套资源。";
  423. break;
  424. case 4:
  425. msg = $"请于{groupInfo.VisitDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(按进度实际公务活动落实情况,出发前5日落实公务)";
  426. break;
  427. case 5:
  428. break;
  429. case 6:
  430. break;
  431. case 7:
  432. //msg = $"请于{groupInfo.VisitEndDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(团组结束前完成)";
  433. msg = $"请于{groupInfo.VisitEndDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(如果需要上传请在团组结束前完成)";
  434. break;
  435. }
  436. break;
  437. case GroupProcessType.Visa:
  438. switch (nodeOrder)
  439. {
  440. case 1:
  441. break;
  442. case 2:
  443. msg = $"请于{groupInfo.VisitDate:yyyy年MM月dd日}内完成该项工作(按进度实际签证办理落实情况,团组出发前上传票据。)";
  444. break;
  445. }
  446. break;
  447. case GroupProcessType.AirTicket:
  448. switch (nodeOrder)
  449. {
  450. case 1:
  451. msg = "建团后打勾确认出团的时候开始24小时内。";
  452. if (groupInfo.Step == 1 || groupInfo.Step == 2)
  453. {
  454. if (groupInfo.StepOperationTime.HasValue)
  455. {
  456. msg = $"请于{groupInfo.StepOperationTime.Value.AddDays(1):yyyy年MM月dd日}内完成该项工作(建团后打勾确认出团的时候开始24小时内)";
  457. }
  458. }
  459. break;
  460. case 2:
  461. break;
  462. case 3:
  463. msg = $"完成机票采购确认(含预算核对、出票确认等)";
  464. break;
  465. case 4:
  466. break;
  467. case 5:
  468. msg = $"请于{groupInfo.VisitDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(团组出发前5日)";
  469. break;
  470. case 6:
  471. break;
  472. case 7:
  473. msg = $"请于{groupInfo.VisitEndDate.AddDays(5):yyyy年MM月dd日}内完成该项工作(机票蓝联打票及上传机票超支费用账单,团组归国后5个工作日内)";
  474. break;
  475. case 8:
  476. msg = $"1. 票据上传(机票报销蓝联、行程单及机票说明) \r\n 2. 请于{groupInfo.VisitEndDate.AddDays(10):yyyy年MM月dd日}内完成该项工作(团组归国后10个工作日内) *按机票报价*0.999折扣出具机票报销蓝联、行程单及机票说明";
  477. break;
  478. }
  479. break;
  480. case GroupProcessType.Hotel:
  481. switch (nodeOrder)
  482. {
  483. case 1:
  484. msg = "1. 筛选并按照预算标准,对目标酒店进行询价、比价、谈价 \r\n2. 建团后打勾确认出团的时候开始2个工作日。";
  485. if (groupInfo.Step == 1 || groupInfo.Step == 2)
  486. {
  487. if (groupInfo.StepOperationTime.HasValue)
  488. {
  489. msg = $"请于{groupInfo.StepOperationTime.Value.AddDays(2):yyyy年MM月dd日}内完成该项工作(建团后打勾确认出团的时候开始2个工作日)";
  490. }
  491. }
  492. break;
  493. case 2:
  494. break;
  495. case 3:
  496. break;
  497. case 4:
  498. msg = $"1.行前再次确认酒店订单、付款状态及入住安排 \r\n 2.请于{groupInfo.VisitDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(团组出发前5天)";
  499. break;
  500. case 5:
  501. msg = $"1.行程结束后整理酒店发票(含超支费用发票)与结算 \r\n 2.请于{groupInfo.VisitEndDate.AddDays(5):yyyy年MM月dd日}内完成该项工作(团组结束后5天内)";
  502. break;
  503. }
  504. break;
  505. case GroupProcessType.LocalGuide:
  506. switch (nodeOrder)
  507. {
  508. case 1:
  509. var airTripCodeInfo = _sqlSugar.Queryable<Air_TicketBlackCode>()
  510. .Where(x => x.IsDel == 0 && x.DiId == groupId)
  511. .OrderByDescending(x => x.CreateTime)
  512. .First();
  513. msg = $"机票行程代码最后一段录入后1个工作日内。";
  514. if (airTripCodeInfo != null)
  515. {
  516. msg = $"请于{airTripCodeInfo.CreateTime.AddDays(1):yyyy年MM月dd日}内完成该项工作(机票行程代码最后一段录入后1个工作日内)";
  517. }
  518. break;
  519. case 2:
  520. msg = $"1.联系并询价地接、餐厅、用车、景点等供应商 \r\n 2. 请于{groupInfo.CreateTime.AddDays(7):yyyy年MM月dd日}内完成该项工作(建团完成后7个工作日内)";
  521. break;
  522. case 3:
  523. msg = $"请于{groupInfo.CreateTime.AddDays(10):yyyy年MM月dd日}内完成该项工作(上一步往后3个工作日内)";
  524. break;
  525. case 4:
  526. msg = $"请于{groupInfo.CreateTime.AddDays(12):yyyy年MM月dd日}内完成该项工作(上一步往后2个工作日内)";
  527. break;
  528. case 5:
  529. var backListInfo = _sqlSugar.Queryable<Grp_InvertedList>().Where(x => x.DiId == groupId && x.IsDel == 0).First();
  530. msg = $"1.制定最终《行程单》及《出行手册》 \r\n2. 倒推表里开行前会 -3天。";
  531. if (backListInfo != null)
  532. {
  533. if (DateTime.TryParse(backListInfo.PreTripMeetingDt, out DateTime dateTime))
  534. {
  535. msg = $"请于{dateTime.AddDays(-3):yyyy年MM月dd日}内完成该项工作(倒推表里开行前会 -3天)";
  536. }
  537. }
  538. break;
  539. case 6:
  540. break;
  541. case 7:
  542. msg = $"请于{groupInfo.VisitEndDate.AddDays(5):yyyy年MM月dd日}内完成该项工作(团组归国后5个工作日内) *上传最终报批行程,确定城市间交通最终版报价分配;地接账单(清楚标注超时及其他项超支费用)、地接交通费用原始票据、城市间交通明细表;";
  543. break;
  544. }
  545. break;
  546. case GroupProcessType.FeeSettle:
  547. switch (nodeOrder)
  548. {
  549. case 1:
  550. msg = "团组报批前";
  551. break;
  552. case 2:
  553. msg = "团组报批前三公费用表";
  554. break;
  555. case 3:
  556. msg = $"1.整理统计团组超支费用、三公报销资料给到各单位 \r\n 2. 请于{groupInfo.VisitEndDate.AddDays(12):yyyy年MM月dd日}内完成该项工作(团组归国后12个工作日内)";
  557. break;
  558. case 4:
  559. break;
  560. }
  561. break;
  562. }
  563. return msg;
  564. }
  565. #region 状态变更 可操作任意节点 进行中<-->已完成 双向变更
  566. /// <summary>
  567. /// 更新节点状态(支持任意节点状态变更,无需处理当前节点)
  568. /// </summary>
  569. /// <param name="nodeId"></param>
  570. /// <param name="currUserId"></param>
  571. /// <param name="targetStatus"></param>
  572. /// <returns></returns>
  573. public async Task<Result> UpdateNodeStatusSimpleAsync(int nodeId, int currUserId, ProcessStatus targetStatus = ProcessStatus.Completed)
  574. {
  575. try
  576. {
  577. var result = await _sqlSugar.Ado.UseTranAsync(async () =>
  578. {
  579. // 1. 获取节点和流程信息
  580. var node = await _sqlSugar.Queryable<Grp_ProcessNode>()
  581. .FirstAsync(n => n.Id == nodeId && n.IsDel == 0)
  582. ?? throw new BusinessException("当前节点不存在或已被删除。");
  583. // 2. 用户权限验证
  584. if (!HasNodeOperationPermission(node, currUserId))
  585. {
  586. throw new BusinessException("当前用户没有操作此节点的权限。");
  587. }
  588. var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  589. .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0)
  590. ?? throw new BusinessException("关联的流程不存在。");
  591. // 3. 检查是否重复操作
  592. if (node.OverallStatus == targetStatus)
  593. {
  594. throw new BusinessException($"当前节点已为{GetStatusDescription(targetStatus)}状态,无需重复操作。");
  595. }
  596. // 4. 存储更新前的值
  597. var nodeBefore = CloneNode(node);
  598. var processBefore = CloneProcess(process);
  599. // 5. 更新节点状态
  600. node.OverallStatus = targetStatus;
  601. node.Operator = currUserId;
  602. node.OperationTime = DateTime.Now;
  603. // 6. 保存节点更新
  604. await _sqlSugar.Updateable(node)
  605. .UpdateColumns(n => new
  606. {
  607. n.OverallStatus,
  608. n.Operator,
  609. n.OperationTime
  610. })
  611. .ExecuteCommandAsync();
  612. // 7. 更新流程状态(基于所有节点的状态计算)
  613. await UpdateProcessOverallStatusAsync(process, currUserId);
  614. // 8. 记录日志
  615. await LogNodeOpAsync(nodeBefore, node, "Update", currUserId);
  616. await LogProcessOpAsync(processBefore, process, "Update", currUserId);
  617. return new Result { Code = StatusCodes.Status200OK, Msg = "操作成功。" };
  618. });
  619. return result.IsSuccess ? result.Data : new Result
  620. {
  621. Code = StatusCodes.Status500InternalServerError,
  622. Msg = result.ErrorMessage
  623. };
  624. }
  625. catch (BusinessException ex)
  626. {
  627. return new Result { Code = StatusCodes.Status400BadRequest, Msg = ex.Message };
  628. }
  629. catch (Exception ex)
  630. {
  631. return new Result { Code = StatusCodes.Status500InternalServerError, Msg = "系统错误,请稍后重试" };
  632. }
  633. }
  634. /// <summary>
  635. /// 更新流程整体状态(根据所有节点状态计算)
  636. /// </summary>
  637. /// <param name="process"></param>
  638. /// <param name="currUserId"></param>
  639. /// <returns></returns>
  640. private async Task UpdateProcessOverallStatusAsync(Grp_ProcessOverview process, int currUserId)
  641. {
  642. // 获取所有节点
  643. var allNodes = await _sqlSugar.Queryable<Grp_ProcessNode>()
  644. .Where(n => n.ProcessId == process.Id && n.IsDel == 0)
  645. .ToListAsync();
  646. // 统计节点状态
  647. int totalCount = allNodes.Count;
  648. int completedCount = allNodes.Count(n => n.OverallStatus == ProcessStatus.Completed);
  649. int inProgressCount = allNodes.Count(n => n.OverallStatus == ProcessStatus.InProgress);
  650. // 判断流程状态
  651. ProcessStatus newProcessStatus;
  652. if (completedCount == totalCount)
  653. {
  654. // 所有节点都已完成
  655. newProcessStatus = ProcessStatus.Completed;
  656. process.EndTime = DateTime.Now;
  657. }
  658. else if (inProgressCount > 0 || completedCount > 0)
  659. {
  660. // 有节点进行中或已完成
  661. newProcessStatus = ProcessStatus.InProgress;
  662. process.EndTime = null;
  663. }
  664. else
  665. {
  666. // 所有节点都未开始
  667. newProcessStatus = ProcessStatus.UnStarted;
  668. process.EndTime = null;
  669. }
  670. // 更新流程状态(只有状态变化时才更新)
  671. if (process.OverallStatus != newProcessStatus)
  672. {
  673. process.OverallStatus = newProcessStatus;
  674. process.UpdatedUserId = currUserId;
  675. process.UpdatedTime = DateTime.Now;
  676. await _sqlSugar.Updateable(process)
  677. .UpdateColumns(p => new
  678. {
  679. p.OverallStatus,
  680. p.EndTime,
  681. p.UpdatedUserId,
  682. p.UpdatedTime
  683. })
  684. .ExecuteCommandAsync();
  685. }
  686. }
  687. /// <summary>
  688. /// 验证用户是否有操作节点的权限
  689. /// </summary>
  690. private static bool HasNodeOperationPermission(Grp_ProcessNode node, int userId)
  691. {
  692. // 如果 OpUserList 为空或 null,表示不限制权限
  693. if (node.OpUserList == null || node.OpUserList.Count == 0)
  694. {
  695. return true;
  696. }
  697. // 检查当前用户是否在权限列表中
  698. return node.OpUserList.Contains(userId);
  699. }
  700. /// <summary>
  701. /// 克隆节点对象
  702. /// </summary>
  703. /// <param name="node"></param>
  704. /// <returns></returns>
  705. private static Grp_ProcessNode CloneNode(Grp_ProcessNode node)
  706. {
  707. return new Grp_ProcessNode()
  708. {
  709. Id = node.Id,
  710. ProcessId = node.ProcessId,
  711. NodeName = node.NodeName,
  712. NodeOrder = node.NodeOrder,
  713. OverallStatus = node.OverallStatus,
  714. Operator = node.Operator,
  715. OperationTime = node.OperationTime,
  716. IsCurrent = node.IsCurrent,
  717. };
  718. }
  719. /// <summary>
  720. /// 克隆流程对象
  721. /// </summary>
  722. /// <param name="process"></param>
  723. /// <returns></returns>
  724. private static Grp_ProcessOverview CloneProcess(Grp_ProcessOverview process)
  725. {
  726. return new Grp_ProcessOverview()
  727. {
  728. Id = process.Id,
  729. GroupId = process.GroupId,
  730. ProcessOrder = process.ProcessOrder,
  731. ProcessType = process.ProcessType,
  732. OverallStatus = process.OverallStatus,
  733. StartTime = process.StartTime,
  734. EndTime = process.EndTime,
  735. UpdatedUserId = process.UpdatedUserId,
  736. UpdatedTime = process.UpdatedTime
  737. };
  738. }
  739. #endregion
  740. #region 状态变更 流程导向
  741. /// <summary>
  742. /// 更新节点状态
  743. /// </summary>
  744. public async Task<Result> UpdateNodeStatusAsync(int nodeId, int currUserId, ProcessStatus processStatus = ProcessStatus.Completed)
  745. {
  746. try
  747. {
  748. // 使用事务确保数据一致性
  749. var result = await _sqlSugar.Ado.UseTranAsync(async () =>
  750. {
  751. // 1. 获取并验证节点
  752. var node = await _sqlSugar.Queryable<Grp_ProcessNode>()
  753. .FirstAsync(n => n.Id == nodeId && n.IsDel == 0)
  754. ?? throw new BusinessException("当前节点不存在或已被删除。");
  755. // 2. 获取流程信息
  756. var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  757. .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0)
  758. ?? throw new BusinessException("关联的流程不存在。");
  759. // 3. 验证节点操作
  760. ValidateNodeOperation(node, processStatus);
  761. // 4. 存储更新前的值
  762. var nodeBefore = new Grp_ProcessNode()
  763. {
  764. Id = node.Id,
  765. ProcessId = node.ProcessId,
  766. NodeName = node.NodeName,
  767. NodeOrder = node.NodeOrder,
  768. OverallStatus = node.OverallStatus,
  769. Operator = node.Operator,
  770. OperationTime = node.OperationTime,
  771. IsCurrent = node.IsCurrent,
  772. };
  773. var processBefore = new Grp_ProcessOverview()
  774. {
  775. Id = process.Id,
  776. GroupId = process.GroupId,
  777. ProcessOrder = process.ProcessOrder,
  778. ProcessType = process.ProcessType,
  779. OverallStatus = process.OverallStatus,
  780. StartTime = process.StartTime,
  781. EndTime = process.EndTime,
  782. UpdatedUserId = process.UpdatedUserId,
  783. UpdatedTime = process.UpdatedTime
  784. };
  785. // 5. 处理特殊状态流转逻辑
  786. if (node.OverallStatus == ProcessStatus.Completed && processStatus == ProcessStatus.InProgress)
  787. {
  788. // 从已完成回退到进行中
  789. await HandleRollbackToInProgressAsync(node, process, currUserId);
  790. }
  791. else
  792. {
  793. // 正常状态更新
  794. await HandleNormalStatusUpdateAsync(node, process, processStatus, currUserId);
  795. }
  796. // 6. 记录日志
  797. await LogNodeOpAsync(nodeBefore, node, "Update", currUserId);
  798. await LogProcessOpAsync(processBefore, process, "Update", currUserId);
  799. return new Result { Code = StatusCodes.Status200OK, Msg = "操作成功。" };
  800. });
  801. return result.IsSuccess ? result.Data : new Result
  802. {
  803. Code = StatusCodes.Status500InternalServerError,
  804. Msg = result.ErrorMessage
  805. };
  806. }
  807. catch (BusinessException ex)
  808. {
  809. return new Result { Code = StatusCodes.Status400BadRequest, Msg = ex.Message };
  810. }
  811. catch (Exception ex)
  812. {
  813. return new Result { Code = StatusCodes.Status500InternalServerError, Msg = "系统错误,请稍后重试" };
  814. }
  815. }
  816. /// <summary>
  817. /// 处理从已完成回退到进行中的逻辑
  818. /// </summary>
  819. private async Task HandleRollbackToInProgressAsync(Grp_ProcessNode node, Grp_ProcessOverview process, int currUserId)
  820. {
  821. // 1. 检查是否可以回退
  822. if (process.OverallStatus == ProcessStatus.Completed && process.ProcessType != GroupProcessType.Invitation)
  823. {
  824. throw new BusinessException("整个流程已完成,无法回退单个节点状态。");
  825. }
  826. // 2. 取消其他当前节点,设置当前节点为当前节点
  827. await ClearOtherCurrentNodesAsync(node.ProcessId, node.Id, currUserId);
  828. // 3. 更新节点状态
  829. node.OverallStatus = ProcessStatus.InProgress;
  830. node.Operator = currUserId;
  831. node.OperationTime = DateTime.Now;
  832. node.IsCurrent = true;
  833. await _sqlSugar.Updateable(node)
  834. .UpdateColumns(n => new
  835. {
  836. n.OverallStatus,
  837. n.Operator,
  838. n.OperationTime,
  839. n.IsCurrent
  840. })
  841. .ExecuteCommandAsync();
  842. // 4. 更新流程状态为进行中(如果已完成的流程回退)
  843. if (process.OverallStatus == ProcessStatus.Completed)
  844. {
  845. process.OverallStatus = ProcessStatus.InProgress;
  846. process.EndTime = null; // 清空完成时间
  847. process.UpdatedUserId = currUserId;
  848. process.UpdatedTime = DateTime.Now;
  849. await _sqlSugar.Updateable(process)
  850. .UpdateColumns(p => new
  851. {
  852. p.OverallStatus,
  853. p.EndTime,
  854. p.UpdatedUserId,
  855. p.UpdatedTime
  856. })
  857. .ExecuteCommandAsync();
  858. }
  859. }
  860. /// <summary>
  861. /// 处理正常状态更新逻辑
  862. /// </summary>
  863. private async Task HandleNormalStatusUpdateAsync(Grp_ProcessNode node, Grp_ProcessOverview process, ProcessStatus newStatus, int currUserId)
  864. {
  865. // 1. 更新节点基础信息
  866. node.OverallStatus = newStatus;
  867. node.Operator = currUserId;
  868. node.OperationTime = DateTime.Now;
  869. // 2. 根据新状态处理节点和流程逻辑
  870. if (newStatus == ProcessStatus.InProgress)
  871. {
  872. // 设置为进行中:需要成为当前节点
  873. await ClearOtherCurrentNodesAsync(node.ProcessId, node.Id, currUserId);
  874. node.IsCurrent = true;
  875. // 更新流程状态为进行中
  876. process.OverallStatus = ProcessStatus.InProgress;
  877. process.UpdatedUserId = currUserId;
  878. process.UpdatedTime = DateTime.Now;
  879. }
  880. else if (newStatus == ProcessStatus.Completed)
  881. {
  882. // 设置为已完成:取消当前节点状态
  883. node.IsCurrent = false;
  884. // 如果是当前节点完成,需要处理流程流转
  885. if (node.IsCurrent || process.ProcessType == GroupProcessType.Invitation)
  886. {
  887. await HandleNodeCompletionAsync(node, process, currUserId);
  888. }
  889. else
  890. {
  891. // 非当前节点完成,检查流程状态
  892. await CheckAndUpdateProcessStatusAsync(process, currUserId);
  893. }
  894. }
  895. else if (newStatus == ProcessStatus.UnStarted)
  896. {
  897. // 重置为未开始:取消当前节点状态
  898. node.IsCurrent = false;
  899. // 更新流程状态
  900. await CheckAndUpdateProcessStatusAsync(process, currUserId);
  901. }
  902. // 3. 保存节点更新
  903. await _sqlSugar.Updateable(node)
  904. .UpdateColumns(n => new
  905. {
  906. n.OverallStatus,
  907. n.Operator,
  908. n.OperationTime,
  909. n.IsCurrent
  910. })
  911. .ExecuteCommandAsync();
  912. // 4. 保存流程更新
  913. await _sqlSugar.Updateable(process)
  914. .UpdateColumns(p => new
  915. {
  916. p.OverallStatus,
  917. p.EndTime,
  918. p.UpdatedUserId,
  919. p.UpdatedTime
  920. })
  921. .ExecuteCommandAsync();
  922. }
  923. /// <summary>
  924. /// 处理节点完成后的流程流转(替代原来的 ProcessCurrentNodeCompletionAsync)
  925. /// </summary>
  926. private async Task HandleNodeCompletionAsync(Grp_ProcessNode currentNode, Grp_ProcessOverview process, int currUserId)
  927. {
  928. // 1. 获取所有节点
  929. var allNodes = await _sqlSugar.Queryable<Grp_ProcessNode>()
  930. .Where(x => x.IsDel == 0 && x.ProcessId == currentNode.ProcessId)
  931. .ToListAsync();
  932. // 2. 商邀流程特殊处理
  933. if (process.ProcessType == GroupProcessType.Invitation)
  934. {
  935. int completedCount = allNodes.Count(n => n.OverallStatus == ProcessStatus.Completed);
  936. int nodeCount = allNodes.Count;
  937. if (completedCount == nodeCount) // 所有节点完成
  938. {
  939. process.OverallStatus = ProcessStatus.Completed;
  940. process.EndTime = DateTime.Now;
  941. }
  942. else if (completedCount > 0)
  943. {
  944. process.OverallStatus = ProcessStatus.InProgress;
  945. }
  946. }
  947. else
  948. {
  949. // 3. 普通流程:查找下一个节点
  950. var nextNode = allNodes
  951. .Where(n => n.NodeOrder == currentNode.NodeOrder + 1)
  952. .FirstOrDefault();
  953. if (nextNode != null)
  954. {
  955. // 激活下一个节点
  956. var nextNodeBefore = new Grp_ProcessNode()
  957. {
  958. Id = nextNode.Id,
  959. ProcessId = nextNode.ProcessId,
  960. NodeName = nextNode.NodeName,
  961. NodeOrder = nextNode.NodeOrder,
  962. OverallStatus = nextNode.OverallStatus,
  963. Operator = nextNode.Operator,
  964. OperationTime = nextNode.OperationTime,
  965. IsCurrent = nextNode.IsCurrent,
  966. };
  967. nextNode.IsCurrent = true;
  968. nextNode.OverallStatus = ProcessStatus.InProgress;
  969. nextNode.Operator = currUserId;
  970. nextNode.OperationTime = DateTime.Now;
  971. await _sqlSugar.Updateable(nextNode)
  972. .UpdateColumns(n => new
  973. {
  974. n.IsCurrent,
  975. n.OverallStatus,
  976. n.Operator,
  977. n.OperationTime
  978. })
  979. .ExecuteCommandAsync();
  980. await LogNodeOpAsync(nextNodeBefore, nextNode, "Start", currUserId);
  981. process.OverallStatus = ProcessStatus.InProgress;
  982. }
  983. else
  984. {
  985. // 没有下一个节点,流程完成
  986. process.OverallStatus = ProcessStatus.Completed;
  987. process.EndTime = DateTime.Now;
  988. }
  989. }
  990. process.UpdatedUserId = currUserId;
  991. process.UpdatedTime = DateTime.Now;
  992. }
  993. /// <summary>
  994. /// 清除其他当前节点
  995. /// </summary>
  996. private async Task ClearOtherCurrentNodesAsync(int processId, int currentNodeId, int currUserId)
  997. {
  998. var otherCurrentNodes = await _sqlSugar.Queryable<Grp_ProcessNode>()
  999. .Where(n => n.ProcessId == processId
  1000. && n.Id != currentNodeId
  1001. && n.IsCurrent
  1002. && n.IsDel == 0)
  1003. .ToListAsync();
  1004. foreach (var otherNode in otherCurrentNodes)
  1005. {
  1006. var before = new Grp_ProcessNode()
  1007. {
  1008. Id = otherNode.Id,
  1009. ProcessId = otherNode.ProcessId,
  1010. NodeName = otherNode.NodeName,
  1011. NodeOrder = otherNode.NodeOrder,
  1012. OverallStatus = otherNode.OverallStatus,
  1013. Operator = otherNode.Operator,
  1014. OperationTime = otherNode.OperationTime,
  1015. IsCurrent = otherNode.IsCurrent,
  1016. };
  1017. otherNode.IsCurrent = false;
  1018. await _sqlSugar.Updateable(otherNode)
  1019. .UpdateColumns(n => new { n.IsCurrent })
  1020. .ExecuteCommandAsync();
  1021. await LogNodeOpAsync(before, otherNode, "Update", currUserId);
  1022. }
  1023. }
  1024. /// <summary>
  1025. /// 检查并更新流程状态
  1026. /// </summary>
  1027. private async Task CheckAndUpdateProcessStatusAsync(Grp_ProcessOverview process, int currUserId)
  1028. {
  1029. // 获取所有节点状态
  1030. var allNodes = await _sqlSugar.Queryable<Grp_ProcessNode>()
  1031. .Where(x => x.IsDel == 0 && x.ProcessId == process.Id)
  1032. .ToListAsync();
  1033. int totalCount = allNodes.Count;
  1034. int completedCount = allNodes.Count(n => n.OverallStatus == ProcessStatus.Completed);
  1035. int inProgressCount = allNodes.Count(n => n.OverallStatus == ProcessStatus.InProgress);
  1036. // 更新流程状态
  1037. if (completedCount == totalCount)
  1038. {
  1039. process.OverallStatus = ProcessStatus.Completed;
  1040. process.EndTime = DateTime.Now;
  1041. }
  1042. else if (completedCount > 0 || inProgressCount > 0)
  1043. {
  1044. process.OverallStatus = ProcessStatus.InProgress;
  1045. process.EndTime = null;
  1046. }
  1047. else
  1048. {
  1049. process.OverallStatus = ProcessStatus.UnStarted;
  1050. process.EndTime = null;
  1051. }
  1052. process.UpdatedUserId = currUserId;
  1053. process.UpdatedTime = DateTime.Now;
  1054. }
  1055. /// <summary>
  1056. /// 验证节点操作权限
  1057. /// </summary>
  1058. private static void ValidateNodeOperation(Grp_ProcessNode node, ProcessStatus targetStatus)
  1059. {
  1060. // 验证状态流转是否合法
  1061. if (node.OverallStatus == targetStatus)
  1062. {
  1063. throw new BusinessException($"当前节点已为{GetStatusDescription(targetStatus)}状态,无需重复操作。");
  1064. }
  1065. // 允许的流转规则:
  1066. // 1. 未开始 → 进行中 → 已完成(正常流程)
  1067. // 2. 已完成 → 进行中(回退操作)
  1068. // 3. 进行中 → 未开始(重置操作)
  1069. // 禁止的操作:已完成 → 未开始(需要先回到进行中)
  1070. if (node.OverallStatus == ProcessStatus.Completed && targetStatus == ProcessStatus.UnStarted)
  1071. {
  1072. throw new BusinessException("已完成节点不可直接更改为未开始状态,请先更改为进行中状态。");
  1073. }
  1074. }
  1075. private static string GetStatusDescription(ProcessStatus status)
  1076. {
  1077. return status switch
  1078. {
  1079. ProcessStatus.UnStarted => "未开始",
  1080. ProcessStatus.InProgress => "进行中",
  1081. ProcessStatus.Completed => "已完成",
  1082. _ => "未知状态"
  1083. };
  1084. }
  1085. #endregion
  1086. /// <summary>
  1087. /// 更新签证节点信息及状态
  1088. /// </summary>
  1089. /// <param name="dto">签证节点更新数据传输对象</param>
  1090. /// <returns>操作结果</returns>
  1091. public async Task<Result> UpdateVisaNodeDetailsAsync(GroupProcessUpdateVisaNodeDetailsDto dto)
  1092. {
  1093. // 1. 获取并验证节点和流程
  1094. var node = await _sqlSugar.Queryable<Grp_ProcessNode>()
  1095. .FirstAsync(n => n.Id == dto.NodeId && n.IsDel == 0)
  1096. ?? throw new BusinessException("当前节点不存在或已被删除。");
  1097. var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  1098. .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0)
  1099. ?? throw new BusinessException("当前流程不存在或已被删除。");
  1100. if (process.ProcessType != GroupProcessType.Visa)
  1101. {
  1102. throw new BusinessException("当前流程节点不为签证流程,不可编辑。");
  1103. }
  1104. // 2. 检查签证子节点 字段信息是否全部填写
  1105. var allSubNodesCompleted = dto.VisaSubNodes?.All(subNode => EntityExtensions.IsCompleted(subNode)) ?? false;
  1106. // 2.1 存储更新前流程及节点信息
  1107. var nodeBefore = new Grp_ProcessNode()
  1108. {
  1109. Id = node.Id,
  1110. ProcessId = node.ProcessId,
  1111. NodeName = node.NodeName,
  1112. NodeOrder = node.NodeOrder,
  1113. OverallStatus = node.OverallStatus,
  1114. Operator = node.Operator,
  1115. OperationTime = node.OperationTime,
  1116. IsCurrent = node.IsCurrent,
  1117. };
  1118. var processBefore = new Grp_ProcessOverview()
  1119. {
  1120. Id = process.Id,
  1121. GroupId = process.GroupId,
  1122. ProcessOrder = process.ProcessOrder,
  1123. ProcessType = process.ProcessType,
  1124. OverallStatus = process.OverallStatus,
  1125. StartTime = process.StartTime,
  1126. EndTime = process.EndTime,
  1127. UpdatedUserId = process.UpdatedUserId,
  1128. UpdatedTime = process.UpdatedTime
  1129. };
  1130. // 3. 更新节点信息
  1131. node.Remark = JsonConvert.SerializeObject(dto.VisaSubNodes);
  1132. node.Operator = dto.CurrUserId;
  1133. node.OperationTime = DateTime.Now;
  1134. if (allSubNodesCompleted)
  1135. {
  1136. node.OverallStatus = ProcessStatus.Completed;
  1137. process.OverallStatus = ProcessStatus.Completed;
  1138. process.EndTime = DateTime.Now;
  1139. process.UpdatedUserId = dto.CurrUserId;
  1140. process.UpdatedTime = DateTime.Now;
  1141. // 更新流程状态
  1142. await _sqlSugar.Updateable(process)
  1143. .UpdateColumns(p => new
  1144. {
  1145. p.OverallStatus,
  1146. p.EndTime,
  1147. p.UpdatedUserId,
  1148. p.UpdatedTime
  1149. })
  1150. .ExecuteCommandAsync();
  1151. //记录流程日志
  1152. await LogProcessOpAsync(processBefore, process, "Update", dto.CurrUserId);
  1153. }
  1154. // 4. 保存节点更新
  1155. await _sqlSugar.Updateable(node)
  1156. .UpdateColumns(n => new
  1157. {
  1158. n.Remark,
  1159. n.Operator,
  1160. n.OperationTime,
  1161. n.OverallStatus
  1162. })
  1163. .ExecuteCommandAsync();
  1164. //记录节点日志
  1165. await LogNodeOpAsync(nodeBefore, node, "Update", dto.CurrUserId);
  1166. return new Result { Code = 200, Msg = "节点信息更新成功。" };
  1167. }
  1168. /// <summary>
  1169. /// 更新节点信息及状态
  1170. /// </summary>
  1171. /// <param name="dto">更新节点信息及状态</param>
  1172. /// <returns>操作结果</returns>
  1173. public async Task<Result> SetActualDoneAsync(GroupProcessSetActualDoneDto dto)
  1174. {
  1175. int nodeId = dto.NodeId;
  1176. var isDtNul = DateTime.TryParse(dto.ActualDone, out DateTime actualDone);
  1177. int currUserId = dto.CurrUserId;
  1178. bool isAssist = dto.IsAssist;
  1179. bool isFileUp = dto.IsFileUp;
  1180. bool isPart = dto.IsPart;
  1181. // 1. 获取并验证节点和流程
  1182. var node = await _sqlSugar.Queryable<Grp_ProcessNode>()
  1183. .FirstAsync(n => n.Id == nodeId && n.IsDel == 0)
  1184. ?? throw new BusinessException("当前节点不存在或已被删除。");
  1185. // 1.1. 用户权限验证
  1186. if (!HasNodeOperationPermission(node, currUserId))
  1187. {
  1188. throw new BusinessException("当前用户没有操作此节点的权限。");
  1189. }
  1190. var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
  1191. .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0)
  1192. ?? throw new BusinessException("当前流程不存在或已被删除。");
  1193. // 2.1 存储更新前流程及节点信息
  1194. var nodeBefore = CloneNode(node);
  1195. if (isDtNul)
  1196. {
  1197. node.ActualDone = actualDone;
  1198. }
  1199. else node.ActualDone = null;
  1200. node.IsAssist = isAssist;
  1201. node.IsFileUp = isFileUp;
  1202. node.IsPart = isPart;
  1203. // 3. 保存节点更新
  1204. await _sqlSugar.Updateable(node)
  1205. .UpdateColumns(n => new
  1206. {
  1207. n.ActualDone,
  1208. n.IsAssist,
  1209. n.IsFileUp,
  1210. n.IsPart,
  1211. })
  1212. .ExecuteCommandAsync();
  1213. //记录节点日志
  1214. await LogNodeOpAsync(nodeBefore, node, "Update", currUserId);
  1215. //当前节点未完成则更改节点状态为已完成
  1216. if (node.OverallStatus == ProcessStatus.InProgress && isDtNul)
  1217. {
  1218. var statusResult = await UpdateNodeStatusSimpleAsync(node.Id,dto.CurrUserId);
  1219. //日志记录执行结果
  1220. _logger.LogInformation($"团组流程设置完成时间:调用更改状态接口(SetActualDoneAsync -> UpdateNodeStatusAsync):[状态变更:进行中 -> 已完成] result: Code={statusResult.Code}, Msg={statusResult.Msg}");
  1221. }
  1222. //当前节点 实际完成时间设置为空且当前状态为已完成 则更改当前节点状态为进行中
  1223. else if (node.OverallStatus == ProcessStatus.Completed && !isDtNul)
  1224. {
  1225. var statusResult = await UpdateNodeStatusSimpleAsync(node.Id, dto.CurrUserId, ProcessStatus.InProgress);
  1226. //日志记录执行结果
  1227. _logger.LogInformation($"团组流程设置完成时间调:用更改状态接口(SetActualDoneAsync -> UpdateNodeStatusAsync):[状态变更:已完成 -> 进行中] result: Code={statusResult.Code}, Msg={statusResult.Msg}");
  1228. }
  1229. return new Result { Code = 200, Msg = "实际操作时间设置成功。" };
  1230. }
  1231. #endregion
  1232. #region 会务流程
  1233. /// <summary>
  1234. /// 设置节点流程模板
  1235. /// </summary>
  1236. /// <param name="groupId"></param>
  1237. /// <param name="currUserId"></param>
  1238. /// <returns></returns>
  1239. public async Task<List<Grp_ConfProcessOverview>> DefaultConfProcessTemps(int groupId, int currUserId)
  1240. {
  1241. var temps = new List<Grp_ConfProcessOverview>();
  1242. //团组验证
  1243. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
  1244. if (groupInfo == null) return temps;
  1245. //// 检查是否已存在流程
  1246. //var existingProcesses = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
  1247. // .Where(p => p.IsDel == 0 && p.GroupId == groupId)
  1248. // .ToListAsync();
  1249. //if (existingProcesses.Any()) return temps;
  1250. var users = await _sqlSugar.Queryable<Sys_Users>()
  1251. .LeftJoin<Sys_Company>((u, c) => u.CompanyId == c.Id)
  1252. .LeftJoin<Sys_Department>((u, c, d) => u.DepId == d.Id)
  1253. .LeftJoin<Sys_JobPost>((u, c, d, jp) => u.JobPostId == jp.Id)
  1254. .Where((u, c, d, jp) => u.IsDel == 0)
  1255. .Select((u, c, d, jp) => new {
  1256. u.Id,
  1257. u.CnName,
  1258. u.CompanyId,
  1259. c.CompanyName,
  1260. u.DepId,
  1261. d.DepName,
  1262. u.JobPostId,
  1263. jp.JobName,
  1264. })
  1265. .ToListAsync();
  1266. //节点可操作用户列表
  1267. var nodeOpUsers = users.Where(u =>
  1268. u.JobName != null && u.DepName.Contains("策划部")
  1269. ).Select(u => u.Id)
  1270. .ToList();
  1271. #region 会务流程
  1272. //参与人 participators
  1273. var defaultParticipators = new List<int>()
  1274. {
  1275. 213, //李新江
  1276. };
  1277. var defaultPorc1 = new List<Grp_ConfProcessNode>() {
  1278. Grp_ConfProcessNode.Create(1,"方案/报价(含成本)","", ProcessStatus.InProgress,true,false, currUserId,defaultParticipators,nodeOpUsers),
  1279. Grp_ConfProcessNode.Create(2,"项目前期比选/招投标相关文件","", ProcessStatus.InProgress,false,false, currUserId,defaultParticipators,nodeOpUsers),
  1280. Grp_ConfProcessNode.Create(3,"参与投标","", ProcessStatus.InProgress,false,false, currUserId,defaultParticipators,nodeOpUsers),
  1281. Grp_ConfProcessNode.Create(4,"拟定/签订合同","", ProcessStatus.InProgress,false,false, currUserId,defaultParticipators,nodeOpUsers),
  1282. Grp_ConfProcessNode.Create(5,"场地预订/物料设计/对接活动所需的供应商/嘉宾邀约","", ProcessStatus.InProgress,false,false, currUserId,defaultParticipators,nodeOpUsers),
  1283. Grp_ConfProcessNode.Create(6,"现场执行","", ProcessStatus.InProgress,false,false, currUserId,defaultParticipators,nodeOpUsers),
  1284. Grp_ConfProcessNode.Create(7,"验收报告/决算表","", ProcessStatus.InProgress,false,false, currUserId,defaultParticipators,nodeOpUsers),
  1285. Grp_ConfProcessNode.Create(8,"跟进项目收款","", ProcessStatus.InProgress,false,true, currUserId,defaultParticipators,nodeOpUsers),
  1286. //Grp_ConfProcessNode.Create(9,"票据上传","该项目相关票据", ProcessStatus.InProgress,false,true, currUserId,defaultParticipators,nodeOpUsers),
  1287. };
  1288. temps.Add(Grp_ConfProcessOverview.Create(groupId, 1, ProcessStatus.InProgress, currUserId, defaultPorc1));
  1289. var defaultPorc2 = new List<Grp_ConfProcessNode>() {
  1290. Grp_ConfProcessNode.Create(1,"方案/报价(含成本)","", ProcessStatus.InProgress,true,false, currUserId,defaultParticipators,nodeOpUsers),
  1291. Grp_ConfProcessNode.Create(2,"拟定/签订合同","", ProcessStatus.InProgress,false,false, currUserId,defaultParticipators,nodeOpUsers),
  1292. Grp_ConfProcessNode.Create(3,"场地预订/物料设计/对接活动所需的供应商/嘉宾邀约","", ProcessStatus.InProgress,false,false, currUserId,defaultParticipators,nodeOpUsers),
  1293. Grp_ConfProcessNode.Create(4,"现场执行","", ProcessStatus.InProgress,false,false, currUserId,defaultParticipators,nodeOpUsers),
  1294. Grp_ConfProcessNode.Create(5,"验收报告/决算表","", ProcessStatus.InProgress,false,false, currUserId,defaultParticipators,nodeOpUsers),
  1295. Grp_ConfProcessNode.Create(6,"跟进项目收款","", ProcessStatus.InProgress,false,true, currUserId,defaultParticipators,nodeOpUsers),
  1296. //Grp_ConfProcessNode.Create(7,"票据上传","该项目相关票据", ProcessStatus.InProgress,false,true, currUserId,defaultParticipators,nodeOpUsers),
  1297. };
  1298. temps.Add(Grp_ConfProcessOverview.Create(groupId, 2, ProcessStatus.InProgress, currUserId, defaultPorc2));
  1299. #endregion
  1300. return temps;
  1301. }
  1302. /// <summary>
  1303. /// 团组会务流程初始化
  1304. /// </summary>
  1305. /// <param name="groupId">团组Id</param>
  1306. /// <param name="currUserId">当前用户Id</param>
  1307. /// <param name="nodeTempId">节点模板Id</param>
  1308. /// <returns>创建的流程信息</returns>
  1309. public async Task<Result> ConfProcessInitAsync(int groupId, int currUserId,int nodeTempId = 1)
  1310. {
  1311. //团组验证
  1312. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
  1313. if (groupInfo == null)
  1314. {
  1315. return new Result { Code = 400, Msg = "团组不存在" };
  1316. }
  1317. // 检查是否已存在流程
  1318. var existingProcesses = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
  1319. .Where(p => p.IsDel == 0 && p.GroupId == groupId)
  1320. .ToListAsync();
  1321. if (existingProcesses.Any())
  1322. {
  1323. return new Result { Code = 400, Msg = "团组会务流程已存在" };
  1324. }
  1325. // 定义默认的流程节点
  1326. var temps = await DefaultConfProcessTemps(groupId, currUserId);
  1327. if (temps == null || temps.Count < 1)
  1328. return new Result { Code = 400, Msg = "团组会务默认流程不存在" };
  1329. var process = temps.FirstOrDefault(x => x.ProcessOrder == nodeTempId);
  1330. process.CreateUserId = currUserId;
  1331. _sqlSugar.BeginTran();
  1332. try
  1333. {
  1334. var processId = await _sqlSugar.Insertable(process).ExecuteReturnIdentityAsync();
  1335. if (processId < 1)
  1336. {
  1337. _sqlSugar.RollbackTran();
  1338. return new Result { Code = 400, Msg = "团组会务流程进度总览添加失败!" };
  1339. }
  1340. process.Id = processId;
  1341. // 记录流程日志
  1342. await LogConfProcessOpAsync(null, process, "Create", currUserId);
  1343. var nodes = process.Nodes.Select((nodeDto, index) => new Grp_ConfProcessNode
  1344. {
  1345. ProcessId = processId,
  1346. NodeName = nodeDto.NodeName,
  1347. NodeOrder = nodeDto.NodeOrder,
  1348. Participators = nodeDto.Participators,
  1349. OpUserList = nodeDto.OpUserList,
  1350. OverallStatus = nodeDto.OverallStatus,
  1351. NodeDescTips = nodeDto.NodeDescTips,
  1352. //Country = nodeDto.Country,
  1353. IsCurrent = nodeDto.IsCurrent,
  1354. IsFileUp = nodeDto.IsFileUp,
  1355. Remark = nodeDto.Remark,
  1356. CreateUserId = currUserId,
  1357. }).ToList();
  1358. var nodeIds = await _sqlSugar.Insertable(nodes).ExecuteCommandAsync();
  1359. if (nodeIds < 1)
  1360. {
  1361. _sqlSugar.RollbackTran();
  1362. return new Result { Code = 400, Msg = "团组流程进度流程节点添加失败!" };
  1363. }
  1364. //设置节点ID
  1365. nodes = await _sqlSugar.Queryable<Grp_ConfProcessNode>().Where(x => x.IsDel == 0 && x.ProcessId == processId).ToListAsync();
  1366. //记录节点日志
  1367. foreach (var node in nodes)
  1368. {
  1369. await LogConfNodeOpAsync(null, node, "Create", currUserId);
  1370. }
  1371. _sqlSugar.CommitTran();
  1372. return new Result { Code = 200, Msg = "添加成功!" };
  1373. }
  1374. catch (Exception ex)
  1375. {
  1376. _sqlSugar.RollbackTran();
  1377. return new Result { Code = 500, Msg = $"操作失败!msg:{ex.Message}" };
  1378. }
  1379. }
  1380. /// <summary>
  1381. /// 获取团组会务流程及节点详情
  1382. /// </summary>
  1383. /// <param name="groupId">团组Id</param>
  1384. /// <param name="currUserId">当前用户Id</param>
  1385. /// <returns></returns>
  1386. public async Task<Result> ConfProcessesDetailsAsync(int groupId, int currUserId = 4)
  1387. {
  1388. //团组验证
  1389. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
  1390. if (groupInfo == null)
  1391. {
  1392. return new Result { Code = 400, Msg = "团组不存在" };
  1393. }
  1394. // 检查是否已存在流程
  1395. var existingProcesses = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
  1396. .Where(p => p.IsDel == 0 && p.GroupId == groupId)
  1397. .ToListAsync();
  1398. if (!existingProcesses.Any())
  1399. {
  1400. //新建团组流程
  1401. var res = await ConfProcessInitAsync(groupId, currUserId);
  1402. if (res.Code != 200)
  1403. {
  1404. return res;
  1405. }
  1406. }
  1407. var users = await _sqlSugar.Queryable<Sys_Users>().Select(x => new { x.Id, x.CnName }).ToListAsync();
  1408. var processData = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
  1409. .Where(p => p.GroupId == groupId && p.IsDel == 0)
  1410. .Mapper(p => p.Nodes, p => p.Nodes.First().ProcessId)
  1411. .ToListAsync();
  1412. // 预先构建用户字典,提升查询性能
  1413. var userDict = users.ToDictionary(u => u.Id, u => u.CnName);
  1414. bool isNodeTemplSwitchable = true;
  1415. var processes = processData.Select(p =>
  1416. {
  1417. var orderedNodes = p.Nodes.OrderBy(n => n.NodeOrder).ToList();
  1418. var totalNodes = orderedNodes.Count;
  1419. isNodeTemplSwitchable = p.OverallStatus != ProcessStatus.Completed;
  1420. return new ConfProcessOverInfoView()
  1421. {
  1422. Id = p.Id,
  1423. GroupId = p.GroupId,
  1424. ProcessType = p.ProcessType,
  1425. ProcessName = p.ProcessType.GetEnumDescription(),
  1426. Nodes = orderedNodes.Select((n, index) =>
  1427. {
  1428. var isLastNode = index == totalNodes - 1;
  1429. // 文件上传按钮启用规则
  1430. bool isEnaFileUpBtn = false;
  1431. if (isLastNode)
  1432. {
  1433. isEnaFileUpBtn = true;
  1434. }
  1435. // 获取操作人姓名(使用字典提升性能)
  1436. string operatorName = "-";
  1437. if (n.Operator.HasValue && userDict.TryGetValue(n.Operator.Value, out var name))
  1438. {
  1439. operatorName = name;
  1440. }
  1441. return new ConfProcessNodeInfoView()
  1442. {
  1443. Id = n.Id,
  1444. ProcessId = n.ProcessId,
  1445. NodeOrder = n.NodeOrder,
  1446. NodeName = n.NodeName,
  1447. NodeDescTips = n.NodeDescTips,
  1448. OverallStatus = n.OverallStatus,
  1449. Participators = n.Participators,
  1450. OpUserList = n.OpUserList,
  1451. StatusText = n.OverallStatus.GetEnumDescription(),
  1452. Operator = operatorName,
  1453. OpeateTime = n.OperationTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? "-",
  1454. ActualDone = n.ActualDone?.ToString("yyyy-MM-dd HH:mm:ss") ?? "",
  1455. IsEnaFileUpBtn = isEnaFileUpBtn,
  1456. IsFileUp = n.IsFileUp, // 票据上传节点 存储值
  1457. };
  1458. }).ToList()
  1459. };
  1460. }).ToList();
  1461. var view = new ConfProcessOverInfo()
  1462. {
  1463. IsNodeTemplSwitchable = isNodeTemplSwitchable,
  1464. ConfProcess = processes
  1465. };
  1466. return new Result { Code = 200, Data = view, Msg = "查询成功!" };
  1467. }
  1468. /// <summary>
  1469. /// 更新节点模板
  1470. /// </summary>
  1471. /// <param name="groupId">团组Id</param>
  1472. /// <param name="nodeTempId">节点模板Id</param>
  1473. /// <param name="currUserId">当前用户Id</param>
  1474. /// <returns></returns>
  1475. public async Task<Result> ConfProcessChangeNodeTempSaveAsync(int groupId, int nodeTempId, int currUserId)
  1476. {
  1477. //节点模板id验证
  1478. var nodeTempIds = new List<int>() { 1, 2 };
  1479. if (!nodeTempIds.Contains(nodeTempId))
  1480. {
  1481. return new Result { Code = 400, Msg = "请传入有效的节点模板Id" };
  1482. }
  1483. //团组验证
  1484. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
  1485. if (groupInfo == null)
  1486. {
  1487. return new Result { Code = 400, Msg = "团组不存在" };
  1488. }
  1489. _sqlSugar.BeginTran();
  1490. try
  1491. {
  1492. // 检查是否已存在流程
  1493. var existingProcesses = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
  1494. .Where(p => p.IsDel == 0 && p.GroupId == groupId)
  1495. .ToListAsync();
  1496. if (existingProcesses.Any())
  1497. {
  1498. // 用户权限验证
  1499. var firstNode = await _sqlSugar.Queryable<Grp_ConfProcessNode>().FirstAsync(x => x.IsDel == 0 && x.ProcessId == existingProcesses.FirstOrDefault().Id);
  1500. if (!HasConfNodeOperationPermission(firstNode, currUserId))
  1501. {
  1502. throw new BusinessException("当前用户没有操作此节点的权限。");
  1503. }
  1504. //团组会流程完成 不可切换模板
  1505. if (existingProcesses.Where(x => x.OverallStatus == ProcessStatus.Completed).ToList().Count > 0)
  1506. {
  1507. _sqlSugar.RollbackTran();
  1508. return new Result { Code = 400, Msg = $"当前团组会务流程已完成,不可切换节点模板。" };
  1509. }
  1510. //删除 原有的节点模板
  1511. var parentIds = existingProcesses.Select(x => x.Id).ToList();
  1512. var updProcesses = await _sqlSugar.Updateable<Grp_ConfProcessOverview>()
  1513. .SetColumns(x => x.DeleteUserId == currUserId)
  1514. .SetColumns(x => x.DeleteTime == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  1515. .SetColumns(x => x.IsDel == 1)
  1516. .Where(x => parentIds.Contains(x.Id))
  1517. .ExecuteCommandAsync();
  1518. var updProcessNodes = await _sqlSugar.Updateable<Grp_ConfProcessNode>()
  1519. .SetColumns(x => x.DeleteUserId == currUserId)
  1520. .SetColumns(x => x.DeleteTime == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  1521. .SetColumns(x => x.IsDel == 1)
  1522. .Where(x => parentIds.Contains(x.ProcessId))
  1523. .ExecuteCommandAsync();
  1524. }
  1525. //更新模板节点信息
  1526. var result = await ConfProcessInitAsync(groupId,currUserId,nodeTempId);
  1527. if (result.Code == 200)
  1528. {
  1529. var infoResult = await ConfProcessesDetailsAsync(groupId, currUserId);
  1530. if (infoResult.Code == 200)
  1531. {
  1532. _sqlSugar.CommitTran();
  1533. return infoResult;
  1534. }
  1535. }
  1536. _sqlSugar.RollbackTran();
  1537. return new Result { Code = 500, Msg = $"操作失败!Msg:{result.Msg}" };
  1538. }
  1539. catch (Exception ex)
  1540. {
  1541. _sqlSugar.RollbackTran();
  1542. return new Result { Code = 500, Msg = $"操作失败!Msg:{ex.Message}" };
  1543. }
  1544. }
  1545. #region 状态变更 可操作任意节点 进行中<-->已完成 双向变更
  1546. /// <summary>
  1547. /// 更新节点状态(支持任意节点状态变更,无需处理当前节点)
  1548. /// </summary>
  1549. /// <param name="nodeId"></param>
  1550. /// <param name="currUserId"></param>
  1551. /// <param name="targetStatus"></param>
  1552. /// <returns></returns>
  1553. public async Task<Result> UpdateConfNodeStatusSimpleAsync(int nodeId, int currUserId, ProcessStatus targetStatus = ProcessStatus.Completed)
  1554. {
  1555. try
  1556. {
  1557. var result = await _sqlSugar.Ado.UseTranAsync(async () =>
  1558. {
  1559. // 1. 获取节点和流程信息
  1560. var node = await _sqlSugar.Queryable<Grp_ConfProcessNode>()
  1561. .FirstAsync(n => n.Id == nodeId && n.IsDel == 0)
  1562. ?? throw new BusinessException("当前节点不存在或已被删除。");
  1563. // 2. 用户权限验证
  1564. if (!HasConfNodeOperationPermission(node, currUserId))
  1565. {
  1566. throw new BusinessException("当前用户没有操作此节点的权限。");
  1567. }
  1568. var process = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
  1569. .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0)
  1570. ?? throw new BusinessException("关联的流程不存在。");
  1571. // 3. 检查是否重复操作
  1572. if (node.OverallStatus == targetStatus)
  1573. {
  1574. throw new BusinessException($"当前节点已为{GetStatusDescription(targetStatus)}状态,无需重复操作。");
  1575. }
  1576. // 4. 存储更新前的值
  1577. var nodeBefore = CloneConfNode(node);
  1578. var processBefore = CloneConfProcess(process);
  1579. // 5. 更新节点状态
  1580. node.OverallStatus = targetStatus;
  1581. node.Operator = currUserId;
  1582. node.OperationTime = DateTime.Now;
  1583. // 6. 保存节点更新
  1584. await _sqlSugar.Updateable(node)
  1585. .UpdateColumns(n => new
  1586. {
  1587. n.OverallStatus,
  1588. n.Operator,
  1589. n.OperationTime
  1590. })
  1591. .ExecuteCommandAsync();
  1592. // 7. 更新流程状态(基于所有节点的状态计算)
  1593. await UpdateConfProcessOverallStatusAsync(process, currUserId);
  1594. // 8. 记录日志
  1595. await LogConfNodeOpAsync(nodeBefore, node, "Update", currUserId);
  1596. await LogConfProcessOpAsync(processBefore, process, "Update", currUserId);
  1597. return new Result { Code = StatusCodes.Status200OK, Msg = "操作成功。" };
  1598. });
  1599. return result.IsSuccess ? result.Data : new Result
  1600. {
  1601. Code = StatusCodes.Status500InternalServerError,
  1602. Msg = result.ErrorMessage
  1603. };
  1604. }
  1605. catch (BusinessException ex)
  1606. {
  1607. return new Result { Code = StatusCodes.Status400BadRequest, Msg = ex.Message };
  1608. }
  1609. catch (Exception ex)
  1610. {
  1611. return new Result { Code = StatusCodes.Status500InternalServerError, Msg = "系统错误,请稍后重试" };
  1612. }
  1613. }
  1614. /// <summary>
  1615. /// 更新流程整体状态(根据所有节点状态计算)
  1616. /// </summary>
  1617. /// <param name="process"></param>
  1618. /// <param name="currUserId"></param>
  1619. /// <returns></returns>
  1620. private async Task UpdateConfProcessOverallStatusAsync(Grp_ConfProcessOverview process, int currUserId)
  1621. {
  1622. // 获取所有节点
  1623. var allNodes = await _sqlSugar.Queryable<Grp_ConfProcessNode>()
  1624. .Where(n => n.ProcessId == process.Id && n.IsDel == 0)
  1625. .ToListAsync();
  1626. // 统计节点状态
  1627. int totalCount = allNodes.Count;
  1628. int completedCount = allNodes.Count(n => n.OverallStatus == ProcessStatus.Completed);
  1629. int inProgressCount = allNodes.Count(n => n.OverallStatus == ProcessStatus.InProgress);
  1630. // 判断流程状态
  1631. ProcessStatus newProcessStatus;
  1632. if (completedCount == totalCount)
  1633. {
  1634. // 所有节点都已完成
  1635. newProcessStatus = ProcessStatus.Completed;
  1636. process.EndTime = DateTime.Now;
  1637. }
  1638. else if (inProgressCount > 0 || completedCount > 0)
  1639. {
  1640. // 有节点进行中或已完成
  1641. newProcessStatus = ProcessStatus.InProgress;
  1642. process.EndTime = null;
  1643. }
  1644. else
  1645. {
  1646. // 所有节点都未开始
  1647. newProcessStatus = ProcessStatus.UnStarted;
  1648. process.EndTime = null;
  1649. }
  1650. // 更新流程状态(只有状态变化时才更新)
  1651. if (process.OverallStatus != newProcessStatus)
  1652. {
  1653. process.OverallStatus = newProcessStatus;
  1654. process.UpdatedUserId = currUserId;
  1655. process.UpdatedTime = DateTime.Now;
  1656. await _sqlSugar.Updateable(process)
  1657. .UpdateColumns(p => new
  1658. {
  1659. p.OverallStatus,
  1660. p.EndTime,
  1661. p.UpdatedUserId,
  1662. p.UpdatedTime
  1663. })
  1664. .ExecuteCommandAsync();
  1665. }
  1666. }
  1667. /// <summary>
  1668. /// 验证用户是否有操作节点的权限
  1669. /// </summary>
  1670. private static bool HasConfNodeOperationPermission(Grp_ConfProcessNode node, int userId)
  1671. {
  1672. // 如果 OpUserList 为空或 null,表示不限制权限
  1673. if (node.OpUserList == null || node.OpUserList.Count == 0)
  1674. {
  1675. return true;
  1676. }
  1677. // 检查当前用户是否在权限列表中
  1678. return node.OpUserList.Contains(userId);
  1679. }
  1680. /// <summary>
  1681. /// 克隆节点对象
  1682. /// </summary>
  1683. /// <param name="node"></param>
  1684. /// <returns></returns>
  1685. private static Grp_ConfProcessNode CloneConfNode(Grp_ConfProcessNode node)
  1686. {
  1687. return new Grp_ConfProcessNode()
  1688. {
  1689. Id = node.Id,
  1690. ProcessId = node.ProcessId,
  1691. NodeName = node.NodeName,
  1692. NodeDescTips = node.NodeDescTips,
  1693. NodeOrder = node.NodeOrder,
  1694. OverallStatus = node.OverallStatus,
  1695. Participators = node.Participators,
  1696. Operator = node.Operator,
  1697. OperationTime = node.OperationTime,
  1698. IsCurrent = node.IsCurrent,
  1699. ActualDone = node.ActualDone,
  1700. IsFileUp = node.IsFileUp,
  1701. OpUserList = node.OpUserList,
  1702. };
  1703. }
  1704. /// <summary>
  1705. /// 克隆流程对象
  1706. /// </summary>
  1707. /// <param name="process"></param>
  1708. /// <returns></returns>
  1709. private static Grp_ConfProcessOverview CloneConfProcess(Grp_ConfProcessOverview process)
  1710. {
  1711. return new Grp_ConfProcessOverview()
  1712. {
  1713. Id = process.Id,
  1714. GroupId = process.GroupId,
  1715. ProcessOrder = process.ProcessOrder,
  1716. ProcessType = process.ProcessType,
  1717. OverallStatus = process.OverallStatus,
  1718. StartTime = process.StartTime,
  1719. EndTime = process.EndTime,
  1720. UpdatedUserId = process.UpdatedUserId,
  1721. UpdatedTime = process.UpdatedTime
  1722. };
  1723. }
  1724. #endregion
  1725. #region 更新节点状态 流程导向
  1726. /// <summary>
  1727. /// 更新团组会务流程节点状态
  1728. /// </summary>
  1729. /// <param name="nodeId">节点ID</param>
  1730. /// <param name="currUserId">当前用户ID</param>
  1731. /// <param name="processStatus">流程状态,默认为已完成</param>
  1732. /// <returns>操作结果</returns>
  1733. public async Task<Result> UpdateConfNodeStatusAsync(int nodeId, int currUserId, ProcessStatus processStatus = ProcessStatus.Completed)
  1734. {
  1735. try
  1736. {
  1737. // 使用事务确保数据一致性
  1738. var result = await _sqlSugar.Ado.UseTranAsync(async () =>
  1739. {
  1740. // 1. 获取并验证节点
  1741. var node = await _sqlSugar.Queryable<Grp_ConfProcessNode>()
  1742. .FirstAsync(n => n.Id == nodeId && n.IsDel == 0) ?? throw new BusinessException("当前节点不存在或已被删除。");
  1743. // 2. 获取流程信息,检查ProcessType
  1744. var process = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
  1745. .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0) ?? throw new BusinessException("关联的流程不存在。");
  1746. // 3. 节点操作验证
  1747. ValidateConfNodeOperation(node, processStatus);
  1748. // 4. 存储更新前的值
  1749. var before = CloneConfNode(node);
  1750. // 5. 更新节点状态
  1751. node.OverallStatus = processStatus;
  1752. node.Operator = currUserId;
  1753. node.OperationTime = DateTime.Now;
  1754. var updateCount = await _sqlSugar.Updateable(node)
  1755. .UpdateColumns(n => new
  1756. {
  1757. n.OverallStatus,
  1758. n.Operator,
  1759. n.OperationTime
  1760. })
  1761. .ExecuteCommandAsync();
  1762. if (updateCount == 0)
  1763. {
  1764. throw new BusinessException("节点状态更新失败。");
  1765. }
  1766. // 6. 记录节点日志
  1767. await LogConfNodeOpAsync(before, node, "Update", currUserId);
  1768. // 7. 如果是完成当前节点,处理流程流转
  1769. if (processStatus == ProcessStatus.Completed)
  1770. {
  1771. await ConfProcessCurrentNodeCompletionAsync(node, currUserId);
  1772. }
  1773. return new Result { Code = StatusCodes.Status200OK, Msg = "操作成功。" };
  1774. });
  1775. return result.IsSuccess ? result.Data : new Result
  1776. {
  1777. Code = StatusCodes.Status500InternalServerError,
  1778. Msg = result.ErrorMessage
  1779. };
  1780. }
  1781. catch (BusinessException ex)
  1782. {
  1783. // 业务异常
  1784. return new Result { Code = StatusCodes.Status400BadRequest, Msg = ex.Message };
  1785. }
  1786. catch (Exception ex)
  1787. {
  1788. // 系统异常
  1789. return new Result { Code = StatusCodes.Status500InternalServerError, Msg = "系统错误,请稍后重试" };
  1790. }
  1791. }
  1792. /// <summary>
  1793. /// 验证会务流程节点操作权限
  1794. /// </summary>
  1795. /// <param name="node">流程节点</param>
  1796. /// <param name="targetStatus">目标状态</param>
  1797. private static void ValidateConfNodeOperation(Grp_ConfProcessNode node, ProcessStatus targetStatus)
  1798. {
  1799. // 验证节点是否已完成
  1800. if (node.OverallStatus == ProcessStatus.Completed)
  1801. {
  1802. throw new BusinessException("当前节点已完成,不可重复操作。");
  1803. }
  1804. // 验证状态流转是否合法(可选)
  1805. //if (targetStatus != ProcessStatus.Completed)
  1806. //{
  1807. // throw new BusinessException("未开始或者进行中的节点只能重新完成,不可进行其他操作。");
  1808. //}
  1809. // 验证是否尝试将已完成节点改为其他状态
  1810. if (node.OverallStatus == ProcessStatus.Completed && targetStatus != ProcessStatus.Completed)
  1811. {
  1812. throw new BusinessException("已完成节点不可修改状态。");
  1813. }
  1814. }
  1815. /// <summary>
  1816. /// 处理当前会务流程节点完成后的流程流转
  1817. /// </summary>
  1818. private async Task ConfProcessCurrentNodeCompletionAsync(Grp_ConfProcessNode currentNode, int currUserId)
  1819. {
  1820. // 1. 获取流程信息
  1821. var process = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
  1822. .FirstAsync(p => p.Id == currentNode.ProcessId && p.IsDel == 0) ?? throw new BusinessException("关联的流程不存在。");
  1823. var processBefore = new Grp_ConfProcessOverview()
  1824. {
  1825. Id = process.Id,
  1826. GroupId = process.GroupId,
  1827. ProcessOrder = process.ProcessOrder,
  1828. ProcessType = process.ProcessType,
  1829. OverallStatus = process.OverallStatus,
  1830. StartTime = process.StartTime,
  1831. EndTime = process.EndTime,
  1832. UpdatedUserId = process.UpdatedUserId,
  1833. UpdatedTime = process.UpdatedTime
  1834. };
  1835. // 2. 取消当前节点的当前状态
  1836. var before = new Grp_ConfProcessNode()
  1837. {
  1838. Id = currentNode.Id,
  1839. ProcessId = currentNode.ProcessId,
  1840. NodeName = currentNode.NodeName,
  1841. NodeOrder = currentNode.NodeOrder,
  1842. OverallStatus = currentNode.OverallStatus,
  1843. Operator = currentNode.Operator,
  1844. OperationTime = currentNode.OperationTime,
  1845. IsCurrent = currentNode.IsCurrent,
  1846. };
  1847. currentNode.IsCurrent = false;
  1848. await _sqlSugar.Updateable(currentNode)
  1849. .UpdateColumns(n => new { n.IsCurrent })
  1850. .ExecuteCommandAsync();
  1851. // 2.1 记录节点日志 取消当前节点状态
  1852. await LogConfNodeOpAsync(before, currentNode, "Update", currUserId);
  1853. // 3. 查找并激活下一个节点
  1854. var nextNode = await _sqlSugar.Queryable<Grp_ConfProcessNode>()
  1855. .Where(n => n.ProcessId == currentNode.ProcessId
  1856. && n.NodeOrder == currentNode.NodeOrder + 1
  1857. && n.IsDel == 0)
  1858. .FirstAsync();
  1859. if (nextNode != null)
  1860. {
  1861. var nextNodeBefore = new Grp_ConfProcessNode()
  1862. {
  1863. Id = nextNode.Id,
  1864. ProcessId = nextNode.ProcessId,
  1865. NodeName = nextNode.NodeName,
  1866. NodeOrder = nextNode.NodeOrder,
  1867. OverallStatus = nextNode.OverallStatus,
  1868. Operator = nextNode.Operator,
  1869. OperationTime = nextNode.OperationTime,
  1870. IsCurrent = nextNode.IsCurrent,
  1871. };
  1872. // 激活下一个节点
  1873. nextNode.IsCurrent = true;
  1874. nextNode.OverallStatus = ProcessStatus.InProgress;
  1875. var updateCount = await _sqlSugar.Updateable(nextNode)
  1876. .UpdateColumns(n => new
  1877. {
  1878. n.IsCurrent,
  1879. n.OverallStatus,
  1880. n.Operator,
  1881. n.OperationTime
  1882. })
  1883. .ExecuteCommandAsync();
  1884. if (updateCount == 0)
  1885. {
  1886. throw new BusinessException("激活下一节点失败");
  1887. }
  1888. // 1.1 记录节点日志 激活下一节点当前节点状态
  1889. await LogConfNodeOpAsync(nextNodeBefore, nextNode, "Start", currUserId);
  1890. // 更新流程状态为进行中
  1891. process.OverallStatus = ProcessStatus.InProgress;
  1892. }
  1893. else
  1894. {
  1895. // 下一节点不存在,整个流程完成
  1896. process.OverallStatus = ProcessStatus.Completed;
  1897. process.EndTime = DateTime.Now;
  1898. }
  1899. // 4. 更新流程信息
  1900. process.UpdatedUserId = currUserId;
  1901. process.UpdatedTime = DateTime.Now;
  1902. var processUpdateCount = await _sqlSugar.Updateable(process)
  1903. .UpdateColumns(p => new
  1904. {
  1905. p.OverallStatus,
  1906. p.EndTime,
  1907. p.UpdatedUserId,
  1908. p.UpdatedTime
  1909. })
  1910. .ExecuteCommandAsync();
  1911. if (processUpdateCount == 0)
  1912. {
  1913. throw new BusinessException("流程状态更新失败。");
  1914. }
  1915. //记录流程日志
  1916. await LogConfProcessOpAsync(processBefore, process, "Update", currUserId);
  1917. }
  1918. #endregion
  1919. /// <summary>
  1920. /// 更新节点信息
  1921. /// </summary>
  1922. /// <param name="dto">更新节点信息</param>
  1923. /// <returns>操作结果</returns>
  1924. public async Task<Result> SetNodeInfoAsync(ConfProcessSetActualDoneDto dto)
  1925. {
  1926. //参与人验证
  1927. if (dto.Participators?.Any() != true) return new Result { Code = 400, Msg = "参与人不能为空。" };
  1928. var isDtNul = DateTime.TryParse(dto.ActualDone, out DateTime actualDone);
  1929. //if (!isDtNul) throw new BusinessException("实际操作时间为空或者格式不对。");
  1930. int nodeId = dto.NodeId;
  1931. int currUserId = dto.CurrUserId;
  1932. bool isFileUp = dto.IsFileUp;
  1933. // 1. 获取并验证节点和流程
  1934. var node = await _sqlSugar.Queryable<Grp_ConfProcessNode>()
  1935. .FirstAsync(n => n.Id == nodeId && n.IsDel == 0);
  1936. if (node == null) return new Result { Code = 400, Msg = "当前节点不存在或已被删除。" };
  1937. // 1.2. 用户权限验证
  1938. if (!HasConfNodeOperationPermission(node, currUserId)) return new Result { Code = 400, Msg = "当前用户没有操作此节点的权限。" };
  1939. var process = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
  1940. .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0);
  1941. if (process == null) return new Result { Code = 400, Msg = "当前流程不存在或已被删除。" };
  1942. // 2.1 存储更新前流程及节点信息
  1943. var nodeBefore = CloneConfNode(node);
  1944. if (isDtNul) node.ActualDone = actualDone;
  1945. else node.ActualDone = null;
  1946. node.IsFileUp = isFileUp;
  1947. node.Participators = dto.Participators;
  1948. // 3. 保存节点更新
  1949. await _sqlSugar.Updateable(node)
  1950. .UpdateColumns(n => new
  1951. {
  1952. n.ActualDone,
  1953. n.Participators,
  1954. n.IsFileUp,
  1955. })
  1956. .ExecuteCommandAsync();
  1957. //记录节点日志
  1958. await LogConfNodeOpAsync(nodeBefore, node, "Update", currUserId);
  1959. //当前节点未完成则更改节点状态为已完成
  1960. if (node.OverallStatus == ProcessStatus.InProgress && isDtNul)
  1961. {
  1962. var statusResult = await UpdateConfNodeStatusSimpleAsync(node.Id, dto.CurrUserId);
  1963. //日志记录执行结果
  1964. _logger.LogInformation($"团组流程设置完成时间:调用更改状态接口(SetActualDoneAsync -> UpdateNodeStatusAsync):[状态变更:进行中 -> 已完成] result: Code={statusResult.Code}, Msg={statusResult.Msg}");
  1965. }
  1966. //当前节点 实际完成时间设置为空且当前状态为已完成 则更改当前节点状态为进行中
  1967. else if (node.OverallStatus == ProcessStatus.Completed && !isDtNul)
  1968. {
  1969. var statusResult = await UpdateConfNodeStatusSimpleAsync(node.Id, dto.CurrUserId, ProcessStatus.InProgress);
  1970. //日志记录执行结果
  1971. _logger.LogInformation($"团组流程设置完成时间调:用更改状态接口(SetActualDoneAsync -> UpdateNodeStatusAsync):[状态变更:已完成 -> 进行中] result: Code={statusResult.Code}, Msg={statusResult.Msg}");
  1972. }
  1973. return new Result { Code = 200, Msg = "设置成功。" };
  1974. }
  1975. #endregion
  1976. #region 操作日志
  1977. #region 团组流程
  1978. /// <summary>
  1979. /// 记录流程操作日志
  1980. /// </summary>
  1981. /// <param name="before">操作前</param>
  1982. /// <param name="after">操作后</param>
  1983. /// <param name="opType">操作类型(Create - 创建、Update - 更新、Complete - 完成)</param>
  1984. /// <param name="operId">操作人ID</param>
  1985. /// <returns>异步任务</returns>
  1986. public async Task LogProcessOpAsync(Grp_ProcessOverview? before, Grp_ProcessOverview? after, string opType, int operId)
  1987. {
  1988. var chgDetails = GetProcessChgDetails(before, after);
  1989. var log = new Grp_ProcessLog
  1990. {
  1991. ProcessId = after?.Id ?? before?.Id,
  1992. GroupId = after?.GroupId ?? before?.GroupId ?? 0,
  1993. OpType = opType,
  1994. OpDesc = GenerateProcessOpDesc(opType, before, after, chgDetails),
  1995. BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  1996. AfterData = after != null ? JsonConvert.SerializeObject(after, GetJsonSettings()) : null,
  1997. ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
  1998. CreateUserId = operId
  1999. };
  2000. await _sqlSugar.Insertable(log).ExecuteCommandAsync();
  2001. }
  2002. /// <summary>
  2003. /// 记录节点操作日志
  2004. /// </summary>
  2005. /// <param name="before">操作前</param>
  2006. /// <param name="after">操作后</param>
  2007. /// <param name="opType">操作类型(Create - 创建、Update - 更新、Start - 启动、Complete - 完成)</param>
  2008. /// <param name="operId">操作人ID</param>
  2009. /// <returns>异步任务</returns>
  2010. public async Task LogNodeOpAsync(Grp_ProcessNode? before, Grp_ProcessNode? after, string opType, int operId)
  2011. {
  2012. var chgDetails = GetNodeChgDetails(before, after);
  2013. var log = new Grp_ProcessLog
  2014. {
  2015. NodeId = after?.Id ?? before?.Id,
  2016. ProcessId = after?.ProcessId ?? before?.ProcessId,
  2017. GroupId = 0, // 通过流程ID关联获取
  2018. OpType = opType,
  2019. OpDesc = GenerateNodeOpDesc(opType, before, after, chgDetails),
  2020. BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  2021. AfterData = after != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  2022. ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
  2023. CreateUserId = operId
  2024. };
  2025. await _sqlSugar.Insertable(log).ExecuteCommandAsync();
  2026. }
  2027. /// <summary>
  2028. /// 获取流程变更详情
  2029. /// </summary>
  2030. /// <param name="before">变更前</param>
  2031. /// <param name="after">变更后</param>
  2032. /// <returns>变更详情</returns>
  2033. private List<FieldChgDetail> GetProcessChgDetails(Grp_ProcessOverview before, Grp_ProcessOverview after)
  2034. {
  2035. var chgDetails = new List<FieldChgDetail>();
  2036. if (before == null || after == null) return chgDetails;
  2037. var props = typeof(Grp_ProcessOverview).GetProperties(BindingFlags.Public | BindingFlags.Instance)
  2038. .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
  2039. foreach (var prop in props)
  2040. {
  2041. var beforeVal = prop.GetValue(before);
  2042. var afterVal = prop.GetValue(after);
  2043. if (!Equals(beforeVal, afterVal))
  2044. {
  2045. chgDetails.Add(new FieldChgDetail
  2046. {
  2047. FieldName = prop.Name,
  2048. BeforeValue = FormatVal(beforeVal),
  2049. AfterValue = FormatVal(afterVal)
  2050. });
  2051. }
  2052. }
  2053. return chgDetails;
  2054. }
  2055. /// <summary>
  2056. /// 获取节点变更详情
  2057. /// </summary>
  2058. /// <param name="before">变更前</param>
  2059. /// <param name="after">变更后</param>
  2060. /// <returns>变更详情</returns>
  2061. private List<FieldChgDetail> GetNodeChgDetails(Grp_ProcessNode before, Grp_ProcessNode after)
  2062. {
  2063. var chgDetails = new List<FieldChgDetail>();
  2064. if (before == null || after == null) return chgDetails;
  2065. var props = typeof(Grp_ProcessNode).GetProperties(BindingFlags.Public | BindingFlags.Instance)
  2066. .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
  2067. foreach (var prop in props)
  2068. {
  2069. var beforeVal = prop.GetValue(before);
  2070. var afterVal = prop.GetValue(after);
  2071. if (!Equals(beforeVal, afterVal))
  2072. {
  2073. chgDetails.Add(new FieldChgDetail
  2074. {
  2075. FieldName = prop.Name,
  2076. BeforeValue = FormatVal(beforeVal),
  2077. AfterValue = FormatVal(afterVal)
  2078. });
  2079. }
  2080. }
  2081. return chgDetails;
  2082. }
  2083. /// <summary>
  2084. /// 生成流程操作描述
  2085. /// </summary>
  2086. /// <param name="opType">操作类型</param>
  2087. /// <param name="before">操作前</param>
  2088. /// <param name="after">操作后</param>
  2089. /// <param name="chgDetails">变更详情</param>
  2090. /// <returns>操作描述</returns>
  2091. private string GenerateProcessOpDesc(string opType, Grp_ProcessOverview before,
  2092. Grp_ProcessOverview after, List<FieldChgDetail> chgDetails)
  2093. {
  2094. var processType = after?.ProcessType ?? before?.ProcessType;
  2095. var processName = GetProcessTypeName(processType);
  2096. if (!chgDetails.Any())
  2097. {
  2098. return opType switch
  2099. {
  2100. "Create" => $"创建流程:{processName}",
  2101. "Update" => $"更新流程:{processName} - 无变更",
  2102. "Start" => $"启动流程:{processName}",
  2103. "Complete" => $"完成流程:{processName}",
  2104. "Delete" => $"删除流程:{processName}",
  2105. _ => $"{opType}:{processName}"
  2106. };
  2107. }
  2108. var chgDesc = string.Join("; ", chgDetails.Select(x =>
  2109. $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
  2110. return $"{GetOpTypeDisplay(opType)}:{processName} - {chgDesc}";
  2111. }
  2112. /// <summary>
  2113. /// 生成节点操作描述
  2114. /// </summary>
  2115. /// <param name="opType">操作类型</param>
  2116. /// <param name="before">操作前</param>
  2117. /// <param name="after">操作后</param>
  2118. /// <param name="chgDetails">变更详情</param>
  2119. /// <returns>操作描述</returns>
  2120. private string GenerateNodeOpDesc(string opType, Grp_ProcessNode before,
  2121. Grp_ProcessNode after, List<FieldChgDetail> chgDetails)
  2122. {
  2123. var nodeName = after?.NodeName ?? before?.NodeName;
  2124. if (!chgDetails.Any())
  2125. {
  2126. return opType switch
  2127. {
  2128. "Create" => $"创建节点:{nodeName}",
  2129. "Update" => $"更新节点:{nodeName} - 无变更",
  2130. "Start" => $"启动节点:{nodeName}",
  2131. "Complete" => $"完成节点:{nodeName}",
  2132. //"Delete" => $"删除节点:{nodeName}",
  2133. _ => $"{opType}:{nodeName}"
  2134. };
  2135. }
  2136. var chgDesc = string.Join("; ", chgDetails.Select(x =>
  2137. $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
  2138. return $"{GetOpTypeDisplay(opType)}:{nodeName} - {chgDesc}";
  2139. }
  2140. /// <summary>
  2141. /// 获取流程类型名称
  2142. /// </summary>
  2143. /// <param name="processType">流程类型</param>
  2144. /// <returns>流程名称</returns>
  2145. private static string GetProcessTypeName(GroupProcessType? processType)
  2146. {
  2147. return processType switch
  2148. {
  2149. GroupProcessType.Invitation => "商邀报批",
  2150. GroupProcessType.Visa => "签证",
  2151. GroupProcessType.AirTicket => "机票",
  2152. GroupProcessType.Hotel => "酒店",
  2153. GroupProcessType.LocalGuide => "地接",
  2154. GroupProcessType.FeeSettle => "费用结算",
  2155. _ => "未知流程"
  2156. };
  2157. }
  2158. /// <summary>
  2159. /// 获取流程日志
  2160. /// </summary>
  2161. /// <param name="processId">流程ID</param>
  2162. /// <returns>日志列表</returns>
  2163. public async Task<List<Grp_ProcessLog>> GetProcessLogsAsync(int processId)
  2164. {
  2165. return await _sqlSugar.Queryable<Grp_ProcessLog>()
  2166. .Where(x => x.ProcessId == processId)
  2167. .OrderByDescending(x => x.CreateTime)
  2168. .ToListAsync();
  2169. }
  2170. /// <summary>
  2171. /// 获取团组流程日志
  2172. /// </summary>
  2173. /// <param name="groupId">团组ID</param>
  2174. /// <returns>日志列表</returns>
  2175. public async Task<List<Grp_ProcessLog>> GetGroupLogsAsync(int groupId)
  2176. {
  2177. return await _sqlSugar.Queryable<Grp_ProcessLog>()
  2178. .Where(x => x.GroupId == groupId)
  2179. .OrderByDescending(x => x.CreateTime)
  2180. .ToListAsync();
  2181. }
  2182. #endregion
  2183. #region 会务流程
  2184. /// <summary>
  2185. /// 记录会务流程操作日志
  2186. /// </summary>
  2187. /// <param name="before">操作前</param>
  2188. /// <param name="after">操作后</param>
  2189. /// <param name="opType">操作类型(Create - 创建、Update - 更新、Complete - 完成)</param>
  2190. /// <param name="operId">操作人ID</param>
  2191. /// <returns>异步任务</returns>
  2192. public async Task LogConfProcessOpAsync(Grp_ConfProcessOverview? before, Grp_ConfProcessOverview? after, string opType, int operId)
  2193. {
  2194. var chgDetails = GetConfProcessChgDetails(before, after);
  2195. var log = new Grp_ConfProcessLog
  2196. {
  2197. ProcessId = after?.Id ?? before?.Id,
  2198. GroupId = after?.GroupId ?? before?.GroupId ?? 0,
  2199. OpType = opType,
  2200. OpDesc = GenerateConfProcessOpDesc(opType, before, after, chgDetails),
  2201. BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  2202. AfterData = after != null ? JsonConvert.SerializeObject(after, GetJsonSettings()) : null,
  2203. ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
  2204. CreateUserId = operId
  2205. };
  2206. await _sqlSugar.Insertable(log).ExecuteCommandAsync();
  2207. }
  2208. /// <summary>
  2209. /// 记录会务节点操作日志
  2210. /// </summary>
  2211. /// <param name="before">操作前</param>
  2212. /// <param name="after">操作后</param>
  2213. /// <param name="opType">操作类型(Create - 创建、Update - 更新、Start - 启动、Complete - 完成)</param>
  2214. /// <param name="operId">操作人ID</param>
  2215. /// <returns>异步任务</returns>
  2216. public async Task LogConfNodeOpAsync(Grp_ConfProcessNode? before, Grp_ConfProcessNode? after, string opType, int operId)
  2217. {
  2218. var chgDetails = GetConfNodeChgDetails(before, after);
  2219. var log = new Grp_ConfProcessLog
  2220. {
  2221. NodeId = after?.Id ?? before?.Id,
  2222. ProcessId = after?.ProcessId ?? before?.ProcessId,
  2223. GroupId = 0, // 通过流程ID关联获取
  2224. OpType = opType,
  2225. OpDesc = GenerateConfNodeOpDesc(opType, before, after, chgDetails),
  2226. BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  2227. AfterData = after != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
  2228. ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
  2229. CreateUserId = operId
  2230. };
  2231. await _sqlSugar.Insertable(log).ExecuteCommandAsync();
  2232. }
  2233. /// <summary>
  2234. /// 获取流程变更详情
  2235. /// </summary>
  2236. /// <param name="before">变更前</param>
  2237. /// <param name="after">变更后</param>
  2238. /// <returns>变更详情</returns>
  2239. private static List<FieldChgDetail> GetConfProcessChgDetails(Grp_ConfProcessOverview before, Grp_ConfProcessOverview after)
  2240. {
  2241. var chgDetails = new List<FieldChgDetail>();
  2242. if (before == null || after == null) return chgDetails;
  2243. var props = typeof(Grp_ConfProcessOverview).GetProperties(BindingFlags.Public | BindingFlags.Instance)
  2244. .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
  2245. foreach (var prop in props)
  2246. {
  2247. var beforeVal = prop.GetValue(before);
  2248. var afterVal = prop.GetValue(after);
  2249. if (!Equals(beforeVal, afterVal))
  2250. {
  2251. chgDetails.Add(new FieldChgDetail
  2252. {
  2253. FieldName = prop.Name,
  2254. BeforeValue = FormatVal(beforeVal),
  2255. AfterValue = FormatVal(afterVal)
  2256. });
  2257. }
  2258. }
  2259. return chgDetails;
  2260. }
  2261. /// <summary>
  2262. /// 获取节点变更详情
  2263. /// </summary>
  2264. /// <param name="before">变更前</param>
  2265. /// <param name="after">变更后</param>
  2266. /// <returns>变更详情</returns>
  2267. private static List<FieldChgDetail> GetConfNodeChgDetails(Grp_ConfProcessNode before, Grp_ConfProcessNode after)
  2268. {
  2269. var chgDetails = new List<FieldChgDetail>();
  2270. if (before == null || after == null) return chgDetails;
  2271. var props = typeof(Grp_ConfProcessNode).GetProperties(BindingFlags.Public | BindingFlags.Instance)
  2272. .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
  2273. foreach (var prop in props)
  2274. {
  2275. var beforeVal = prop.GetValue(before);
  2276. var afterVal = prop.GetValue(after);
  2277. if (!Equals(beforeVal, afterVal))
  2278. {
  2279. chgDetails.Add(new FieldChgDetail
  2280. {
  2281. FieldName = prop.Name,
  2282. BeforeValue = FormatVal(beforeVal),
  2283. AfterValue = FormatVal(afterVal)
  2284. });
  2285. }
  2286. }
  2287. return chgDetails;
  2288. }
  2289. /// <summary>
  2290. /// 生成流程操作描述
  2291. /// </summary>
  2292. /// <param name="opType">操作类型</param>
  2293. /// <param name="before">操作前</param>
  2294. /// <param name="after">操作后</param>
  2295. /// <param name="chgDetails">变更详情</param>
  2296. /// <returns>操作描述</returns>
  2297. private static string GenerateConfProcessOpDesc(string opType, Grp_ConfProcessOverview before,
  2298. Grp_ConfProcessOverview after, List<FieldChgDetail> chgDetails)
  2299. {
  2300. var processType = after?.ProcessType ?? before?.ProcessType;
  2301. var processName = GetConfProcessTypeName(processType);
  2302. if (!chgDetails.Any())
  2303. {
  2304. return opType switch
  2305. {
  2306. "Create" => $"创建流程:{processName}",
  2307. "Update" => $"更新流程:{processName} - 无变更",
  2308. "Start" => $"启动流程:{processName}",
  2309. "Complete" => $"完成流程:{processName}",
  2310. "Delete" => $"删除流程:{processName}",
  2311. _ => $"{opType}:{processName}"
  2312. };
  2313. }
  2314. var chgDesc = string.Join("; ", chgDetails.Select(x =>
  2315. $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
  2316. return $"{GetOpTypeDisplay(opType)}:{processName} - {chgDesc}";
  2317. }
  2318. /// <summary>
  2319. /// 生成节点操作描述
  2320. /// </summary>
  2321. /// <param name="opType">操作类型</param>
  2322. /// <param name="before">操作前</param>
  2323. /// <param name="after">操作后</param>
  2324. /// <param name="chgDetails">变更详情</param>
  2325. /// <returns>操作描述</returns>
  2326. private static string GenerateConfNodeOpDesc(string opType, Grp_ConfProcessNode before,
  2327. Grp_ConfProcessNode after, List<FieldChgDetail> chgDetails)
  2328. {
  2329. var nodeName = after?.NodeName ?? before?.NodeName;
  2330. if (!chgDetails.Any())
  2331. {
  2332. return opType switch
  2333. {
  2334. "Create" => $"创建节点:{nodeName}",
  2335. "Update" => $"更新节点:{nodeName} - 无变更",
  2336. "Start" => $"启动节点:{nodeName}",
  2337. "Complete" => $"完成节点:{nodeName}",
  2338. //"Delete" => $"删除节点:{nodeName}",
  2339. _ => $"{opType}:{nodeName}"
  2340. };
  2341. }
  2342. var chgDesc = string.Join("; ", chgDetails.Select(x =>
  2343. $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
  2344. return $"{GetOpTypeDisplay(opType)}:{nodeName} - {chgDesc}";
  2345. }
  2346. /// <summary>
  2347. /// 获取流程类型名称
  2348. /// </summary>
  2349. /// <param name="processType">流程类型</param>
  2350. /// <returns>流程名称</returns>
  2351. private static string GetConfProcessTypeName(ConfProcessType? processType)
  2352. {
  2353. return processType switch
  2354. {
  2355. ConfProcessType.Conference => "会务",
  2356. _ => "未知流程"
  2357. };
  2358. }
  2359. /// <summary>
  2360. /// 获取会务流程日志
  2361. /// </summary>
  2362. /// <param name="processId">流程ID</param>
  2363. /// <returns>日志列表</returns>
  2364. public async Task<List<Grp_ConfProcessLog>> GetConfProcessLogsAsync(int processId)
  2365. {
  2366. return await _sqlSugar.Queryable<Grp_ConfProcessLog>()
  2367. .Where(x => x.ProcessId == processId)
  2368. .OrderByDescending(x => x.CreateTime)
  2369. .ToListAsync();
  2370. }
  2371. /// <summary>
  2372. /// 获取团组会务流程日志
  2373. /// </summary>
  2374. /// <param name="groupId">团组ID</param>
  2375. /// <returns>日志列表</returns>
  2376. public async Task<List<Grp_ConfProcessLog>> GetGroupConfLogsAsync(int groupId)
  2377. {
  2378. return await _sqlSugar.Queryable<Grp_ConfProcessLog>()
  2379. .Where(x => x.GroupId == groupId)
  2380. .OrderByDescending(x => x.CreateTime)
  2381. .ToListAsync();
  2382. }
  2383. #endregion
  2384. #region 日志私有方法
  2385. /// <summary>
  2386. /// 获取JSON序列化设置
  2387. /// </summary>
  2388. /// <returns>JSON设置</returns>
  2389. private static JsonSerializerSettings GetJsonSettings()
  2390. {
  2391. return new JsonSerializerSettings
  2392. {
  2393. ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
  2394. NullValueHandling = NullValueHandling.Ignore,
  2395. DateFormatString = "yyyy-MM-dd HH:mm:ss",
  2396. Formatting = Formatting.None
  2397. };
  2398. }
  2399. /// <summary>
  2400. /// 获取操作类型显示
  2401. /// </summary>
  2402. /// <param name="opType">操作类型</param>
  2403. /// <returns>显示名称</returns>
  2404. private static string GetOpTypeDisplay(string opType)
  2405. {
  2406. return opType switch
  2407. {
  2408. "Create" => "创建",
  2409. "Update" => "更新",
  2410. "Start" => "启动",
  2411. "Complete" => "完成",
  2412. "Delete" => "删除",
  2413. "StatusChg" => "状态变更",
  2414. _ => opType
  2415. };
  2416. }
  2417. /// <summary>
  2418. /// 获取字段显示名称
  2419. /// </summary>
  2420. /// <param name="fieldName">字段名</param>
  2421. /// <returns>显示名称</returns>
  2422. private static string GetFieldDisplayName(string fieldName)
  2423. {
  2424. return fieldName switch
  2425. {
  2426. "OverallStatus" => "状态",
  2427. "ProcessOrder" => "流程顺序",
  2428. "StartTime" => "开始时间",
  2429. "EndTime" => "结束时间",
  2430. "UpdatedUserId" => "更新人",
  2431. "UpdatedTime" => "更新时间",
  2432. "NodeOrder" => "节点顺序",
  2433. "NodeName" => "节点名称",
  2434. "NodeDescTips" => "节点描述提示",
  2435. "IsCurrent" => "当前节点",
  2436. "Participator" => "参与人",
  2437. "Operator" => "操作人",
  2438. "OperationTime" => "操作时间",
  2439. "ActualDone" => "实际完成时间",
  2440. "IsFileUp" => "是否上传文件(签证、机票、酒店、地接 流程结尾节点使用)",
  2441. "IsAssist" => "是否协助(财务流程首节点使用)",
  2442. "IsPart" => "是否参与(商邀 第五步使用)",
  2443. _ => fieldName
  2444. };
  2445. }
  2446. /// <summary>
  2447. /// 格式化值显示
  2448. /// </summary>
  2449. /// <param name="value">值</param>
  2450. /// <returns>格式化值</returns>
  2451. private static string FormatVal(object value)
  2452. {
  2453. if (value == null) return "空";
  2454. if (value is ProcessStatus status)
  2455. {
  2456. return status switch
  2457. {
  2458. ProcessStatus.UnStarted => "未开始",
  2459. ProcessStatus.InProgress => "进行中",
  2460. ProcessStatus.Completed => "已完成",
  2461. _ => status.ToString()
  2462. };
  2463. }
  2464. if (value is bool boolVal) return boolVal ? "是" : "否";
  2465. if (value is DateTime dateVal) return dateVal.ToString("yyyy-MM-dd HH:mm");
  2466. var strVal = value.ToString();
  2467. return string.IsNullOrEmpty(strVal) ? "空" : strVal;
  2468. }
  2469. /// <summary>
  2470. /// 检查是否排除字段
  2471. /// </summary>
  2472. /// <param name="fieldName">字段名</param>
  2473. /// <returns>是否排除</returns>
  2474. private static bool IsExclField(string fieldName)
  2475. {
  2476. var exclFields = new List<string>
  2477. {
  2478. "Id", "CreateTime", "CreateUserId", "Nodes", "Process" // 导航属性
  2479. };
  2480. return exclFields.Contains(fieldName);
  2481. }
  2482. #endregion
  2483. #endregion
  2484. }
  2485. }