From 21d11a9805909bc06d9254dedf5cb5adc39cd0b4 Mon Sep 17 00:00:00 2001 From: fuenmao <980740792@qq.com> Date: Mon, 13 Oct 2025 09:51:04 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B6=85=E7=BA=A7=E7=AE=A1=E7=90=86=E5=91=98?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E6=95=B0=E6=8D=AE=E4=B8=8D=E5=8F=97=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/Medicinal/MedicinalService.cs | 35 ++++++++----------- .../Prescription/PrescriptionService.cs | 17 ++++----- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/SimpleAdmin.Application/Services/Medicinal/MedicinalService.cs b/SimpleAdmin.Application/Services/Medicinal/MedicinalService.cs index acfd831..0b20973 100644 --- a/SimpleAdmin.Application/Services/Medicinal/MedicinalService.cs +++ b/SimpleAdmin.Application/Services/Medicinal/MedicinalService.cs @@ -27,7 +27,7 @@ public class MedicinalService : DbRepository, IMedicinalService //var dataScope = await _sysUserService.GetLoginUserApiDataScope(); //dataScope?.Remove(UserManager.OrgId); - // 2. 分页查询药材基本信息(1次查询,原有逻辑不变) + // 2. 分页查询药材基本信息 var query = this.AsQueryable() //.WhereIF(dataScope.Any(), m => dataScope.Contains(m.Id)) .WhereIF(!string.IsNullOrEmpty(medicinalName), m => m.MedicinalName.Contains(medicinalName)) @@ -42,11 +42,10 @@ public class MedicinalService : DbRepository, IMedicinalService var pageInfo = await query.ToPagedListAsync(page, size); if (!pageInfo.List.Any()) return pageInfo; - // 3. 批量计算:成本、剩余价值、总使用量(仅2次查询,无循环查询) + // 3. 批量计算:成本、剩余价值、总使用量 var medicinalIds = pageInfo.List.Select(dto => dto.Id).ToList(); var costAndUsageDict = await BatchCalculateCostAndValue(medicinalIds); - - // 4. 内存中赋值所有指标(无数据库查询) + foreach (var item in pageInfo.List) { if (costAndUsageDict.TryGetValue(item.Id, out var metrics)) @@ -102,10 +101,8 @@ public class MedicinalService : DbRepository, IMedicinalService .LeftJoin((ph, p, pe) => p.PersonId == pe.Id) // 再关联员工 .LeftJoin((ph, p, pe, emp) => pe.EmployeeId != null && pe.EmployeeId == emp.Id) - // 条件也使用多参数 .Where((ph, p, pe, emp) => ph.MedicinalId == medicinalId) - .OrderByDescending((ph, p, pe, emp) => p.VisitDate) - // 投影使用所有表别名 + .OrderByDescending((ph, p, pe, emp) => p.CreateTime) .Select((ph, p, pe, emp) => new MedicinalConsumptionRecordDto { AmountG = ph.AmountG, @@ -114,7 +111,6 @@ public class MedicinalService : DbRepository, IMedicinalService EmployeeName = emp == null ? null : emp.Name, UseTime = p.VisitDate }) - // 分页 .ToPagedListAsync(page, size); } @@ -142,7 +138,7 @@ public class MedicinalService : DbRepository, IMedicinalService foreach (var item in returnList.List) { - if (item.AddTime.Date == DateTime.Now.Date) + if (item.AddTime.Date == DateTime.Now.Date || UserManager.SuperAdmin) { item.CanEdit = true; } @@ -156,7 +152,7 @@ public class MedicinalService : DbRepository, IMedicinalService /// public async Task AddMedicinal(AddMedicinalInput input) { - if (input.TotalPrice <= 0 || input.AddWeight <= 0) throw Oops.Bah("请填写正确的金额或者重量!"); + if (input.TotalPrice <= 0 || input.AddWeight <= 0) throw Oops.Bah("请填写正确的金额!"); // 开启事务 await Context.Ado.BeginTranAsync(); try @@ -213,21 +209,20 @@ public class MedicinalService : DbRepository, IMedicinalService /// /// 修改药材添加记录 /// - /// /// /// /// public async Task UpdateAdditionRecord(UpdateMedicinalAddLogInput input) { - if(input.TotalPrice<=0 || input.AddWeight<=0) throw Oops.Bah("请填写正确的金额或者重量!"); + if(input.TotalPrice<=0 || input.AddWeight<=0) throw Oops.Bah("请填写正确的金额!"); var log = await Context.Queryable().Where(l => l.Id == input.Id).FirstAsync(); if (log == null) { throw Oops.Bah("记录不存在!"); } - // 检查是否是当天记录 - if (log.CreateTime.Value.Date != DateTime.Now.Date) + // 检查是否是当天记录,超级管理员随时都可以操作 + if (log.CreateTime.Value.Date != DateTime.Now.Date && !UserManager.SuperAdmin) { throw Oops.Bah("只能修改当天的记录!"); } @@ -282,8 +277,8 @@ public class MedicinalService : DbRepository, IMedicinalService throw Oops.Bah("记录不存在!"); } - // 检查是否是当天记录 - if (log.CreateTime.Value.Date != DateTime.Now.Date) + // 检查是否是当天记录,超级管理员随时都可以操作 + if (log.CreateTime.Value.Date != DateTime.Now.Date && !UserManager.SuperAdmin) { throw Oops.Bah("只能删除当天的记录!"); } @@ -334,13 +329,13 @@ public class MedicinalService : DbRepository, IMedicinalService var result = new Dictionary(); if (!medicinalIds.Any()) return result; - // 1. 批量查询所有药材的【当前库存】(1次查询) + // 1. 批量查询所有药材的【当前库存】 var medicinals = await Context.Queryable() .Where(m => medicinalIds.Contains(m.Id)) .Select(m => new { m.Id, m.Number }) // Number = 当前库存 .ToListAsync(); - // 2. 批量查询所有药材的【入库记录】(1次查询,仅查需要的字段) + // 2. 批量查询所有药材的【入库记录】 var allAddLogs = await Context.Queryable() .Where(l => medicinalIds.Contains(l.MedicinalId)) .OrderBy(l => l.CreateTime) // 先进先出:按入库时间排序 @@ -352,7 +347,7 @@ public class MedicinalService : DbRepository, IMedicinalService }) .ToListAsync(); - // 3. 按【药材ID】分组入库记录(内存处理,无数据库查询) + // 3. 按【药材ID】分组入库记录 var groupedLogs = allAddLogs .GroupBy(l => l.MedicinalId) .ToDictionary(g => g.Key, g => g.ToList()); @@ -377,7 +372,7 @@ public class MedicinalService : DbRepository, IMedicinalService // 计算总使用量:总入库量 - 当前库存(若结果为负,说明数据异常,按0处理) totalUsedWeight = Math.Max(0, totalIn - med.Number); - // 若有使用量,继续用先进先出法计算使用成本 + // 若有使用量,先进先出法计算使用成本 if (totalUsedWeight > 0 && groupedLogs.TryGetValue(med.Id, out var validLogs)) { decimal remainingToCalculate = totalUsedWeight; diff --git a/SimpleAdmin.Application/Services/Prescription/PrescriptionService.cs b/SimpleAdmin.Application/Services/Prescription/PrescriptionService.cs index 5006675..11733c6 100644 --- a/SimpleAdmin.Application/Services/Prescription/PrescriptionService.cs +++ b/SimpleAdmin.Application/Services/Prescription/PrescriptionService.cs @@ -53,7 +53,10 @@ public class PrescriptionService : DbRepository, IPrescriptionServ // 设置可编辑状态 foreach (var item in prescriptionPage.List) { - item.CanEdit = item.CreateTime.Date == DateTime.Now.Date; + if (item.CreateTime.Date == DateTime.Now.Date || UserManager.SuperAdmin) + { + item.CanEdit = true; + } } // 获取当前页所有处方的药材明细 @@ -109,7 +112,6 @@ public class PrescriptionService : DbRepository, IPrescriptionServ //家属成本多十个点 if (item.PersonType== PersonType.Relative) { - // 四舍五入规则 item.TotalMedicineCost = Math.Round(item.TotalMedicineCost * (decimal)1.1, 2, MidpointRounding.AwayFromZero); } } @@ -133,7 +135,7 @@ public class PrescriptionService : DbRepository, IPrescriptionServ // 批量查询所有涉及药材的入库记录(按时间排序) var addLogs = await Context.Queryable() .Where(l => medicinalIds.Contains(l.MedicinalId)) - .OrderBy(l => l.CreateTime) // FIFO核心:按入库时间排序 + .OrderBy(l => l.CreateTime) .Select(l => new MedicinalFifoRecord { MedicinalId = l.MedicinalId, @@ -228,7 +230,7 @@ public class PrescriptionService : DbRepository, IPrescriptionServ Name = pe.Name }) .FirstAsync(); - + employee = empPerson; } var RelationShipName = await GetRelationTypes(); @@ -392,7 +394,7 @@ public class PrescriptionService : DbRepository, IPrescriptionServ }; await Context.Storageable(person).ExecuteCommandAsync(); - // ---------- 4. 处方保存/编辑逻辑(沿用原逻辑) ---------- + // ---------- 4. 处方保存/编辑逻辑---------- Prescription prescription; if (input.Id.HasValue) // 编辑 { @@ -402,7 +404,7 @@ public class PrescriptionService : DbRepository, IPrescriptionServ if (prescription == null) throw Oops.Bah("处方不存在"); - if (prescription.CreateTime.Value.Date != DateTime.Now.Date) + if (prescription.CreateTime.Value.Date != DateTime.Now.Date && !UserManager.SuperAdmin) throw Oops.Bah("只能编辑当天的处方"); // 查询原有消耗记录并恢复库存 @@ -415,7 +417,6 @@ public class PrescriptionService : DbRepository, IPrescriptionServ foreach (var g in oldHerbs.GroupBy(x => x.MedicinalId)) { // 恢复库存(加回已消耗) - // 注意:使用 Updateable 批量操作替换为 SetColumns 表达式累加数值 await Context.Updateable() .SetColumns(m => new Medicinal { Number = m.Number + g.Sum(x => x.AmountG) }) .Where(m => m.Id == g.Key) @@ -423,7 +424,7 @@ public class PrescriptionService : DbRepository, IPrescriptionServ } } - // 删除原有处方药材(物理删除或软删按你系统策略) + // 删除原有处方药材 await Context.Deleteable() .Where(ph => ph.PrescriptionId == input.Id.Value) .ExecuteCommandAsync();