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(!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(q.OrderBy.HasValue, x => x.OrderBy == q.OrderBy.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.OrderBy);
return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
}
[HttpGet("{id}")]
public async Task GetAsync(string id)
{
var item = await _rep.GetByIdAsync(id);
if (item == null) return NotFound();
item.OrderByText = AdoS0SalesRules.PriorityCodeOrderByText(item.OrderBy);
return Ok(item);
}
[HttpPost]
public async Task CreateAsync([FromBody] AdoS0PriorityCodeUpsertDto dto)
{
var now = DateTime.Now;
var entity = new AdoS0PriorityCode
{
Id = Guid.NewGuid().ToString("D").ToUpperInvariant(),
DomainCode = dto.DomainCode,
Descr = dto.Descr ?? string.Empty,
Value = dto.Value,
Priority = dto.Priority,
OrderBy = dto.OrderBy,
SourceTable = dto.SourceTable,
SourceColumn = dto.SourceColumn,
SourceType = dto.SourceType,
SourceId = dto.SourceId,
ValueType = dto.ValueType,
ValueId = dto.ValueId,
IsActive = dto.IsActive,
CreateUser = dto.CreateUser,
LegacyCreateUser = dto.LegacyCreateUser,
CreateTime = now,
UpdateUser = dto.UpdateUser,
UpdateTime = null
};
await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
return Ok(entity);
}
[HttpPut("{id}")]
public async Task UpdateAsync(string id, [FromBody] AdoS0PriorityCodeUpsertDto dto)
{
var entity = await _rep.GetByIdAsync(id);
if (entity == null) return NotFound();
entity.DomainCode = dto.DomainCode;
entity.Descr = dto.Descr ?? string.Empty;
entity.Value = dto.Value;
entity.Priority = dto.Priority;
entity.OrderBy = dto.OrderBy;
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.LegacyCreateUser = dto.LegacyCreateUser;
entity.UpdateTime = DateTime.Now;
await _rep.AsUpdateable(entity).ExecuteCommandAsync();
return Ok(entity);
}
[HttpPatch("{id}/toggle-enabled")]
public async Task ToggleActiveAsync(string 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}")]
public async Task DeleteAsync(string id)
{
var item = await _rep.GetByIdAsync(id);
if (item == null) return NotFound();
await _rep.DeleteAsync(item);
return Ok(new { message = "删除成功" });
}
}