You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
5.5 KiB
C#

using Admin.Core.Model;
using Admin.Core.Common.Resource;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Admin.Core.Common;
using Admin.Core.IService.ISys;
using Admin.Core.Model.Sys;
namespace Admin.Core.Api
{
/// <summary>
/// SysPostController
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class SysPostController : BaseApiController
{
/// <summary>
/// _sysPostService
/// </summary>
private readonly ISysPostService _sysPostService;
/// <summary>
/// 构造方法
/// </summary>
/// <param name="SysPostService"></param>
public SysPostController(ISysPostService SysPostService)
{
_sysPostService = SysPostService;
}
/// <summary>
/// 分页查询
/// </summary>
/// <param name="pageQuery"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<PageModel<SysPost>>> GetByPage([FromBody] PageQuery<SysPost> pageQuery)
{
if (!pageQuery.Query.IsNotEmptyOrNull())
{
pageQuery.Query = new SysPost();
}
var data = await _sysPostService.SelectPostList(pageQuery);
return SuccessPage(data);
}
/// <summary>
/// 根据ID查询
/// </summary>
/// <param name="id">主键</param>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<SysPost>> GetByID(int id)
{
return new MessageModel<SysPost>()
{
msg = Resource_SysBase.OprateSuccess,
success = true,
data = await _sysPostService.SelectPostById(id)
};
}
/// <summary>
/// 新增一条数据
/// </summary>
/// <param name="request">SysPost</param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<int>> Add([FromBody] SysPost request)
{
var data = new MessageModel<int>();
if (!await _sysPostService.CheckPostNameUnique(request))
{
data.success = false;
data.msg = "岗位名称已存在!";
return data;
}
if (!await _sysPostService.CheckPostCodeUnique(request))
{
data.success = false;
data.msg = "岗位编码已存在!";
return data;
}
data.data = await _sysPostService.InsertPost(request);
if (data.data > 0)
{
data.success = true;
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.success = false;
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
/// <summary>
/// 更新一条数据
/// </summary>
/// <param name="request">SysPost</param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<bool>> Update([FromBody] SysPost request)
{
var data = new MessageModel<bool>();
if (!await _sysPostService.CheckPostNameUnique(request))
{
data.success = false;
data.msg = "岗位名称已存在!";
return data;
}
if (!await _sysPostService.CheckPostCodeUnique(request))
{
data.success = false;
data.msg = "岗位编码已存在!";
return data;
}
data.success = await _sysPostService.UpdatePost(request);
if (data.success)
{
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
/// <summary>
/// 删除一条数据
/// </summary>
/// <param name="ids">主键</param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<bool>> Del(List<int> ids)
{
var data = new MessageModel<bool>();
if ((await _sysPostService.CountUserPostById(ids)) > 0)
{
data.success = false;
data.msg = "岗位已分配用户,不可删除!";
return data;
}
data.success = await _sysPostService.DeletePostByIds(ids);
if (data.success)
{
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
/// <summary>
/// 数据导出
/// </summary>
/// <param name="post"></param>
/// <returns></returns>
[HttpPost]
public Task<MessageModel<string>> Export(SysPost post)
{
return null;
}
/// <summary>
/// 获取岗位选择框列表
/// </summary>
[HttpGet]
public async Task<MessageModel<List<SysPost>>> Optionselect()
{
return Success(await _sysPostService.SelectPostAll());
}
}
}