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 物料替代关系(ItemSubstituteDetail 语义)
///
[ApiController]
[Route("api/s0/sales/item-substitute-details")]
[AllowAnonymous]
[NonUnify]
public class AdoS0ItemSubstituteDetailsController : ControllerBase
{
private readonly SqlSugarRepository _rep;
public AdoS0ItemSubstituteDetailsController(SqlSugarRepository rep)
{
_rep = rep;
}
[HttpGet]
public async Task GetPagedAsync([FromQuery] AdoS0ItemSubstituteDetailQueryDto q)
{
(q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
var query = _rep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(q.Domain), x => x.Domain == q.Domain)
.WhereIF(!string.IsNullOrWhiteSpace(q.SubstituteType), x => x.SubstituteType == q.SubstituteType)
.WhereIF(
!string.IsNullOrWhiteSpace(q.Keyword),
x =>
x.ParentItem.Contains(q.Keyword!) ||
x.ItemNum.Contains(q.Keyword!) ||
x.SubstituteItem.Contains(q.Keyword!) ||
(x.Remark != null && x.Remark.Contains(q.Keyword!)));
var total = await query.CountAsync();
var list = await query
.OrderByDescending(x => x.CreateTime)
.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] AdoS0ItemSubstituteDetailUpsertDto dto)
{
var now = DateTime.Now;
var entity = new AdoS0ItemSubstituteDetail
{
ParentItem = dto.ParentItem.Trim(),
ItemNum = dto.ItemNum.Trim(),
SubstituteItem = dto.SubstituteItem.Trim(),
SubstituteQty = dto.SubstituteQty,
SubstituteType = string.IsNullOrWhiteSpace(dto.SubstituteType) ? null : dto.SubstituteType.Trim(),
Remark = dto.Remark,
Domain = dto.Domain.Trim(),
CreateUser = dto.CreateUser,
CreateTime = dto.CreateTime ?? now,
UpdateUser = dto.UpdateUser,
UpdateTime = dto.UpdateTime
};
await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
return Ok(entity);
}
[HttpPut("{id:long}")]
public async Task UpdateAsync(long id, [FromBody] AdoS0ItemSubstituteDetailUpsertDto dto)
{
var entity = await _rep.GetByIdAsync(id);
if (entity == null) return NotFound();
entity.ParentItem = dto.ParentItem.Trim();
entity.ItemNum = dto.ItemNum.Trim();
entity.SubstituteItem = dto.SubstituteItem.Trim();
entity.SubstituteQty = dto.SubstituteQty;
entity.SubstituteType = string.IsNullOrWhiteSpace(dto.SubstituteType) ? null : dto.SubstituteType.Trim();
entity.Remark = dto.Remark;
entity.Domain = dto.Domain.Trim();
entity.UpdateUser = dto.UpdateUser;
entity.UpdateTime = dto.UpdateTime ?? 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 = "删除成功" });
}
}