using Admin.NET.Plugin.AiDOP.Dto.S0.Sales;
using Admin.NET.Plugin.AiDOP.Entity.S0.Sales;
using Admin.NET.Plugin.AiDOP.Infrastructure;
namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Sales;
///
/// S0 物料主数据
///
[ApiController]
[Route("api/s0/sales/materials")]
[AllowAnonymous]
[NonUnify]
public class AdoS0MaterialsController : ControllerBase
{
private readonly SqlSugarRepository _rep;
public AdoS0MaterialsController(SqlSugarRepository rep)
{
_rep = rep;
}
[HttpGet]
public async Task GetPagedAsync([FromQuery] AdoS0MaterialQueryDto q)
{
(q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
var query = _rep.AsQueryable()
.WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId.Value)
.WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId.Value)
.WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x => x.Code.Contains(q.Keyword!) || x.Name.Contains(q.Keyword!))
.WhereIF(!string.IsNullOrWhiteSpace(q.Code), x => x.Code.Contains(q.Code!))
.WhereIF(!string.IsNullOrWhiteSpace(q.Name), x => x.Name.Contains(q.Name!))
.WhereIF(!string.IsNullOrWhiteSpace(q.Spec), x => x.Spec!.Contains(q.Spec!))
.WhereIF(!string.IsNullOrWhiteSpace(q.DrawingNo), x => x.DrawingNo!.Contains(q.DrawingNo!))
.WhereIF(!string.IsNullOrWhiteSpace(q.PlCategory), x => x.PlCategory == q.PlCategory)
.WhereIF(!string.IsNullOrWhiteSpace(q.MaterialType), x => x.MaterialType == q.MaterialType)
.WhereIF(!string.IsNullOrWhiteSpace(q.Language), x => x.Language == q.Language)
.WhereIF(q.IsEnabled.HasValue, x => x.IsEnabled == q.IsEnabled.Value);
var total = await query.CountAsync();
var list = await query
.OrderByDescending(x => x.CreatedAt)
.Skip((q.Page - 1) * q.PageSize)
.Take(q.PageSize)
.ToListAsync();
return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
}
[HttpGet("{id:long}")]
public async Task GetAsync(long id)
{
var item = await _rep.GetByIdAsync(id);
return item == null ? NotFound() : Ok(item);
}
[HttpPost]
public async Task CreateAsync([FromBody] AdoS0MaterialUpsertDto dto)
{
var entity = new AdoS0Material
{
CompanyRefId = dto.CompanyRefId,
FactoryRefId = dto.FactoryRefId,
Code = dto.Code,
Name = dto.Name,
NameEn = dto.NameEn,
MaterialType = dto.MaterialType,
Unit = dto.Unit,
Spec = dto.Spec,
PlCategory = dto.PlCategory,
DrawingNo = dto.DrawingNo,
Language = dto.Language,
BizVersion = dto.BizVersion,
ProductCode = dto.ProductCode,
MaterialAttribute = dto.MaterialAttribute,
DefaultLocationId = dto.DefaultLocationId,
DefaultRackId = dto.DefaultRackId,
StockTypeCode = dto.StockTypeCode,
SafetyStock = dto.SafetyStock,
ShelfLifeDays = dto.ShelfLifeDays,
ExpireWarningDays = dto.ExpireWarningDays,
PurchaseLeadDays = dto.PurchaseLeadDays,
MinOrderQty = dto.MinOrderQty,
MaxOrderQty = dto.MaxOrderQty,
OrderMultiple = dto.OrderMultiple,
PreparationLeadDays = dto.PreparationLeadDays,
IsOnDemand = dto.IsOnDemand,
SpecialReqType = dto.SpecialReqType,
IsInspectionRequired = dto.IsInspectionRequired,
InspectionDays = dto.InspectionDays,
IsKeyMaterial = dto.IsKeyMaterial,
IsMainMaterial = dto.IsMainMaterial,
IsPreprocess = dto.IsPreprocess,
IsAutoBatch = dto.IsAutoBatch,
IsLabelRequired = dto.IsLabelRequired,
IsBatchFifoReminder = dto.IsBatchFifoReminder,
IsBatchFifoStrict = dto.IsBatchFifoStrict,
InventoryTurnoverRate = dto.InventoryTurnoverRate,
Remark = dto.Remark,
IsEnabled = dto.IsEnabled,
CreatedAt = DateTime.Now
};
entity.ForbidStatus = AdoS0SalesRules.ForbidStatusFromIsEnabled(dto.IsEnabled);
await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
return Ok(entity);
}
[HttpPut("{id:long}")]
public async Task UpdateAsync(long id, [FromBody] AdoS0MaterialUpsertDto dto)
{
var entity = await _rep.GetByIdAsync(id);
if (entity == null) return NotFound();
entity.CompanyRefId = dto.CompanyRefId;
entity.FactoryRefId = dto.FactoryRefId;
entity.Code = dto.Code;
entity.Name = dto.Name;
entity.NameEn = dto.NameEn;
entity.MaterialType = dto.MaterialType;
entity.Unit = dto.Unit;
entity.Spec = dto.Spec;
entity.PlCategory = dto.PlCategory;
entity.DrawingNo = dto.DrawingNo;
entity.Language = dto.Language;
entity.BizVersion = dto.BizVersion;
entity.ProductCode = dto.ProductCode;
entity.MaterialAttribute = dto.MaterialAttribute;
entity.DefaultLocationId = dto.DefaultLocationId;
entity.DefaultRackId = dto.DefaultRackId;
entity.StockTypeCode = dto.StockTypeCode;
entity.SafetyStock = dto.SafetyStock;
entity.ShelfLifeDays = dto.ShelfLifeDays;
entity.ExpireWarningDays = dto.ExpireWarningDays;
entity.PurchaseLeadDays = dto.PurchaseLeadDays;
entity.MinOrderQty = dto.MinOrderQty;
entity.MaxOrderQty = dto.MaxOrderQty;
entity.OrderMultiple = dto.OrderMultiple;
entity.PreparationLeadDays = dto.PreparationLeadDays;
entity.IsOnDemand = dto.IsOnDemand;
entity.SpecialReqType = dto.SpecialReqType;
entity.IsInspectionRequired = dto.IsInspectionRequired;
entity.InspectionDays = dto.InspectionDays;
entity.IsKeyMaterial = dto.IsKeyMaterial;
entity.IsMainMaterial = dto.IsMainMaterial;
entity.IsPreprocess = dto.IsPreprocess;
entity.IsAutoBatch = dto.IsAutoBatch;
entity.IsLabelRequired = dto.IsLabelRequired;
entity.IsBatchFifoReminder = dto.IsBatchFifoReminder;
entity.IsBatchFifoStrict = dto.IsBatchFifoStrict;
entity.InventoryTurnoverRate = dto.InventoryTurnoverRate;
entity.Remark = dto.Remark;
entity.IsEnabled = dto.IsEnabled;
entity.ForbidStatus = AdoS0SalesRules.ForbidStatusFromIsEnabled(dto.IsEnabled);
entity.UpdatedAt = DateTime.Now;
await _rep.AsUpdateable(entity).ExecuteCommandAsync();
return Ok(entity);
}
[HttpPatch("{id:long}/toggle-enabled")]
public async Task ToggleEnabledAsync(long id, [FromBody] AdoS0ToggleEnabledDto dto)
{
var entity = await _rep.GetByIdAsync(id);
if (entity == null) return NotFound();
entity.IsEnabled = dto.IsEnabled;
entity.ForbidStatus = AdoS0SalesRules.ForbidStatusFromIsEnabled(dto.IsEnabled);
entity.UpdatedAt = DateTime.Now;
await _rep.AsUpdateable(entity).ExecuteCommandAsync();
return Ok(entity);
}
[HttpDelete("{id:long}")]
public async Task DeleteAsync(long id)
{
var item = await _rep.GetByIdAsync(id);
if (item == null) return NotFound();
await _rep.DeleteAsync(item);
return Ok(new { message = "删除成功" });
}
}