using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.TCBGetPressureTestReportResponse.Types;
namespace Admin.NET.Application;
///
/// 商品追溯服务
///
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class ProductRetrospectService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
private readonly PrintCodeDetailService _codeDetailService;
private readonly UserManager _userManager;
public ProductRetrospectService(SqlSugarRepository rep,
PrintCodeDetailService codeDetailService,
UserManager userManager)
{
_rep = rep;
_codeDetailService = codeDetailService;
_userManager = userManager;
}
///
/// 分页查询商品追溯
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task> Page(ProductRetrospectInput input)
{
var query = _rep.AsQueryable().Where(a => !a.IsDelete)
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
u.OddNumber.Contains(input.SearchKey.Trim())
|| u.Code.Contains(input.SearchKey.Trim())
|| u.CodeType.Contains(input.SearchKey.Trim())
|| u.BusinessType.Contains(input.SearchKey.Trim())
|| u.Destination.Contains(input.SearchKey.Trim())
|| u.Location.Contains(input.SearchKey.Trim())
|| u.Receipt.Contains(input.SearchKey.Trim())
|| u.Department.Contains(input.SearchKey.Trim())
|| u.Batch.Contains(input.SearchKey.Trim())
|| u.Unit.Contains(input.SearchKey.Trim())
|| u.BaseUnit.Contains(input.SearchKey.Trim())
|| u.Remarks.Contains(input.SearchKey.Trim())
)
.WhereIF(!string.IsNullOrWhiteSpace(input.OddNumber), u => u.OddNumber.Contains(input.OddNumber.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Code), u => u.Code.Contains(input.Code.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.CodeType), u => u.CodeType.Contains(input.CodeType.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.BusinessType), u => u.BusinessType.Contains(input.BusinessType.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Destination), u => u.Destination.Contains(input.Destination.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Location), u => u.Location.Contains(input.Location.Trim()))
.WhereIF(input.WarehouseID>0, u => u.WarehouseID == input.WarehouseID)
.WhereIF(!string.IsNullOrWhiteSpace(input.Receipt), u => u.Receipt.Contains(input.Receipt.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Department), u => u.Department.Contains(input.Department.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Batch), u => u.Batch.Contains(input.Batch.Trim()))
.WhereIF(input.Count>0, u => u.Count == input.Count)
.WhereIF(!string.IsNullOrWhiteSpace(input.Unit), u => u.Unit.Contains(input.Unit.Trim()))
.WhereIF(input.BaseCount>0, u => u.BaseCount == input.BaseCount)
.WhereIF(!string.IsNullOrWhiteSpace(input.BaseUnit), u => u.BaseUnit.Contains(input.BaseUnit.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Remarks), u => u.Remarks.Contains(input.Remarks.Trim()))
.Select();
if(input.ScanCodeTimeRange != null && input.ScanCodeTimeRange.Count >0)
{
DateTime? start= input.ScanCodeTimeRange[0];
query = query.WhereIF(start.HasValue, u => u.ScanCodeTime > start);
if (input.ScanCodeTimeRange.Count >1 && input.ScanCodeTimeRange[1].HasValue)
{
var end = input.ScanCodeTimeRange[1].Value.AddDays(1);
query = query.Where(u => u.ScanCodeTime < end);
}
}
if(input.WarehousingDateRange != null && input.WarehousingDateRange.Count >0)
{
DateTime? start= input.WarehousingDateRange[0];
query = query.WhereIF(start.HasValue, u => u.WarehousingDate > start);
if (input.WarehousingDateRange.Count >1 && input.WarehousingDateRange[1].HasValue)
{
var end = input.WarehousingDateRange[1].Value.AddDays(1);
query = query.Where(u => u.WarehousingDate < end);
}
}
return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
}
///
/// 增加商品追溯
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Add")]
public async Task Add(AddProductRetrospectInput input)
{
var entity = input.Adapt();
await _rep.InsertAsync(entity);
return entity.Id;
}
///
/// 删除商品追溯
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Delete")]
public async Task Delete(DeleteProductRetrospectInput 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(UpdateProductRetrospectInput input)
{
var entity = input.Adapt();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
///
/// 获取商品追溯
///
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task Detail([FromQuery] QueryByIdProductRetrospectInput 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();
}
///
/// 获取商品追溯列表
///
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "RetrospectByProductCode")]
public async Task> RetrospectByProductCode(string? productCode)
{
return await _rep.AsQueryable().Where(u => !u.IsDelete && u.Code == productCode).Select().ToListAsync();
}
///
/// 查询打印条码状态
///
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "CheckCodeStatus")]
public async Task CheckCodeStatus(string? productCode)
{
var codeEnt = await _rep.AsQueryable().Where(u => !u.IsDelete && u.Code == productCode).Select().ToListAsync();
if (codeEnt == null || codeEnt.Count<1)
{
return 0;
}
var last = codeEnt.OrderBy(a => a.WarehousingDate).FirstOrDefault();
var receipt = last.Receipt == null ? "" : last.Receipt;
if (receipt.Contains("汇报单"))
{
return 1;
}
else if (receipt.Contains("仓库"))
{
return 2;
}
else if (receipt.Contains("发货"))
{
return 3;
}
else if (receipt.Contains("分销"))
{
return 3;
}
return 3;
}
public async Task AddRetrospect(PrintCodeDetail item ,ReportTable report,string receipt, string location ,long? sourceId,long? warehousingId)
{
var userId = _userManager.UserId;
var userName = _userManager.RealName;
var retrospect1 = new AddProductRetrospectInput()
{
BaseCount = item.BaseCount,
BaseUnit = item.BaseUnit,
Batch = report.Batch,
BusinessType = report.ProductType,
CodeType = item.CodeName,
Location = location,
Department = report.ProductionLine,
WarehouseID = warehousingId,
WarehousingDate = DateTime.Now,
Count = 1,
CreateTime = DateTime.Now,
ScanCodeTime = DateTime.Now,
CreateUserId = userId,
CreateUserName = userName,
MaterialsId = report.MaterialsId,
OddNumber = report.OddNumber,
Receipt = receipt,
SourceId = sourceId,
Unit = item.Unit,
Name = item.CodeName,
Code = item.Code,
//Destination = invoice.Consignee
};
await Add(retrospect1);
}
}