using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;
///
/// 发货通知单服务
///
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class InvoiceService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
private readonly MaterialsService _materialsService;
private readonly SysUnitService _sysUnitService;
private readonly PrintCodeDetailService _codeDetailService;
private readonly ProductRetrospectService _productRetrospect;
private readonly ReportTableService _reportTableService;
private readonly UserManager _userManager;
public InvoiceService(SqlSugarRepository rep,
UserManager userManager,
MaterialsService materialsService,
SysUnitService sysUnitService,
PrintCodeDetailService codeDetailService,
ReportTableService reportTableService,
ProductRetrospectService productRetrospect)
{
_rep = rep;
_userManager = userManager;
_materialsService = materialsService;
_sysUnitService = sysUnitService;
_codeDetailService = codeDetailService;
_productRetrospect = productRetrospect;
_reportTableService = reportTableService;
}
///
/// 分页查询发货通知单
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task> 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();
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);
}
///
/// 增加发货通知单
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Add")]
public async Task Add(AddInvoiceInput input)
{
var entity = input.Adapt();
await _rep.InsertAsync(entity);
return entity.Id;
}
///
/// 删除发货通知单
///
///
///
[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); //真删除
}
///
/// 更新发货通知单
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Update")]
public async Task Update(UpdateInvoiceInput input)
{
var entity = input.Adapt();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
///
/// 获取发货通知单
///
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task Detail([FromQuery] QueryByIdInvoiceInput 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 = "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 repeatCodes = await _codeDetailService.GetRepeat(input.CodeDatas);
var invoice = await Detail(new QueryByIdInvoiceInput() { Id = input.WarehousingTableId.Value });
if (invoice == null)
{
throw Oops.Oh(ErrorCodeEnum.xg1002);
}
//var units = await _sysUnitService.ListByGroupId(materials.UnitGroupId);
//var baseUnit = units.FirstOrDefault().Name;
//var userId = _userManager.UserId;
//var userName = _userManager.RealName;
//var topDatas = input.CodeDatas.FindAll(a => string.IsNullOrEmpty(a.FatherCode));
//foreach (var dic in list)
//{
// var item = dic.Key;
// var retrospect1 = new AddProductRetrospectInput()
// {
// BaseCount = item.BaseCount,
// BaseUnit = baseUnit,
// Batch = warehousing.Batch,
// BusinessType = "生产任务单",
// CodeType = item.CodeName,
// Department = warehousing.ProductionLine,
// //WarehouseID = input.WarehouseId,
// WarehousingDate = DateTime.Now,
// Count = 1,
// CreateTime = DateTime.Now,
// ScanCodeTime = DateTime.Now,
// CreateUserId = userId,
// CreateUserName = userName,
// MaterialsId = dic.Value,
// OddNumber = warehousing.OddNumber,
// Receipt = "汇报单",
// SourceId = warehousing.Id,
// Unit = item.Unit,
// Name = materials.Name,
// Code = item.Code
// };//Destination = warehousing.Supplier,
// await _productRetrospect.Add(retrospect1);
//}
//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);
//}
}
}