| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- using Admin.NET.Plugin.AiDOP.Dto.S0.Supply;
- using Admin.NET.Plugin.AiDOP.Entity.S0.Supply;
- using Admin.NET.Plugin.AiDOP.Infrastructure;
- namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Supply;
- /// <summary>
- /// S0 品类采购前置期主数据(订单评审/MRP 取数优先配置)。
- /// </summary>
- [ApiController]
- [Route("api/s0/supply/category-lead-times")]
- [AllowAnonymous]
- [NonUnify]
- public class AdoS0CategoryLeadTimesController : ControllerBase
- {
- private readonly SqlSugarRepository<AdoS0CategoryLeadTime> _rep;
- public AdoS0CategoryLeadTimesController(SqlSugarRepository<AdoS0CategoryLeadTime> rep)
- {
- _rep = rep;
- }
- [HttpGet]
- public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0CategoryLeadTimeQueryDto q)
- {
- (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
- var baseQuery = _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.CategoryCode), x => x.CategoryCode.Contains(q.CategoryCode!))
- .WhereIF(!string.IsNullOrWhiteSpace(q.OwnerApplication), x => x.OwnerApplication.Contains(q.OwnerApplication!))
- .WhereIF(q.IsActive.HasValue, x => x.IsActive == q.IsActive.Value)
- .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
- x => x.CategoryCode.Contains(q.Keyword!)
- || (x.CategoryName != null && x.CategoryName.Contains(q.Keyword!))
- || x.OwnerApplication.Contains(q.Keyword!));
- var total = await baseQuery.CountAsync();
- var list = await baseQuery
- .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<IActionResult> GetAsync(long id)
- {
- var item = await _rep.GetByIdAsync(id);
- if (item == null) return NotFound();
- return Ok(item);
- }
- [HttpPost]
- public async Task<IActionResult> CreateAsync([FromBody] AdoS0CategoryLeadTimeUpsertDto dto)
- {
- var uniqueErr = await EnsureUniqueAsync(dto, null);
- if (uniqueErr != null) return uniqueErr;
- var now = DateTime.Now;
- var entity = new AdoS0CategoryLeadTime
- {
- CompanyRefId = dto.CompanyRefId,
- FactoryRefId = dto.FactoryRefId,
- DomainCode = dto.DomainCode,
- CategoryCode = dto.CategoryCode.Trim(),
- CategoryName = dto.CategoryName,
- OwnerApplication = dto.OwnerApplication.Trim(),
- LeadTimeDays = dto.LeadTimeDays,
- Remarks = dto.Remarks,
- 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<IActionResult> UpdateAsync(long id, [FromBody] AdoS0CategoryLeadTimeUpsertDto dto)
- {
- var entity = await _rep.GetByIdAsync(id);
- if (entity == null) return NotFound();
- var uniqueErr = await EnsureUniqueAsync(dto, id);
- if (uniqueErr != null) return uniqueErr;
- entity.CompanyRefId = dto.CompanyRefId;
- entity.FactoryRefId = dto.FactoryRefId;
- entity.DomainCode = dto.DomainCode;
- entity.CategoryCode = dto.CategoryCode.Trim();
- entity.CategoryName = dto.CategoryName;
- entity.OwnerApplication = dto.OwnerApplication.Trim();
- entity.LeadTimeDays = dto.LeadTimeDays;
- entity.Remarks = dto.Remarks;
- 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<IActionResult> ToggleActiveAsync(long id, [FromBody] AdoS0CategoryLeadTimeToggleActiveDto 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<IActionResult> DeleteAsync(long id)
- {
- var item = await _rep.GetByIdAsync(id);
- if (item == null) return NotFound();
- await _rep.DeleteAsync(item);
- return Ok(new { message = "删除成功" });
- }
- private async Task<IActionResult?> EnsureUniqueAsync(AdoS0CategoryLeadTimeUpsertDto dto, long? id)
- {
- var categoryCode = dto.CategoryCode.Trim();
- var ownerApplication = dto.OwnerApplication.Trim();
- if (await _rep.IsAnyAsync(x =>
- x.Id != (id ?? 0)
- && x.FactoryRefId == dto.FactoryRefId
- && x.CategoryCode == categoryCode
- && x.OwnerApplication == ownerApplication))
- {
- return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.InvalidRequest,
- "同工厂下“品类编码 + 物料属性”已存在前置期配置");
- }
- return null;
- }
- }
|