using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
using SqlSugar;
namespace Admin.NET.Application;
///
/// 调库出库服务
///
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class WarehouseTransferService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _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;
public WarehouseTransferService(SqlSugarRepository rep,
UserManager userManager,
PrintCodeDetailService codeDetailService,
ReportTableService reportTableService,
OutboundService outboundService,
OutboundDetailService outboundDetailService,
ProductRetrospectService productRetrospect)
{
_rep = rep;
_userManager = userManager;
_codeDetailService = codeDetailService;
_productRetrospect = productRetrospect;
_reportTableService = reportTableService;
_outboundService = outboundService;
_outboundDetailService = outboundDetailService;
}
///
/// 分页查询调库出库
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task> Page(WarehouseTransferInput input)
{
var query = _rep.AsQueryable().Where(a => !a.IsDelete)
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
u.Remarks.Contains(input.SearchKey.Trim())
)
.WhereIF(input.WarehouseId>0, u => u.WarehouseId == input.WarehouseId)
.WhereIF(input.TargetWarehouseId>0, u => u.TargetWarehouseId == input.TargetWarehouseId)
.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(AddWarehouseTransferInput input)
{
var entity = input.Adapt();
await _rep.InsertAsync(entity);
return entity.Id;
}
///
/// 删除调库出库
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Delete")]
public async Task Delete(DeleteWarehouseTransferInput 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(UpdateWarehouseTransferInput input)
{
var entity = input.Adapt();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
///
/// 获取调库出库
///
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task Detail([FromQuery] QueryByIdWarehouseTransferInput input)
{
return await _rep.GetFirstAsync(u => u.Id == input.Id);
}
///
/// 获取调库出库列表
///
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task> List()
{
return await _rep.AsQueryable().Select().Where(a => !a.IsDelete).ToListAsync();
}
///
/// 商品调库
///
///
///
[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 QueryByIdWarehouseTransferInput() { 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();
var list = new List();
foreach (var c1 in repeatCodes)
{
codeIds.Add(c1.Id);
c1.WarehouseID = null;
//c1.FatherCode = null;
//c1.FatherId = null;
await _codeDetailService.UpdateByEntity(c1.Adapt());
list.Add(c1);
var child1 = details.Find(a => a.FatherCode == c1.Code);
if (child1 != null)
{
child1.WarehouseID = null;
await _codeDetailService.UpdateByEntity(child1.Adapt());
list.Add(child1);
var child2 = details.Find(a => a.FatherCode == c1.Code);
if (child2 != null)
{
child2.WarehouseID = null;
await _codeDetailService.UpdateByEntity(child2.Adapt());
list.Add(child2);
var child3 = details.Find(a => a.FatherCode == c1.Code);
if (child3 != null)
{
child3.WarehouseID = null;
await _codeDetailService.UpdateByEntity(child3.Adapt());
list.Add(child3);
var child4 = details.Find(a => a.FatherCode == c1.Code);
if (child4 != null)
{
child4.WarehouseID = null;
await _codeDetailService.UpdateByEntity(child4.Adapt());
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(), report, "调库出库单", "", item.ReportTableId, input.WarehousingTableId);
}
}
static Dictionary reportDic = new Dictionary();
private async Task 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;
}
}
}