ProcessOverviewRepository.cs 156 KB

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