AdoS0MaterialsController.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. var entity = new AdoS0Material
  53. {
  54. CompanyRefId = dto.CompanyRefId,
  55. FactoryRefId = dto.FactoryRefId,
  56. Code = dto.Code,
  57. Name = dto.Name,
  58. NameEn = dto.NameEn,
  59. MaterialType = dto.MaterialType,
  60. Unit = dto.Unit,
  61. Spec = dto.Spec,
  62. PlCategory = dto.PlCategory,
  63. DrawingNo = dto.DrawingNo,
  64. Language = dto.Language,
  65. BizVersion = dto.BizVersion,
  66. ProductCode = dto.ProductCode,
  67. MaterialAttribute = dto.MaterialAttribute,
  68. DefaultLocationId = dto.DefaultLocationId,
  69. DefaultRackId = dto.DefaultRackId,
  70. StockTypeCode = dto.StockTypeCode,
  71. SafetyStock = dto.SafetyStock,
  72. ShelfLifeDays = dto.ShelfLifeDays,
  73. ExpireWarningDays = dto.ExpireWarningDays,
  74. PurchaseLeadDays = dto.PurchaseLeadDays,
  75. MinOrderQty = dto.MinOrderQty,
  76. MaxOrderQty = dto.MaxOrderQty,
  77. OrderMultiple = dto.OrderMultiple,
  78. PreparationLeadDays = dto.PreparationLeadDays,
  79. IsOnDemand = dto.IsOnDemand,
  80. SpecialReqType = dto.SpecialReqType,
  81. IsInspectionRequired = dto.IsInspectionRequired,
  82. InspectionDays = dto.InspectionDays,
  83. IsKeyMaterial = dto.IsKeyMaterial,
  84. IsMainMaterial = dto.IsMainMaterial,
  85. IsPreprocess = dto.IsPreprocess,
  86. IsAutoBatch = dto.IsAutoBatch,
  87. IsLabelRequired = dto.IsLabelRequired,
  88. IsBatchFifoReminder = dto.IsBatchFifoReminder,
  89. IsBatchFifoStrict = dto.IsBatchFifoStrict,
  90. InventoryTurnoverRate = dto.InventoryTurnoverRate,
  91. Remark = dto.Remark,
  92. IsEnabled = dto.IsEnabled,
  93. CreatedAt = DateTime.Now
  94. };
  95. entity.ForbidStatus = AdoS0SalesRules.ForbidStatusFromIsEnabled(dto.IsEnabled);
  96. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  97. return Ok(entity);
  98. }
  99. [HttpPut("{id:long}")]
  100. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0MaterialUpsertDto dto)
  101. {
  102. var entity = await _rep.GetByIdAsync(id);
  103. if (entity == null) return NotFound();
  104. entity.CompanyRefId = dto.CompanyRefId;
  105. entity.FactoryRefId = dto.FactoryRefId;
  106. entity.Code = dto.Code;
  107. entity.Name = dto.Name;
  108. entity.NameEn = dto.NameEn;
  109. entity.MaterialType = dto.MaterialType;
  110. entity.Unit = dto.Unit;
  111. entity.Spec = dto.Spec;
  112. entity.PlCategory = dto.PlCategory;
  113. entity.DrawingNo = dto.DrawingNo;
  114. entity.Language = dto.Language;
  115. entity.BizVersion = dto.BizVersion;
  116. entity.ProductCode = dto.ProductCode;
  117. entity.MaterialAttribute = dto.MaterialAttribute;
  118. entity.DefaultLocationId = dto.DefaultLocationId;
  119. entity.DefaultRackId = dto.DefaultRackId;
  120. entity.StockTypeCode = dto.StockTypeCode;
  121. entity.SafetyStock = dto.SafetyStock;
  122. entity.ShelfLifeDays = dto.ShelfLifeDays;
  123. entity.ExpireWarningDays = dto.ExpireWarningDays;
  124. entity.PurchaseLeadDays = dto.PurchaseLeadDays;
  125. entity.MinOrderQty = dto.MinOrderQty;
  126. entity.MaxOrderQty = dto.MaxOrderQty;
  127. entity.OrderMultiple = dto.OrderMultiple;
  128. entity.PreparationLeadDays = dto.PreparationLeadDays;
  129. entity.IsOnDemand = dto.IsOnDemand;
  130. entity.SpecialReqType = dto.SpecialReqType;
  131. entity.IsInspectionRequired = dto.IsInspectionRequired;
  132. entity.InspectionDays = dto.InspectionDays;
  133. entity.IsKeyMaterial = dto.IsKeyMaterial;
  134. entity.IsMainMaterial = dto.IsMainMaterial;
  135. entity.IsPreprocess = dto.IsPreprocess;
  136. entity.IsAutoBatch = dto.IsAutoBatch;
  137. entity.IsLabelRequired = dto.IsLabelRequired;
  138. entity.IsBatchFifoReminder = dto.IsBatchFifoReminder;
  139. entity.IsBatchFifoStrict = dto.IsBatchFifoStrict;
  140. entity.InventoryTurnoverRate = dto.InventoryTurnoverRate;
  141. entity.Remark = dto.Remark;
  142. entity.IsEnabled = dto.IsEnabled;
  143. entity.ForbidStatus = AdoS0SalesRules.ForbidStatusFromIsEnabled(dto.IsEnabled);
  144. entity.UpdatedAt = DateTime.Now;
  145. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  146. return Ok(entity);
  147. }
  148. [HttpPatch("{id:long}/toggle-enabled")]
  149. public async Task<IActionResult> ToggleEnabledAsync(long id, [FromBody] AdoS0ToggleEnabledDto dto)
  150. {
  151. var entity = await _rep.GetByIdAsync(id);
  152. if (entity == null) return NotFound();
  153. entity.IsEnabled = dto.IsEnabled;
  154. entity.ForbidStatus = AdoS0SalesRules.ForbidStatusFromIsEnabled(dto.IsEnabled);
  155. entity.UpdatedAt = DateTime.Now;
  156. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  157. return Ok(entity);
  158. }
  159. [HttpDelete("{id:long}")]
  160. public async Task<IActionResult> DeleteAsync(long id)
  161. {
  162. var item = await _rep.GetByIdAsync(id);
  163. if (item == null) return NotFound();
  164. await _rep.DeleteAsync(item);
  165. return Ok(new { message = "删除成功" });
  166. }
  167. }