using System.CodeDom.Compiler; using JNPF.Common.Core.Manager; using JNPF.Common.Filter; using JNPF.Common.Security; using JNPF.DependencyInjection; using JNPF.DynamicApiController; using JNPF.Systems.Entitys.Permission; using JNPF.Systems.Entitys.System; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using NPOI.SS.Formula.Functions; using SqlSugar; using Tnb.EquipMgr.Entities; using Tnb.EquipMgr.Entities.Dto; using Tnb.EquipMgr.Interfaces; namespace Tnb.EquipMgr { /// /// 设备备品备件 /// [ApiDescriptionSettings(Tag = ModuleConsts.Tag, Area = ModuleConsts.Area, Order = 700)] [Route("api/[area]/[controller]/[action]")] public class EqpEquipSparePartsService : IEqpEquipSparePartsService, IDynamicApiController, ITransient { private readonly ISqlSugarRepository _repository; private readonly IUserManager _userManager; public EqpEquipSparePartsService(ISqlSugarRepository repository, IUserManager userManager) { _userManager = userManager; _repository = repository; } [HttpPost] public async Task AddEquipSpareParts(EquipSparePartsInput input) { List oldList = await _repository.GetListAsync(x => x.equip_id == input.equip_id); List list = new(); string orgId = _userManager?.GetUserInfo().Result.organizeId ?? ""; if (input != null && input.spare_parts_ids != null) { foreach (string spare_parts_id in input.spare_parts_ids) { if (oldList.Any(x => x.spare_parts_id == spare_parts_id)) { continue; } list.Add(new EqpEquipSpareParts() { id = SnowflakeIdHelper.NextId(), equip_id = input.equip_id, spare_parts_id = spare_parts_id, create_id = _userManager?.UserId ?? "", create_time = DateTime.Now, org_id = orgId, }); } } _ = await _repository.InsertRangeAsync(list); } [HttpPost] public async Task GetEquipSparePartsList(EquipQueryInput input) { ISqlSugarClient db = _repository.AsSugarClient(); Dictionary? queryJson = new(); if (!string.IsNullOrEmpty(input.queryJson)) { queryJson = JsonConvert.DeserializeObject>(input.queryJson); } var eqpSpareParts = await db.Queryable().ToListAsync(); var InstockDs= await db.Queryable().ToListAsync(); var RequisitionDs = await db.Queryable().ToListAsync(); SqlSugarPagedList result = await db.Queryable() .LeftJoin((a, b) => a.spare_parts_id == b.id) .LeftJoin((a, b, c) => c.EnCode == Tnb.BasicData.DictConst.SparePartsType && c.DeleteMark == null) .LeftJoin((a, b, c, d) => d.DictionaryTypeId == c.Id && b.type_id == d.EnCode) .Where((a, b, c, d) => a.equip_id == input.equip_id) .WhereIF(queryJson != null && queryJson.ContainsKey("code"), (a, b, c, d) => b.code.Contains(queryJson!["code"])) .WhereIF(queryJson != null && queryJson.ContainsKey("name"), (a, b, c, d) => b.name.Contains(queryJson!["name"])) .Select((a, b, c, d) => new EquipSparePartsQueryOutput { id = a.id, type_code = d.EnCode, type_name = d.FullName, code = b.code, name = b.name, specification = b.specification, num = 0 }).ToPagedListAsync(input.currentPage, input.pageSize); foreach (var item in result.list) { try { var part = eqpSpareParts.Where(p => p.code == item.code).First(); var Instocknum = InstockDs.Where(p => p.spare_parts_id == part.id).Sum(p => p.quantity); var Requisitionum = RequisitionDs.Where(p => p.spare_parts_id == part.id).Sum(p => p.quantity); item.num = Instocknum > Requisitionum ? Instocknum - Requisitionum : 0; } catch (Exception) { } } return PageResult.SqlSugarPageResult(result); } //获取备品备件历史 [HttpPost] public async Task GetEquipSparePartRecord(EquipSparePartRecordInput input) { ISqlSugarClient db = _repository.AsSugarClient(); var partsids = await db.Queryable().Where(p => p.equip_id == input.equip_id).Select(p => p.spare_parts_id).ToListAsync(); var Instocks = await db.Queryable() .LeftJoin((a, b) => a.instock_id == b.id) .LeftJoin((a, b, c) => a.spare_parts_id == c.id) .LeftJoin((a, b, c, d) => b.instock_id == d.Id) .Where((a, b, c, d) => partsids.Contains(a.spare_parts_id!)) .Select((a, b, c, d) => new EquipSparePartRecordOut() { id = a.id, code = c.code, name = c.name, type = "入库", execuser = d.RealName, execdatetime = b.create_time != null ? b.create_time.ToString() : "", num = a.quantity }) .ToListAsync(); var Requisitions = await db.Queryable() .LeftJoin((a, b) => a.spare_parts_requisition_id == b.id) .LeftJoin((a, b, c) => a.spare_parts_id == c.id) .LeftJoin((a, b, c, d) => b.recipient_id == d.Id) .Where((a, b, c, d) => partsids.Contains(a.spare_parts_id!)) .Select((a, b, c, d) => new EquipSparePartRecordOut() { id = a.id, code = c.code, name = c.name, type = "领用", execuser = d.RealName, execdatetime = b.create_time != null ? b.create_time.ToString()!.Substring(0,19) : "", num = a.quantity }) .ToListAsync(); Instocks.AddRange(Requisitions); var result = Instocks.OrderByDescending(p => DateTime.Parse(p.execdatetime!)); return result; } } }