410 lines
17 KiB
C#
410 lines
17 KiB
C#
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;
|
|
|
|
namespace Admin.NET.Application;
|
|
/// <summary>
|
|
/// 汇报单服务
|
|
/// </summary>
|
|
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
|
|
public class ReportTableService : IDynamicApiController, ITransient
|
|
{
|
|
private readonly SqlSugarRepository<ReportTable> _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<ReportTable> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分页查询汇报单
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[ApiDescriptionSettings(Name = "Page")]
|
|
public async Task<SqlSugarPagedList<ReportTableOutput>> 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<ReportTableOutput>();
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 增加汇报单
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[ApiDescriptionSettings(Name = "Add")]
|
|
public async Task<long> Add(AddReportTableInput input)
|
|
{
|
|
var entity = input.Adapt<ReportTable>();
|
|
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<PrintCodeDetail>();
|
|
await _codeDetailService.UpdateByEntity(ent);
|
|
await _productRetrospectService.AddRetrospect(ent, entity, "汇报单", entity.ProductionLine, entity.Id, null);
|
|
}
|
|
}
|
|
|
|
return entity.Id;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 删除汇报单
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[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); //真删除
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新汇报单
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[ApiDescriptionSettings(Name = "Update")]
|
|
public async Task Update(UpdateReportTableInput input)
|
|
{
|
|
var entity = input.Adapt<ReportTable>();
|
|
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取汇报单
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[ApiDescriptionSettings(Name = "Detail")]
|
|
public async Task<ReportTable> Detail([FromQuery] QueryByIdReportTableInput input)
|
|
{
|
|
return await _rep.GetFirstAsync(u => u.Id == input.Id || (input.SourceId > 0 && u.SourceId == input.SourceId));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取汇报单
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[ApiDescriptionSettings(Name = "GetByCode")]
|
|
public async Task<ReportTable> 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);
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 获取汇报单
|
|
/// </summary>
|
|
/// <param name="sourceId"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[ApiDescriptionSettings(Name = "GetBySource")]
|
|
public async Task<ReportTable> GetBySource(long? sourceId)
|
|
{
|
|
return await _rep.GetFirstAsync(u => u.SourceId == sourceId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取汇报单列表
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[ApiDescriptionSettings(Name = "List")]
|
|
public async Task<List<ReportTableOutput>> List()
|
|
{
|
|
return await _rep.AsQueryable().Where(a => !a.IsDelete).Select<ReportTableOutput>().ToListAsync();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 新增汇报单
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[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<ReportTable>();
|
|
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增打印详情单
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[ApiDescriptionSettings(Name = "AddPrintDetail")]
|
|
public async Task<List<PrintCodeTreeData>> AddPrintDetail(AddReportContext input)
|
|
{
|
|
var result = new List<PrintCodeTreeData>();
|
|
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<SysUnitOutput> tempUnits = new List<SysUnitOutput>();
|
|
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<PrintCodeTreeData>();
|
|
if (others.Count < 1)
|
|
{
|
|
result.Add(treeData1);
|
|
continue;
|
|
}
|
|
treeData1.Children = new List<PrintCodeTreeData>();
|
|
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<PrintCodeTreeData>();
|
|
if (tempUnits.Count > 1)
|
|
{
|
|
treeData2.Children = new List<PrintCodeTreeData>();
|
|
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<PrintCodeTreeData>();
|
|
if (tempUnits.Count > 2)
|
|
{
|
|
treeData3.Children = new List<PrintCodeTreeData>();
|
|
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<PrintCodeTreeData>();
|
|
treeData3.Children.Add(treeData4);
|
|
}
|
|
}
|
|
treeData2.Children.Add(treeData3);
|
|
}
|
|
}
|
|
treeData1.Children.Add(treeData2);
|
|
}
|
|
result.Add(treeData1);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取打印详情单
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[ApiDescriptionSettings(Name = "GetPrintDetail")]
|
|
public async Task<List<PrintCodeTreeData>> GetPrintDetail(long? id)
|
|
{
|
|
var result = new List<PrintCodeTreeData>();
|
|
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<PrintCodeTreeData>();
|
|
var list1 = printDetails.FindAll(a => a.FatherId == item.Id);
|
|
if (list1.Count > 0)
|
|
{
|
|
treeData1.Children = new List<PrintCodeTreeData>();
|
|
treeData1.HasChildren = false;
|
|
foreach (var dt in list1)
|
|
{
|
|
var list2 = printDetails.FindAll(a => a.FatherId == dt.Id);
|
|
var treeData2 = dt.Adapt<PrintCodeTreeData>();
|
|
if (list2.Count > 0)
|
|
{
|
|
treeData2.Children = new List<PrintCodeTreeData>();
|
|
treeData2.HasChildren = false;
|
|
foreach (var dt3 in list2)
|
|
{
|
|
var list3 = printDetails.FindAll(a => a.FatherId == dt3.Id);
|
|
var treeData3 = dt3.Adapt<PrintCodeTreeData>();
|
|
if (list3.Count > 0)
|
|
{
|
|
treeData3.Children = new List<PrintCodeTreeData>();
|
|
treeData3.HasChildren = false;
|
|
foreach (var dt4 in list3)
|
|
{
|
|
var treeData4 = dt4.Adapt<PrintCodeTreeData>();
|
|
treeData3.Children.Add(treeData4);
|
|
}
|
|
}
|
|
treeData2.Children.Add(treeData3);
|
|
}
|
|
}
|
|
treeData1.Children.Add(treeData2);
|
|
}
|
|
}
|
|
|
|
result.Add(treeData1);
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
|