203 lines
7.8 KiB
C#
203 lines
7.8 KiB
C#
|
using System;
|
|||
|
using AGSS.Models.Entities;
|
|||
|
using AGSS.Models.Template;
|
|||
|
using Microsoft.AspNetCore.Authorization;
|
|||
|
using Microsoft.AspNetCore.Identity;
|
|||
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
using System.Linq;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using AGSS.DbSet;
|
|||
|
|
|||
|
namespace AGSS.Controllers.Admin
|
|||
|
{
|
|||
|
|
|||
|
[Route("api/v1/[controller]/[action]")]
|
|||
|
public class AdminDictionaryController : ControllerBase
|
|||
|
{
|
|||
|
private readonly ApplicationDbContext _dbContext;
|
|||
|
private readonly UserManager<UserModel> _userManager;
|
|||
|
|
|||
|
public AdminDictionaryController(ApplicationDbContext dbContext, UserManager<UserModel> userManager)
|
|||
|
{
|
|||
|
_dbContext = dbContext;
|
|||
|
_userManager = userManager;
|
|||
|
}
|
|||
|
|
|||
|
[HttpPost]
|
|||
|
[Authorize(Roles = "Admin")]
|
|||
|
public async Task<IActionResult> GetParentDictionaries([FromBody] string label)
|
|||
|
{
|
|||
|
|
|||
|
|
|||
|
// 确保 label 不是 null
|
|||
|
label ??= string.Empty;
|
|||
|
|
|||
|
var parentDictionaries = _dbContext.Dictionaries.Where(d => d.ParentId == null && (string.IsNullOrEmpty(label) || d.Label.Contains(label))).ToList();
|
|||
|
return Ok(new ReturnTemplate(200, "查询成功", parentDictionaries));
|
|||
|
}
|
|||
|
|
|||
|
[HttpPost]
|
|||
|
[Authorize(Roles = "Admin")]
|
|||
|
public async Task<IActionResult> AddParentDictionary([FromBody] DictionaryModel dictionary)
|
|||
|
{
|
|||
|
|
|||
|
|
|||
|
if (dictionary == null || string.IsNullOrWhiteSpace(dictionary.Label) || string.IsNullOrWhiteSpace(dictionary.Value))
|
|||
|
{
|
|||
|
return Ok(new ReturnTemplate(400, "请求参数无效,请提供Label和Value", null));
|
|||
|
}
|
|||
|
|
|||
|
dictionary.Uuid = Guid.NewGuid().ToString();
|
|||
|
dictionary.CreateTime = DateTime.UtcNow;
|
|||
|
dictionary.CreateUserId = _userManager.GetUserId(User);
|
|||
|
|
|||
|
_dbContext.Dictionaries.Add(dictionary);
|
|||
|
await _dbContext.SaveChangesAsync();
|
|||
|
|
|||
|
return Ok(new ReturnTemplate(200, "添加父级字典成功", dictionary));
|
|||
|
}
|
|||
|
|
|||
|
[HttpPut]
|
|||
|
[Authorize(Roles = "Admin")]
|
|||
|
public async Task<IActionResult> UpdateParentDictionary([FromBody] DictionaryModel dictionary)
|
|||
|
{
|
|||
|
|
|||
|
|
|||
|
if (dictionary == null || string.IsNullOrWhiteSpace(dictionary.Uuid) || string.IsNullOrWhiteSpace(dictionary.Label))
|
|||
|
{
|
|||
|
return Ok(new ReturnTemplate(400, "请求参数无效,请提供Uuid和Label", null));
|
|||
|
}
|
|||
|
|
|||
|
var existingDictionary = _dbContext.Dictionaries.FirstOrDefault(d => d.Uuid == dictionary.Uuid && d.ParentId == null);
|
|||
|
if (existingDictionary == null)
|
|||
|
{
|
|||
|
return Ok(new ReturnTemplate(404, "未找到指定的父级字典", null));
|
|||
|
}
|
|||
|
|
|||
|
existingDictionary.Label = dictionary.Label;
|
|||
|
await _dbContext.SaveChangesAsync();
|
|||
|
|
|||
|
return Ok(new ReturnTemplate(200, "更新父级字典成功", existingDictionary));
|
|||
|
}
|
|||
|
|
|||
|
[HttpDelete]
|
|||
|
[Authorize(Roles = "Admin")]
|
|||
|
public async Task<IActionResult> DeleteParentDictionary([FromBody] string uuid)
|
|||
|
{
|
|||
|
|
|||
|
|
|||
|
if (string.IsNullOrWhiteSpace(uuid))
|
|||
|
{
|
|||
|
return Ok(new ReturnTemplate(400, "请求参数无效,请提供Uuid", null));
|
|||
|
}
|
|||
|
|
|||
|
var parentDictionary = _dbContext.Dictionaries.FirstOrDefault(d => d.Uuid == uuid && d.ParentId == null);
|
|||
|
if (parentDictionary == null)
|
|||
|
{
|
|||
|
return Ok(new ReturnTemplate(404, "未找到指定的父级字典", null));
|
|||
|
}
|
|||
|
|
|||
|
_dbContext.Dictionaries.RemoveRange(_dbContext.Dictionaries.Where(d => d.ParentId == uuid));
|
|||
|
_dbContext.Dictionaries.Remove(parentDictionary);
|
|||
|
await _dbContext.SaveChangesAsync();
|
|||
|
|
|||
|
return Ok(new ReturnTemplate(200, "删除父级字典成功", null));
|
|||
|
}
|
|||
|
|
|||
|
[HttpPost]
|
|||
|
[Authorize(Roles = "Admin")]
|
|||
|
public async Task<IActionResult> CreateChildDictionary([FromBody] DictionaryModel dictionary)
|
|||
|
{
|
|||
|
|
|||
|
|
|||
|
if (dictionary == null || string.IsNullOrWhiteSpace(dictionary.ParentId) || string.IsNullOrWhiteSpace(dictionary.ParentValue) || string.IsNullOrWhiteSpace(dictionary.Label) || string.IsNullOrWhiteSpace(dictionary.Value))
|
|||
|
{
|
|||
|
return Ok(new ReturnTemplate(400, "请求参数无效,请提供ParentId、ParentValue、Label和Value", null));
|
|||
|
}
|
|||
|
|
|||
|
dictionary.Uuid = Guid.NewGuid().ToString();
|
|||
|
dictionary.CreateTime = DateTime.UtcNow;
|
|||
|
dictionary.CreateUserId = _userManager.GetUserId(User);
|
|||
|
|
|||
|
_dbContext.Dictionaries.Add(dictionary);
|
|||
|
await _dbContext.SaveChangesAsync();
|
|||
|
|
|||
|
return Ok(new ReturnTemplate(200, "创建子级字典成功", dictionary));
|
|||
|
}
|
|||
|
|
|||
|
[HttpPut]
|
|||
|
[Authorize(Roles = "Admin")]
|
|||
|
public async Task<IActionResult> UpdateChildDictionary([FromBody] DictionaryModel dictionary)
|
|||
|
{
|
|||
|
|
|||
|
|
|||
|
if (dictionary == null || string.IsNullOrWhiteSpace(dictionary.Uuid) || string.IsNullOrWhiteSpace(dictionary.Label) || string.IsNullOrWhiteSpace(dictionary.Value))
|
|||
|
{
|
|||
|
return Ok(new ReturnTemplate(400, "请求参数无效,请提供Uuid、Label和Value", null));
|
|||
|
}
|
|||
|
|
|||
|
var existingDictionary = _dbContext.Dictionaries.FirstOrDefault(d => d.Uuid == dictionary.Uuid && d.ParentId != null);
|
|||
|
if (existingDictionary == null)
|
|||
|
{
|
|||
|
return Ok(new ReturnTemplate(404, "未找到指定的子级字典", null));
|
|||
|
}
|
|||
|
|
|||
|
existingDictionary.Label = dictionary.Label;
|
|||
|
existingDictionary.LabelEn = dictionary.LabelEn;
|
|||
|
existingDictionary.Remark = dictionary.Remark;
|
|||
|
existingDictionary.Value = dictionary.Value;
|
|||
|
existingDictionary.Tag = dictionary.Tag;
|
|||
|
await _dbContext.SaveChangesAsync();
|
|||
|
|
|||
|
return Ok(new ReturnTemplate(200, "更新子级字典成功", existingDictionary));
|
|||
|
}
|
|||
|
|
|||
|
[HttpDelete]
|
|||
|
[Authorize(Roles = "Admin")]
|
|||
|
public async Task<IActionResult> DeleteChildDictionary([FromBody] string uuid)
|
|||
|
{
|
|||
|
|
|||
|
if (string.IsNullOrWhiteSpace(uuid))
|
|||
|
{
|
|||
|
return Ok(new ReturnTemplate(400, "请求参数无效,请提供Uuid", null));
|
|||
|
}
|
|||
|
|
|||
|
var childDictionary = _dbContext.Dictionaries.FirstOrDefault(d => d.Uuid == uuid && d.ParentId != null);
|
|||
|
if (childDictionary == null)
|
|||
|
{
|
|||
|
return Ok(new ReturnTemplate(404, "未找到指定的子级字典", null));
|
|||
|
}
|
|||
|
|
|||
|
_dbContext.Dictionaries.Remove(childDictionary);
|
|||
|
await _dbContext.SaveChangesAsync();
|
|||
|
|
|||
|
return Ok(new ReturnTemplate(200, "删除子级字典成功", null));
|
|||
|
}
|
|||
|
|
|||
|
[HttpGet]
|
|||
|
public IActionResult GetChildDictionaries([FromBody] ChildDictionaryRequest request)
|
|||
|
{
|
|||
|
if (string.IsNullOrWhiteSpace(request.Value))
|
|||
|
{
|
|||
|
return Ok(new ReturnTemplate(400, "请求参数无效,请提供Value", null));
|
|||
|
}
|
|||
|
|
|||
|
var childDictionaries = _dbContext.Dictionaries
|
|||
|
.Where(d => d.ParentValue == request.Value &&
|
|||
|
(request.Tag == null || (!string.IsNullOrEmpty(d.Tag) && d.Tag.Contains("," + request.Tag + ","))))
|
|||
|
.ToList();
|
|||
|
|
|||
|
if (request.Tag != null)
|
|||
|
{
|
|||
|
childDictionaries = childDictionaries.Where(d => !string.IsNullOrEmpty(d.Tag)).ToList();
|
|||
|
}
|
|||
|
|
|||
|
return Ok(new ReturnTemplate(200, "查询成功", childDictionaries));
|
|||
|
}
|
|||
|
public class ChildDictionaryRequest
|
|||
|
{
|
|||
|
public string Value { get; set; }
|
|||
|
public string Tag { get; set; }
|
|||
|
}
|
|||
|
}
|
|||
|
}
|