| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- using System.Text.Json;
- 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;
- /// <summary>
- /// S0 订单优先级业务规则(ado_s0_sales_order_priority_rule)
- /// </summary>
- [ApiController]
- [Route("api/s0/sales/priority-rules")]
- [AllowAnonymous]
- [NonUnify]
- public class AdoS0PriorityRulesController : ControllerBase
- {
- private readonly SqlSugarRepository<AdoS0PriorityRule> _rep;
- public AdoS0PriorityRulesController(SqlSugarRepository<AdoS0PriorityRule> rep)
- {
- _rep = rep;
- }
- [HttpGet]
- public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0PriorityRuleQueryDto 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(q.IsEnabled.HasValue, x => x.IsEnabled == q.IsEnabled!.Value)
- .WhereIF(
- !string.IsNullOrWhiteSpace(q.Keyword),
- x => x.Code.Contains(q.Keyword!) || x.Name.Contains(q.Keyword!));
- var total = await query.CountAsync();
- var list = await query
- .OrderBy(x => x.PriorityLevel)
- .Skip((q.Page - 1) * q.PageSize)
- .Take(q.PageSize)
- .ToListAsync();
- foreach (var item in list)
- FillRuleExprLabels(item);
- return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
- }
- [HttpGet("{id:long}")]
- public async Task<IActionResult> GetAsync(long id)
- {
- var item = await _rep.GetByIdAsync(id);
- if (item == null) return NotFound();
- FillRuleExprLabels(item);
- return Ok(item);
- }
- [HttpPost]
- public async Task<IActionResult> CreateAsync([FromBody] AdoS0PriorityRuleUpsertDto dto)
- {
- var now = DateTime.Now;
- var entity = MapToEntity(dto);
- entity.CreatedAt = now;
- await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
- FillRuleExprLabels(entity);
- return Ok(entity);
- }
- [HttpPut("{id:long}")]
- public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0PriorityRuleUpsertDto dto)
- {
- var entity = await _rep.GetByIdAsync(id);
- if (entity == null) return NotFound();
- ApplyUpsert(entity, dto);
- entity.UpdatedAt = DateTime.Now;
- await _rep.AsUpdateable(entity).ExecuteCommandAsync();
- FillRuleExprLabels(entity);
- return Ok(entity);
- }
- [HttpPatch("{id:long}/toggle-enabled")]
- public async Task<IActionResult> ToggleEnabledAsync(long id, [FromBody] AdoS0PriorityRuleToggleDto 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<IActionResult> DeleteAsync(long id)
- {
- var item = await _rep.GetByIdAsync(id);
- if (item == null) return NotFound();
- await _rep.DeleteAsync(item);
- return Ok(new { message = "删除成功" });
- }
- // ── helpers ──────────────────────────────────────────────────────────────
- private static AdoS0PriorityRule MapToEntity(AdoS0PriorityRuleUpsertDto dto)
- {
- var entity = new AdoS0PriorityRule();
- ApplyUpsert(entity, dto);
- return entity;
- }
- private static void ApplyUpsert(AdoS0PriorityRule entity, AdoS0PriorityRuleUpsertDto dto)
- {
- entity.CompanyRefId = dto.CompanyRefId;
- entity.FactoryRefId = dto.FactoryRefId;
- entity.Code = dto.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.IsEnabled = dto.IsEnabled;
- entity.Remark = dto.Remark;
- entity.RuleExpr = BuildRuleExpr(dto.CustomerType, dto.OrderType, dto.DueStatus);
- }
- private static string? BuildRuleExpr(string? customerType, string? orderType, string? dueStatus)
- {
- if (string.IsNullOrWhiteSpace(customerType) &&
- string.IsNullOrWhiteSpace(orderType) &&
- string.IsNullOrWhiteSpace(dueStatus))
- return null;
- return JsonSerializer.Serialize(new
- {
- customerType,
- orderType,
- dueStatus,
- });
- }
- private static void FillRuleExprLabels(AdoS0PriorityRule item)
- {
- if (string.IsNullOrWhiteSpace(item.RuleExpr)) return;
- try
- {
- var doc = JsonSerializer.Deserialize<JsonElement>(item.RuleExpr);
- if (doc.TryGetProperty("customerType", out var ct)) item.CustomerTypeLabel = ct.GetString();
- if (doc.TryGetProperty("orderType", out var ot)) item.OrderTypeLabel = ot.GetString();
- if (doc.TryGetProperty("dueStatus", out var ds)) item.DueStatusLabel = ds.GetString();
- }
- catch { /* 忽略格式错误的历史数据 */ }
- }
- }
|