AdoS0EmpWorkDutiesController.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using Admin.NET.Plugin.AiDOP.Dto.S0.Warehouse;
  2. using Admin.NET.Plugin.AiDOP.Entity.S0.Warehouse;
  3. using Admin.NET.Plugin.AiDOP.Infrastructure;
  4. namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Warehouse;
  5. /// <summary>
  6. /// S0 物料职责维护(EmpWorkDutyMaster 语义)
  7. /// </summary>
  8. [ApiController]
  9. [Route("api/s0/warehouse/emp-work-duties")]
  10. [AllowAnonymous]
  11. [NonUnify]
  12. public class AdoS0EmpWorkDutiesController : ControllerBase
  13. {
  14. private readonly SqlSugarRepository<AdoS0EmpWorkDutyMaster> _rep;
  15. private readonly SqlSugarRepository<AdoS0EmployeeMaster> _empRep;
  16. private readonly SqlSugarRepository<AdoS0LocationMaster> _locRep;
  17. public AdoS0EmpWorkDutiesController(
  18. SqlSugarRepository<AdoS0EmpWorkDutyMaster> rep,
  19. SqlSugarRepository<AdoS0EmployeeMaster> empRep,
  20. SqlSugarRepository<AdoS0LocationMaster> locRep)
  21. {
  22. _rep = rep;
  23. _empRep = empRep;
  24. _locRep = locRep;
  25. }
  26. [HttpGet]
  27. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0EmpWorkDutyQueryDto q)
  28. {
  29. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  30. var query = _rep.AsQueryable()
  31. .WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId!.Value)
  32. .WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId!.Value)
  33. .WhereIF(!string.IsNullOrWhiteSpace(q.DomainCode), x => x.DomainCode == q.DomainCode)
  34. .WhereIF(!string.IsNullOrWhiteSpace(q.Employee), x => x.Employee.Contains(q.Employee!))
  35. .WhereIF(!string.IsNullOrWhiteSpace(q.Location), x => x.Location == q.Location)
  36. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
  37. x => x.Employee.Contains(q.Keyword!) || (x.ItemNum1 != null && x.ItemNum1.Contains(q.Keyword!)));
  38. var total = await query.CountAsync();
  39. var list = await query
  40. .OrderBy(x => x.Employee)
  41. .Skip((q.Page - 1) * q.PageSize)
  42. .Take(q.PageSize)
  43. .ToListAsync();
  44. await ApplyDisplayFieldsAsync(list);
  45. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  46. }
  47. [HttpGet("{id:long}")]
  48. public async Task<IActionResult> GetAsync(long id)
  49. {
  50. var item = await _rep.GetByIdAsync(id);
  51. if (item == null) return NotFound();
  52. await ApplyDisplayFieldsAsync(new List<AdoS0EmpWorkDutyMaster> { item });
  53. return Ok(item);
  54. }
  55. [HttpPost]
  56. public async Task<IActionResult> CreateAsync([FromBody] AdoS0EmpWorkDutyUpsertDto dto)
  57. {
  58. var entity = new AdoS0EmpWorkDutyMaster
  59. {
  60. CompanyRefId = dto.CompanyRefId,
  61. FactoryRefId = dto.FactoryRefId,
  62. DomainCode = dto.DomainCode ?? string.Empty,
  63. Employee = dto.Employee,
  64. ItemNum1 = dto.ItemNum1,
  65. ItemNum2 = dto.ItemNum2,
  66. Location = dto.Location,
  67. ProdLine = dto.ProdLine,
  68. Duty = dto.Duty,
  69. EmpType = dto.EmpType,
  70. Ufld2 = dto.Ufld2,
  71. CreateUser = dto.CreateUser,
  72. CreateTime = DateTime.Now
  73. };
  74. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  75. return Ok(entity);
  76. }
  77. [HttpPut("{id:long}")]
  78. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0EmpWorkDutyUpsertDto dto)
  79. {
  80. var entity = await _rep.GetByIdAsync(id);
  81. if (entity == null) return NotFound();
  82. entity.CompanyRefId = dto.CompanyRefId;
  83. entity.FactoryRefId = dto.FactoryRefId;
  84. entity.DomainCode = dto.DomainCode ?? string.Empty;
  85. entity.Employee = dto.Employee;
  86. entity.ItemNum1 = dto.ItemNum1;
  87. entity.ItemNum2 = dto.ItemNum2;
  88. entity.Location = dto.Location;
  89. entity.ProdLine = dto.ProdLine;
  90. entity.Duty = dto.Duty;
  91. entity.EmpType = dto.EmpType;
  92. entity.Ufld2 = dto.Ufld2;
  93. entity.UpdateUser = dto.UpdateUser;
  94. entity.UpdateTime = DateTime.Now;
  95. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  96. return Ok(entity);
  97. }
  98. [HttpDelete("{id:long}")]
  99. public async Task<IActionResult> DeleteAsync(long id)
  100. {
  101. var item = await _rep.GetByIdAsync(id);
  102. if (item == null) return NotFound();
  103. await _rep.DeleteAsync(item);
  104. return Ok(new { message = "删除成功" });
  105. }
  106. private static readonly Dictionary<string, string> DutyMap = new(StringComparer.OrdinalIgnoreCase)
  107. {
  108. ["iss-po"] = "采购收货",
  109. ["iss-so"] = "销售发货",
  110. ["iss-wo"] = "发料",
  111. ["packing"] = "退料",
  112. ["rct-unp"] = "计划外入库",
  113. ["rct-wo"] = "入库",
  114. ["Up-Shelf"] = "上架"
  115. };
  116. private async Task ApplyDisplayFieldsAsync(List<AdoS0EmpWorkDutyMaster> duties)
  117. {
  118. if (duties.Count == 0) return;
  119. var domainCodes = duties.Select(d => d.DomainCode).Distinct().ToList();
  120. var employees = await _empRep.AsQueryable()
  121. .Where(e => domainCodes.Contains(e.DomainCode))
  122. .ToListAsync();
  123. var locations = await _locRep.AsQueryable()
  124. .Where(l => domainCodes.Contains(l.DomainCode))
  125. .ToListAsync();
  126. foreach (var duty in duties)
  127. {
  128. var emp = employees.Find(e =>
  129. string.Equals(e.DomainCode, duty.DomainCode, StringComparison.OrdinalIgnoreCase)
  130. && string.Equals(e.Employee, duty.Employee, StringComparison.OrdinalIgnoreCase));
  131. duty.EmployeeName = emp?.Name;
  132. if (!string.IsNullOrWhiteSpace(duty.Location))
  133. {
  134. var loc = locations.Find(l =>
  135. string.Equals(l.DomainCode, duty.DomainCode, StringComparison.OrdinalIgnoreCase)
  136. && string.Equals(l.Location, duty.Location, StringComparison.OrdinalIgnoreCase));
  137. duty.LocationName = $"{duty.Location} {loc?.Descr ?? ""}".Trim();
  138. }
  139. // Duty 翻译(原始多值逗号/空格拼接场景兼容)
  140. if (!string.IsNullOrWhiteSpace(duty.Duty))
  141. {
  142. var parts = duty.Duty.Split(new[] { ',', ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
  143. duty.DutyDisplay = string.Join("、", parts.Select(p => DutyMap.TryGetValue(p.Trim(), out var cn) ? cn : p.Trim()));
  144. }
  145. }
  146. }
  147. }