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.

193 lines
6.1 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 System.Linq;
using Admin.Core.Common;
using Admin.Core.IService.ISys;
using Admin.Core.Model.Sys;
namespace Admin.Core.Api
{
/// <summary>
/// SysConfigController
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class SysConfigController : BaseApiUserController
{
/// <summary>
/// _sysConfigService
/// </summary>
private readonly ISysConfigService _sysConfigService;
private const int i=0;
/// <summary>
/// 构造方法
/// </summary>
/// <param name="SysConfigService"></param>
/// <param name="sysUserService"></param>
public SysConfigController(ISysConfigService SysConfigService, ISysUserService sysUserService) : base(sysUserService)
{
_sysConfigService = SysConfigService;
_sysUserService = sysUserService;
}
/// <summary>
/// 分页查询
/// </summary>
/// <param name="pageQuery"></param>
/// <returns></returns>
[HttpPost]
[Action("参数设置", ActionType.QUERY)]
public async Task<MessageModel<PageModel<SysConfig>>> GetByPage([FromBody] PageQuery<SysConfig> pageQuery)
{
Expression<Func<SysConfig, bool>> whereExpression = x => true;
if (pageQuery.Query.ConfigName.IsNotEmptyOrNull())
{
whereExpression = whereExpression.And(x => x.ConfigName.Contains(pageQuery.Query.ConfigName));
}
if (pageQuery.Query.ConfigKey.IsNotEmptyOrNull())
{
whereExpression = whereExpression.And(x => x.ConfigKey.Contains(pageQuery.Query.ConfigKey));
}
if (pageQuery.Query.CreateBy.IsNotEmptyOrNull())
{
whereExpression = whereExpression.And(x => x.CreateBy.Contains(pageQuery.Query.CreateBy));
}
if (pageQuery.Query.ConfigType.IsNotEmptyOrNull())
{
whereExpression = whereExpression.And(x => x.ConfigType == pageQuery.Query.ConfigType);
}
var data = await _sysConfigService.QueryPageAsync(whereExpression, pageQuery.Page, pageQuery.PageSize, "ConfigID asc");
return Success(data);
}
/// <summary>
/// 根据ID查询
/// </summary>
/// <param name="id">主键</param>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<SysConfig>> GetByID(int id)
{
return new MessageModel<SysConfig>()
{
msg = Resource_SysBase.OprateSuccess,
success = true,
data = await _sysConfigService.QueryByIdAsync(id)
};
}
/// <summary>
/// 新增一条数据
/// </summary>
/// <param name="request">SysConfig</param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<int>> Add([FromBody] SysConfig request)
{
var data = new MessageModel<int>();
if (await _sysConfigService.CheckConfigKeyUnique(request))
{
data.success = false;
data.msg = "参数键名已存在";
return data;
}
request.CreateTime = DateTime.Now;
request.UpdateTime = DateTime.Now;
request.CreateBy = CurrentUser.LoginName;
request.UpdateBy = CurrentUser.LoginName;
data.data = await _sysConfigService.AddAsync(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">SysConfig</param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<bool>> Update([FromBody] SysConfig request)
{
var data = new MessageModel<bool>();
if (await _sysConfigService.CheckConfigKeyUnique(request))
{
data.success = false;
data.msg = "参数键名已存在";
return data;
}
request.UpdateTime = DateTime.Now;
request.UpdateBy = CurrentUser.LoginName;
data.success = await _sysConfigService.UpdateAsync(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>();
data.success = await _sysConfigService.DeleteByIdAsync(ids);
if (data.success)
{
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
/// <summary>
/// 根据参数键名查询参数值
/// </summary>
/// <param name="configKey"></param>
/// <returns></returns>
[HttpGet]
[AllowAnonymous]
public async Task<MessageModel<string>> GetConfigKey(string configKey)
{
var config = await _sysConfigService.QueryAsync(x => x.ConfigKey == configKey);
return Success<string>(config.FirstOrDefault().ConfigValue);
}
}
}