DigitalFactory/Admin.NET/Admin.NET.Application/Service/ProductRetrospect/ProductRetrospectService.cs

238 lines
9.5 KiB
C#

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;
/// <summary>
/// 商品追溯服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class ProductRetrospectService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<ProductRetrospect> _rep;
private readonly PrintCodeDetailService _codeDetailService;
private readonly UserManager _userManager;
public ProductRetrospectService(SqlSugarRepository<ProductRetrospect> rep,
PrintCodeDetailService codeDetailService,
UserManager userManager)
{
_rep = rep;
_codeDetailService = codeDetailService;
_userManager = userManager;
}
/// <summary>
/// 分页查询商品追溯
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task<SqlSugarPagedList<ProductRetrospectOutput>> 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<ProductRetrospectOutput>();
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);
}
/// <summary>
/// 增加商品追溯
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Add")]
public async Task<long> Add(AddProductRetrospectInput input)
{
var entity = input.Adapt<ProductRetrospect>();
await _rep.InsertAsync(entity);
return entity.Id;
}
/// <summary>
/// 删除商品追溯
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[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); //真删除
}
/// <summary>
/// 更新商品追溯
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Update")]
public async Task Update(UpdateProductRetrospectInput input)
{
var entity = input.Adapt<ProductRetrospect>();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
/// <summary>
/// 获取商品追溯
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task<ProductRetrospect> Detail([FromQuery] QueryByIdProductRetrospectInput 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<ProductRetrospectOutput>> List()
{
return await _rep.AsQueryable().Select<ProductRetrospectOutput>().Where(a => !a.IsDelete).ToListAsync();
}
/// <summary>
/// 获取商品追溯列表
/// </summary>
/// <param name="productCode"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "RetrospectByProductCode")]
public async Task<List<ProductRetrospectOutput>> RetrospectByProductCode(string? productCode)
{
return await _rep.AsQueryable().Where(u => !u.IsDelete && u.Code == productCode).Select<ProductRetrospectOutput>().ToListAsync();
}
/// <summary>
/// 查询打印条码状态
/// </summary>
/// <param name="productCode"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "CheckCodeStatus")]
public async Task<CheckCodeStatusOutInput> CheckCodeStatus(string? productCode)
{
int state = 0;
var codeEnt = await _rep.AsQueryable().Where(u => !u.IsDelete && u.Code == productCode).Select<ProductRetrospectOutput>().ToListAsync();
if (codeEnt == null || codeEnt.Count<1)
{
state = 0;
}
var last = codeEnt.OrderBy(a => a.WarehousingDate).FirstOrDefault();
var receipt = last.Receipt == null ? "" : last.Receipt;
if (receipt.Contains("汇报单"))
{
state = 1;
}
else if (receipt.Contains("仓库"))
{
state = 2;
}
else if (receipt.Contains("发货"))
{
state = 3;
}
else if (receipt.Contains("分销"))
{
state = 3;
}
return new CheckCodeStatusOutInput { State = state };
}
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);
}
}