using AgriculturalPlatform.Api.Data; using AgriculturalPlatform.Api.Dtos; using AgriculturalPlatform.Api.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace AgriculturalPlatform.Api.Controllers; /// 数据字典 [ApiController] [Route("api/dicts")] [Authorize] public class DictsController(AppDbContext db) : ControllerBase { /// 字典类型列表 [HttpGet] public async Task>> List(string? keyword, [FromQuery] int page = 1, [FromQuery] int pageSize = 20) { var query = db.SysDicts.AsQueryable(); if (!string.IsNullOrWhiteSpace(keyword)) query = query.Where(d => d.Name.Contains(keyword) || d.Code.Contains(keyword)); var total = await query.CountAsync(); var items = await query.OrderBy(d => d.Sort).ThenByDescending(d => d.Id) .Skip((page - 1) * pageSize).Take(pageSize) .Select(d => new DictDto(d.Id, d.Name, d.Code, d.Remark, d.IsSystem, d.Sort, d.Items.Count)) .ToListAsync(); return Ok(new PagedResult(items, total)); } /// 字典全部(含项,供下拉缓存使用) [HttpGet("all")] public async Task All() { var items = await db.SysDicts.AsNoTracking() .Include(d => d.Items.Where(i => i.Enabled)) .OrderBy(d => d.Sort) .Select(d => new { d.Code, Items = d.Items.OrderBy(i => i.Sort) .Select(i => new { i.Label, i.Value, i.Ext, i.IsDefault }) }) .ToListAsync(); return Ok(items); } /// 字典详情(含字典项) [HttpGet("{id:int}")] public async Task> Get(int id) { var dict = await db.SysDicts.Include(d => d.Items).FirstOrDefaultAsync(d => d.Id == id); if (dict is null) return NotFound(); var dto = new DictDto(dict.Id, dict.Name, dict.Code, dict.Remark, dict.IsSystem, dict.Sort, dict.Items.Count); var items = dict.Items.OrderBy(i => i.Sort) .Select(i => new DictItemDto(i.Id, i.DictId, i.Label, i.Value, i.Ext, i.IsDefault, i.Enabled, i.Sort)) .ToList(); return Ok(new DictDetailDto(dto, items)); } /// 新增字典类型 [HttpPost] public async Task> Create(DictSaveRequest req) { if (string.IsNullOrWhiteSpace(req.Code)) return BadRequest(new { message = "字典编码不能为空" }); if (await db.SysDicts.AnyAsync(d => d.Code == req.Code.Trim())) return BadRequest(new { message = "字典编码已存在" }); var dict = new SysDict { Name = req.Name, Code = req.Code.Trim(), Remark = req.Remark, Sort = req.Sort, CreatedAt = DateTime.Now }; db.SysDicts.Add(dict); await db.SaveChangesAsync(); return Ok(new DictDto(dict.Id, dict.Name, dict.Code, dict.Remark, dict.IsSystem, dict.Sort, 0)); } /// 修改字典类型 [HttpPut("{id:int}")] public async Task Update(int id, DictSaveRequest req) { var dict = await db.SysDicts.FindAsync(id); if (dict is null) return NotFound(); if (dict.Code != req.Code.Trim() && await db.SysDicts.AnyAsync(d => d.Code == req.Code.Trim())) return BadRequest(new { message = "字典编码已存在" }); dict.Name = req.Name; dict.Code = req.Code.Trim(); dict.Remark = req.Remark; dict.Sort = req.Sort; await db.SaveChangesAsync(); return Ok(new { message = "保存成功" }); } /// 删除字典类型(系统内置不可删除) [HttpDelete("{id:int}")] public async Task Delete(int id) { var dict = await db.SysDicts.FindAsync(id); if (dict is null) return NotFound(); if (dict.IsSystem) return BadRequest(new { message = "系统内置字典不可删除" }); db.SysDicts.Remove(dict); await db.SaveChangesAsync(); return Ok(new { message = "删除成功" }); } // ---------- 字典项 ---------- /// 新增字典项 [HttpPost("{dictId:int}/items")] public async Task> CreateItem(int dictId, DictItemSaveRequest req) { var dict = await db.SysDicts.FindAsync(dictId); if (dict is null) return NotFound(); if (await db.SysDictItems.AnyAsync(i => i.DictId == dictId && i.Value == req.Value)) return BadRequest(new { message = "字典项值已存在" }); var item = new SysDictItem { DictId = dictId, Label = req.Label, Value = req.Value, Ext = req.Ext, IsDefault = req.IsDefault, Enabled = req.Enabled, Sort = req.Sort }; db.SysDictItems.Add(item); await db.SaveChangesAsync(); return Ok(new DictItemDto(item.Id, item.DictId, item.Label, item.Value, item.Ext, item.IsDefault, item.Enabled, item.Sort)); } /// 修改字典项 [HttpPut("items/{id:int}")] public async Task UpdateItem(int id, DictItemSaveRequest req) { var item = await db.SysDictItems.FindAsync(id); if (item is null) return NotFound(); item.Label = req.Label; item.Value = req.Value; item.Ext = req.Ext; item.IsDefault = req.IsDefault; item.Enabled = req.Enabled; item.Sort = req.Sort; await db.SaveChangesAsync(); return Ok(new { message = "保存成功" }); } /// 删除字典项 [HttpDelete("items/{id:int}")] public async Task DeleteItem(int id) { var item = await db.SysDictItems.FindAsync(id); if (item is null) return NotFound(); db.SysDictItems.Remove(item); await db.SaveChangesAsync(); return Ok(new { message = "删除成功" }); } }