using Admin.NET.Core.Service; using Admin.NET.Application.Const; using Admin.NET.Application.Entity; using Microsoft.AspNetCore.Http; using Admin.NET.Application.Service.ReportTable.Dto; using static SKIT.FlurlHttpClient.Wechat.Api.Models.ComponentTCBBatchCreateContainerServiceVersionRequest.Types; using Nest; using Admin.NET.Application.Utils; using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; using SkiaSharp; namespace Admin.NET.Application; /// /// 汇报单服务 /// [ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)] public class ReportTableService : IDynamicApiController, ITransient { private readonly SqlSugarRepository _rep; private readonly SysUnitService _repUnit; private readonly SysUnitGroupService _repUnitGroup; private readonly PrintCodeDetailService _codeDetailService; //private readonly ReportDetailTableService _reportDetailTable; private readonly UserManager _userManager; private readonly PrintDataService _printDataService; private readonly PrintRecordsService _printRecordsService; private readonly ProductRetrospectService _productRetrospectService; public ReportTableService(SqlSugarRepository rep, UserManager userManager, SysUnitService repUnit, SysUnitGroupService repUnitGroup, PrintCodeDetailService codeDetailService, PrintDataService printDataService, PrintRecordsService printRecordsService, ProductRetrospectService productRetrospectService) { _rep = rep; _repUnit = repUnit; _repUnitGroup = repUnitGroup; _codeDetailService = codeDetailService; _userManager = userManager; _printDataService = printDataService; _printRecordsService = printRecordsService; _productRetrospectService = productRetrospectService; } /// /// 分页查询汇报单 /// /// /// [HttpPost] [ApiDescriptionSettings(Name = "Page")] public async Task> Page(ReportTableInput input) { var query = _rep.AsQueryable().Where(a => !a.IsDelete) .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u => u.OddNumber.Contains(input.SearchKey.Trim()) || u.ProductType.Contains(input.SearchKey.Trim()) || u.ProductionLine.Contains(input.SearchKey.Trim()) || u.CodeNum.Contains(input.SearchKey.Trim()) || u.SourceNumber.Contains(input.SearchKey.Trim()) || u.Remarks.Contains(input.SearchKey.Trim()) ) .WhereIF(!string.IsNullOrWhiteSpace(input.OddNumber), u => u.OddNumber.Contains(input.OddNumber.Trim())) .WhereIF(input.State > 0, u => u.State == input.State) .WhereIF(!string.IsNullOrWhiteSpace(input.ProductType), u => u.ProductType.Contains(input.ProductType.Trim())) .WhereIF(!string.IsNullOrWhiteSpace(input.ProductionLine), u => u.ProductionLine.Contains(input.ProductionLine.Trim())) .WhereIF(!string.IsNullOrWhiteSpace(input.CodeNum), u => u.CodeNum.Contains(input.CodeNum.Trim())) .WhereIF(!string.IsNullOrWhiteSpace(input.SourceNumber), u => u.SourceNumber.Contains(input.SourceNumber.Trim())) .WhereIF(!string.IsNullOrWhiteSpace(input.Remarks), u => u.Remarks.Contains(input.Remarks.Trim())) .LeftJoin((u, detail) => u.Id == detail.ReportTableId) // 左连接的条件 .GroupBy((u, detail) => u.Id) .Select((u, detail) => new ReportTableOutput { Id = u.Id, OddNumber = u.OddNumber, StartDate = u.StartDate, EndDate = u.EndDate, State = u.State, ProductType = u.ProductType, ProductionLine = u.ProductionLine, CodeNum = u.CodeNum, SourceNumber = u.SourceNumber, SourceId = u.SourceId, MaterialsId = u.MaterialsId, ProductCount = SqlFunc.AggregateSum(detail.Count), BaseProductCount = u.BaseProductCount, Unit = u.Unit, Batch = u.Batch, Name = u.Name, Remarks = u.Remarks, TenantId = u.TenantId, CreateTime = u.CreateTime, UpdateTime = u.UpdateTime, CreateUserId = u.CreateUserId, CreateUserName = u.CreateUserName, UpdateUserId = u.UpdateUserId, UpdateUserName = u.UpdateUserName, IsDelete = u.IsDelete }); if (input.StartDateRange != null && input.StartDateRange.Count >0) { DateTime? start= input.StartDateRange[0]; query = query.WhereIF(start.HasValue, u => u.StartDate > start); if (input.StartDateRange.Count >1 && input.StartDateRange[1].HasValue) { var end = input.StartDateRange[1].Value.AddDays(1); query = query.Where(u => u.StartDate < end); } } return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize); } /// /// 增加汇报单 /// /// /// [HttpPost] [ApiDescriptionSettings(Name = "Add")] public async Task Add(AddReportTableInput input) { var entity = input.Adapt(); if (input.UpdateUserId > 0) { entity.SourceId = input.UpdateUserId; } var reportId = await _rep.InsertAsync(entity); if (input.UpdateUserId > 0) { var details = await _codeDetailService.List(); if (details == null || details.Count < 1) { return 0; } var printDetails = details.FindAll(a => a.TempListId == input.UpdateUserId); if (printDetails.Count < 1) { return 0; } for (int i = 0; i < printDetails.Count; i++) { var item = printDetails[i]; item.ReportTableId = entity.Id; var ent = item.Adapt(); await _codeDetailService.UpdateByEntity(ent); await _productRetrospectService.AddRetrospect(ent, entity, "汇报单", entity.ProductionLine, entity.Id, null); } } return entity.Id; } /// /// 删除汇报单 /// /// /// [HttpPost] [ApiDescriptionSettings(Name = "Delete")] public async Task Delete(DeleteReportTableInput input) { var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002); await _rep.FakeDeleteAsync(entity); //假删除 //await _rep.DeleteAsync(entity); //真删除 } /// /// 更新汇报单 /// /// /// [HttpPost] [ApiDescriptionSettings(Name = "Update")] public async Task Update(UpdateReportTableInput input) { var entity = input.Adapt(); await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync(); } /// /// 获取汇报单 /// /// /// [HttpGet] [ApiDescriptionSettings(Name = "Detail")] public async Task Detail([FromQuery] QueryByIdReportTableInput input) { return await _rep.GetFirstAsync(u => u.Id == input.Id || (input.SourceId > 0 && u.SourceId == input.SourceId)); } /// /// 获取汇报单 /// /// /// [HttpGet] [ApiDescriptionSettings(Name = "GetByCode")] public async Task GetByCode([FromQuery] QueryByIdReportTableInput input) { var codeModel = await _codeDetailService.GetByProductCode(input.Code); if (codeModel == null) { throw Oops.Oh(ErrorCodeEnum.D1002); } return await _rep.GetFirstAsync(u => u.SourceId == codeModel.ReportTableId); } /// /// 获取汇报单 /// /// /// [HttpGet] [ApiDescriptionSettings(Name = "GetBySource")] public async Task GetBySource(long? sourceId) { return await _rep.GetFirstAsync(u => u.SourceId == sourceId); } /// /// 获取汇报单列表 /// /// /// [HttpGet] [ApiDescriptionSettings(Name = "List")] public async Task> List() { return await _rep.AsQueryable().Where(a => !a.IsDelete).Select().ToListAsync(); } /// /// 新增汇报单 /// /// /// [HttpPost] [ApiDescriptionSettings(Name = "AddReport")] public async Task AddReport(AddReportContext input) { var unitGroup = await _repUnitGroup.Detail(new QueryByIdSysUnitGroupInput() { Id = input.UnitGroupId }); if (unitGroup == null) { throw new ArgumentNullException(nameof(unitGroup)); } var units = await _repUnit.ListByGroupId(unitGroup.Id); if (units == null || units.Count < 1) { throw new ArgumentNullException(nameof(unitGroup)); } var unit = units.Find(a => a.Name == input.Name); if (unit == null) throw new ArgumentNullException(nameof(unitGroup)); var newReport = new AddReportTableInput() { CreateTime = DateTime.Now, IsDelete = false, OddNumber = DateTime.Now.ToString("yyyyMMddhhmmss"), State = 1 }; var addReport = await Add(newReport); var others = units.FindAll(a => a.Rate < unit.Rate); var entity = input.Adapt(); await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync(); } /// /// 新增打印详情单 /// /// /// [HttpPost] [ApiDescriptionSettings(Name = "AddPrintDetail")] public async Task> AddPrintDetail(AddReportContext input) { var result = new List(); var unitGroup = await _repUnitGroup.Detail(new QueryByIdSysUnitGroupInput() { Id = input.UnitGroupId }); if (unitGroup == null) { throw new ArgumentNullException(nameof(unitGroup)); } var units = await _repUnit.ListByGroupId(unitGroup.Id); if (units == null || units.Count < 1) { throw new ArgumentNullException(nameof(unitGroup)); } var unit = units.Find(a => a.Name == input.Package); if (unit == null) throw new ArgumentNullException(nameof(unit)); var newReport = new AddPrintRecordsInput() { CreateTime = DateTime.Now, Unit = input.Package, Name = input.Name, IsDelete = false, Batch = input.Batch, ProductDate = input.ProductDate, LoseDate = input.LoseDate, ProductCount = input.PrintDatas?.Count, MaterialsId = input.MaterialsId }; var addReport = await _printRecordsService.Add(newReport); var others = units.FindAll(a => a.Rate < unit.Rate).OrderBy(a => a.Rate).ToList(); others.Reverse(); int toltalCount = unit.Rate.ToInt(); var baseUnit = others.Count > 0 ? others.LastOrDefault().Name : unit.Name; var userId = _userManager.UserId; var userName = _userManager.RealName; List tempUnits = new List(); tempUnits.AddRange(others); foreach (var item in input.PrintDatas) { var code = CodeHelper.GetCode(item.BarCode, item.QrCode); var codeType = string.IsNullOrEmpty(item.BarCode) ? "二维码" : "条码"; var detail = new AddPrintCodeDetailInput() { TempListId = addReport, Code = code, CodeName = codeType, ChildCount = unit.ChildUnitCount, Count = 1, Unit = unit.Name, BaseCount = toltalCount, BaseUnit = baseUnit, PrintCodeTime = DateTime.Now, CreateUserId = userId, MaterialsId = input.MaterialsId, CreateUserName = userName, }; var detailId = await _codeDetailService.Add(detail); var treeData1 = detail.Adapt(); if (others.Count < 1) { result.Add(treeData1); continue; } treeData1.Children = new List(); var currUnit = tempUnits.FirstOrDefault(); var printDatas = await _printDataService.GetPrintDatas(input.UnitGroupId, currUnit.Name, codeType, unit.ChildUnitCount); foreach (var dt in printDatas) { var code2 = CodeHelper.GetCode(dt.BarCode, dt.QrCode); var detail2 = new AddPrintCodeDetailInput() { TempListId = addReport, FatherCode = code, FatherId = detailId, MaterialsId = input.MaterialsId, Code = code2, ChildCount = currUnit.ChildUnitCount, CodeName = codeType, Count = 1, Unit = currUnit.Name, BaseCount = currUnit.Rate, BaseUnit = baseUnit, PrintCodeTime = DateTime.Now, CreateUserId = userId, CreateUserName = userName }; var detailId2 = await _codeDetailService.Add(detail2); var treeData2 = detail2.Adapt(); if (tempUnits.Count > 1) { treeData2.Children = new List(); var currUnit3 = tempUnits[1]; var printDatas3 = await _printDataService.GetPrintDatas(input.UnitGroupId, currUnit3.Name, codeType, currUnit.ChildUnitCount); foreach (var dt3 in printDatas3) { var code3 = CodeHelper.GetCode(dt3.BarCode, dt3.QrCode); var detail3 = new AddPrintCodeDetailInput() { TempListId = addReport, FatherCode = code2, FatherId = detailId2, MaterialsId = input.MaterialsId, Code = code3, ChildCount = currUnit3.ChildUnitCount, CodeName = codeType, Count = 1, Unit = currUnit3.Name, BaseCount = currUnit3.Rate, BaseUnit = baseUnit, PrintCodeTime = DateTime.Now, CreateUserId = userId, CreateUserName = userName }; var detailId3 = await _codeDetailService.Add(detail3); var treeData3 = detail3.Adapt(); if (tempUnits.Count > 2) { treeData3.Children = new List(); var currUnit4 = tempUnits[2]; var printDatas4 = await _printDataService.GetPrintDatas(input.UnitGroupId, currUnit4.Name, codeType, currUnit3.ChildUnitCount); foreach (var dt4 in printDatas4) { var code4 = CodeHelper.GetCode(dt4.BarCode, dt4.QrCode); var detail4 = new AddPrintCodeDetailInput() { TempListId = addReport, FatherCode = code3, FatherId = detailId3, MaterialsId = input.MaterialsId, Code = code4, ChildCount = currUnit4.ChildUnitCount, CodeName = codeType, Count = 1, Unit = currUnit4.Name, BaseCount = currUnit4.Rate, BaseUnit = baseUnit, PrintCodeTime = DateTime.Now, CreateUserId = userId, CreateUserName = userName }; var treeData4 = detail4.Adapt(); treeData3.Children.Add(treeData4); } } treeData2.Children.Add(treeData3); } } treeData1.Children.Add(treeData2); } result.Add(treeData1); } return result; } /// /// 获取打印详情单 /// /// /// [HttpGet] [ApiDescriptionSettings(Name = "GetPrintDetail")] public async Task> GetPrintDetail(long? id) { var result = new List(); var details = await _codeDetailService.List(); if (details == null || details.Count < 1) { return result; } var printDetails = details.FindAll(a => a.TempListId == id || a.ReportTableId == id); if (printDetails.Count<1) { return result; } var father = printDetails.FindAll(a => a.FatherId == null || a.FatherId == 0); foreach (var item in father) { var treeData1 = item.Adapt(); var list1 = printDetails.FindAll(a => a.FatherId == item.Id); if (list1.Count > 0) { treeData1.Children = new List(); treeData1.HasChildren = false; foreach (var dt in list1) { var list2 = printDetails.FindAll(a => a.FatherId == dt.Id); var treeData2 = dt.Adapt(); if (list2.Count > 0) { treeData2.Children = new List(); treeData2.HasChildren = false; foreach (var dt3 in list2) { var list3 = printDetails.FindAll(a => a.FatherId == dt3.Id); var treeData3 = dt3.Adapt(); if (list3.Count > 0) { treeData3.Children = new List(); treeData3.HasChildren = false; foreach (var dt4 in list3) { var treeData4 = dt4.Adapt(); treeData3.Children.Add(treeData4); } } treeData2.Children.Add(treeData3); } } treeData1.Children.Add(treeData2); } } result.Add(treeData1); } return result; } }