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 订单优先级配置(PriorityCode 语义)
///
[ApiController]
[Route("api/s0/sales/order-priority-rules")]
[AllowAnonymous]
[NonUnify]
public class AdoS0OrderPriorityRulesController : ControllerBase
{
private readonly SqlSugarRepository _rep;
public AdoS0OrderPriorityRulesController(SqlSugarRepository rep)
{
_rep = rep;
}
[HttpGet]
public async Task GetPagedAsync([FromQuery] AdoS0PriorityCodeQueryDto 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.DomainCode), x => x.DomainCode == q.DomainCode)
.WhereIF(!string.IsNullOrWhiteSpace(q.Descr), x => x.Descr.Contains(q.Descr!))
.WhereIF(q.Priority.HasValue, x => x.Priority == q.Priority.Value)
.WhereIF(!string.IsNullOrWhiteSpace(q.SourceTable), x => x.SourceTable != null && x.SourceTable.Contains(q.SourceTable!))
.WhereIF(!string.IsNullOrWhiteSpace(q.SourceColumn), x => x.SourceColumn != null && x.SourceColumn.Contains(q.SourceColumn!))
.WhereIF(!string.IsNullOrWhiteSpace(q.Value), x => x.Value != null && x.Value.Contains(q.Value!))
.WhereIF(q.IsActive.HasValue, x => x.IsActive == q.IsActive.Value)
.WhereIF(
!string.IsNullOrWhiteSpace(q.Keyword),
x => x.Descr.Contains(q.Keyword!) ||
(x.SourceTable != null && x.SourceTable.Contains(q.Keyword!)) ||
(x.Value != null && x.Value.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();
foreach (var x in list)
x.OrderByText = AdoS0SalesRules.PriorityCodeOrderByText(x.OrderByCode);
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);
if (item == null) return NotFound();
item.OrderByText = AdoS0SalesRules.PriorityCodeOrderByText(item.OrderByCode);
return Ok(item);
}
[HttpPost]
public async Task CreateAsync([FromBody] AdoS0PriorityCodeUpsertDto dto)
{
var now = DateTime.Now;
var entity = new AdoS0PriorityCode
{
CompanyRefId = dto.CompanyRefId,
FactoryRefId = dto.FactoryRefId,
DomainCode = dto.DomainCode,
Descr = dto.Descr,
Value = dto.Value,
Priority = dto.Priority,
OrderByCode = dto.OrderByCode,
SourceTable = dto.SourceTable,
SourceColumn = dto.SourceColumn,
SourceType = dto.SourceType,
SourceId = dto.SourceId,
ValueType = dto.ValueType,
ValueId = dto.ValueId,
IsActive = dto.IsActive,
CreateUser = dto.CreateUser,
CreateTime = now,
UpdateUser = dto.UpdateUser,
UpdateTime = null
};
await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
return Ok(entity);
}
[HttpPut("{id:long}")]
public async Task UpdateAsync(long id, [FromBody] AdoS0PriorityCodeUpsertDto dto)
{
var entity = await _rep.GetByIdAsync(id);
if (entity == null) return NotFound();
entity.CompanyRefId = dto.CompanyRefId;
entity.FactoryRefId = dto.FactoryRefId;
entity.DomainCode = dto.DomainCode;
entity.Descr = dto.Descr;
entity.Value = dto.Value;
entity.Priority = dto.Priority;
entity.OrderByCode = dto.OrderByCode;
entity.SourceTable = dto.SourceTable;
entity.SourceColumn = dto.SourceColumn;
entity.SourceType = dto.SourceType;
entity.SourceId = dto.SourceId;
entity.ValueType = dto.ValueType;
entity.ValueId = dto.ValueId;
entity.IsActive = dto.IsActive;
entity.UpdateUser = dto.UpdateUser;
entity.UpdateTime = DateTime.Now;
await _rep.AsUpdateable(entity).ExecuteCommandAsync();
return Ok(entity);
}
[HttpPatch("{id:long}/toggle-enabled")]
public async Task ToggleActiveAsync(long id, [FromBody] AdoS0PriorityCodeToggleActiveDto dto)
{
var entity = await _rep.GetByIdAsync(id);
if (entity == null) return NotFound();
entity.IsActive = dto.IsActive;
entity.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 = "删除成功" });
}
}