diff --git a/src/YhCore.Repository/IRepository.cs b/src/YhCore.Repository/IRepository.cs index bff9c53..f4126e8 100644 --- a/src/YhCore.Repository/IRepository.cs +++ b/src/YhCore.Repository/IRepository.cs @@ -19,6 +19,13 @@ namespace YhCore.Repository Task AnyAsync(Expression> predicate); Task CountAsync(Expression> predicate = null); + // ---- 读取(追踪版)---- + // 查出的实体受变更追踪器管理,修改属性后直接 SaveChangesAsync 即可落库。 + // 仅供写场景使用,纯查询请用上面的 NoTracking 版本。 + Task> FindTrackedAsync(Expression> predicate); + Task> GetAllTrackedAsync(); + Task FirstOrDefaultTrackedAsync(Expression> predicate); + /// 分页查询(页码从 0 开始),对应 PageUtil + PageVO Task> PageAsync(Expression> predicate, int page, int size, Expression> orderBy = null, bool asc = true); diff --git a/src/YhCore.Repository/RepositoryBase.cs b/src/YhCore.Repository/RepositoryBase.cs index a59a822..216415d 100644 --- a/src/YhCore.Repository/RepositoryBase.cs +++ b/src/YhCore.Repository/RepositoryBase.cs @@ -43,6 +43,21 @@ namespace YhCore.Repository return await DbSet.AsNoTracking().FirstOrDefaultAsync(predicate); } + public virtual async Task> FindTrackedAsync(Expression> predicate) + { + return await DbSet.Where(predicate).ToListAsync(); + } + + public virtual async Task> GetAllTrackedAsync() + { + return await DbSet.ToListAsync(); + } + + public virtual async Task FirstOrDefaultTrackedAsync(Expression> predicate) + { + return await DbSet.FirstOrDefaultAsync(predicate); + } + public virtual async Task AnyAsync(Expression> predicate) { return await DbSet.AnyAsync(predicate); diff --git a/src/YhCore.Service/Impl/DictionaryService.cs b/src/YhCore.Service/Impl/DictionaryService.cs index 6fbfa5b..a2c8930 100644 --- a/src/YhCore.Service/Impl/DictionaryService.cs +++ b/src/YhCore.Service/Impl/DictionaryService.cs @@ -125,7 +125,7 @@ namespace YhCore.Service.Impl throw new DException("已存在该分类,请重新填写!"); } - var obj = await _dic.FirstOrDefaultAsync(x => x.SerID == model.SerID); + var obj = await _dic.FirstOrDefaultTrackedAsync(x => x.SerID == model.SerID); if (obj == null) throw new DException("未找到要更新的分类!"); obj.Name = model.Name; @@ -141,7 +141,7 @@ namespace YhCore.Service.Impl { if (model == null) throw new DException("参数错误!"); - var current = await _dic.FirstOrDefaultAsync(m => m.SerID == model.SerID); + var current = await _dic.FirstOrDefaultTrackedAsync(m => m.SerID == model.SerID); if (current == null) throw new DException("未找到要编辑的分类!"); var dup = await _dic.AnyAsync(m => diff --git a/src/YhCore.Service/Impl/DocPrintClassifyService.cs b/src/YhCore.Service/Impl/DocPrintClassifyService.cs index 306b610..048cc89 100644 --- a/src/YhCore.Service/Impl/DocPrintClassifyService.cs +++ b/src/YhCore.Service/Impl/DocPrintClassifyService.cs @@ -119,7 +119,7 @@ namespace YhCore.Service.Impl { if (ids == null || ids.Length == 0) throw new DException("ids is not null"); - var items = await _dic.FindAsync(m => ids.Contains(m.SerID)); + var items = await _dic.FindTrackedAsync(m => ids.Contains(m.SerID)); if (items.Count == 0) throw new DException("未找到要删除的分类!"); foreach (var item in items) @@ -189,7 +189,7 @@ namespace YhCore.Service.Impl { if (ids == null || ids.Length == 0) throw new DException("ids is not null"); - var items = await _dic.FindAsync(m => ids.Contains(m.SerID.ToString())); + var items = await _dic.FindTrackedAsync(m => ids.Contains(m.SerID.ToString())); foreach (var item in items) { item.Enable = 3; @@ -200,7 +200,7 @@ namespace YhCore.Service.Impl public async Task SetDictionaryOrdAsync(string schSerId, int dicSerId, bool show) { - var obj = await _ord.FirstOrDefaultAsync(m => m.DicSerID == dicSerId && m.SchSerID == schSerId); + var obj = await _ord.FirstOrDefaultTrackedAsync(m => m.DicSerID == dicSerId && m.SchSerID == schSerId); if (obj == null) { await _ord.AddAsync(new Common_DictionaryOrd @@ -224,7 +224,7 @@ namespace YhCore.Service.Impl if (string.IsNullOrEmpty(ids)) throw new DException("ids is not null"); var ssids = ids.Split(',').Select(a => int.Parse(a)).ToArray(); - var list = await _ord.FindAsync(m => m.SchSerID == schSerId); + var list = await _ord.FindTrackedAsync(m => m.SchSerID == schSerId); var i = 0; foreach (var id in ssids) @@ -274,7 +274,7 @@ namespace YhCore.Service.Impl public async Task SetSchZyBitAsync(string schSerId, bool zyBit) { - var obj = await _zybit.FirstOrDefaultAsync(a => a.SchSerID == schSerId); + var obj = await _zybit.FirstOrDefaultTrackedAsync(a => a.SchSerID == schSerId); if (obj == null) { await _zybit.AddAsync(new WY_SchZyBit { SchSerID = schSerId, ziyuanbit = zyBit }); diff --git a/src/YhCore.Service/Impl/ReviewProcessService.cs b/src/YhCore.Service/Impl/ReviewProcessService.cs index 71cb8da..9c995b3 100644 --- a/src/YhCore.Service/Impl/ReviewProcessService.cs +++ b/src/YhCore.Service/Impl/ReviewProcessService.cs @@ -159,7 +159,7 @@ namespace YhCore.Service.Impl a.flowname != null && a.flowname.Trim() == flowname && a.enable != 3)).Count; if (dup > 1) throw new DException("流程名不能重名!"); - var info = await _flowinfo.FirstOrDefaultAsync(a => a.SerID == model.SerID); + var info = await _flowinfo.FirstOrDefaultTrackedAsync(a => a.SerID == model.SerID); if (info == null) throw new DException("未找到要修改的流程!"); info.flowname = model.flowname; @@ -198,7 +198,7 @@ namespace YhCore.Service.Impl { if (model == null) throw new DException("参数错误!"); - var info = await _flowstep.FirstOrDefaultAsync(a => a.SerID == model.SerID); + var info = await _flowstep.FirstOrDefaultTrackedAsync(a => a.SerID == model.SerID); if (info == null) throw new DException("未找到要修改的步骤!"); info.flowid = model.flowid; @@ -207,7 +207,7 @@ namespace YhCore.Service.Impl await _flowstep.UpdateAsync(info); // 重写该步骤的人员绑定(先清后加) - var existing = await _groupuser.FindAsync(a => a.usergroup == info.SerID); + var existing = await _groupuser.FindTrackedAsync(a => a.usergroup == info.SerID); foreach (var itt in existing) { await _groupuser.RemoveAsync(itt); @@ -243,7 +243,7 @@ namespace YhCore.Service.Impl if (string.IsNullOrWhiteSpace(ids)) throw new DException("ids is not null"); var lids = ids.Split(',').Select(a => int.Parse(a)).ToArray(); - var flows = await _flowinfo.FindAsync(a => lids.Contains(a.SerID)); + var flows = await _flowinfo.FindTrackedAsync(a => lids.Contains(a.SerID)); foreach (var item in flows) { item.enable = 3; @@ -256,7 +256,7 @@ namespace YhCore.Service.Impl if (ids == null || ids.Length == 0) throw new DException("ids is not null"); var intIds = ids.Select(d => (int)d).ToArray(); - var objlist = await _flowstep.FindAsync(m => intIds.Contains(m.SerID)); + var objlist = await _flowstep.FindTrackedAsync(m => intIds.Contains(m.SerID)); foreach (var item in objlist) { await _flowstep.RemoveAsync(item); diff --git a/src/YhCore.Service/Impl/WYDocPrintService.cs b/src/YhCore.Service/Impl/WYDocPrintService.cs index 74c882c..3f45d1d 100644 --- a/src/YhCore.Service/Impl/WYDocPrintService.cs +++ b/src/YhCore.Service/Impl/WYDocPrintService.cs @@ -138,7 +138,7 @@ namespace YhCore.Service.Impl { await SetChaoSendAsync(int.Parse(uid), schSerId, mysenduserid); - var model = await _doc.FirstOrDefaultAsync(m => m.SerID == serId); + var model = await _doc.FirstOrDefaultTrackedAsync(m => m.SerID == serId); if (model != null) { model.SendUserID = senduserid; @@ -249,7 +249,7 @@ namespace YhCore.Service.Impl /// public async Task EditAsync(WYDocPrintSaveDto model) { - var info = await _doc.FirstOrDefaultAsync(x => x.SerID == model.SerID); + var info = await _doc.FirstOrDefaultTrackedAsync(x => x.SerID == model.SerID); if (info == null) throw new DException("未找到该文印记录!"); info.DocType = model.DocType; @@ -303,7 +303,7 @@ namespace YhCore.Service.Impl var idArr = ids.Split(',') .Where(a => !string.IsNullOrEmpty(a) && decimal.TryParse(a, out _)) .Select(decimal.Parse).ToArray(); - var objlist = await _doc.FindAsync(m => idArr.Contains(m.SerID)); + var objlist = await _doc.FindTrackedAsync(m => idArr.Contains(m.SerID)); foreach (var item in objlist) { await _doc.RemoveAsync(item); @@ -334,7 +334,7 @@ namespace YhCore.Service.Impl await _flowtranct.AddAsync(tranct); await _flowtranct.SaveChangesAsync(); - var docModel = await _doc.FirstOrDefaultAsync(m => m.SerID == model.dataid); + var docModel = await _doc.FirstOrDefaultTrackedAsync(m => m.SerID == model.dataid); if (docModel != null) { docModel.DocStatus = model.actionstatus.ToString(); @@ -665,7 +665,7 @@ namespace YhCore.Service.Impl using var tx = await _db.Database.BeginTransactionAsync(); try { - var rdmodel = await _doc.FirstOrDefaultAsync(m => m.SerID == model.dataid); + var rdmodel = await _doc.FirstOrDefaultTrackedAsync(m => m.SerID == model.dataid); if (rdmodel == null) throw new DException("未找到该文印记录!"); if (rdmodel.DocStatus != "0") @@ -766,7 +766,7 @@ namespace YhCore.Service.Impl /// public async Task AffirmApplyforOkAsync(FlowTranctDto model) { - var itt = await _doc.FirstOrDefaultAsync(m => m.SerID == model.dataid); + var itt = await _doc.FirstOrDefaultTrackedAsync(m => m.SerID == model.dataid); if (itt == null) throw new DException("未找到该文印记录!"); var claimed = await _flowtranct.AnyAsync(a => a.dataid == model.dataid @@ -817,7 +817,7 @@ namespace YhCore.Service.Impl { if (flowid <= 0) return; - var list = await _zhipai.FirstOrDefaultAsync(a => a.UserID == uid && a.MSerID == dataid + var list = await _zhipai.FirstOrDefaultTrackedAsync(a => a.UserID == uid && a.MSerID == dataid && a.SchSerID == schSerId && a.FlowID == flowid && a.FlowStepID == flowstepid && a.type == type); if (list == null) { @@ -1240,7 +1240,7 @@ namespace YhCore.Service.Impl /// 自由流程挂链(旧 ApproveFlow.SetPrimarySteptID:从 stepid 起沿 laststep 链回写 dataid)。 private async Task SetPrimarySteptIDAsync(int dataid, int stepid) { - var node = await _freeflowinfo.FirstOrDefaultAsync(a => a.SerID == stepid); + var node = await _freeflowinfo.FirstOrDefaultTrackedAsync(a => a.SerID == stepid); if (node != null) { node.dataid = dataid; @@ -1250,7 +1250,7 @@ namespace YhCore.Service.Impl var nextSer = stepid; while (true) { - var next = await _freeflowinfo.FirstOrDefaultAsync(a => a.laststep == nextSer); + var next = await _freeflowinfo.FirstOrDefaultTrackedAsync(a => a.laststep == nextSer); if (next == null) break; next.dataid = dataid; await _freeflowinfo.SaveChangesAsync(); @@ -1261,7 +1261,7 @@ namespace YhCore.Service.Impl /// 抄送人落库(旧 ApproveFlow.SetChaoSend:按用户+flowType 全删再插)。 private async Task SetChaoSendAsync(int uid, string schSerId, string senduserid) { - var olds = await _senduser.FindAsync(a => a.flowType == DocPrintConstants.AppCode && a.userid == uid); + var olds = await _senduser.FindTrackedAsync(a => a.flowType == DocPrintConstants.AppCode && a.userid == uid); foreach (var old in olds) { await _senduser.RemoveAsync(old); @@ -1287,7 +1287,7 @@ namespace YhCore.Service.Impl var fids = ParseIds(upFile); foreach (var fid in fids) { - var file = await _files.FirstOrDefaultAsync(x => x.SerID == fid); + var file = await _files.FirstOrDefaultTrackedAsync(x => x.SerID == fid); if (file != null) { file.TopicID = topicId; diff --git a/src/YhCore.Service/Impl/WYSHRoleService.cs b/src/YhCore.Service/Impl/WYSHRoleService.cs index 74852a9..6d11c1e 100644 --- a/src/YhCore.Service/Impl/WYSHRoleService.cs +++ b/src/YhCore.Service/Impl/WYSHRoleService.cs @@ -95,7 +95,7 @@ namespace YhCore.Service.Impl public async Task SaveIsSHRoleAsync(string schSerId, string val) { - var list = await _dic.FindAsync(a => a.Schserid == schSerId && a.typeID == "isUseSHRole"); + var list = await _dic.FindTrackedAsync(a => a.Schserid == schSerId && a.typeID == "isUseSHRole"); if (list.Count > 0) { list[0].CodeID = val; @@ -155,7 +155,7 @@ namespace YhCore.Service.Impl if (list.Count > 0) throw new DException("审核角色名称重复了,更新失败!"); - var list2 = await _role.FindAsync(a => a.SchSerId == schSerId && a.SerID == serId); + var list2 = await _role.FindTrackedAsync(a => a.SchSerId == schSerId && a.SerID == serId); if (list2.Count > 0) { list2[0].SHRoleName = roleName; @@ -196,7 +196,7 @@ namespace YhCore.Service.Impl if (list.Count > 0) throw new DException("审核角色名称已经使用,更新失败!"); - var tp = await _role.FirstOrDefaultAsync(a => a.SerID == roleInfo.SerID); + var tp = await _role.FirstOrDefaultTrackedAsync(a => a.SerID == roleInfo.SerID); if (tp != null) { tp.SHRoleName = roleInfo.SHRoleName; @@ -211,9 +211,9 @@ namespace YhCore.Service.Impl public async Task DelSHRoleAsync(string schSerId, List listSHRoleId) { var ids = listSHRoleId ?? new List(); - var list = await _role.FindAsync(a => a.SchSerId == schSerId && ids.Contains(a.SerID)); - var listSHTea = await _shTea.FindAsync(a => a.SchSerID == schSerId && ids.Contains(a.SHRoleID)); - var listBSHTea = await _bshTea.FindAsync(a => a.SchSerID == schSerId && ids.Contains(a.SHRoleID)); + var list = await _role.FindTrackedAsync(a => a.SchSerId == schSerId && ids.Contains(a.SerID)); + var listSHTea = await _shTea.FindTrackedAsync(a => a.SchSerID == schSerId && ids.Contains(a.SHRoleID)); + var listBSHTea = await _bshTea.FindTrackedAsync(a => a.SchSerID == schSerId && ids.Contains(a.SHRoleID)); if (list.Count > 0) { @@ -236,7 +236,7 @@ namespace YhCore.Service.Impl public async Task DelSHTeaRowAsync(string schSerId, decimal serId) { - var list = await _shTea.FindAsync(a => a.SchSerID == schSerId && a.SerID == serId); + var list = await _shTea.FindTrackedAsync(a => a.SchSerID == schSerId && a.SerID == serId); if (list.Count > 0) { await _shTea.RemoveAsync(list[0]); @@ -269,7 +269,7 @@ namespace YhCore.Service.Impl else { // update - var list2 = await _shTea.FindAsync(a => a.SchSerID == schSerId && a.SerID == data.SerID); + var list2 = await _shTea.FindTrackedAsync(a => a.SchSerID == schSerId && a.SerID == data.SerID); if (list2.Count > 0) { var tp = list2[0]; @@ -287,7 +287,7 @@ namespace YhCore.Service.Impl public async Task EditSHTeaAsync(string schSerId, string userId, decimal shRoleId, List listSHTea) { - var list = await _shTea.FindAsync(a => a.SchSerID == schSerId && a.SHRoleID == shRoleId); + var list = await _shTea.FindTrackedAsync(a => a.SchSerID == schSerId && a.SHRoleID == shRoleId); if (list.Count > 0) { @@ -343,7 +343,7 @@ namespace YhCore.Service.Impl public async Task EditBSHTeaAsync(string schSerId, string userId, decimal shRoleId, List listBSHTea) { - var list = await _bshTea.FindAsync(a => a.SchSerID == schSerId && a.SHRoleID == shRoleId); + var list = await _bshTea.FindTrackedAsync(a => a.SchSerID == schSerId && a.SHRoleID == shRoleId); if (list.Count > 0) { @@ -486,7 +486,7 @@ namespace YhCore.Service.Impl if (dup.Count > 1) throw new DException("流程名不能重名!"); - var list = await _flowinfo.FindAsync(a => a.SerID == model.SerID); + var list = await _flowinfo.FindTrackedAsync(a => a.SerID == model.SerID); if (list.Count > 0) { var info = list[0]; @@ -547,12 +547,12 @@ namespace YhCore.Service.Impl public async Task StepDeleteAsync(List ids) { - var list = await _flowstep.FindAsync(a => ids.Contains(a.SerID)); + var list = await _flowstep.FindTrackedAsync(a => ids.Contains(a.SerID)); if (list.Count > 0) { foreach (var item in list) { - var listrole = await _groupSHRole.FindAsync(a => a.usergroup == item.SerID); + var listrole = await _groupSHRole.FindTrackedAsync(a => a.usergroup == item.SerID); if (listrole.Count > 0) { foreach (var r in listrole) @@ -572,7 +572,7 @@ namespace YhCore.Service.Impl if (list.Count > 0) throw new DException("同一个流程中有相同步骤名称,更新失败!"); - var list2 = await _flowstep.FindAsync(a => a.SerID == serId); + var list2 = await _flowstep.FindTrackedAsync(a => a.SerID == serId); if (list2.Count > 0) { list2[0].StepName = stepName; @@ -620,7 +620,7 @@ namespace YhCore.Service.Impl if (list.Count <= 1) throw new DException("至少要有一个审核角色,删除失败!"); - var list2 = await _groupSHRole.FindAsync(a => a.usergroup == stepid && a.SHRoleid == shRoleid); + var list2 = await _groupSHRole.FindTrackedAsync(a => a.usergroup == stepid && a.SHRoleid == shRoleid); foreach (var item in list2) await _groupSHRole.RemoveAsync(item); await _groupSHRole.SaveChangesAsync(); @@ -942,7 +942,7 @@ namespace YhCore.Service.Impl var listggall = await _dic.FindAsync(a => (a.Schserid == schSerId || a.Schserid == null) && listYZFLID.Contains(a.typeID)); - var list = await _docNum.FindAsync(a => a.DocPrintID == docPrintID); + var list = await _docNum.FindTrackedAsync(a => a.DocPrintID == docPrintID); foreach (var item in data) { @@ -1018,7 +1018,7 @@ namespace YhCore.Service.Impl { if (data == null) return; - var list = await _docSH.FindAsync(a => a.DocPrintSerID == docPrintID && a.flowSerID == data.SerID); + var list = await _docSH.FindTrackedAsync(a => a.DocPrintSerID == docPrintID && a.flowSerID == data.SerID); if (list.Count > 0) { foreach (var item in list) @@ -1715,10 +1715,10 @@ namespace YhCore.Service.Impl var liststepByUser = (await _docSH.FindAsync(a => a.DocPrintSerID == dataid && a.stepSerID == model.stepid && a.flowSerID == flowid)).ToList(); - var listSH = (await _flowtranct.FindAsync(a => + var listSH = (await _flowtranct.FindTrackedAsync(a => a.dataid == model.dataid && a.Pid == model.Pid && a.stepid == model.stepid)).ToList(); - var listDoc = await _doc.FindAsync(a => a.SerID == dataid); + var listDoc = await _doc.FindTrackedAsync(a => a.SerID == dataid); var liststepByUserall = (await _docSH.FindAsync(a => a.DocPrintSerID == dataid && a.flowSerID == flowid)).ToList(); @@ -2022,7 +2022,7 @@ namespace YhCore.Service.Impl public async Task TuiHuiAsync(string userId, string cont, decimal docId) { - var list = await _doc.FindAsync(a => a.SerID == docId); + var list = await _doc.FindTrackedAsync(a => a.SerID == docId); if (list.Count == 0) throw new DException("退回失败!"); @@ -2038,7 +2038,7 @@ namespace YhCore.Service.Impl public async Task UnTuiHuiAsync(string userId, decimal docId) { - var list = await _doc.FindAsync(a => a.SerID == docId); + var list = await _doc.FindTrackedAsync(a => a.SerID == docId); if (list.Count == 0) throw new DException("撤销退回失败!"); @@ -2067,8 +2067,8 @@ namespace YhCore.Service.Impl var listdec = listDocPrintId.Select(d => (decimal?)d).ToList(); - var listdoc = await _doc.FindAsync(a => listDocPrintId.Contains(a.SerID)); - var listdocnum = await _docNum.FindAsync(a => listdec.Contains(a.DocPrintID)); + var listdoc = await _doc.FindTrackedAsync(a => listDocPrintId.Contains(a.SerID)); + var listdocnum = await _docNum.FindTrackedAsync(a => listdec.Contains(a.DocPrintID)); DateTime dtnow = DateTime.Now; foreach (var item in listdoc) @@ -2281,7 +2281,7 @@ namespace YhCore.Service.Impl public async Task SetDownFileTimesAsync(int serId) { - var list = await _upfiles.FindAsync(a => a.SerID == serId); + var list = await _upfiles.FindTrackedAsync(a => a.SerID == serId); if (list.Count == 0) throw new DException("记录下载次数失败!"); diff --git a/src/YhCore.Task/Jobs/CleanupLogJob.cs b/src/YhCore.Task/Jobs/CleanupLogJob.cs index fef9f92..9625662 100644 --- a/src/YhCore.Task/Jobs/CleanupLogJob.cs +++ b/src/YhCore.Task/Jobs/CleanupLogJob.cs @@ -31,7 +31,7 @@ namespace YhCore.Tasks.Jobs try { - var oldList = await repo.FindAsync(l => l.CreateTime < cutoff); + var oldList = await repo.FindTrackedAsync(l => l.CreateTime < cutoff); if (oldList.Count == 0) { logger.LogInformation("审计日志清理完成:无 30 天前的数据");