using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using Znyc.Recruitment.Admin.AspNetCore.Controllers;
using Znyc.Recruitment.Admin.AspNetCore.Entitys;
using Znyc.Recruitment.Admin.AspNetCore.Mvc;
using Znyc.Recruitment.Admin.Commons.Entitys;
using Znyc.Recruitment.Admin.Commons.Log;
using Znyc.Recruitment.Admin.Commons.Mapping;
using Znyc.Recruitment.Admin.Security.Dtos;
using Znyc.Recruitment.Admin.Security.Entitys;
using Znyc.Recruitment.Admin.Security.IServices;
namespace Znyc.Recruitment.Admin.WebApi.Controllers
{
///
/// 功能菜单接口
///
[ApiController]
[Route("api/Security/[controller]")]
public class MenuController : AreaApiController
{
///
/// 构造函数
///
///
public MenuController(IMenuService service) : base(service)
{
_service = service;
}
///
/// 新增前处理数据
///
///
protected override void OnBeforeInsert(MenuEntity info)
{
info.Id = 0;
info.CreatedTime = DateTime.Now;
info.CreatedUserId = CurrentUser.UserId;
info.IsDeleted = false;
if (info.SortCode == null)
{
info.SortCode = 99;
}
if (string.IsNullOrEmpty(info.ParentId.ToString()))
{
info.Layers = 1;
info.ParentId = 0;
}
else
{
info.Layers = _service.Get(info.ParentId).Layers + 1;
}
if (info.MenuType == "F")
{
info.IsFrame = false;
info.Component = "";
info.UrlAddress = "";
}
}
///
/// 新增
///
///
///
[HttpPost("Insert")]
[FunctionAuthorize("Add")]
public async Task InsertAsync(MenuInputDto info)
{
CommonResult result = new CommonResult();
MenuEntity menu = info.MapTo();
long ln = 0;
if (info.IsBatch)
{
string strEnCode = info.EnCode;
MenuEntity listInfo = new MenuEntity();
listInfo = menu;
listInfo.FullName = "列表";
listInfo.EnCode = strEnCode + "/List";
listInfo.Icon = "list";
OnBeforeInsert(listInfo);
long listId = info.ParentId;
ln = _service.Insert(listInfo);
MenuEntity addInfo = new MenuEntity();
addInfo = menu;
addInfo.Id = 0;
addInfo.FullName = "新增";
addInfo.EnCode = strEnCode + "/Add";
addInfo.ParentId = listId;
addInfo.Icon = "add";
addInfo.SortCode = 1;
OnBeforeInsert(addInfo);
ln = _service.Insert(addInfo);
MenuEntity viewInfo = new MenuEntity();
viewInfo = menu;
viewInfo.Id = 0;
viewInfo.FullName = "查看";
viewInfo.EnCode = strEnCode + "/View";
viewInfo.ParentId = listId;
viewInfo.Icon = "eye-open";
viewInfo.SortCode = 1;
OnBeforeInsert(viewInfo);
ln = _service.Insert(viewInfo);
MenuEntity editnfo = new MenuEntity();
editnfo = menu;
editnfo.Id = 0;
editnfo.FullName = "修改";
editnfo.EnCode = strEnCode + "/Edit";
editnfo.ParentId = listId;
editnfo.Icon = "write";
editnfo.SortCode = 2;
OnBeforeInsert(editnfo);
ln = _service.Insert(editnfo);
MenuEntity enableInfo = new MenuEntity();
enableInfo = menu;
enableInfo.Id = 0;
enableInfo.FullName = "禁用";
enableInfo.EnCode = strEnCode + "/Enable";
enableInfo.ParentId = listId;
enableInfo.Icon = "pause";
enableInfo.SortCode = 3;
OnBeforeInsert(enableInfo);
ln = _service.Insert(enableInfo);
MenuEntity enableInfo1 = new MenuEntity();
enableInfo1 = menu;
enableInfo1.Id = 0;
enableInfo1.FullName = "启用";
enableInfo1.EnCode = strEnCode + "/Enable";
enableInfo1.ParentId = listId;
enableInfo1.Icon = "play";
enableInfo1.SortCode = 4;
OnBeforeInsert(enableInfo1);
ln = _service.Insert(enableInfo1);
MenuEntity deleteSoftInfo = new MenuEntity();
deleteSoftInfo = menu;
deleteSoftInfo.Id = 0;
deleteSoftInfo.FullName = "软删除";
deleteSoftInfo.EnCode = strEnCode + "/DeleteSoft";
deleteSoftInfo.ParentId = listId;
deleteSoftInfo.Icon = "remove";
deleteSoftInfo.SortCode = 5;
OnBeforeInsert(deleteSoftInfo);
ln = _service.Insert(deleteSoftInfo);
MenuEntity deleteInfo = new MenuEntity();
deleteInfo = menu;
deleteInfo.Id = 0;
deleteInfo.FullName = "删除";
deleteInfo.EnCode = strEnCode + "/Delete";
deleteInfo.ParentId = listId;
deleteInfo.Icon = "remove";
deleteInfo.SortCode = 6;
OnBeforeInsert(deleteInfo);
ln = _service.Insert(deleteInfo);
}
else
{
OnBeforeInsert(menu);
ln = await _service.InsertAsync(menu);
}
if (ln >= 0)
{
result.ErrCode = ErrCode.successCode;
result.ErrMsg = ErrCode.err0;
}
else
{
result.ErrCode = "43001";
}
return ToJsonContent(result);
}
///
/// 在更新数据前对数据的修改操作
///
///
///
protected override void OnBeforeUpdate(MenuEntity info)
{
info.ModifiedUserId = CurrentUser.UserId;
info.ModifiedTime = DateTime.Now;
if (info.SortCode == null)
{
info.SortCode = 99;
}
if (string.IsNullOrEmpty(info.ParentId.ToString()))
{
info.Layers = 1;
info.ParentId = 0;
}
else
{
info.Layers = _service.Get(info.ParentId).Layers + 1;
}
}
///
/// 在软删除数据前对数据的修改操作
///
///
///
protected override void OnBeforeSoftDelete(MenuEntity info)
{
info.IsDeleted = true;
}
///
/// 异步更新数据
///
///
///
[HttpPost("Update")]
[FunctionAuthorize("Edit")]
public async Task UpdateAsync(MenuInputDto tinfo)
{
CommonResult result = new CommonResult();
MenuEntity info = _service.Get(tinfo.Id);
info.FullName = tinfo.FullName;
info.EnCode = tinfo.EnCode;
info.SystemTypeId = tinfo.SystemTypeId;
info.ParentId = tinfo.ParentId;
info.Icon = tinfo.Icon;
info.SortCode = tinfo.SortCode;
info.Description = tinfo.Description;
info.MenuType = tinfo.MenuType;
info.ActiveMenu = tinfo.ActiveMenu;
if (info.MenuType == "F")
{
info.IsFrame = false;
info.Component = "";
info.UrlAddress = "";
}
else
{
info.Component = tinfo.Component;
info.IsFrame = tinfo.IsFrame;
info.UrlAddress = tinfo.UrlAddress;
}
info.IsShow = tinfo.IsShow;
OnBeforeUpdate(info);
bool bl = await _service.UpdateAsync(info, tinfo.Id).ConfigureAwait(false);
if (bl)
{
result.ErrCode = ErrCode.successCode;
result.ErrMsg = ErrCode.err0;
}
else
{
result.ErrMsg = ErrCode.err43002;
result.ErrCode = "43002";
}
return ToJsonContent(result);
}
///
/// 获取功能菜单适用于Vue 树形列表
///
/// 子系统Id
///
[HttpGet("GetAllMenuTreeTable")]
[FunctionAuthorize("List")]
public async Task GetAllMenuTreeTable(string systemTypeId)
{
CommonResult result = new CommonResult();
try
{
System.Collections.Generic.List list = await _service.GetAllMenuTreeTable(systemTypeId);
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);
}
/////
///// 异步批量物理删除
/////
/////
//[HttpDelete("DeleteBatchAsync")]
//[FunctionAuthorize("Delete")]
//public override async Task DeleteBatchAsync(DeletesInputDto info)
//{
// var result = new CommonResult();
// if (info.Ids.Length > 0)
// {
// result = await _service.DeleteBatchWhereAsync(info).ConfigureAwait(false);
// if (result.Success)
// {
// result.ErrCode = ErrCode.successCode;
// result.ErrMsg = ErrCode.err0;
// }
// else
// {
// result.ErrCode = "43003";
// }
// }
// return ToJsonContent(result);
//}
}
}