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
{
///
/// SysConfigController
///
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class SysConfigController : BaseApiUserController
{
///
/// _sysConfigService
///
private readonly ISysConfigService _sysConfigService;
private const int i=0;
///
/// 构造方法
///
///
///
public SysConfigController(ISysConfigService SysConfigService, ISysUserService sysUserService) : base(sysUserService)
{
_sysConfigService = SysConfigService;
_sysUserService = sysUserService;
}
///
/// 分页查询
///
///
///
[HttpPost]
[Action("参数设置", ActionType.QUERY)]
public async Task>> GetByPage([FromBody] PageQuery pageQuery)
{
Expression> 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);
}
///
/// 根据ID查询
///
/// 主键
///
[HttpGet]
public async Task> GetByID(int id)
{
return new MessageModel()
{
msg = Resource_SysBase.OprateSuccess,
success = true,
data = await _sysConfigService.QueryByIdAsync(id)
};
}
///
/// 新增一条数据
///
/// SysConfig
///
[HttpPost]
public async Task> Add([FromBody] SysConfig request)
{
var data = new MessageModel();
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;
}
///
/// 更新一条数据
///
/// SysConfig
///
[HttpPost]
public async Task> Update([FromBody] SysConfig request)
{
var data = new MessageModel();
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;
}
///
/// 删除一条数据
///
/// 主键
///
[HttpPost]
public async Task> Del(List ids)
{
var data = new MessageModel();
data.success = await _sysConfigService.DeleteByIdAsync(ids);
if (data.success)
{
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
///
/// 根据参数键名查询参数值
///
///
///
[HttpGet]
[AllowAnonymous]
public async Task> GetConfigKey(string configKey)
{
var config = await _sysConfigService.QueryAsync(x => x.ConfigKey == configKey);
return Success(config.FirstOrDefault().ConfigValue);
}
}
}