using Admin.NET.Plugin.AiDOP.Dto; using Admin.NET.Plugin.AiDOP.Entity; using Admin.NET.Plugin.AiDOP.Infrastructure; namespace Admin.NET.Plugin.AiDOP.Controllers; [ApiController] [Route("api/[controller]")] [AllowAnonymous] [NonUnify] public class PlanController : ControllerBase { private readonly SqlSugarRepository _planRep; public PlanController(SqlSugarRepository planRep) { _planRep = planRep; } [HttpGet] public async Task GetPlans([FromQuery] PlanQueryDto query) { (query.Page, query.PageSize) = PagingGuard.Normalize(query.Page, query.PageSize); var q = _planRep.AsQueryable(); if (!string.IsNullOrWhiteSpace(query.PlanNo)) q = q.Where(x => x.PlanNo.Contains(query.PlanNo)); if (!string.IsNullOrWhiteSpace(query.PlanType)) q = q.Where(x => x.PlanType == query.PlanType); if (!string.IsNullOrWhiteSpace(query.Status)) q = q.Where(x => x.Status == query.Status); var total = await q.CountAsync(); var list = await q .OrderByDescending(x => x.CreatedTime) .Skip((query.Page - 1) * query.PageSize) .Take(query.PageSize) .ToListAsync(); return Ok(new { total, page = query.Page, pageSize = query.PageSize, list = list.Select(x => new PlanDetailDto { Id = x.Id, PlanNo = x.PlanNo, PlanType = x.PlanType, ProductName = x.ProductName, Quantity = x.Quantity, CompletedQuantity = x.CompletedQuantity, StartDate = x.StartDate, EndDate = x.EndDate, Status = x.Status, Priority = x.Priority, Remark = x.Remark, CreatedTime = x.CreatedTime, UpdatedTime = x.UpdatedTime, CreatedBy = x.CreatedBy }).ToList() }); } [HttpGet("{id:long}")] public async Task GetPlan(long id) { var item = await _planRep.GetByIdAsync(id); if (item == null) return NotFound(); return Ok(new PlanDetailDto { Id = item.Id, PlanNo = item.PlanNo, PlanType = item.PlanType, ProductName = item.ProductName, Quantity = item.Quantity, CompletedQuantity = item.CompletedQuantity, StartDate = item.StartDate, EndDate = item.EndDate, Status = item.Status, Priority = item.Priority, Remark = item.Remark, CreatedTime = item.CreatedTime, UpdatedTime = item.UpdatedTime, CreatedBy = item.CreatedBy }); } [HttpPost] public async Task CreatePlan([FromBody] PlanCreateDto dto) { var item = new AdoPlan { PlanNo = $"PL{DateTime.Now:yyyyMMddHHmmss}{Random.Shared.Next(1000, 9999)}", PlanType = dto.PlanType, ProductName = dto.ProductName, Quantity = dto.Quantity, StartDate = dto.StartDate, EndDate = dto.EndDate, Priority = dto.Priority, Remark = dto.Remark, Status = "未开始", CreatedTime = DateTime.Now }; await _planRep.AsInsertable(item).ExecuteReturnEntityAsync(); return Ok(new { id = item.Id, message = "创建成功" }); } [HttpPut("{id:long}")] public async Task UpdatePlan(long id, [FromBody] PlanUpdateDto dto) { var item = await _planRep.GetByIdAsync(id); if (item == null) return NotFound(); item.PlanType = dto.PlanType; item.ProductName = dto.ProductName; item.Quantity = dto.Quantity; item.StartDate = dto.StartDate; item.EndDate = dto.EndDate; item.Priority = dto.Priority; item.Remark = dto.Remark; item.Status = dto.Status; item.UpdatedTime = DateTime.Now; await _planRep.AsUpdateable(item).ExecuteCommandAsync(); return Ok(new { message = "更新成功" }); } [HttpDelete("{id:long}")] public async Task DeletePlan(long id) { var item = await _planRep.GetByIdAsync(id); if (item == null) return NotFound(); await _planRep.DeleteAsync(item); return Ok(new { message = "删除成功" }); } }