PlanController.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using Admin.NET.Plugin.AiDOP.Dto;
  2. using Admin.NET.Plugin.AiDOP.Entity;
  3. using Admin.NET.Plugin.AiDOP.Infrastructure;
  4. namespace Admin.NET.Plugin.AiDOP.Controllers;
  5. [ApiController]
  6. [Route("api/[controller]")]
  7. [AllowAnonymous]
  8. [NonUnify]
  9. public class PlanController : ControllerBase
  10. {
  11. private readonly SqlSugarRepository<AdoPlan> _planRep;
  12. public PlanController(SqlSugarRepository<AdoPlan> planRep)
  13. {
  14. _planRep = planRep;
  15. }
  16. [HttpGet]
  17. public async Task<IActionResult> GetPlans([FromQuery] PlanQueryDto query)
  18. {
  19. (query.Page, query.PageSize) = PagingGuard.Normalize(query.Page, query.PageSize);
  20. var q = _planRep.AsQueryable();
  21. if (!string.IsNullOrWhiteSpace(query.PlanNo))
  22. q = q.Where(x => x.PlanNo.Contains(query.PlanNo));
  23. if (!string.IsNullOrWhiteSpace(query.PlanType))
  24. q = q.Where(x => x.PlanType == query.PlanType);
  25. if (!string.IsNullOrWhiteSpace(query.Status))
  26. q = q.Where(x => x.Status == query.Status);
  27. var total = await q.CountAsync();
  28. var list = await q
  29. .OrderByDescending(x => x.CreatedTime)
  30. .Skip((query.Page - 1) * query.PageSize)
  31. .Take(query.PageSize)
  32. .ToListAsync();
  33. return Ok(new
  34. {
  35. total,
  36. page = query.Page,
  37. pageSize = query.PageSize,
  38. list = list.Select(x => new PlanDetailDto
  39. {
  40. Id = x.Id,
  41. PlanNo = x.PlanNo,
  42. PlanType = x.PlanType,
  43. ProductName = x.ProductName,
  44. Quantity = x.Quantity,
  45. CompletedQuantity = x.CompletedQuantity,
  46. StartDate = x.StartDate,
  47. EndDate = x.EndDate,
  48. Status = x.Status,
  49. Priority = x.Priority,
  50. Remark = x.Remark,
  51. CreatedTime = x.CreatedTime,
  52. UpdatedTime = x.UpdatedTime,
  53. CreatedBy = x.CreatedBy
  54. }).ToList()
  55. });
  56. }
  57. [HttpGet("{id:long}")]
  58. public async Task<IActionResult> GetPlan(long id)
  59. {
  60. var item = await _planRep.GetByIdAsync(id);
  61. if (item == null)
  62. return NotFound();
  63. return Ok(new PlanDetailDto
  64. {
  65. Id = item.Id,
  66. PlanNo = item.PlanNo,
  67. PlanType = item.PlanType,
  68. ProductName = item.ProductName,
  69. Quantity = item.Quantity,
  70. CompletedQuantity = item.CompletedQuantity,
  71. StartDate = item.StartDate,
  72. EndDate = item.EndDate,
  73. Status = item.Status,
  74. Priority = item.Priority,
  75. Remark = item.Remark,
  76. CreatedTime = item.CreatedTime,
  77. UpdatedTime = item.UpdatedTime,
  78. CreatedBy = item.CreatedBy
  79. });
  80. }
  81. [HttpPost]
  82. public async Task<IActionResult> CreatePlan([FromBody] PlanCreateDto dto)
  83. {
  84. var item = new AdoPlan
  85. {
  86. PlanNo = $"PL{DateTime.Now:yyyyMMddHHmmss}{Random.Shared.Next(1000, 9999)}",
  87. PlanType = dto.PlanType,
  88. ProductName = dto.ProductName,
  89. Quantity = dto.Quantity,
  90. StartDate = dto.StartDate,
  91. EndDate = dto.EndDate,
  92. Priority = dto.Priority,
  93. Remark = dto.Remark,
  94. Status = "未开始",
  95. CreatedTime = DateTime.Now
  96. };
  97. await _planRep.AsInsertable(item).ExecuteReturnEntityAsync();
  98. return Ok(new { id = item.Id, message = "创建成功" });
  99. }
  100. [HttpPut("{id:long}")]
  101. public async Task<IActionResult> UpdatePlan(long id, [FromBody] PlanUpdateDto dto)
  102. {
  103. var item = await _planRep.GetByIdAsync(id);
  104. if (item == null)
  105. return NotFound();
  106. item.PlanType = dto.PlanType;
  107. item.ProductName = dto.ProductName;
  108. item.Quantity = dto.Quantity;
  109. item.StartDate = dto.StartDate;
  110. item.EndDate = dto.EndDate;
  111. item.Priority = dto.Priority;
  112. item.Remark = dto.Remark;
  113. item.Status = dto.Status;
  114. item.UpdatedTime = DateTime.Now;
  115. await _planRep.AsUpdateable(item).ExecuteCommandAsync();
  116. return Ok(new { message = "更新成功" });
  117. }
  118. [HttpDelete("{id:long}")]
  119. public async Task<IActionResult> DeletePlan(long id)
  120. {
  121. var item = await _planRep.GetByIdAsync(id);
  122. if (item == null)
  123. return NotFound();
  124. await _planRep.DeleteAsync(item);
  125. return Ok(new { message = "删除成功" });
  126. }
  127. }