DigitalFactory/Admin.NET/Admin.NET.Application/Service/Outbound/OutboundService.cs

188 lines
6.9 KiB
C#

using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
using Nest;
namespace Admin.NET.Application;
/// <summary>
/// 出库单服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class OutboundService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<Outbound> _rep;
private readonly OutboundDetailService _outboundDetailService;
private readonly PrintCodeDetailService _codeDetailService;
public OutboundService(SqlSugarRepository<Outbound> rep, OutboundDetailService outboundDetailService, PrintCodeDetailService codeDetailService)
{
_rep = rep;
_outboundDetailService = outboundDetailService;
_codeDetailService = codeDetailService;
}
/// <summary>
/// 分页查询出库单
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task<SqlSugarPagedList<OutboundOutput>> Page(OutboundInput input)
{
var query = _rep.AsQueryable().Where(a => !a.IsDelete)
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
u.CodeNum.Contains(input.SearchKey.Trim())
|| u.SourceCodeNum.Contains(input.SearchKey.Trim())
|| u.BusinessType.Contains(input.SearchKey.Trim())
|| u.Consignee.Contains(input.SearchKey.Trim())
)
.WhereIF(!string.IsNullOrWhiteSpace(input.CodeNum), u => u.CodeNum.Contains(input.CodeNum.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.SourceCodeNum), u => u.SourceCodeNum.Contains(input.SourceCodeNum.Trim()))
.WhereIF(input.State>0, u => u.State == input.State)
.WhereIF(!string.IsNullOrWhiteSpace(input.BusinessType), u => u.BusinessType.Contains(input.BusinessType.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Consignee), u => u.Consignee.Contains(input.Consignee.Trim()))
.WhereIF(input.ExaminerId>0, u => u.ExaminerId == input.ExaminerId)
.Select<OutboundOutput>();
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(AddOutboundInput input)
{
var entity = input.Adapt<Outbound>();
await _rep.InsertAsync(entity);
return entity.Id;
}
/// <summary>
/// 删除出库单
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Delete")]
public async Task Delete(DeleteOutboundInput 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(UpdateOutboundInput input)
{
var entity = input.Adapt<Outbound>();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
/// <summary>
/// 获取出库单
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task<Outbound> Detail([FromQuery] QueryByIdOutboundInput input)
{
return await _rep.GetFirstAsync(u => u.Id == input.Id);
}
/// <summary>
/// 获取出库单列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task<List<OutboundOutput>> List()
{
return await _rep.AsQueryable().Where(a => !a.IsDelete).Select<OutboundOutput>().ToListAsync();
}
/// <summary>
/// 获取出库单
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "GetOutboundDetail")]
public async Task<List<PrintCodeTreeData>> GetOutboundDetail([FromQuery] QueryByIdOutboundInput input)
{
var outbound = await _outboundDetailService.GetByOutboundId(input.Id);
if (string.IsNullOrEmpty(outbound.CodeTableIds))
{
throw Oops.Oh(ErrorCodeEnum.xg1002);
}
var result = new List<PrintCodeTreeData>();
var details = await _codeDetailService.List();
if (details == null || details.Count < 1)
{
return result;
}
var ids = outbound.CodeTableIds.Split(',').Select(a => Convert.ToInt64(a)).ToList();
//var printDetails = details.FindAll(a => ids.Contains(a.Id));
//if (printDetails.Count < 1)
//{
// return result;
//}
var father = details.FindAll(a => ids.Contains(a.Id));
foreach (var item in father)
{
var treeData1 = item.Adapt<PrintCodeTreeData>();
var list1 = details.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 = details.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 = details.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;
}
}