AdoS0MaterialsController.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using Admin.NET.Plugin.AiDOP.Dto.S0.Sales;
  2. using Admin.NET.Plugin.AiDOP.Entity.S0.Sales;
  3. using Admin.NET.Plugin.AiDOP.Infrastructure;
  4. namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Sales;
  5. /// <summary>
  6. /// S0 物料主数据
  7. /// </summary>
  8. [ApiController]
  9. [Route("api/s0/sales/materials")]
  10. [AllowAnonymous]
  11. [NonUnify]
  12. public class AdoS0MaterialsController : ControllerBase
  13. {
  14. private readonly SqlSugarRepository<AdoS0Material> _rep;
  15. public AdoS0MaterialsController(SqlSugarRepository<AdoS0Material> rep)
  16. {
  17. _rep = rep;
  18. }
  19. [HttpGet]
  20. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0MaterialQueryDto q)
  21. {
  22. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  23. var query = _rep.AsQueryable()
  24. .WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId.Value)
  25. .WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId.Value)
  26. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x => x.Code.Contains(q.Keyword!) || x.Name.Contains(q.Keyword!))
  27. .WhereIF(!string.IsNullOrWhiteSpace(q.Code), x => x.Code.Contains(q.Code!))
  28. .WhereIF(!string.IsNullOrWhiteSpace(q.Name), x => x.Name.Contains(q.Name!))
  29. .WhereIF(!string.IsNullOrWhiteSpace(q.Spec), x => x.Spec!.Contains(q.Spec!))
  30. .WhereIF(!string.IsNullOrWhiteSpace(q.DrawingNo), x => x.DrawingNo!.Contains(q.DrawingNo!))
  31. .WhereIF(!string.IsNullOrWhiteSpace(q.PlCategory), x => x.PlCategory == q.PlCategory)
  32. .WhereIF(!string.IsNullOrWhiteSpace(q.MaterialType), x => x.MaterialType == q.MaterialType)
  33. .WhereIF(!string.IsNullOrWhiteSpace(q.Language), x => x.Language == q.Language)
  34. .WhereIF(q.IsEnabled.HasValue, x => x.IsEnabled == q.IsEnabled.Value);
  35. var total = await query.CountAsync();
  36. var list = await query
  37. .OrderByDescending(x => x.CreatedAt)
  38. .Skip((q.Page - 1) * q.PageSize)
  39. .Take(q.PageSize)
  40. .ToListAsync();
  41. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  42. }
  43. [HttpGet("{id:long}")]
  44. public async Task<IActionResult> GetAsync(long id)
  45. {
  46. var item = await _rep.GetByIdAsync(id);
  47. return item == null ? NotFound() : Ok(item);
  48. }
  49. [HttpPost]
  50. public async Task<IActionResult> CreateAsync([FromBody] AdoS0MaterialUpsertDto dto)
  51. {
  52. if (await _rep.IsAnyAsync(x =>
  53. x.CompanyRefId == dto.CompanyRefId &&
  54. x.FactoryRefId == dto.FactoryRefId &&
  55. x.Code == dto.Code))
  56. {
  57. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.MaterialCodeExists, "物料编码已存在");
  58. }
  59. var entity = new AdoS0Material
  60. {
  61. CompanyRefId = dto.CompanyRefId,
  62. FactoryRefId = dto.FactoryRefId,
  63. Code = dto.Code,
  64. Name = dto.Name,
  65. NameEn = dto.NameEn,
  66. MaterialType = dto.MaterialType,
  67. Unit = dto.Unit,
  68. Spec = dto.Spec,
  69. PlCategory = dto.PlCategory,
  70. DrawingNo = dto.DrawingNo,
  71. Language = dto.Language,
  72. BizVersion = dto.BizVersion,
  73. ProductCode = dto.ProductCode,
  74. MaterialAttribute = dto.MaterialAttribute,
  75. DefaultLocationId = dto.DefaultLocationId,
  76. DefaultRackId = dto.DefaultRackId,
  77. StockTypeCode = dto.StockTypeCode,
  78. SafetyStock = dto.SafetyStock,
  79. ShelfLifeDays = dto.ShelfLifeDays,
  80. ExpireWarningDays = dto.ExpireWarningDays,
  81. PurchaseLeadDays = dto.PurchaseLeadDays,
  82. MinOrderQty = dto.MinOrderQty,
  83. MaxOrderQty = dto.MaxOrderQty,
  84. OrderMultiple = dto.OrderMultiple,
  85. PreparationLeadDays = dto.PreparationLeadDays,
  86. IsOnDemand = dto.IsOnDemand,
  87. SpecialReqType = dto.SpecialReqType,
  88. IsInspectionRequired = dto.IsInspectionRequired,
  89. InspectionDays = dto.InspectionDays,
  90. IsKeyMaterial = dto.IsKeyMaterial,
  91. IsMainMaterial = dto.IsMainMaterial,
  92. IsPreprocess = dto.IsPreprocess,
  93. IsAutoBatch = dto.IsAutoBatch,
  94. IsLabelRequired = dto.IsLabelRequired,
  95. IsBatchFifoReminder = dto.IsBatchFifoReminder,
  96. IsBatchFifoStrict = dto.IsBatchFifoStrict,
  97. InventoryTurnoverRate = dto.InventoryTurnoverRate,
  98. Remark = dto.Remark,
  99. IsEnabled = dto.IsEnabled,
  100. CreatedAt = DateTime.Now
  101. };
  102. entity.ForbidStatus = AdoS0SalesRules.ForbidStatusFromIsEnabled(dto.IsEnabled);
  103. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  104. return Ok(entity);
  105. }
  106. [HttpPut("{id:long}")]
  107. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0MaterialUpsertDto dto)
  108. {
  109. var entity = await _rep.GetByIdAsync(id);
  110. if (entity == null) return NotFound();
  111. if (await _rep.IsAnyAsync(x =>
  112. x.Id != id &&
  113. x.CompanyRefId == dto.CompanyRefId &&
  114. x.FactoryRefId == dto.FactoryRefId &&
  115. x.Code == dto.Code))
  116. {
  117. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.MaterialCodeExists, "物料编码已存在");
  118. }
  119. entity.CompanyRefId = dto.CompanyRefId;
  120. entity.FactoryRefId = dto.FactoryRefId;
  121. entity.Code = dto.Code;
  122. entity.Name = dto.Name;
  123. entity.NameEn = dto.NameEn;
  124. entity.MaterialType = dto.MaterialType;
  125. entity.Unit = dto.Unit;
  126. entity.Spec = dto.Spec;
  127. entity.PlCategory = dto.PlCategory;
  128. entity.DrawingNo = dto.DrawingNo;
  129. entity.Language = dto.Language;
  130. entity.BizVersion = dto.BizVersion;
  131. entity.ProductCode = dto.ProductCode;
  132. entity.MaterialAttribute = dto.MaterialAttribute;
  133. entity.DefaultLocationId = dto.DefaultLocationId;
  134. entity.DefaultRackId = dto.DefaultRackId;
  135. entity.StockTypeCode = dto.StockTypeCode;
  136. entity.SafetyStock = dto.SafetyStock;
  137. entity.ShelfLifeDays = dto.ShelfLifeDays;
  138. entity.ExpireWarningDays = dto.ExpireWarningDays;
  139. entity.PurchaseLeadDays = dto.PurchaseLeadDays;
  140. entity.MinOrderQty = dto.MinOrderQty;
  141. entity.MaxOrderQty = dto.MaxOrderQty;
  142. entity.OrderMultiple = dto.OrderMultiple;
  143. entity.PreparationLeadDays = dto.PreparationLeadDays;
  144. entity.IsOnDemand = dto.IsOnDemand;
  145. entity.SpecialReqType = dto.SpecialReqType;
  146. entity.IsInspectionRequired = dto.IsInspectionRequired;
  147. entity.InspectionDays = dto.InspectionDays;
  148. entity.IsKeyMaterial = dto.IsKeyMaterial;
  149. entity.IsMainMaterial = dto.IsMainMaterial;
  150. entity.IsPreprocess = dto.IsPreprocess;
  151. entity.IsAutoBatch = dto.IsAutoBatch;
  152. entity.IsLabelRequired = dto.IsLabelRequired;
  153. entity.IsBatchFifoReminder = dto.IsBatchFifoReminder;
  154. entity.IsBatchFifoStrict = dto.IsBatchFifoStrict;
  155. entity.InventoryTurnoverRate = dto.InventoryTurnoverRate;
  156. entity.Remark = dto.Remark;
  157. entity.IsEnabled = dto.IsEnabled;
  158. entity.ForbidStatus = AdoS0SalesRules.ForbidStatusFromIsEnabled(dto.IsEnabled);
  159. entity.UpdatedAt = DateTime.Now;
  160. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  161. return Ok(entity);
  162. }
  163. [HttpPatch("{id:long}/toggle-enabled")]
  164. public async Task<IActionResult> ToggleEnabledAsync(long id, [FromBody] AdoS0ToggleEnabledDto dto)
  165. {
  166. var entity = await _rep.GetByIdAsync(id);
  167. if (entity == null) return NotFound();
  168. entity.IsEnabled = dto.IsEnabled;
  169. entity.ForbidStatus = AdoS0SalesRules.ForbidStatusFromIsEnabled(dto.IsEnabled);
  170. entity.UpdatedAt = DateTime.Now;
  171. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  172. return Ok(entity);
  173. }
  174. [HttpDelete("{id:long}")]
  175. public async Task<IActionResult> DeleteAsync(long id)
  176. {
  177. var item = await _rep.GetByIdAsync(id);
  178. if (item == null) return NotFound();
  179. await _rep.DeleteAsync(item);
  180. return Ok(new { message = "删除成功" });
  181. }
  182. }