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/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] AdoS0OrderPriorityRuleQueryDto 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.SourceEntity), x => x.SourceEntity == q.SourceEntity) .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] AdoS0OrderPriorityRuleUpsertDto dto) { var entity = new AdoS0OrderPriorityRule { CompanyRefId = dto.CompanyRefId, FactoryRefId = dto.FactoryRefId, Code = AdoS0SalesRules.ResolveOrderPriorityRuleCodeForCreate( dto.Code, DateTime.Now, Random.Shared.Next(1000, 9999)), Name = dto.Name, PriorityLevel = dto.PriorityLevel, SortDirection = dto.SortDirection, SourceEntity = dto.SourceEntity, SourceField = dto.SourceField, SourceFieldType = dto.SourceFieldType, SourceLinkField = dto.SourceLinkField, WorkOrderField = dto.WorkOrderField, WorkOrderFieldType = dto.WorkOrderFieldType, WorkOrderLinkField = dto.WorkOrderLinkField, RuleExpr = dto.RuleExpr, Remark = dto.Remark, IsEnabled = dto.IsEnabled, CreatedAt = DateTime.Now }; await _rep.AsInsertable(entity).ExecuteReturnEntityAsync(); return Ok(entity); } [HttpPut("{id:long}")] public async Task UpdateAsync(long id, [FromBody] AdoS0OrderPriorityRuleUpsertDto dto) { var entity = await _rep.GetByIdAsync(id); if (entity == null) return NotFound(); entity.CompanyRefId = dto.CompanyRefId; entity.FactoryRefId = dto.FactoryRefId; entity.Code = AdoS0SalesRules.ResolveOrderPriorityRuleCodeForUpdate(dto.Code, entity.Code); entity.Name = dto.Name; entity.PriorityLevel = dto.PriorityLevel; entity.SortDirection = dto.SortDirection; entity.SourceEntity = dto.SourceEntity; entity.SourceField = dto.SourceField; entity.SourceFieldType = dto.SourceFieldType; entity.SourceLinkField = dto.SourceLinkField; entity.WorkOrderField = dto.WorkOrderField; entity.WorkOrderFieldType = dto.WorkOrderFieldType; entity.WorkOrderLinkField = dto.WorkOrderLinkField; entity.RuleExpr = dto.RuleExpr; entity.Remark = dto.Remark; entity.IsEnabled = 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.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 = "删除成功" }); } }