333 lines
13 KiB
C#
333 lines
13 KiB
C#
using Admin.NET.Core.Service;
|
|
using Admin.NET.Application.Const;
|
|
using Admin.NET.Application.Entity;
|
|
using Microsoft.AspNetCore.Http;
|
|
using NewLife.Reflection;
|
|
using Nest;
|
|
|
|
namespace Admin.NET.Application;
|
|
/// <summary>
|
|
/// 发货通知单服务
|
|
/// </summary>
|
|
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
|
|
public class InvoiceService : IDynamicApiController, ITransient
|
|
{
|
|
private readonly SqlSugarRepository<Invoice> _rep;
|
|
private readonly PrintCodeDetailService _codeDetailService;
|
|
private readonly ProductRetrospectService _productRetrospect;
|
|
private readonly ReportTableService _reportTableService;
|
|
private readonly OutboundService _outboundService;
|
|
private readonly OutboundDetailService _outboundDetailService;
|
|
private readonly UserManager _userManager;
|
|
private readonly MaterialListService _materialListService;
|
|
public InvoiceService(SqlSugarRepository<Invoice> rep,
|
|
UserManager userManager,
|
|
PrintCodeDetailService codeDetailService,
|
|
ReportTableService reportTableService,
|
|
OutboundService outboundService,
|
|
OutboundDetailService outboundDetailService,
|
|
ProductRetrospectService productRetrospect,
|
|
MaterialListService materialListService)
|
|
{
|
|
_rep = rep;
|
|
_userManager = userManager;
|
|
_codeDetailService = codeDetailService;
|
|
_productRetrospect = productRetrospect;
|
|
_reportTableService = reportTableService;
|
|
_outboundService = outboundService;
|
|
_outboundDetailService = outboundDetailService;
|
|
_materialListService = materialListService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分页查询发货通知单
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[ApiDescriptionSettings(Name = "Page")]
|
|
public async Task<SqlSugarPagedList<InvoiceOutput>> Page(InvoiceInput input)
|
|
{
|
|
var query = _rep.AsQueryable().Where(a => !a.IsDelete)
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
|
|
u.CodeNum.Contains(input.SearchKey.Trim())
|
|
|| u.BusinessType.Contains(input.SearchKey.Trim())
|
|
|| u.Custom.Contains(input.SearchKey.Trim())
|
|
|| u.Warehouse.Contains(input.SearchKey.Trim())
|
|
|| u.Department.Contains(input.SearchKey.Trim())
|
|
|| u.CarNumber.Contains(input.SearchKey.Trim())
|
|
|| u.ParkingSpace.Contains(input.SearchKey.Trim())
|
|
|| u.Salesman.Contains(input.SearchKey.Trim())
|
|
|| u.Consignee.Contains(input.SearchKey.Trim())
|
|
|| u.DeliveryAddress.Contains(input.SearchKey.Trim())
|
|
)
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.CodeNum), u => u.CodeNum.Contains(input.CodeNum.Trim()))
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.BusinessType), u => u.BusinessType.Contains(input.BusinessType.Trim()))
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.Custom), u => u.Custom.Contains(input.Custom.Trim()))
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.Warehouse), u => u.Warehouse.Contains(input.Warehouse.Trim()))
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.Department), u => u.Department.Contains(input.Department.Trim()))
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.CarNumber), u => u.CarNumber.Contains(input.CarNumber.Trim()))
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.ParkingSpace), u => u.ParkingSpace.Contains(input.ParkingSpace.Trim()))
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.Salesman), u => u.Salesman.Contains(input.Salesman.Trim()))
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.Consignee), u => u.Consignee.Contains(input.Consignee.Trim()))
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.DeliveryAddress), u => u.DeliveryAddress.Contains(input.DeliveryAddress.Trim()))
|
|
.Select<InvoiceOutput>();
|
|
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);
|
|
}
|
|
}
|
|
if(input.DeliveryDateRange != null && input.DeliveryDateRange.Count >0)
|
|
{
|
|
DateTime? start= input.DeliveryDateRange[0];
|
|
query = query.WhereIF(start.HasValue, u => u.DeliveryDate > start);
|
|
if (input.DeliveryDateRange.Count >1 && input.DeliveryDateRange[1].HasValue)
|
|
{
|
|
var end = input.DeliveryDateRange[1].Value.AddDays(1);
|
|
query = query.Where(u => u.DeliveryDate < 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(AddInvoiceInput input)
|
|
{
|
|
var entity = input.Adapt<Invoice>();
|
|
await _rep.InsertAsync(entity);
|
|
var id = entity.Id;
|
|
if (input.TableData!=null&&input.TableData.Count>0)
|
|
{
|
|
foreach (var item in input.TableData)
|
|
{
|
|
item.SourceId = id;
|
|
await _materialListService.Add(item);
|
|
}
|
|
}
|
|
return id;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除发货通知单
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[ApiDescriptionSettings(Name = "Delete")]
|
|
public async Task Delete(DeleteInvoiceInput 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(UpdateInvoiceInput input)
|
|
{
|
|
var entity = input.Adapt<Invoice>();
|
|
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
|
|
if (input.TableData != null && input.TableData.Count > 0)
|
|
{
|
|
var list = await _materialListService.ListBySourceId(input.Id);
|
|
if (list!=null)
|
|
{
|
|
for (int i = 0; i < list.Count; i++)
|
|
{
|
|
await _materialListService.Delete(new DeleteMaterialListInput() { Id = list[i].Id });
|
|
}
|
|
}
|
|
foreach (var item in input.TableData)
|
|
{
|
|
item.SourceId = input.Id;
|
|
await _materialListService.Add(item);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取发货通知单
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[ApiDescriptionSettings(Name = "Detail")]
|
|
public async Task<Invoice> Detail([FromQuery] QueryByIdInvoiceInput input)
|
|
{
|
|
return await _rep.GetFirstAsync(u => u.Id == input.Id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取发货通知单列表
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[ApiDescriptionSettings(Name = "List")]
|
|
public async Task<List<InvoiceOutput>> List()
|
|
{
|
|
return await _rep.AsQueryable().Where(a => !a.IsDelete).Select<InvoiceOutput>().ToListAsync();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 商品出货
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[ApiDescriptionSettings(Name = "ShipmentProduct")]
|
|
public async Task ShipmentProduct(AddProductCodeInput input)
|
|
{
|
|
if (input == null || input.WarehousingTableId == null || input.CodeDatas == null || input.CodeDatas.Count == 0)
|
|
{
|
|
throw Oops.Oh(ErrorCodeEnum.xg1002);
|
|
}
|
|
|
|
|
|
var invoice = await Detail(new QueryByIdInvoiceInput() { Id = input.WarehousingTableId.Value });
|
|
if (invoice == null)
|
|
{
|
|
throw Oops.Oh(ErrorCodeEnum.xg1002);
|
|
}
|
|
var codes = string.IsNullOrEmpty(input.CodeDatas[0].BarCode) ? input.CodeDatas.ConvertAll(a => a.QrCode) : input.CodeDatas.ConvertAll(a => a.BarCode);
|
|
var details = (await _codeDetailService.List()).FindAll(a => !string.IsNullOrEmpty(a.FatherCode));
|
|
var repeatCodes = details.Where(a => !a.IsDelete && codes.Any(b => b == a.Code)).ToList();
|
|
if (repeatCodes == null || repeatCodes.Count < 1)
|
|
{
|
|
throw Oops.Oh(ErrorCodeEnum.xg1002);
|
|
}
|
|
|
|
var userId = _userManager.UserId;
|
|
var userName = _userManager.RealName;
|
|
var addOutbound = new AddOutboundInput()
|
|
{
|
|
BusinessType = invoice.BusinessType,
|
|
CodeNum = invoice.CodeNum,
|
|
Consignee = invoice.Consignee,
|
|
CreateTime = invoice.CreateTime,
|
|
CreateUserId = userId,
|
|
CreateUserName = userName,
|
|
SourceCodeNum = invoice.Id.ToString(),
|
|
StartDate = invoice.StartDate,
|
|
State = 0,
|
|
Warehouse = invoice.Warehouse,
|
|
WarehouseId = invoice.WarehouseId,
|
|
TenantId = invoice.TenantId,
|
|
};
|
|
var outbound = await _outboundService.Add(addOutbound);
|
|
|
|
|
|
var codeIds = new List<long>();
|
|
var list = new List<PrintCodeDetailOutput>();
|
|
foreach (var c1 in repeatCodes)
|
|
{
|
|
codeIds.Add(c1.Id);
|
|
c1.WarehouseID = null;
|
|
//c1.FatherCode = null;
|
|
//c1.FatherId = null;
|
|
await _codeDetailService.UpdateByEntity(c1.Adapt<PrintCodeDetail>());
|
|
list.Add(c1);
|
|
var child1 = details.Find(a => a.FatherCode == c1.Code);
|
|
if (child1!=null)
|
|
{
|
|
child1.WarehouseID = null;
|
|
await _codeDetailService.UpdateByEntity(child1.Adapt<PrintCodeDetail>());
|
|
list.Add(child1);
|
|
|
|
var child2 = details.Find(a => a.FatherCode == c1.Code);
|
|
if (child2 != null)
|
|
{
|
|
child2.WarehouseID = null;
|
|
await _codeDetailService.UpdateByEntity(child2.Adapt<PrintCodeDetail>());
|
|
list.Add(child2);
|
|
|
|
var child3 = details.Find(a => a.FatherCode == c1.Code);
|
|
if (child3 != null)
|
|
{
|
|
child3.WarehouseID = null;
|
|
await _codeDetailService.UpdateByEntity(child3.Adapt<PrintCodeDetail>());
|
|
list.Add(child3);
|
|
|
|
var child4 = details.Find(a => a.FatherCode == c1.Code);
|
|
if (child4 != null)
|
|
{
|
|
child4.WarehouseID = null;
|
|
await _codeDetailService.UpdateByEntity(child4.Adapt<PrintCodeDetail>());
|
|
list.Add(child4);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
var addOutboundDetail = new AddOutboundDetailInput() { CodeTableIds = string.Join(",", codeIds), OutboundId = outbound };
|
|
await _outboundDetailService.Add(addOutboundDetail);
|
|
|
|
foreach (var item in list)
|
|
{
|
|
var report = await GetReport(item.ReportTableId);
|
|
await _productRetrospect.AddRetrospect(item.Adapt<PrintCodeDetail>(), report, "发货通知单", addOutbound.Consignee, outbound, input.WarehousingTableId);
|
|
}
|
|
|
|
//var report = await _reportTableService.GetBySource(input.WarehousingTableId);
|
|
//if (report == null)
|
|
//{
|
|
// var newReport = new AddReportTableInput() { CreateTime = DateTime.Now, IsDelete = false, OddNumber = warehousing.OddNumber, State = 0, MaterialsId = warehousing.MaterialsId, SourceId = input.WarehousingTableId, ProductType = warehousing.ProductType, ProductionLine = warehousing.ProductionLine };
|
|
// var addReport = await _reportTableService.Add(newReport);
|
|
//}
|
|
|
|
}
|
|
|
|
static Dictionary<long, ReportTable> reportDic = new Dictionary<long, ReportTable>();
|
|
private async Task<ReportTable> GetReport(long? reportId)
|
|
{
|
|
if (reportId == null)
|
|
{
|
|
return new ReportTable();
|
|
}
|
|
if (reportDic.ContainsKey(reportId.Value))
|
|
{
|
|
return reportDic[reportId.Value];
|
|
}
|
|
var report = await _reportTableService.GetBySource(reportId);
|
|
if (report == null)
|
|
{
|
|
return new ReportTable();
|
|
}
|
|
else
|
|
{
|
|
return report;
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取物料列表列表
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[ApiDescriptionSettings(Name = "MaterialListById")]
|
|
public async Task<List<MaterialListOutput>> MaterialListById(long? id)
|
|
{
|
|
return await _materialListService.ListBySourceId(id);
|
|
}
|
|
}
|
|
|