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 WarehousingStatisticsService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
public WarehousingStatisticsService(SqlSugarRepository rep)
{
_rep = rep;
}
///
/// 分页查询入库统计
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task> Page(WarehousingStatisticsInput input)
{
var query = _rep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
u.Name.Contains(input.SearchKey.Trim())
|| u.CodeNum.Contains(input.SearchKey.Trim())
|| u.Unit.Contains(input.SearchKey.Trim())
|| u.BaseUnit.Contains(input.SearchKey.Trim())
|| u.Brand.Contains(input.SearchKey.Trim())
|| u.Specifications.Contains(input.SearchKey.Trim())
|| u.WarehousingType.Contains(input.SearchKey.Trim())
|| u.WarehouseLocation.Contains(input.SearchKey.Trim())
|| u.Supplier.Contains(input.SearchKey.Trim())
|| u.Remarks.Contains(input.SearchKey.Trim())
)
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.CodeNum), u => u.CodeNum.Contains(input.CodeNum.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.Brand), u => u.Brand.Contains(input.Brand.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Specifications), u => u.Specifications.Contains(input.Specifications.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.WarehousingType), u => u.WarehousingType.Contains(input.WarehousingType.Trim()))
.WhereIF(input.MaterialsId > 0, u => u.MaterialsId == input.MaterialsId)
.WhereIF(input.SourceId > 0, u => u.SourceId == input.SourceId)
.WhereIF(input.WarehouseId > 0, u => u.WarehouseId == input.WarehouseId)
.WhereIF(!string.IsNullOrWhiteSpace(input.WarehouseLocation), u => u.WarehouseLocation.Contains(input.WarehouseLocation.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Supplier), u => u.Supplier.Contains(input.Supplier.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Remarks), u => u.Remarks.Contains(input.Remarks.Trim()))
.Select();
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(AddWarehousingStatisticsInput input)
{
//input.WarehousingDate = DateTime.Now;
//var materials = await _materialsService.Detail(new QueryByIdMaterialsInput() { Id = input.MaterialsId.Value });
//SysUnitOutput unit = null;
//string unitScale = string.Empty;
//if (materials != null)
//{
// input.Specifications = materials.Specifications;
// input.CodeNum = materials.CodeNum;
// input.Brand = materials.Brand;
// var units = await _sysUnitService.ListByGroupId(materials.UnitGroupId.Value);
// unit = units.Find(a => a.Name == input.Unit);
// if (unit!=null)
// {
// input.BaseCount = input.Count * unit.Rate;
// input.BaseUnit = units.FirstOrDefault(a => a.IsBaseUnit == true).Name;
// unitScale = SysUnitService.GetUnitScale(units);
// }
//}
var entity = input.Adapt();
await _rep.InsertAsync(entity);
//var details = await _warehouseDetails.List();
//if (details != null && details.Any(a => a.MaterialsId == input.MaterialsId && a.Unit == input.Unit))
//{
// var detail = details.Find(a => a.MaterialsId == input.MaterialsId && a.Unit == input.Unit);
// detail.Count += input.Count;
// if (unit != null)
// {
// detail.BaseCount = detail.Count * unit.Rate;
// }
// await _warehouseDetails.UpdateByEntity(detail.Adapt());
//}
//else
//{
// var newEnt = input.Adapt();
// if (materials!=null)
// {
// newEnt.GoodCode = materials.BarCode;
// newEnt.MaterialsNum = materials.CodeNum;
// newEnt.UnitPrice = materials.Price;
// newEnt.Custom = input.Supplier;
// if (unit != null)
// {
// newEnt.BaseCount = newEnt.Count * unit.Rate;
// }
// newEnt.TotalPrice = newEnt.BaseCount * materials.Price;
// newEnt.PackageScale = unitScale;
// newEnt.Classify = (await _materialClassifyService.Detail(new QueryByIdMaterialClassifyInput() { Id = materials.Classify })).Name;
// var addId = await _warehouseDetails.Add(newEnt);
// }
//}
return entity.Id;
}
///
/// 删除入库统计
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Delete")]
public async Task Delete(DeleteWarehousingStatisticsInput 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(UpdateWarehousingStatisticsInput input)
{
var entity = input.Adapt();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
///
/// 获取入库统计
///
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task Detail([FromQuery] QueryByIdWarehousingStatisticsInput input)
{
return await _rep.GetFirstAsync(u => u.Id == input.Id);
}
///
/// 获取入库统计列表
///
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task> List()
{
return await _rep.AsQueryable().Select().ToListAsync();
}
}