| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038 |
- using AutoMapper;
- using Newtonsoft.Json;
- using OASystem.Domain;
- using OASystem.Domain.Dtos.Groups;
- using OASystem.Domain.Entities.Groups;
- using OASystem.Domain.Entities.Resource;
- using OASystem.Domain.ViewModels.Groups;
- using Org.BouncyCastle.Asn1.X500;
- using System.Reflection;
- using System.Runtime.Intrinsics.Arm;
- using System.Text.RegularExpressions;
- namespace OASystem.Infrastructure.Repositories.Groups
- {
- /// <summary>
- /// 团组流程总览表仓储
- /// </summary>
- public class ProcessOverviewRepository : BaseRepository<Grp_ProcessOverview, Grp_ProcessOverview>
- {
- private readonly IMapper _mapper;
- private readonly DelegationInfoRepository _groupRep;
- public ProcessOverviewRepository(SqlSugarClient sqlSugar, IMapper mapper, DelegationInfoRepository groupRep) : base(sqlSugar)
- {
- _mapper = mapper;
- _groupRep = groupRep;
- }
- #region 团组流程
- /// <summary>
- /// 基础数据初始化-团组流程
- /// </summary>
- /// <param name="groupId"></param>
- /// <param name="currUserId"></param>
- /// <returns></returns>
- public async Task<List<Grp_ProcessOverview>> ProcessDataInitAsync(int groupId, int currUserId, List<string> visaCountries)
- {
- var processs = new List<Grp_ProcessOverview>();
- //团组验证
- var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
- if (groupInfo == null) return processs;
- // 检查是否已存在流程
- var existingProcesses = await _sqlSugar.Queryable<Grp_ProcessOverview>()
- .Where(p => p.IsDel == 0 && p.GroupId == groupId)
- .ToListAsync();
- if (existingProcesses.Any()) return processs;
- #region 商邀报批流程
- var custInfo = await _sqlSugar.Queryable<Grp_TourClientList>()
- .Where(c => c.DiId == groupId && c.IsDel == 0)
- .OrderByDescending(c => c.CreateTime)
- .FirstAsync();
- string oaNode2Tips = "客户提供完整名单后,2周内取得邀请函(翻译件)。";
- if (custInfo != null)
- {
- oaNode2Tips = $"请于{custInfo.CreateTime.AddDays(14):yyyy年MM月dd日}内完成该项工作(客户提供完整名单后,2周内取得邀请函(翻译件))";
- }
- var oaNode4Tips = $"请于{groupInfo.VisitDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(按进度实际公务活动落实情况,出发前5日落实公务)";
- var oaNode7Tips = $"请于{groupInfo.VisitEndDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(团组结束前完成)";
- processs.Add(
- Grp_ProcessOverview.Create(groupId, 1, GroupProcessType.Invitation, ProcessStatus.InProgress, currUserId,
- new List<Grp_ProcessNode>()
- {
- Grp_ProcessNode.Create(1, "报批基础资料准备","更新报批行程和请示,提供其他报批所需材料,4个工作日内完成。",ProcessStatus.InProgress, true,false,false,false,currUserId),
- Grp_ProcessNode.Create(2, "报批邀请函资料准备",oaNode2Tips, ProcessStatus.InProgress, false,false,false,false,currUserId),
- Grp_ProcessNode.Create(3, "获得批件","提供完整的报批全套资源。",ProcessStatus.InProgress, false,false,false,false, currUserId ),
- Grp_ProcessNode.Create(4, "对接公务",oaNode4Tips,ProcessStatus.InProgress, false,false,false,false, currUserId),
- Grp_ProcessNode.Create(5, "参与翻译对接","",ProcessStatus.InProgress, false,false,false,true, currUserId),
- Grp_ProcessNode.Create(6, "商邀文案配合","",ProcessStatus.InProgress, false,false,false,false, currUserId),
- Grp_ProcessNode.Create(7, "票据上传(相关票据)",oaNode7Tips,ProcessStatus.InProgress, false,false,true,false, currUserId),
- }));
- #endregion
- #region 签证流程
- //单独处理签证流程节点
- var visaNodes = new List<Grp_ProcessNode>();
- if (visaCountries != null && visaCountries.Count > 0)
- {
- var visaDefualtNodes = new List<VisaProcessNode>();
- for (int i = 1; i < visaCountries.Count + 1; i++)
- {
- visaDefualtNodes.Add(VisaProcessNode.Info(i, visaCountries[i - 1].ToString()));
- }
- var visaNode2Tips = $"请于{groupInfo.VisitDate:yyyy年MM月dd日}内完成该项工作(按进度实际签证办理落实情况,团组出发前上传票据。)";
- visaNodes.Add(Grp_ProcessNode.Create(1, "签证信息", "", ProcessStatus.InProgress, true, false, false, false, currUserId, JsonConvert.SerializeObject(visaDefualtNodes)));
- visaNodes.Add(Grp_ProcessNode.Create(2, "票据上传(明细表、费用票据、保单及超支费用账单)", visaNode2Tips, ProcessStatus.InProgress, false, false, true, false, currUserId));
- }
- processs.Add(Grp_ProcessOverview.Create(groupId, 2, GroupProcessType.Visa, ProcessStatus.UnStarted, currUserId, visaNodes));
- #endregion
- #region 机票流程
- string airNode1Tips = "建团后打勾确认出团的时候开始24小时内。";
- if (groupInfo.Step == 1 || groupInfo.Step == 2)
- {
- if (groupInfo.StepOperationTime.HasValue)
- {
- airNode1Tips = $"请于{groupInfo.StepOperationTime.Value.AddDays(1):yyyy年MM月dd日}内完成该项工作(建团后打勾确认出团的时候开始24小时内)";
- }
- }
- var airNode3Tips = $"完成机票采购确认(含预算核对、出票确认等)";
- var airNode5Tips = $"请于{groupInfo.VisitDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(团组出发前5日)";
- var airNode7Tips = $"请于{groupInfo.VisitEndDate.AddDays(5):yyyy年MM月dd日}内完成该项工作(团组归国后5个工作日内)";
- var airNode8Tips = $"1. 票据上传(机票报销蓝联、行程单及机票说明) \r\n 2. 请于{groupInfo.VisitEndDate.AddDays(10):yyyy年MM月dd日}内完成该项工作(团组归国后10个工作日内) *按机票报价*0.999折扣出具机票报销蓝联、行程单及机票说明";
- processs.Add(
- Grp_ProcessOverview.Create(groupId, 3, GroupProcessType.AirTicket, ProcessStatus.InProgress, currUserId,
- new List<Grp_ProcessNode>()
- {
- Grp_ProcessNode.Create(1, "初步拟定航程方案及价格", airNode1Tips, ProcessStatus.InProgress, true,false,false,false, currUserId ),
- Grp_ProcessNode.Create(2, "机票占位、续位", "", ProcessStatus.UnStarted, false,false,false,false,currUserId ),
- Grp_ProcessNode.Create(3, "完成机票采购确认", airNode3Tips, ProcessStatus.UnStarted,false,false,false,false, currUserId),
- Grp_ProcessNode.Create(4, "进行出票操作并核查信息", "", ProcessStatus.UnStarted, false,false,false,false, currUserId),
- Grp_ProcessNode.Create(5, "机票已出", airNode5Tips, ProcessStatus.UnStarted, false,false,false,false, currUserId),
- Grp_ProcessNode.Create(6, "完成机票选座", "", ProcessStatus.UnStarted, false,false,false,false,currUserId),
- Grp_ProcessNode.Create(7, "票据上传(机票超支费用账单)", airNode7Tips, ProcessStatus.UnStarted, false,false,true,false, currUserId),
- Grp_ProcessNode.Create(8, "票据上传", airNode8Tips, ProcessStatus.UnStarted, false,false,true,false, currUserId)
- }
- )
- );
- #endregion
- #region 酒店流程
- string hotelNode1Tips = "1. 筛选并按照预算标准,对目标酒店进行询价、比价、谈价 \r\n2. 建团后打勾确认出团的时候开始2个工作日。";
- if (groupInfo.Step == 1 || groupInfo.Step == 2)
- {
- if (groupInfo.StepOperationTime.HasValue)
- {
- hotelNode1Tips = $"请于{groupInfo.StepOperationTime.Value.AddDays(2):yyyy年MM月dd日}内完成该项工作(建团后打勾确认出团的时候开始2个工作日)";
- }
- }
- var hotelNode4Tips = $"1.行前再次确认酒店订单、付款状态及入住安排 \r\n 2.请于{groupInfo.VisitDate.AddDays(-5):yyyy年MM月dd日}内完成该项工作(团组出发前5天)";
- var hotelNode5Tips = $"1.行程结束后整理酒店发票(含超支费用发票)与结算 \r\n 2.请于{groupInfo.VisitEndDate.AddDays(5):yyyy年MM月dd日}内完成该项工作(团组结束后5天内)";
- processs.Add(
- Grp_ProcessOverview.Create(groupId, 4, GroupProcessType.Hotel, ProcessStatus.InProgress, currUserId,
- new List<Grp_ProcessNode>()
- {
- Grp_ProcessNode.Create(1, "按照预算,询价、比价、谈价", hotelNode1Tips, ProcessStatus.InProgress, true, false, false, false, currUserId),
- Grp_ProcessNode.Create(2, "获取酒店确认函与入住名单核对", "", ProcessStatus.UnStarted, false, false, false,false, currUserId ),
- Grp_ProcessNode.Create(3, "预订酒店并录入OA", "", ProcessStatus.UnStarted,false, false, false,false,currUserId ),
- Grp_ProcessNode.Create(4, "行前再次确认酒店相关情况", hotelNode4Tips,ProcessStatus.UnStarted, false, false, false,false,currUserId ),
- Grp_ProcessNode.Create(5, "行程结束后整理酒店发票与结算", hotelNode5Tips, ProcessStatus.UnStarted, false, false, true,false, currUserId ),
- }
- )
- );
- #endregion
- #region 地接流程
- var airTripCodeInfo = await _sqlSugar.Queryable<Air_TicketBlackCode>()
- .Where(x => x.IsDel == 0 && x.DiId == groupId)
- .OrderByDescending(x => x.CreateTime)
- .FirstAsync();
- string opNode1Tips = $"机票行程代码最后一段录入后1个工作日内。";
- if (airTripCodeInfo != null)
- {
- opNode1Tips = $"请于{airTripCodeInfo.CreateTime.AddDays(1):yyyy年MM月dd日}内完成该项工作(机票行程代码最后一段录入后1个工作日内)";
- }
- string opNode2Tips = $"1.联系并询价地接、餐厅、用车、景点等供应商 \r\n 2. 请于{groupInfo.CreateTime.AddDays(7):yyyy年MM月dd日}内完成该项工作(建团完成后7个工作日内)";
- string opNode3Tips = $"请于{groupInfo.CreateTime.AddDays(10):yyyy年MM月dd日}内完成该项工作(上一步往后3个工作日内)";
- string opNode4Tips = $"请于{groupInfo.CreateTime.AddDays(12):yyyy年MM月dd日}内完成该项工作(上一步往后2个工作日内)";
- var backListInfo = await _sqlSugar.Queryable<Grp_InvertedList>().Where(x => x.DiId == groupId && x.IsDel == 0).FirstAsync();
- string opNode5Tips = $"1.制定最终《行程单》及《出行手册》 \r\n2. 倒推表里开行前会 -3天。";
- if (backListInfo != null)
- {
- if (DateTime.TryParse(backListInfo.PreTripMeetingDt, out DateTime dateTime))
- {
- opNode5Tips = $"请于{dateTime.AddDays(-3):yyyy年MM月dd日}内完成该项工作(倒推表里开行前会 -3天)";
- }
- }
- string opNode7Tips = $"请于{groupInfo.VisitEndDate.AddDays(5):yyyy年MM月dd日}内完成该项工作(团组归国后5个工作日内) *上传最终报批行程,确定城市间交通最终版报价分配;地接账单(清楚标注超时及其他项超支费用)、地接交通费用原始票据、城市间交通明细表;";
- processs.Add(
- Grp_ProcessOverview.Create(groupId, 5, GroupProcessType.LocalGuide, ProcessStatus.InProgress, currUserId,
- new List<Grp_ProcessNode>()
- {
- Grp_ProcessNode.Create(1,"根据机票方案出框架行程", opNode1Tips,ProcessStatus.InProgress, true, false, false,false,currUserId ),
- Grp_ProcessNode.Create(2,"联系并询价地接相关的供应商", opNode2Tips,ProcessStatus.UnStarted, false, false, false,false, currUserId ),
- Grp_ProcessNode.Create(3,"提交供应商报价及比价表", opNode3Tips, ProcessStatus.UnStarted, false, false, false, false,currUserId),
- Grp_ProcessNode.Create(4,"执行采购流程", opNode4Tips, ProcessStatus.UnStarted, false, false, false,false, currUserId),
- Grp_ProcessNode.Create(5,"制定最终行程单及出行手册", opNode5Tips, ProcessStatus.UnStarted, false, false, false,false, currUserId ),
- Grp_ProcessNode.Create(6,"送机", "", ProcessStatus.UnStarted, false, false, false,false, currUserId ),
- Grp_ProcessNode.Create(7,"最终版报批行程、票据上传", opNode7Tips, ProcessStatus.UnStarted, false, false, true, false,currUserId )
- }
- )
- );
- #endregion
- #region 费用结算流程
- var feeNode3Tips = $"1.整理统计团组超支费用、三公报销资料给到各单位 \r\n 2. 请于{groupInfo.VisitEndDate.AddDays(12):yyyy年MM月dd日}内完成该项工作(团组归国后12个工作日内)";
- processs.Add(
- Grp_ProcessOverview.Create(groupId, 6, GroupProcessType.FeeSettle, ProcessStatus.InProgress, currUserId,
- new List<Grp_ProcessNode>()
- {
- Grp_ProcessNode.Create(1, "城市间交通报批金额核定", "团组报批前", ProcessStatus.InProgress, true, true, false,false,currUserId ),
- Grp_ProcessNode.Create(2, "团组全程各段机票打票金额的核定", "团组报批后、订票前", ProcessStatus.UnStarted, false, false, false,false,currUserId ),
- Grp_ProcessNode.Create(3, "整理统计相关财务资料给到各单位", feeNode3Tips, ProcessStatus.UnStarted, false, false, false,false,currUserId ),
- Grp_ProcessNode.Create(4, "费用结算完毕", "", ProcessStatus.UnStarted, false, false, false,false, currUserId ),
- }
- )
- );
- #endregion
- return processs;
- }
- /// <summary>
- /// 团组流程初始化
- /// </summary>
- /// <param name="request">创建流程请求参数</param>
- /// <returns>创建的流程信息</returns>
- public async Task<Result> ProcessInitAsync(int groupId, int currUserId)
- {
- //团组验证
- var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
- if (groupInfo == null)
- {
- return new Result { Code = 400, Msg = "团组不存在" };
- }
- // 检查是否已存在流程
- var existingProcesses = await _sqlSugar.Queryable<Grp_ProcessOverview>()
- .Where(p => p.IsDel == 0 && p.GroupId == groupId)
- .ToListAsync();
- if (existingProcesses.Any())
- {
- return new Result { Code = 400, Msg = "该团组的流程已存在" };
- }
- //处理签证国家
- var visaCountries = _groupRep.GroupSplitCountry(groupInfo.VisitCountry);
- // 定义默认的流程节点
- var processs = await ProcessDataInitAsync(groupId, currUserId, visaCountries);
- _sqlSugar.BeginTran();
- foreach (var item in processs)
- {
- var processId = await _sqlSugar.Insertable(item).ExecuteReturnIdentityAsync();
- if (processId < 1)
- {
- _sqlSugar.RollbackTran();
- return new Result { Code = 400, Msg = "团组流程进度总览表添加失败!" };
- }
- item.Id = processId;
- // 记录流程日志
- await LogProcessOpAsync(null, item, "Create", currUserId);
- var nodes = item.Nodes.Select((nodeDto, index) => new Grp_ProcessNode
- {
- ProcessId = processId,
- NodeName = nodeDto.NodeName,
- NodeOrder = nodeDto.NodeOrder,
- OverallStatus = nodeDto.OverallStatus,
- NodeDescTips = nodeDto.NodeDescTips,
- //Country = nodeDto.Country,
- IsCurrent = nodeDto.IsCurrent,
- IsAssist = nodeDto.IsAssist,
- IsPart = nodeDto.IsPart,
- IsFileUp = nodeDto.IsFileUp,
- Remark = nodeDto.Remark
- }).ToList();
- var nodeIds = await _sqlSugar.Insertable(nodes).ExecuteCommandAsync();
- if (nodeIds < 1)
- {
- _sqlSugar.RollbackTran();
- return new Result { Code = 400, Msg = "团组流程进度流程节点添加失败!" };
- }
- //设置节点ID
- nodes = await _sqlSugar.Queryable<Grp_ProcessNode>().Where(x => x.IsDel == 0 && x.ProcessId == processId).ToListAsync();
- //记录节点日志
- foreach (var node in nodes)
- {
- await LogNodeOpAsync(null, node, "Create", currUserId);
- }
- }
- _sqlSugar.CommitTran();
- return new Result { Code = 200, Msg = "添加成功!" }; ;
- }
- /// <summary>
- /// 获取团组的所有流程及节点详情
- /// </summary>
- /// <param name="request">创建流程请求参数</param>
- /// <returns>创建的流程信息</returns>
- public async Task<Result> ProcessesDetailsAsync(int groupId)
- {
- //团组验证
- var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
- if (groupInfo == null)
- {
- return new Result { Code = 400, Msg = "团组不存在" };
- }
- // 检查是否已存在流程
- var existingProcesses = await _sqlSugar.Queryable<Grp_ProcessOverview>().Where(p => p.IsDel == 0 && p.GroupId == groupId).ToListAsync();
- if (!existingProcesses.Any())
- {
- //新建团组流程
- var res = await ProcessInitAsync(groupId, 4);
- if (res.Code != 200)
- {
- return res;
- }
- }
- var users = await _sqlSugar.Queryable<Sys_Users>().Select(x => new { x.Id, x.CnName }).ToListAsync();
- var processData = await _sqlSugar.Queryable<Grp_ProcessOverview>()
- .Where(p => p.GroupId == groupId && p.IsDel == 0)
- .Mapper(p => p.Nodes, p => p.Nodes.First().ProcessId)
- .ToListAsync();
- // 预先构建用户字典,提升查询性能
- var userDict = users.ToDictionary(u => u.Id, u => u.CnName);
- var processes = processData.Select(p =>
- {
- var orderedNodes = p.Nodes.OrderBy(n => n.NodeOrder).ToList();
- var totalNodes = orderedNodes.Count;
- return new
- {
- p.Id,
- p.GroupId,
- p.ProcessType,
- ProcessName = p.ProcessType.GetEnumDescription(),
- Nodes = orderedNodes.Select((n, index) =>
- {
- var isLastNode = index == totalNodes - 1;
- var isSecondLastNode = index == totalNodes - 2;
- var isFifthStep = index == 4;
- // 计算按钮状态
- bool isEnaAssistBtn = p.ProcessType == GroupProcessType.FeeSettle && n.NodeOrder == 1;
- // 文件上传按钮启用规则
- bool isEnaFileUpBtn = false;
- // 是否参与按钮启用
- bool isEnaPartBtn = false;
- // 规则1:商邀流程第五步启用参与按钮
- if (p.ProcessType == GroupProcessType.Invitation && isFifthStep)
- {
- isEnaPartBtn = true;
- }
- // 规则2:机票流程倒数第二步启用上传按钮
- else if (p.ProcessType == GroupProcessType.AirTicket && isSecondLastNode)
- {
- isEnaFileUpBtn = true;
- }
- // 规则3:默认流程节点最后一步启用上传按钮
- else if (isLastNode && p.ProcessType != GroupProcessType.FeeSettle)
- {
- isEnaFileUpBtn = true;
- }
- // 处理签证子节点
- List<VisaProcessNode> visaSubNodes = new();
- if (p.ProcessType == GroupProcessType.Visa && n.NodeOrder == 1)
- {
- visaSubNodes = JsonConvert.DeserializeObject<List<VisaProcessNode>>(n.Remark ?? "[]")
- ?? new List<VisaProcessNode>();
- }
- // 获取操作人姓名(使用字典提升性能)
- string operatorName = "-";
- if (n.Operator.HasValue && userDict.TryGetValue(n.Operator.Value, out var name))
- {
- operatorName = name;
- }
- return new
- {
- n.Id,
- n.ProcessId,
- n.NodeOrder,
- n.NodeName,
- n.OverallStatus,
- StatusText = n.OverallStatus.GetEnumDescription(),
- Operator = operatorName,
- OpeateTime = n.OperationTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? "-",
- ActualDone = n.ActualDone?.ToString("yyyy-MM-dd HH:mm:ss") ?? "",
- n.NodeDescTips,
- isEnaAssistBtn, // 是否启用财务流程首节点协助按钮
- n.IsAssist, // 财务流程首节点 存储值
- isEnaFileUpBtn, // 是否启用上传文件按钮
- n.IsFileUp, // 票据上传节点 存储值
- isEnaPartBtn, // 是否启用参与按钮
- n.IsPart, // 参与按钮 存储值
- visaSubNodes // 签证节点类型使用
- };
- }).ToList()
- };
- }).ToList();
- return new Result { Code = 200, Data = processes, Msg = "查询成功!" };
- }
- /// <summary>
- /// 更新节点状态
- /// </summary>
- /// <param name="nodeId">节点ID</param>
- /// <param name="currUserId">当前用户ID</param>
- /// <param name="processStatus">流程状态,默认为已完成</param>
- /// <returns>操作结果</returns>
- public async Task<Result> UpdateNodeStatusAsync(int nodeId, int currUserId, ProcessStatus processStatus = ProcessStatus.Completed)
- {
- try
- {
- // 使用事务确保数据一致性
- var result = await _sqlSugar.Ado.UseTranAsync(async () =>
- {
- // 1. 获取并验证节点
- var node = await _sqlSugar.Queryable<Grp_ProcessNode>()
- .FirstAsync(n => n.Id == nodeId && n.IsDel == 0) ?? throw new BusinessException("当前节点不存在或已被删除。");
- // 2. 获取流程信息,检查ProcessType
- var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
- .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0) ?? throw new BusinessException("关联的流程不存在。");
- // 3. 节点操作验证
- ValidateNodeOperation(node, processStatus);
- // 4. 存储更新前的值
- var before = new Grp_ProcessNode()
- {
- Id = node.Id,
- ProcessId = node.ProcessId,
- NodeName = node.NodeName,
- NodeOrder = node.NodeOrder,
- OverallStatus = node.OverallStatus,
- Operator = node.Operator,
- OperationTime = node.OperationTime,
- IsCurrent = node.IsCurrent,
- };
- // 5. 更新节点状态
- node.OverallStatus = processStatus;
- node.Operator = currUserId;
- node.OperationTime = DateTime.Now;
- var updateCount = await _sqlSugar.Updateable(node)
- .UpdateColumns(n => new
- {
- n.OverallStatus,
- n.Operator,
- n.OperationTime
- })
- .ExecuteCommandAsync();
- if (updateCount == 0)
- {
- throw new BusinessException("节点状态更新失败。");
- }
- // 6. 记录节点日志
- await LogNodeOpAsync(before, node, "Update", currUserId);
- // 7. 如果是完成当前节点,处理流程流转
- // 当前节点或者流程类型为商邀可进入状态流转
- if (processStatus == ProcessStatus.Completed && (node.IsCurrent || process.ProcessType == GroupProcessType.Invitation))
- {
- await ProcessCurrentNodeCompletionAsync(node, currUserId);
- }
- return new Result { Code = StatusCodes.Status200OK, Msg = "操作成功。" };
- });
- return result.IsSuccess ? result.Data : new Result
- {
- Code = StatusCodes.Status500InternalServerError,
- Msg = result.ErrorMessage
- };
- }
- catch (BusinessException ex)
- {
- // 业务异常
- return new Result { Code = StatusCodes.Status400BadRequest, Msg = ex.Message };
- }
- catch (Exception ex)
- {
- // 系统异常
- return new Result { Code = StatusCodes.Status500InternalServerError, Msg = "系统错误,请稍后重试" };
- }
- }
- /// <summary>
- /// 验证节点操作权限
- /// </summary>
- /// <param name="node">流程节点</param>
- /// <param name="targetStatus">目标状态</param>
- private static void ValidateNodeOperation(Grp_ProcessNode node, ProcessStatus targetStatus)
- {
- // 验证节点是否已完成
- if (node.OverallStatus == ProcessStatus.Completed)
- {
- throw new BusinessException("当前节点已完成,不可重复操作。");
- }
- // 验证状态流转是否合法(可选)
- //if (targetStatus != ProcessStatus.Completed)
- //{
- // throw new BusinessException("未开始或者进行中的节点只能重新完成,不可进行其他操作。");
- //}
- // 验证是否尝试将已完成节点改为其他状态
- if (node.OverallStatus == ProcessStatus.Completed && targetStatus != ProcessStatus.Completed)
- {
- throw new BusinessException("已完成节点不可修改状态。");
- }
- }
- /// <summary>
- /// 处理当前节点完成后的流程流转
- /// </summary>
- private async Task ProcessCurrentNodeCompletionAsync(Grp_ProcessNode currentNode, int currUserId)
- {
- // 1. 获取流程信息
- var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
- .FirstAsync(p => p.Id == currentNode.ProcessId && p.IsDel == 0);
- if (process == null)
- {
- throw new BusinessException("关联的流程不存在。");
- }
- var processBefore = new Grp_ProcessOverview()
- {
- Id = process.Id,
- GroupId = process.GroupId,
- ProcessOrder = process.ProcessOrder,
- ProcessType = process.ProcessType,
- OverallStatus = process.OverallStatus,
- StartTime = process.StartTime,
- EndTime = process.EndTime,
- UpdatedUserId = process.UpdatedUserId,
- UpdatedTime = process.UpdatedTime
- };
- // 2. 取消当前节点的当前状态
- var before = new Grp_ProcessNode()
- {
- Id = currentNode.Id,
- ProcessId = currentNode.ProcessId,
- NodeName = currentNode.NodeName,
- NodeOrder = currentNode.NodeOrder,
- OverallStatus = currentNode.OverallStatus,
- Operator = currentNode.Operator,
- OperationTime = currentNode.OperationTime,
- IsCurrent = currentNode.IsCurrent,
- };
- currentNode.IsCurrent = false;
- await _sqlSugar.Updateable(currentNode)
- .UpdateColumns(n => new { n.IsCurrent })
- .ExecuteCommandAsync();
- // 2.1 记录节点日志 取消当前节点状态
- await LogNodeOpAsync(before, currentNode, "Update", currUserId);
- // 3. 查找并激活下一个节点 商邀节点单独处理
- if (process.ProcessType == GroupProcessType.Invitation)
- {
- var invitaNodeStatus = await _sqlSugar.Queryable<Grp_ProcessNode>()
- .Where(x => x.IsDel == 0 && x.ProcessId == currentNode.ProcessId)
- .ToListAsync();
- int completedCount = invitaNodeStatus.Count(n => n.OverallStatus == ProcessStatus.Completed);
- int nodeCount = invitaNodeStatus.Count;
- if (completedCount == nodeCount) //全部子节点完成,该流程完成
- {
- process.OverallStatus = ProcessStatus.Completed;
- process.EndTime = DateTime.Now;
- }
- }
- else
- {
- var nextNode = await _sqlSugar.Queryable<Grp_ProcessNode>()
- .Where(n => n.ProcessId == currentNode.ProcessId
- && n.NodeOrder == currentNode.NodeOrder + 1
- && n.IsDel == 0)
- .FirstAsync();
- if (nextNode != null)
- {
- var nextNodeBefore = new Grp_ProcessNode()
- {
- Id = nextNode.Id,
- ProcessId = nextNode.ProcessId,
- NodeName = nextNode.NodeName,
- NodeOrder = nextNode.NodeOrder,
- OverallStatus = nextNode.OverallStatus,
- Operator = nextNode.Operator,
- OperationTime = nextNode.OperationTime,
- IsCurrent = nextNode.IsCurrent,
- };
- // 激活下一个节点
- nextNode.IsCurrent = true;
- nextNode.OverallStatus = ProcessStatus.InProgress;
- //nextNode.Operator = currUserId;
- //nextNode.OperationTime = DateTime.Now;
- var updateCount = await _sqlSugar.Updateable(nextNode)
- .UpdateColumns(n => new
- {
- n.IsCurrent,
- n.OverallStatus,
- n.Operator,
- n.OperationTime
- })
- .ExecuteCommandAsync();
- if (updateCount == 0)
- {
- throw new BusinessException("激活下一节点失败");
- }
- // 1.1 记录节点日志 激活下一节点当前节点状态
- await LogNodeOpAsync(nextNodeBefore, nextNode, "Start", currUserId);
- // 更新流程状态为进行中
- process.OverallStatus = ProcessStatus.InProgress;
- }
- else
- {
- // 下一节点不存在,整个流程完成
- process.OverallStatus = ProcessStatus.Completed;
- process.EndTime = DateTime.Now;
- }
- }
- // 4. 更新流程信息
- process.UpdatedUserId = currUserId;
- process.UpdatedTime = DateTime.Now;
- var processUpdateCount = await _sqlSugar.Updateable(process)
- .UpdateColumns(p => new
- {
- p.OverallStatus,
- p.EndTime,
- p.UpdatedUserId,
- p.UpdatedTime
- })
- .ExecuteCommandAsync();
- if (processUpdateCount == 0)
- {
- throw new BusinessException("流程状态更新失败。");
- }
- //记录流程日志
- await LogProcessOpAsync(processBefore, process, "Update", currUserId);
- }
- /// <summary>
- /// 更新签证节点信息及状态
- /// </summary>
- /// <param name="dto">签证节点更新数据传输对象</param>
- /// <returns>操作结果</returns>
- public async Task<Result> UpdateVisaNodeDetailsAsync(GroupProcessUpdateVisaNodeDetailsDto dto)
- {
- // 1. 获取并验证节点和流程
- var node = await _sqlSugar.Queryable<Grp_ProcessNode>()
- .FirstAsync(n => n.Id == dto.NodeId && n.IsDel == 0)
- ?? throw new BusinessException("当前节点不存在或已被删除。");
- var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
- .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0)
- ?? throw new BusinessException("当前流程不存在或已被删除。");
- if (process.ProcessType != GroupProcessType.Visa)
- {
- throw new BusinessException("当前流程节点不为签证流程,不可编辑。");
- }
- // 2. 检查签证子节点 字段信息是否全部填写
- var allSubNodesCompleted = dto.VisaSubNodes?.All(subNode => EntityExtensions.IsCompleted(subNode)) ?? false;
- // 2.1 存储更新前流程及节点信息
- var nodeBefore = new Grp_ProcessNode()
- {
- Id = node.Id,
- ProcessId = node.ProcessId,
- NodeName = node.NodeName,
- NodeOrder = node.NodeOrder,
- OverallStatus = node.OverallStatus,
- Operator = node.Operator,
- OperationTime = node.OperationTime,
- IsCurrent = node.IsCurrent,
- };
- var processBefore = new Grp_ProcessOverview()
- {
- Id = process.Id,
- GroupId = process.GroupId,
- ProcessOrder = process.ProcessOrder,
- ProcessType = process.ProcessType,
- OverallStatus = process.OverallStatus,
- StartTime = process.StartTime,
- EndTime = process.EndTime,
- UpdatedUserId = process.UpdatedUserId,
- UpdatedTime = process.UpdatedTime
- };
- // 3. 更新节点信息
- node.Remark = JsonConvert.SerializeObject(dto.VisaSubNodes);
- node.Operator = dto.CurrUserId;
- node.OperationTime = DateTime.Now;
- if (allSubNodesCompleted)
- {
- node.OverallStatus = ProcessStatus.Completed;
- process.OverallStatus = ProcessStatus.Completed;
- process.EndTime = DateTime.Now;
- process.UpdatedUserId = dto.CurrUserId;
- process.UpdatedTime = DateTime.Now;
- // 更新流程状态
- await _sqlSugar.Updateable(process)
- .UpdateColumns(p => new
- {
- p.OverallStatus,
- p.EndTime,
- p.UpdatedUserId,
- p.UpdatedTime
- })
- .ExecuteCommandAsync();
- //记录流程日志
- await LogProcessOpAsync(processBefore, process, "Update", dto.CurrUserId);
- }
- // 4. 保存节点更新
- await _sqlSugar.Updateable(node)
- .UpdateColumns(n => new
- {
- n.Remark,
- n.Operator,
- n.OperationTime,
- n.OverallStatus
- })
- .ExecuteCommandAsync();
- //记录节点日志
- await LogNodeOpAsync(nodeBefore, node, "Update", dto.CurrUserId);
- return new Result { Code = 200, Msg = "节点信息更新成功。" };
- }
- /// <summary>
- /// 更新节点信息及状态
- /// </summary>
- /// <param name="dto">签证节点更新数据传输对象</param>
- /// <returns>操作结果</returns>
- public async Task<Result> SetActualDoneAsync(GroupProcessSetActualDoneDto dto)
- {
- int nodeId = dto.NodeId;
- var isDtNul = DateTime.TryParse(dto.ActualDone, out DateTime actualDone);
- int currUserId = dto.CurrUserId;
- bool isAssist = dto.IsAssist;
- bool isFileUp = dto.IsFileUp;
- bool isPart = dto.IsPart;
- // 1. 获取并验证节点和流程
- var node = await _sqlSugar.Queryable<Grp_ProcessNode>()
- .FirstAsync(n => n.Id == nodeId && n.IsDel == 0)
- ?? throw new BusinessException("当前节点不存在或已被删除。");
- var process = await _sqlSugar.Queryable<Grp_ProcessOverview>()
- .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0)
- ?? throw new BusinessException("当前流程不存在或已被删除。");
- // 2.1 存储更新前流程及节点信息
- var nodeBefore = new Grp_ProcessNode()
- {
- Id = node.Id,
- ProcessId = node.ProcessId,
- NodeName = node.NodeName,
- NodeOrder = node.NodeOrder,
- OverallStatus = node.OverallStatus,
- Operator = node.Operator,
- OperationTime = node.OperationTime,
- IsCurrent = node.IsCurrent
- };
- if (isDtNul)
- {
- node.ActualDone = actualDone;
- }
- node.IsAssist = isAssist;
- node.IsFileUp = isFileUp;
- node.IsPart = isPart;
- // 3. 保存节点更新
- await _sqlSugar.Updateable(node)
- .UpdateColumns(n => new
- {
- ActualDone = isDtNul ? node.ActualDone : null,
- n.IsAssist,
- n.IsFileUp,
- n.IsPart,
- })
- .ExecuteCommandAsync();
- //记录节点日志
- await LogNodeOpAsync(nodeBefore, node, "Update", currUserId);
- return new Result { Code = 200, Msg = "实际操作时间设置成功。" };
- }
- #endregion
- #region 会务流程
- /// <summary>
- /// 设置节点流程模板
- /// </summary>
- /// <param name="groupId"></param>
- /// <param name="currUserId"></param>
- /// <returns></returns>
- public async Task<List<Grp_ConfProcessOverview>> DefaultConfProcessTemps(int groupId, int currUserId)
- {
- var temps = new List<Grp_ConfProcessOverview>();
- //团组验证
- var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
- if (groupInfo == null) return temps;
- //// 检查是否已存在流程
- //var existingProcesses = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
- // .Where(p => p.IsDel == 0 && p.GroupId == groupId)
- // .ToListAsync();
- //if (existingProcesses.Any()) return temps;
- #region 会务流程
- //参与人 participators
- var defaultParticipators = new List<ParticipatorInfo>() { new() { UserId = 213, UserName = "李新江" } };
- var defaultPorc1 = new List<Grp_ConfProcessNode>() {
- Grp_ConfProcessNode.Create(1,"方案/报价(含成本)","", ProcessStatus.InProgress,true,false, currUserId,defaultParticipators),
- Grp_ConfProcessNode.Create(2,"项目前期比选/招投标相关文件","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
- Grp_ConfProcessNode.Create(3,"参与投标","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
- Grp_ConfProcessNode.Create(4,"拟定/签订合同","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
- Grp_ConfProcessNode.Create(5,"场地预订/物料设计/对接活动所需的供应商/嘉宾邀约","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
- Grp_ConfProcessNode.Create(6,"现场执行","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
- Grp_ConfProcessNode.Create(7,"验收报告/决算表","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
- Grp_ConfProcessNode.Create(8,"跟进项目收款","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
- };
- temps.Add(Grp_ConfProcessOverview.Create(groupId, 1, ProcessStatus.InProgress, currUserId, defaultPorc1));
- var defaultPorc2 = new List<Grp_ConfProcessNode>() {
- Grp_ConfProcessNode.Create(1,"方案/报价(含成本)","", ProcessStatus.InProgress,true,false, currUserId,defaultParticipators),
- Grp_ConfProcessNode.Create(2,"拟定/签订合同","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
- Grp_ConfProcessNode.Create(3,"场地预订/物料设计/对接活动所需的供应商/嘉宾邀约","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
- Grp_ConfProcessNode.Create(4,"现场执行","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
- Grp_ConfProcessNode.Create(5,"验收报告/决算表","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
- Grp_ConfProcessNode.Create(6,"跟进项目收款","", ProcessStatus.UnStarted,false,false, currUserId,defaultParticipators),
- };
- temps.Add(Grp_ConfProcessOverview.Create(groupId, 2, ProcessStatus.InProgress, currUserId, defaultPorc1));
- #endregion
- return temps;
- }
- /// <summary>
- /// 团组会务流程初始化
- /// </summary>
- /// <param name="groupId">团组Id</param>
- /// <param name="currUserId">当前用户Id</param>
- /// <param name="nodeTempId">节点模板Id</param>
- /// <returns>创建的流程信息</returns>
- public async Task<Result> ConfProcessInitAsync(int groupId, int currUserId,int nodeTempId = 1)
- {
- //团组验证
- var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
- if (groupInfo == null)
- {
- return new Result { Code = 400, Msg = "团组不存在" };
- }
- // 检查是否已存在流程
- var existingProcesses = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
- .Where(p => p.IsDel == 0 && p.GroupId == groupId)
- .ToListAsync();
- if (existingProcesses.Any())
- {
- return new Result { Code = 400, Msg = "团组会务流程已存在" };
- }
- // 定义默认的流程节点
- var temps = await DefaultConfProcessTemps(groupId, currUserId);
- if (temps == null || temps.Count < 1)
- return new Result { Code = 400, Msg = "团组会务默认流程不存在" };
- var process = temps.FirstOrDefault(x => x.ProcessOrder == nodeTempId);
- process.CreateUserId = currUserId;
- _sqlSugar.BeginTran();
- try
- {
- var processId = await _sqlSugar.Insertable(process).ExecuteReturnIdentityAsync();
- if (processId < 1)
- {
- _sqlSugar.RollbackTran();
- return new Result { Code = 400, Msg = "团组会务流程进度总览添加失败!" };
- }
- process.Id = processId;
- // 记录流程日志
- await LogConfProcessOpAsync(null, process, "Create", currUserId);
- var nodes = process.Nodes.Select((nodeDto, index) => new Grp_ConfProcessNode
- {
- ProcessId = processId,
- NodeName = nodeDto.NodeName,
- NodeOrder = nodeDto.NodeOrder,
- Participator = JsonConvert.SerializeObject(nodeDto.Participators.Select(p => p.UserId).ToList()),
- OverallStatus = nodeDto.OverallStatus,
- NodeDescTips = nodeDto.NodeDescTips,
- //Country = nodeDto.Country,
- IsCurrent = nodeDto.IsCurrent,
- IsFileUp = nodeDto.IsFileUp,
- Remark = nodeDto.Remark,
- CreateUserId = currUserId,
- }).ToList();
- var nodeIds = await _sqlSugar.Insertable(nodes).ExecuteCommandAsync();
- if (nodeIds < 1)
- {
- _sqlSugar.RollbackTran();
- return new Result { Code = 400, Msg = "团组流程进度流程节点添加失败!" };
- }
- //设置节点ID
- nodes = await _sqlSugar.Queryable<Grp_ConfProcessNode>().Where(x => x.IsDel == 0 && x.ProcessId == processId).ToListAsync();
- //记录节点日志
- foreach (var node in nodes)
- {
- await LogConfNodeOpAsync(null, node, "Create", currUserId);
- }
- _sqlSugar.CommitTran();
- return new Result { Code = 200, Msg = "添加成功!" };
- }
- catch (Exception ex)
- {
- _sqlSugar.RollbackTran();
- return new Result { Code = 500, Msg = $"操作失败!msg:{ex.Message}" };
- }
- }
- /// <summary>
- /// 获取团组会务流程及节点详情
- /// </summary>
- /// <param name="groupId">团组Id</param>
- /// <param name="currUserId">当前用户Id</param>
- /// <returns></returns>
- public async Task<Result> ConfProcessesDetailsAsync(int groupId, int currUserId = 4)
- {
- //团组验证
- var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
- if (groupInfo == null)
- {
- return new Result { Code = 400, Msg = "团组不存在" };
- }
- // 检查是否已存在流程
- var existingProcesses = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
- .Where(p => p.IsDel == 0 && p.GroupId == groupId)
- .ToListAsync();
- if (!existingProcesses.Any())
- {
- //新建团组流程
- var res = await ConfProcessInitAsync(groupId, currUserId);
- if (res.Code != 200)
- {
- return res;
- }
- }
- var users = await _sqlSugar.Queryable<Sys_Users>().Select(x => new { x.Id, x.CnName }).ToListAsync();
- var processData = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
- .Where(p => p.GroupId == groupId && p.IsDel == 0)
- .Mapper(p => p.Nodes, p => p.Nodes.First().ProcessId)
- .ToListAsync();
- // 预先构建用户字典,提升查询性能
- var userDict = users.ToDictionary(u => u.Id, u => u.CnName);
- var processes = processData.Select(p =>
- {
- var orderedNodes = p.Nodes.OrderBy(n => n.NodeOrder).ToList();
- var totalNodes = orderedNodes.Count;
- return new ConfProcessOverInfoView()
- {
- Id = p.Id,
- GroupId = p.GroupId,
- ProcessType = p.ProcessType,
- ProcessName = p.ProcessType.GetEnumDescription(),
- Nodes = orderedNodes.Select((n, index) =>
- {
- //var isLastNode = index == totalNodes - 1;
- //// 文件上传按钮启用规则
- //bool isEnaFileUpBtn = false;
- //if (isLastNode )
- //{
- // isEnaFileUpBtn = true;
- //}
- //获取参与人姓名
- var participators = new List<ParticipatorInfo>();
- var participatorArray = JsonConvert.DeserializeObject<List<int>>(n.Participator);
- if (participatorArray?.Count > 0)
- {
- foreach (var item in participatorArray)
- {
- if (userDict.TryGetValue(item, out var userName))
- {
- participators.Add(new ParticipatorInfo
- {
- UserId = item,
- UserName = userName
- });
- }
- }
- }
- // 获取操作人姓名(使用字典提升性能)
- string operatorName = "-";
- if (n.Operator.HasValue && userDict.TryGetValue(n.Operator.Value, out var name))
- {
- operatorName = name;
- }
- return new ConfProcessNodeInfoView()
- {
- Id = n.Id,
- ProcessId = n.ProcessId,
- NodeOrder = n.NodeOrder,
- NodeName = n.NodeName,
- NodeDescTips = n.NodeDescTips,
- OverallStatus = n.OverallStatus,
- Participators = participators,
- StatusText = n.OverallStatus.GetEnumDescription(),
- Operator = operatorName,
- OpeateTime = n.OperationTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? "-",
- ActualDone = n.ActualDone?.ToString("yyyy-MM-dd HH:mm:ss") ?? "",
- IsFileUp = n.IsFileUp, // 票据上传节点 存储值
- };
- }).ToList()
- };
- }).ToList();
- return new Result { Code = 200, Data = processes, Msg = "查询成功!" };
- }
- /// <summary>
- /// 更新节点模板
- /// </summary>
- /// <param name="groupId">团组Id</param>
- /// <param name="nodeTempId">节点模板Id</param>
- /// <param name="currUserId">当前用户Id</param>
- /// <returns></returns>
- public async Task<Result> ConfProcessChangeNodeTempSaveAsync(int groupId, int nodeTempId, int currUserId)
- {
- //节点模板id验证
- var nodeTempIds = new List<int>() { 1, 2 };
- if (!nodeTempIds.Contains(nodeTempId))
- {
- return new Result { Code = 400, Msg = "请传入有效的节点模板Id" };
- }
- //团组验证
- var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(g => g.Id == groupId);
- if (groupInfo == null)
- {
- return new Result { Code = 400, Msg = "团组不存在" };
- }
- _sqlSugar.BeginTran();
- try
- {
- // 检查是否已存在流程
- var existingProcesses = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
- .Where(p => p.IsDel == 0 && p.GroupId == groupId)
- .ToListAsync();
- if (existingProcesses.Any())
- {
- //删除 原有的节点模板
- var parentIds = existingProcesses.Select(x => x.Id).ToList();
- var updProcesses = await _sqlSugar.Updateable<Grp_ConfProcessOverview>()
- .SetColumns(x => x.DeleteUserId == currUserId)
- .SetColumns(x => x.DeleteTime == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
- .SetColumns(x => x.IsDel == 1)
- .Where(x => parentIds.Contains(x.Id))
- .ExecuteCommandAsync();
- var updProcessNodes = await _sqlSugar.Updateable<Grp_ConfProcessNode>()
- .SetColumns(x => x.DeleteUserId == currUserId)
- .SetColumns(x => x.DeleteTime == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
- .SetColumns(x => x.IsDel == 1)
- .Where(x => parentIds.Contains(x.ProcessId))
- .ExecuteCommandAsync();
- }
- //更新模板节点信息
- var result = await ConfProcessInitAsync(groupId,currUserId,nodeTempId);
- if (result.Code == 200)
- {
- var infoResult = await ConfProcessesDetailsAsync(groupId, currUserId);
- if (infoResult.Code == 200)
- {
- _sqlSugar.CommitTran();
- return infoResult;
- }
- }
- _sqlSugar.RollbackTran();
- return new Result { Code = 500, Msg = $"操作失败!Msg:{result.Msg}" };
- }
- catch (Exception ex)
- {
- _sqlSugar.RollbackTran();
- return new Result { Code = 500, Msg = $"操作失败!Msg:{ex.Message}" };
- }
- }
- /// <summary>
- /// 更新团组会务流程节点状态
- /// </summary>
- /// <param name="nodeId">节点ID</param>
- /// <param name="currUserId">当前用户ID</param>
- /// <param name="processStatus">流程状态,默认为已完成</param>
- /// <returns>操作结果</returns>
- public async Task<Result> UpdateConfNodeStatusAsync(int nodeId, int currUserId, ProcessStatus processStatus = ProcessStatus.Completed)
- {
- try
- {
- // 使用事务确保数据一致性
- var result = await _sqlSugar.Ado.UseTranAsync(async () =>
- {
- // 1. 获取并验证节点
- var node = await _sqlSugar.Queryable<Grp_ConfProcessNode>()
- .FirstAsync(n => n.Id == nodeId && n.IsDel == 0) ?? throw new BusinessException("当前节点不存在或已被删除。");
- // 2. 获取流程信息,检查ProcessType
- var process = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
- .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0) ?? throw new BusinessException("关联的流程不存在。");
- // 3. 节点操作验证
- ValidateConfNodeOperation(node, processStatus);
- // 4. 存储更新前的值
- var before = new Grp_ConfProcessNode()
- {
- Id = node.Id,
- ProcessId = node.ProcessId,
- NodeName = node.NodeName,
- NodeDescTips = node.NodeDescTips,
- NodeOrder = node.NodeOrder,
- Participator = node.Participator,
- OverallStatus = node.OverallStatus,
- Operator = node.Operator,
- OperationTime = node.OperationTime,
- IsCurrent = node.IsCurrent,
- ActualDone = node.ActualDone,
- IsFileUp = node.IsFileUp,
- };
- // 5. 更新节点状态
- node.OverallStatus = processStatus;
- node.Operator = currUserId;
- node.OperationTime = DateTime.Now;
- var updateCount = await _sqlSugar.Updateable(node)
- .UpdateColumns(n => new
- {
- n.OverallStatus,
- n.Operator,
- n.OperationTime
- })
- .ExecuteCommandAsync();
- if (updateCount == 0)
- {
- throw new BusinessException("节点状态更新失败。");
- }
- // 6. 记录节点日志
- await LogConfNodeOpAsync(before, node, "Update", currUserId);
- // 7. 如果是完成当前节点,处理流程流转
- if (processStatus == ProcessStatus.Completed)
- {
- await ConfProcessCurrentNodeCompletionAsync(node, currUserId);
- }
- return new Result { Code = StatusCodes.Status200OK, Msg = "操作成功。" };
- });
- return result.IsSuccess ? result.Data : new Result
- {
- Code = StatusCodes.Status500InternalServerError,
- Msg = result.ErrorMessage
- };
- }
- catch (BusinessException ex)
- {
- // 业务异常
- return new Result { Code = StatusCodes.Status400BadRequest, Msg = ex.Message };
- }
- catch (Exception ex)
- {
- // 系统异常
- return new Result { Code = StatusCodes.Status500InternalServerError, Msg = "系统错误,请稍后重试" };
- }
- }
- /// <summary>
- /// 验证会务流程节点操作权限
- /// </summary>
- /// <param name="node">流程节点</param>
- /// <param name="targetStatus">目标状态</param>
- private static void ValidateConfNodeOperation(Grp_ConfProcessNode node, ProcessStatus targetStatus)
- {
- // 验证节点是否已完成
- if (node.OverallStatus == ProcessStatus.Completed)
- {
- throw new BusinessException("当前节点已完成,不可重复操作。");
- }
- // 验证状态流转是否合法(可选)
- //if (targetStatus != ProcessStatus.Completed)
- //{
- // throw new BusinessException("未开始或者进行中的节点只能重新完成,不可进行其他操作。");
- //}
- // 验证是否尝试将已完成节点改为其他状态
- if (node.OverallStatus == ProcessStatus.Completed && targetStatus != ProcessStatus.Completed)
- {
- throw new BusinessException("已完成节点不可修改状态。");
- }
- }
- /// <summary>
- /// 处理当前会务流程节点完成后的流程流转
- /// </summary>
- private async Task ConfProcessCurrentNodeCompletionAsync(Grp_ConfProcessNode currentNode, int currUserId)
- {
- // 1. 获取流程信息
- var process = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
- .FirstAsync(p => p.Id == currentNode.ProcessId && p.IsDel == 0) ?? throw new BusinessException("关联的流程不存在。");
- var processBefore = new Grp_ConfProcessOverview()
- {
- Id = process.Id,
- GroupId = process.GroupId,
- ProcessOrder = process.ProcessOrder,
- ProcessType = process.ProcessType,
- OverallStatus = process.OverallStatus,
- StartTime = process.StartTime,
- EndTime = process.EndTime,
- UpdatedUserId = process.UpdatedUserId,
- UpdatedTime = process.UpdatedTime
- };
- // 2. 取消当前节点的当前状态
- var before = new Grp_ConfProcessNode()
- {
- Id = currentNode.Id,
- ProcessId = currentNode.ProcessId,
- NodeName = currentNode.NodeName,
- NodeOrder = currentNode.NodeOrder,
- OverallStatus = currentNode.OverallStatus,
- Operator = currentNode.Operator,
- OperationTime = currentNode.OperationTime,
- IsCurrent = currentNode.IsCurrent,
- };
- currentNode.IsCurrent = false;
- await _sqlSugar.Updateable(currentNode)
- .UpdateColumns(n => new { n.IsCurrent })
- .ExecuteCommandAsync();
- // 2.1 记录节点日志 取消当前节点状态
- await LogConfNodeOpAsync(before, currentNode, "Update", currUserId);
- // 3. 查找并激活下一个节点
- var nextNode = await _sqlSugar.Queryable<Grp_ConfProcessNode>()
- .Where(n => n.ProcessId == currentNode.ProcessId
- && n.NodeOrder == currentNode.NodeOrder + 1
- && n.IsDel == 0)
- .FirstAsync();
- if (nextNode != null)
- {
- var nextNodeBefore = new Grp_ConfProcessNode()
- {
- Id = nextNode.Id,
- ProcessId = nextNode.ProcessId,
- NodeName = nextNode.NodeName,
- NodeOrder = nextNode.NodeOrder,
- OverallStatus = nextNode.OverallStatus,
- Operator = nextNode.Operator,
- OperationTime = nextNode.OperationTime,
- IsCurrent = nextNode.IsCurrent,
- };
- // 激活下一个节点
- nextNode.IsCurrent = true;
- nextNode.OverallStatus = ProcessStatus.InProgress;
- var updateCount = await _sqlSugar.Updateable(nextNode)
- .UpdateColumns(n => new
- {
- n.IsCurrent,
- n.OverallStatus,
- n.Operator,
- n.OperationTime
- })
- .ExecuteCommandAsync();
- if (updateCount == 0)
- {
- throw new BusinessException("激活下一节点失败");
- }
- // 1.1 记录节点日志 激活下一节点当前节点状态
- await LogConfNodeOpAsync(nextNodeBefore, nextNode, "Start", currUserId);
- // 更新流程状态为进行中
- process.OverallStatus = ProcessStatus.InProgress;
- }
- else
- {
- // 下一节点不存在,整个流程完成
- process.OverallStatus = ProcessStatus.Completed;
- process.EndTime = DateTime.Now;
- }
- // 4. 更新流程信息
- process.UpdatedUserId = currUserId;
- process.UpdatedTime = DateTime.Now;
- var processUpdateCount = await _sqlSugar.Updateable(process)
- .UpdateColumns(p => new
- {
- p.OverallStatus,
- p.EndTime,
- p.UpdatedUserId,
- p.UpdatedTime
- })
- .ExecuteCommandAsync();
- if (processUpdateCount == 0)
- {
- throw new BusinessException("流程状态更新失败。");
- }
- //记录流程日志
- await LogConfProcessOpAsync(processBefore, process, "Update", currUserId);
- }
- /// <summary>
- /// 更新节点信息
- /// </summary>
- /// <param name="dto">签证节点更新数据传输对象</param>
- /// <returns>操作结果</returns>
- public async Task<Result> SetNodeInfoAsync(ConfProcessSetActualDoneDto dto)
- {
- //参与人验证
- if (dto.Participators?.Count < 1)
- {
- throw new BusinessException("参与人不能为空。");
- }
- int nodeId = dto.NodeId;
- var isDtNul = DateTime.TryParse(dto.ActualDone, out DateTime actualDone);
- int currUserId = dto.CurrUserId;
- bool isFileUp = dto.IsFileUp;
- // 1. 获取并验证节点和流程
- var node = await _sqlSugar.Queryable<Grp_ConfProcessNode>()
- .FirstAsync(n => n.Id == nodeId && n.IsDel == 0)
- ?? throw new BusinessException("当前节点不存在或已被删除。");
- var process = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
- .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0)
- ?? throw new BusinessException("当前流程不存在或已被删除。");
- // 2.1 存储更新前流程及节点信息
- var nodeBefore = new Grp_ConfProcessNode()
- {
- Id = node.Id,
- ProcessId = node.ProcessId,
- NodeName = node.NodeName,
- NodeOrder = node.NodeOrder,
- OverallStatus = node.OverallStatus,
- Operator = node.Operator,
- OperationTime = node.OperationTime,
- IsCurrent = node.IsCurrent
- };
- if (isDtNul)
- {
- node.ActualDone = actualDone;
- }
- node.IsFileUp = isFileUp;
- node.Participator = JsonConvert.SerializeObject(node.Participators.Select(x => x.UserId).ToList());
- // 3. 保存节点更新
- await _sqlSugar.Updateable(node)
- .UpdateColumns(n => new
- {
- ActualDone = isDtNul ? node.ActualDone : null,
- n.Participator,
- n.IsFileUp,
- })
- .ExecuteCommandAsync();
- //记录节点日志
- await LogConfNodeOpAsync(nodeBefore, node, "Update", currUserId);
- return new Result { Code = 200, Msg = "实际操作时间设置成功。" };
- }
- #endregion
- #region 操作日志
- #region 团组流程
- /// <summary>
- /// 记录流程操作日志
- /// </summary>
- /// <param name="before">操作前</param>
- /// <param name="after">操作后</param>
- /// <param name="opType">操作类型(Create - 创建、Update - 更新、Complete - 完成)</param>
- /// <param name="operId">操作人ID</param>
- /// <returns>异步任务</returns>
- public async Task LogProcessOpAsync(Grp_ProcessOverview before, Grp_ProcessOverview after, string opType, int operId)
- {
- var chgDetails = GetProcessChgDetails(before, after);
- var log = new Grp_ProcessLog
- {
- ProcessId = after?.Id ?? before?.Id,
- GroupId = after?.GroupId ?? before?.GroupId ?? 0,
- OpType = opType,
- OpDesc = GenerateProcessOpDesc(opType, before, after, chgDetails),
- BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
- AfterData = after != null ? JsonConvert.SerializeObject(after, GetJsonSettings()) : null,
- ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
- CreateUserId = operId
- };
- await _sqlSugar.Insertable(log).ExecuteCommandAsync();
- }
- /// <summary>
- /// 记录节点操作日志
- /// </summary>
- /// <param name="before">操作前</param>
- /// <param name="after">操作后</param>
- /// <param name="opType">操作类型(Create - 创建、Update - 更新、Start - 启动、Complete - 完成)</param>
- /// <param name="operId">操作人ID</param>
- /// <returns>异步任务</returns>
- public async Task LogNodeOpAsync(Grp_ProcessNode before, Grp_ProcessNode after, string opType, int operId)
- {
- var chgDetails = GetNodeChgDetails(before, after);
- var log = new Grp_ProcessLog
- {
- NodeId = after?.Id ?? before?.Id,
- ProcessId = after?.ProcessId ?? before?.ProcessId,
- GroupId = 0, // 通过流程ID关联获取
- OpType = opType,
- OpDesc = GenerateNodeOpDesc(opType, before, after, chgDetails),
- BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
- AfterData = after != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
- ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
- CreateUserId = operId
- };
- await _sqlSugar.Insertable(log).ExecuteCommandAsync();
- }
- /// <summary>
- /// 获取流程变更详情
- /// </summary>
- /// <param name="before">变更前</param>
- /// <param name="after">变更后</param>
- /// <returns>变更详情</returns>
- private List<FieldChgDetail> GetProcessChgDetails(Grp_ProcessOverview before, Grp_ProcessOverview after)
- {
- var chgDetails = new List<FieldChgDetail>();
- if (before == null || after == null) return chgDetails;
- var props = typeof(Grp_ProcessOverview).GetProperties(BindingFlags.Public | BindingFlags.Instance)
- .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
- foreach (var prop in props)
- {
- var beforeVal = prop.GetValue(before);
- var afterVal = prop.GetValue(after);
- if (!Equals(beforeVal, afterVal))
- {
- chgDetails.Add(new FieldChgDetail
- {
- FieldName = prop.Name,
- BeforeValue = FormatVal(beforeVal),
- AfterValue = FormatVal(afterVal)
- });
- }
- }
- return chgDetails;
- }
- /// <summary>
- /// 获取节点变更详情
- /// </summary>
- /// <param name="before">变更前</param>
- /// <param name="after">变更后</param>
- /// <returns>变更详情</returns>
- private List<FieldChgDetail> GetNodeChgDetails(Grp_ProcessNode before, Grp_ProcessNode after)
- {
- var chgDetails = new List<FieldChgDetail>();
- if (before == null || after == null) return chgDetails;
- var props = typeof(Grp_ProcessNode).GetProperties(BindingFlags.Public | BindingFlags.Instance)
- .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
- foreach (var prop in props)
- {
- var beforeVal = prop.GetValue(before);
- var afterVal = prop.GetValue(after);
- if (!Equals(beforeVal, afterVal))
- {
- chgDetails.Add(new FieldChgDetail
- {
- FieldName = prop.Name,
- BeforeValue = FormatVal(beforeVal),
- AfterValue = FormatVal(afterVal)
- });
- }
- }
- return chgDetails;
- }
- /// <summary>
- /// 生成流程操作描述
- /// </summary>
- /// <param name="opType">操作类型</param>
- /// <param name="before">操作前</param>
- /// <param name="after">操作后</param>
- /// <param name="chgDetails">变更详情</param>
- /// <returns>操作描述</returns>
- private string GenerateProcessOpDesc(string opType, Grp_ProcessOverview before,
- Grp_ProcessOverview after, List<FieldChgDetail> chgDetails)
- {
- var processType = after?.ProcessType ?? before?.ProcessType;
- var processName = GetProcessTypeName(processType);
- if (!chgDetails.Any())
- {
- return opType switch
- {
- "Create" => $"创建流程:{processName}",
- "Update" => $"更新流程:{processName} - 无变更",
- "Start" => $"启动流程:{processName}",
- "Complete" => $"完成流程:{processName}",
- "Delete" => $"删除流程:{processName}",
- _ => $"{opType}:{processName}"
- };
- }
- var chgDesc = string.Join("; ", chgDetails.Select(x =>
- $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
- return $"{GetOpTypeDisplay(opType)}:{processName} - {chgDesc}";
- }
- /// <summary>
- /// 生成节点操作描述
- /// </summary>
- /// <param name="opType">操作类型</param>
- /// <param name="before">操作前</param>
- /// <param name="after">操作后</param>
- /// <param name="chgDetails">变更详情</param>
- /// <returns>操作描述</returns>
- private string GenerateNodeOpDesc(string opType, Grp_ProcessNode before,
- Grp_ProcessNode after, List<FieldChgDetail> chgDetails)
- {
- var nodeName = after?.NodeName ?? before?.NodeName;
- if (!chgDetails.Any())
- {
- return opType switch
- {
- "Create" => $"创建节点:{nodeName}",
- "Update" => $"更新节点:{nodeName} - 无变更",
- "Start" => $"启动节点:{nodeName}",
- "Complete" => $"完成节点:{nodeName}",
- //"Delete" => $"删除节点:{nodeName}",
- _ => $"{opType}:{nodeName}"
- };
- }
- var chgDesc = string.Join("; ", chgDetails.Select(x =>
- $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
- return $"{GetOpTypeDisplay(opType)}:{nodeName} - {chgDesc}";
- }
- /// <summary>
- /// 获取流程类型名称
- /// </summary>
- /// <param name="processType">流程类型</param>
- /// <returns>流程名称</returns>
- private static string GetProcessTypeName(GroupProcessType? processType)
- {
- return processType switch
- {
- GroupProcessType.Invitation => "商邀报批",
- GroupProcessType.Visa => "签证",
- GroupProcessType.AirTicket => "机票",
- GroupProcessType.Hotel => "酒店",
- GroupProcessType.LocalGuide => "地接",
- GroupProcessType.FeeSettle => "费用结算",
- _ => "未知流程"
- };
- }
- /// <summary>
- /// 获取流程日志
- /// </summary>
- /// <param name="processId">流程ID</param>
- /// <returns>日志列表</returns>
- public async Task<List<Grp_ProcessLog>> GetProcessLogsAsync(int processId)
- {
- return await _sqlSugar.Queryable<Grp_ProcessLog>()
- .Where(x => x.ProcessId == processId)
- .OrderByDescending(x => x.CreateTime)
- .ToListAsync();
- }
- /// <summary>
- /// 获取团组流程日志
- /// </summary>
- /// <param name="groupId">团组ID</param>
- /// <returns>日志列表</returns>
- public async Task<List<Grp_ProcessLog>> GetGroupLogsAsync(int groupId)
- {
- return await _sqlSugar.Queryable<Grp_ProcessLog>()
- .Where(x => x.GroupId == groupId)
- .OrderByDescending(x => x.CreateTime)
- .ToListAsync();
- }
- #endregion
- #region 会务流程
- /// <summary>
- /// 记录会务流程操作日志
- /// </summary>
- /// <param name="before">操作前</param>
- /// <param name="after">操作后</param>
- /// <param name="opType">操作类型(Create - 创建、Update - 更新、Complete - 完成)</param>
- /// <param name="operId">操作人ID</param>
- /// <returns>异步任务</returns>
- public async Task LogConfProcessOpAsync(Grp_ConfProcessOverview before, Grp_ConfProcessOverview after, string opType, int operId)
- {
- var chgDetails = GetConfProcessChgDetails(before, after);
- var log = new Grp_ConfProcessLog
- {
- ProcessId = after?.Id ?? before?.Id,
- GroupId = after?.GroupId ?? before?.GroupId ?? 0,
- OpType = opType,
- OpDesc = GenerateConfProcessOpDesc(opType, before, after, chgDetails),
- BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
- AfterData = after != null ? JsonConvert.SerializeObject(after, GetJsonSettings()) : null,
- ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
- CreateUserId = operId
- };
- await _sqlSugar.Insertable(log).ExecuteCommandAsync();
- }
- /// <summary>
- /// 记录会务节点操作日志
- /// </summary>
- /// <param name="before">操作前</param>
- /// <param name="after">操作后</param>
- /// <param name="opType">操作类型(Create - 创建、Update - 更新、Start - 启动、Complete - 完成)</param>
- /// <param name="operId">操作人ID</param>
- /// <returns>异步任务</returns>
- public async Task LogConfNodeOpAsync(Grp_ConfProcessNode before, Grp_ConfProcessNode after, string opType, int operId)
- {
- var chgDetails = GetConfNodeChgDetails(before, after);
- var log = new Grp_ConfProcessLog
- {
- NodeId = after?.Id ?? before?.Id,
- ProcessId = after?.ProcessId ?? before?.ProcessId,
- GroupId = 0, // 通过流程ID关联获取
- OpType = opType,
- OpDesc = GenerateConfNodeOpDesc(opType, before, after, chgDetails),
- BeforeData = before != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
- AfterData = after != null ? JsonConvert.SerializeObject(before, GetJsonSettings()) : null,
- ChgFields = string.Join(",", chgDetails.Select(x => x.FieldName)),
- CreateUserId = operId
- };
- await _sqlSugar.Insertable(log).ExecuteCommandAsync();
- }
- /// <summary>
- /// 获取流程变更详情
- /// </summary>
- /// <param name="before">变更前</param>
- /// <param name="after">变更后</param>
- /// <returns>变更详情</returns>
- private static List<FieldChgDetail> GetConfProcessChgDetails(Grp_ConfProcessOverview before, Grp_ConfProcessOverview after)
- {
- var chgDetails = new List<FieldChgDetail>();
- if (before == null || after == null) return chgDetails;
- var props = typeof(Grp_ConfProcessOverview).GetProperties(BindingFlags.Public | BindingFlags.Instance)
- .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
- foreach (var prop in props)
- {
- var beforeVal = prop.GetValue(before);
- var afterVal = prop.GetValue(after);
- if (!Equals(beforeVal, afterVal))
- {
- chgDetails.Add(new FieldChgDetail
- {
- FieldName = prop.Name,
- BeforeValue = FormatVal(beforeVal),
- AfterValue = FormatVal(afterVal)
- });
- }
- }
- return chgDetails;
- }
- /// <summary>
- /// 获取节点变更详情
- /// </summary>
- /// <param name="before">变更前</param>
- /// <param name="after">变更后</param>
- /// <returns>变更详情</returns>
- private static List<FieldChgDetail> GetConfNodeChgDetails(Grp_ConfProcessNode before, Grp_ConfProcessNode after)
- {
- var chgDetails = new List<FieldChgDetail>();
- if (before == null || after == null) return chgDetails;
- var props = typeof(Grp_ConfProcessNode).GetProperties(BindingFlags.Public | BindingFlags.Instance)
- .Where(p => p.CanRead && p.CanWrite && !IsExclField(p.Name));
- foreach (var prop in props)
- {
- var beforeVal = prop.GetValue(before);
- var afterVal = prop.GetValue(after);
- if (!Equals(beforeVal, afterVal))
- {
- chgDetails.Add(new FieldChgDetail
- {
- FieldName = prop.Name,
- BeforeValue = FormatVal(beforeVal),
- AfterValue = FormatVal(afterVal)
- });
- }
- }
- return chgDetails;
- }
- /// <summary>
- /// 生成流程操作描述
- /// </summary>
- /// <param name="opType">操作类型</param>
- /// <param name="before">操作前</param>
- /// <param name="after">操作后</param>
- /// <param name="chgDetails">变更详情</param>
- /// <returns>操作描述</returns>
- private static string GenerateConfProcessOpDesc(string opType, Grp_ConfProcessOverview before,
- Grp_ConfProcessOverview after, List<FieldChgDetail> chgDetails)
- {
- var processType = after?.ProcessType ?? before?.ProcessType;
- var processName = GetConfProcessTypeName(processType);
- if (!chgDetails.Any())
- {
- return opType switch
- {
- "Create" => $"创建流程:{processName}",
- "Update" => $"更新流程:{processName} - 无变更",
- "Start" => $"启动流程:{processName}",
- "Complete" => $"完成流程:{processName}",
- "Delete" => $"删除流程:{processName}",
- _ => $"{opType}:{processName}"
- };
- }
- var chgDesc = string.Join("; ", chgDetails.Select(x =>
- $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
- return $"{GetOpTypeDisplay(opType)}:{processName} - {chgDesc}";
- }
- /// <summary>
- /// 生成节点操作描述
- /// </summary>
- /// <param name="opType">操作类型</param>
- /// <param name="before">操作前</param>
- /// <param name="after">操作后</param>
- /// <param name="chgDetails">变更详情</param>
- /// <returns>操作描述</returns>
- private static string GenerateConfNodeOpDesc(string opType, Grp_ConfProcessNode before,
- Grp_ConfProcessNode after, List<FieldChgDetail> chgDetails)
- {
- var nodeName = after?.NodeName ?? before?.NodeName;
- if (!chgDetails.Any())
- {
- return opType switch
- {
- "Create" => $"创建节点:{nodeName}",
- "Update" => $"更新节点:{nodeName} - 无变更",
- "Start" => $"启动节点:{nodeName}",
- "Complete" => $"完成节点:{nodeName}",
- //"Delete" => $"删除节点:{nodeName}",
- _ => $"{opType}:{nodeName}"
- };
- }
- var chgDesc = string.Join("; ", chgDetails.Select(x =>
- $"{GetFieldDisplayName(x.FieldName)} ({x.BeforeValue} -> {x.AfterValue})"));
- return $"{GetOpTypeDisplay(opType)}:{nodeName} - {chgDesc}";
- }
- /// <summary>
- /// 获取流程类型名称
- /// </summary>
- /// <param name="processType">流程类型</param>
- /// <returns>流程名称</returns>
- private static string GetConfProcessTypeName(ConfProcessType? processType)
- {
- return processType switch
- {
- ConfProcessType.Conference => "会务",
- _ => "未知流程"
- };
- }
- /// <summary>
- /// 获取会务流程日志
- /// </summary>
- /// <param name="processId">流程ID</param>
- /// <returns>日志列表</returns>
- public async Task<List<Grp_ConfProcessLog>> GetConfProcessLogsAsync(int processId)
- {
- return await _sqlSugar.Queryable<Grp_ConfProcessLog>()
- .Where(x => x.ProcessId == processId)
- .OrderByDescending(x => x.CreateTime)
- .ToListAsync();
- }
- /// <summary>
- /// 获取团组会务流程日志
- /// </summary>
- /// <param name="groupId">团组ID</param>
- /// <returns>日志列表</returns>
- public async Task<List<Grp_ConfProcessLog>> GetGroupConfLogsAsync(int groupId)
- {
- return await _sqlSugar.Queryable<Grp_ConfProcessLog>()
- .Where(x => x.GroupId == groupId)
- .OrderByDescending(x => x.CreateTime)
- .ToListAsync();
- }
- #endregion
- #region 日志私有方法
- /// <summary>
- /// 获取JSON序列化设置
- /// </summary>
- /// <returns>JSON设置</returns>
- private static JsonSerializerSettings GetJsonSettings()
- {
- return new JsonSerializerSettings
- {
- ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
- NullValueHandling = NullValueHandling.Ignore,
- DateFormatString = "yyyy-MM-dd HH:mm:ss",
- Formatting = Formatting.None
- };
- }
- /// <summary>
- /// 获取操作类型显示
- /// </summary>
- /// <param name="opType">操作类型</param>
- /// <returns>显示名称</returns>
- private static string GetOpTypeDisplay(string opType)
- {
- return opType switch
- {
- "Create" => "创建",
- "Update" => "更新",
- "Start" => "启动",
- "Complete" => "完成",
- "Delete" => "删除",
- "StatusChg" => "状态变更",
- _ => opType
- };
- }
- /// <summary>
- /// 获取字段显示名称
- /// </summary>
- /// <param name="fieldName">字段名</param>
- /// <returns>显示名称</returns>
- private static string GetFieldDisplayName(string fieldName)
- {
- return fieldName switch
- {
- "OverallStatus" => "状态",
- "ProcessOrder" => "流程顺序",
- "StartTime" => "开始时间",
- "EndTime" => "结束时间",
- "UpdatedUserId" => "更新人",
- "UpdatedTime" => "更新时间",
- "NodeOrder" => "节点顺序",
- "NodeName" => "节点名称",
- "NodeDescTips" => "节点描述提示",
- "IsCurrent" => "当前节点",
- "Participator" => "参与人",
- "Operator" => "操作人",
- "OperationTime" => "操作时间",
- "ActualDone" => "实际完成时间",
- "IsFileUp" => "是否上传文件(签证、机票、酒店、地接 流程结尾节点使用)",
- "IsAssist" => "是否协助(财务流程首节点使用)",
- "IsPart" => "是否参与(商邀 第五步使用)",
- _ => fieldName
- };
- }
- /// <summary>
- /// 格式化值显示
- /// </summary>
- /// <param name="value">值</param>
- /// <returns>格式化值</returns>
- private static string FormatVal(object value)
- {
- if (value == null) return "空";
- if (value is ProcessStatus status)
- {
- return status switch
- {
- ProcessStatus.UnStarted => "未开始",
- ProcessStatus.InProgress => "进行中",
- ProcessStatus.Completed => "已完成",
- _ => status.ToString()
- };
- }
- if (value is bool boolVal) return boolVal ? "是" : "否";
- if (value is DateTime dateVal) return dateVal.ToString("yyyy-MM-dd HH:mm");
- var strVal = value.ToString();
- return string.IsNullOrEmpty(strVal) ? "空" : strVal;
- }
- /// <summary>
- /// 检查是否排除字段
- /// </summary>
- /// <param name="fieldName">字段名</param>
- /// <returns>是否排除</returns>
- private static bool IsExclField(string fieldName)
- {
- var exclFields = new List<string>
- {
- "Id", "CreateTime", "CreateUserId", "Nodes", "Process" // 导航属性
- };
- return exclFields.Contains(fieldName);
- }
- #endregion
- #endregion
- }
- }
|