ProcessOverviewRepository.cs 152 KB

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