ProcessOverviewRepository.cs 124 KB

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