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; 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; public ReportTableService(SqlSugarRepository rep, UserManager userManager, SysUnitService repUnit, SysUnitGroupService repUnitGroup, PrintCodeDetailService codeDetailService, ReportDetailTableService reportDetailTable, PrintDataService printDataService) { _rep = rep; _repUnit = repUnit; _repUnitGroup = repUnitGroup; _codeDetailService = codeDetailService; _reportDetailTable = reportDetailTable; _userManager = userManager; _printDataService = printDataService; } /// /// 分页查询汇报单 /// /// /// [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())) .Select(); 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(); await _rep.InsertAsync(entity); 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); } /// /// 获取汇报单列表 /// /// /// [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 = "AddPrintList")] public async Task> AddPrintList(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.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).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 = string.IsNullOrEmpty(item.BarCode) ? item.QrCode : item.BarCode; 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, 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(currUnit.Name, codeType, currUnit.ChildUnitCount); foreach (var dt in printDatas) { var code2 = string.IsNullOrEmpty(dt.BarCode) ? dt.QrCode : dt.BarCode; var detail2 = new AddPrintCodeDetailInput() { TempListId = addReport, FatherCode = code, FatherId = detailId, 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(currUnit3.Name, codeType, currUnit3.ChildUnitCount); foreach (var dt3 in printDatas3) { var code3 = string.IsNullOrEmpty(dt3.BarCode) ? dt3.QrCode : dt3.BarCode; var detail3 = new AddPrintCodeDetailInput() { TempListId = addReport, FatherCode = code2, FatherId = detailId2, 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(currUnit4.Name, codeType, currUnit4.ChildUnitCount); foreach (var dt4 in printDatas4) { var code4 = string.IsNullOrEmpty(dt4.BarCode) ? dt4.QrCode : dt4.BarCode; var detail4 = new AddPrintCodeDetailInput() { TempListId = addReport, FatherCode = code3, FatherId = detailId3, 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; } }