using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading.Tasks;
using Yitter.IdGenerator;
using Znyc.Cloudcar.Admin.AspNetCore.Controllers;
using Znyc.Cloudcar.Admin.AspNetCore.Entitys;
using Znyc.Cloudcar.Admin.AspNetCore.Mvc;
using Znyc.Cloudcar.Admin.AspNetCore.ViewModel;
using Znyc.Cloudcar.Admin.Commons;
using Znyc.Cloudcar.Admin.Commons.Cache;
using Znyc.Cloudcar.Admin.Commons.Entitys;
using Znyc.Cloudcar.Admin.Commons.Extensions;
using Znyc.Cloudcar.Admin.Commons.Log;
using Znyc.Cloudcar.Admin.Commons.Mapping;
using Znyc.Cloudcar.Admin.Security.Dtos;
using Znyc.Cloudcar.Admin.Security.Entitys;
using Znyc.Cloudcar.Admin.Security.IServices;
namespace Znyc.Cloudcar.Admin.WebApi.Controllers
{
///
/// 数据字典接口
///
[ApiController]
[Route("api/Security/[controller]")]
public class DictionaryController : AreaApiController
{
//private readonly IDictionaryService _service;
///
/// 构造函数
///
///
public DictionaryController(IDictionaryService service) :
base(service)
{
_service = service;
}
///
/// 新增前处理数据
///
///
protected override void OnBeforeInsert(DictionaryEntity info)
{
info.Id = 0;
info.CreatedTime = DateTime.Now;
info.CreatedUserId = CurrentUser.UserId;
info.IsDeleted = false;
}
///
/// 在更新数据前对数据的修改操作
///
///
///
protected override void OnBeforeUpdate(DictionaryEntity info)
{
info.ModifiedUserId = CurrentUser.UserId;
info.ModifiedTime = DateTime.Now;
}
///
/// 在软删除数据前对数据的修改操作
///
///
///
protected override void OnBeforeSoftDelete(DictionaryEntity info)
{
info.IsDeleted = true;
}
///
/// 异步新增数据
///
///
///
[HttpPost("Insert")]
[FunctionAuthorize("Add")]
public async Task InsertAsync(DictionaryInputDto tinfo)
{
CommonResult result = new CommonResult();
CacheHelper cacheHelper = new CacheHelper();
if (tinfo.ParentId == 0)
{
DictionaryEntity isExsit = await _service.GetByEnCodAsynce(tinfo.Code);
if (isExsit != null)
{
result.ErrMsg = ReturnConst.Dictionary_Code_Exist;
return ToJsonContent(result);
}
}
else
{
tinfo.Code = (await _service.GetDictionaryById(tinfo.ParentId)).Code;
}
DictionaryEntity info = tinfo.MapTo();
OnBeforeInsert(info);
info.Id = YitIdHelper.NextId();
info.Code = tinfo.Code;
info.Description = info.Description == null ? CommonConst.String_Empty : info.Description;
int ln = await _service.InsertAsync(info).ConfigureAwait(false);
if (ln > 0)
{
cacheHelper.Remove("dictionary:list");
result.ErrCode = ErrCode.successCode;
result.ErrMsg = ErrCode.err0;
}
else
{
result.ErrMsg = ErrCode.err43001;
result.ErrCode = "43001";
}
return ToJsonContent(result);
}
///
/// 异步更新数据
///
///
///
[HttpPost("Update")]
[FunctionAuthorize("Edit")]
public async Task UpdateAsync(DictionaryInputDto tinfo)
{
CommonResult result = new CommonResult();
CacheHelper cacheHelper = new CacheHelper();
if (tinfo.ParentId == 0)
{
DictionaryEntity isExsit = await _service.GetByEnCodAsynce(tinfo.Code, tinfo.Id);
if (isExsit != null)
{
result.ErrMsg = ReturnConst.Dictionary_Code_Exist;
return ToJsonContent(result);
}
}
else
{
tinfo.Code = (await _service.GetDictionaryById(tinfo.ParentId)).Code;
}
DictionaryEntity info = _service.Get(tinfo.Id);
info.ParentId = tinfo.ParentId;
info.Code = tinfo.Code;
info.Value = tinfo.Value;
info.Description = tinfo.Description;
info.Name = tinfo.Name;
info.IsEnabled = tinfo.IsEnabled;
info.Sort = tinfo.Sort;
info.Description = info.Description == null ? CommonConst.String_Empty : tinfo.Description;
OnBeforeUpdate(info);
bool bl = await _service.UpdateAsync(info, tinfo.Id).ConfigureAwait(false);
if (bl)
{
cacheHelper.Remove("dictionary:list");
result.ErrCode = ErrCode.successCode;
result.ErrMsg = ErrCode.err0;
}
else
{
result.ErrMsg = ErrCode.err43002;
result.ErrCode = "43002";
}
return ToJsonContent(result);
}
///
/// 获取功能菜单适用于Vue 树形列表
///
///
[HttpGet("GetAllDictionaryTreeTable")]
[FunctionAuthorize("List")]
public async Task GetAllDictionaryTreeTable()
{
CommonResult result = new CommonResult();
try
{
System.Collections.Generic.List list = await _service.GetAllDictionaryTreeTable();
result.Success = true;
result.ErrCode = ErrCode.successCode;
result.ResData = list;
}
catch (Exception ex)
{
Log4NetHelper.Error("获取菜单异常", ex);
result.ErrMsg = ErrCode.err40110;
result.ErrCode = "40110";
}
return ToJsonContent(result);
}
///
/// 查询单条数据字典
///
///
///
[HttpGet("GetDictionaryById")]
[FunctionAuthorize("")]
public async Task GetDictionaryById(long id)
{
CommonResult result = new CommonResult
{
ResData = await _service.GetDictionaryById(id),
ErrCode = ErrCode.successCode,
ErrMsg = ErrCode.err0
};
return ToJsonContent(result);
}
///
/// 异步批量禁用数据
///
///
[HttpPost("SetEnabledMarktBatchAsync")]
[FunctionAuthorize("")]
public async Task SetEnabledMarktBatchAsync(UpdateEnableViewModel info)
{
CommonResult result = new CommonResult();
CacheHelper cacheHelper = new CacheHelper();
string where = string.Empty;
if (typeof(int) == typeof(string))
{
@where = "id in ('" + info.Ids.Join(",").Trim(',').Replace(",", "','") + "')";
}
else if (typeof(int) == typeof(int))
{
@where = "id in (" + info.Ids.Join(",") + ")";
}
if (!string.IsNullOrEmpty(where))
{
bool bl = false;
if (info.Flag == "1")
{
bl = true;
}
bool blResult = await _service.SetEnabledMarkByWhereAsync(bl, where, CurrentUser.UserId);
if (blResult)
{
cacheHelper.Remove("dictionary:list");
result.ErrCode = ErrCode.successCode;
result.ErrMsg = ErrCode.err0;
}
else
{
result.ErrMsg = ErrCode.err43002;
result.ErrCode = "43002";
}
}
return ToJsonContent(result);
}
///
/// 异步软删除信息
///
/// 主键Id
/// 删除标识,默认为1:即设为删除,0:未删除
[HttpDelete("DeleteSoftAsync")]
[FunctionAuthorize("DeleteSoft")]
public async Task DeleteSoftAsync(long id, string bltag = "1")
{
CommonResult result = new CommonResult();
System.Collections.Generic.IEnumerable dictionaryList = await _service.GetListWhereAsync($" ParentId={id} and IsDeleted=0 and IsEnabled=1");
if (dictionaryList != null && dictionaryList.Count() > 0)
{
result.ErrMsg = "字典存在启用下级,不能删除";
return ToJsonContent(result);
}
CacheHelper cacheHelper = new CacheHelper();
bool bl = false;
if (bltag == "0")
{
bl = true;
}
bool blResult = await _service.DeleteSoftAsync(bl, id, CurrentUser.UserId);
if (blResult)
{
cacheHelper.Remove("dictionary:list");
result.ErrCode = ErrCode.successCode;
result.ErrMsg = ErrCode.err0;
}
else
{
result.ErrMsg = ErrCode.err43002;
result.ErrCode = "43002";
}
return ToJsonContent(result);
}
///
/// 异步批量软删除信息
///
///
///
[HttpDelete("DeleteSoftBatchAsync")]
[FunctionAuthorize("DeleteSoft")]
public async Task DeleteSoftBatchAsync(UpdateEnableViewModel info)
{
CommonResult result = new CommonResult();
CacheHelper cacheHelper = new CacheHelper();
string where = string.Empty;
string key = string.Empty;
if (typeof(int) == typeof(string))
{
@where = "id in ('" + info.Ids.Join(",").Trim(',').Replace(",", "','") + "')";
@key = " ParentId in ('" + info.Ids.Join(",").Trim(',').Replace(",", "','") + "') and IsDeleted=0 and IsEnabled=1";
}
else if (typeof(int) == typeof(int))
{
@where = "id in (" + info.Ids.Join(",") + ")";
@key = " ParentId in (" + info.Ids.Join(",") + ") and IsDeleted=0 and IsEnabled=1";
}
if (!string.IsNullOrEmpty(key))
{
System.Collections.Generic.IEnumerable dictionaryList = await _service.GetListWhereAsync(key);
if (dictionaryList != null && dictionaryList.Count() > 0)
{
result.ErrMsg = "字典存在启用下级,不能删除";
return ToJsonContent(result);
}
}
if (!string.IsNullOrEmpty(where))
{
bool bl = false;
if (info.Flag == "0")
{
bl = true;
}
bool blResult = await _service.DeleteSoftBatchAsync(bl, where, CurrentUser.UserId);
if (blResult)
{
cacheHelper.Remove("dictionary:list");
result.ErrCode = ErrCode.successCode;
result.ErrMsg = ErrCode.err0;
}
else
{
result.ErrMsg = ErrCode.err43002;
result.ErrCode = "43002";
}
}
return ToJsonContent(result);
}
///
/// 根据ParentId查询数据字典列表
///
///
///
[HttpGet("GetListByPidAsync")]
[FunctionAuthorize("")]
public async Task GetListByPidAsync(long pid)
{
var result = new CommonResult
{
ResData = await _service.GetListByPidAsync(pid),
ErrCode = ErrCode.successCode,
ErrMsg = ErrCode.err0
};
return ToJsonContent(result);
}
}
}