using JNPF.Common.Core.Manager; using JNPF.Common.Enums; using JNPF.Common.Extension; using JNPF.Common.Filter; using JNPF.DependencyInjection; using JNPF.DynamicApiController; using JNPF.FriendlyException; using JNPF.LinqBuilder; using JNPF.Systems.Entitys.Dto.DataInterFace; using JNPF.Systems.Entitys.Dto.DataInterfaceLog; using JNPF.Systems.Entitys.Dto.System.DataInterfaceLog; using JNPF.Systems.Entitys.Dto.System.InterfaceOauth; using JNPF.Systems.Entitys.Permission; using JNPF.Systems.Entitys.System; using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; namespace JNPF.Systems.System { /// /// 接口认证 /// 版 本:V3.2 /// 版 权:拓通智联科技有限公司(http://www.tuotong-tech.com) /// 日 期:2021-06-01. /// [ApiDescriptionSettings(Tag = "System", Name = "InterfaceOauth", Order = 202)] [Route("api/system/[controller]")] public class InterfaceOauthService : IDynamicApiController, ITransient { /// /// 服务基本仓储. /// private readonly ISqlSugarRepository _repository; /// /// 用户管理. /// private readonly IUserManager _userManager; /// /// 初始化一个类型的新实例. /// public InterfaceOauthService( ISqlSugarRepository repository, IUserManager userManager) { _repository = repository; _userManager = userManager; } #region Get /// /// 信息. /// /// 请求参数. /// [HttpGet("{id}")] public async Task GetInfo(string id) { var info = await _repository.GetFirstAsync(x => x.Id == id && x.DeleteMark == null); var output = info.Adapt(); if (info.IsNotEmptyOrNull() && info.DataInterfaceIds.IsNotEmptyOrNull()) { var ids = info.DataInterfaceIds.Split(","); output.list = await _repository.AsSugarClient().Queryable() .Where(a => ids.Contains(a.Id)) .Select(a => new DataInterfaceListOutput { id = a.Id, fullName = a.FullName, enCode = a.EnCode, path = a.Path, requestParameters = a.RequestParameters, dataType = a.DataType, requestMethod = SqlFunc.IF(a.RequestMethod.Equals("1")).Return("新增").ElseIF(a.RequestMethod.Equals("2")).Return("修改") .ElseIF(a.RequestMethod.Equals("3")).Return("查询").ElseIF(a.RequestMethod.Equals("4")).Return("删除") .ElseIF(a.RequestMethod.Equals("5")).Return("存储过程").ElseIF(a.RequestMethod.Equals("6")).Return("Get") .End("Post") }).ToListAsync(); } return output; } /// /// 列表. /// [HttpGet("")] public async Task GetList([FromQuery] PageInputBase input) { var list = await _repository.AsSugarClient().Queryable((a, b) => new JoinQueryInfos(JoinType.Left, b.Id == a.CreatorUserId)) .Where(a => a.DeleteMark == null) .WhereIF(!string.IsNullOrEmpty(input.keyword), a => a.AppId.Contains(input.keyword) || a.AppName.Contains(input.keyword)) .OrderBy(a => a.SortCode).OrderBy(a => a.CreatorTime, OrderByType.Desc) .Select((a, b) => new InterfaceOauthListOutput { id = a.Id, lastModifyTime = a.LastModifyTime, enabledMark = a.EnabledMark, creatorUser = SqlFunc.MergeString(b.RealName, "/", b.Account), appId = a.AppId, appName = a.AppName, usefulLife = a.UsefulLife, sortCode = a.SortCode, creatorTime = a.CreatorTime }).ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(list); } /// /// 获取秘钥. /// /// [HttpGet("getAppSecret")] public async Task GetAppSecret() { return Guid.NewGuid().ToString().Replace("-", string.Empty); } /// /// 日志. /// /// /// /// [HttpGet("dataInterfaceLog/{id}")] public async Task GetList(string id, [FromQuery] DataInterfaceLogListQuery input) { var entity = await _repository.GetFirstAsync(x => x.Id == id && x.DeleteMark == null); var whereLambda = LinqExpression.And(); if (!input.startTime.IsNullOrEmpty() && !input.endTime.IsNullOrEmpty()) { var startTime = Convert.ToDateTime(string.Format("{0:yyyy-MM-dd 00:00:00}", input.startTime?.TimeStampToDateTime())); var endTime = Convert.ToDateTime(string.Format("{0:yyyy-MM-dd 23:59:59}", input.endTime?.TimeStampToDateTime())); whereLambda = whereLambda.And(a => SqlFunc.Between(a.invokTime, startTime, endTime)); } var list = await _repository.AsSugarClient().Queryable((a, b, c) => new JoinQueryInfos(JoinType.Left, b.Id == a.UserId, JoinType.Left, a.InvokId == c.Id)) .Where(a => a.OauthAppId == entity.AppId) .WhereIF(input.keyword.IsNotEmptyOrNull(), a => a.UserId.Contains(input.keyword) || a.InvokIp.Contains(input.keyword)) .Select((a, b, c) => new DataInterfaceLogListOutput { id = a.Id, fullName = c.FullName, enCode = c.EnCode, invokDevice = a.InvokDevice, invokIp = a.InvokIp, userId = SqlFunc.MergeString(b.RealName, "/", b.Account), invokTime = a.InvokTime, invokType = a.InvokType, invokWasteTime = a.InvokWasteTime }).MergeTable().Where(whereLambda).OrderBy(a => a.invokTime).ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(list); } #endregion #region Post /// /// 新增. /// /// 请求参数. /// [HttpPost("")] public async Task Create_Api([FromBody] InterfaceOauthInput input) { if (await _repository.IsAnyAsync(x => (x.AppId == input.appId || x.AppName == input.appName) && x.DeleteMark == null)) throw Oops.Oh(ErrorCode.D3001); var entity = input.Adapt(); if (input.usefulLife.IsNullOrEmpty() || input.usefulLife == "0") { entity.UsefulLife = null; } var isOk = await _repository.AsInsertable(entity).IgnoreColumns(ignoreNullColumn: true).CallEntityMethod(m => m.Creator()).ExecuteCommandAsync(); if (isOk < 1) throw Oops.Oh(ErrorCode.COM1000); } /// /// 删除. /// /// 请求参数. /// [HttpDelete("{id}")] public async Task Delete(string id) { var entity = await _repository.GetFirstAsync(x => x.Id == id && x.DeleteMark == null); if (entity == null) throw Oops.Oh(ErrorCode.COM1005); var isOk = await _repository.AsUpdateable(entity).CallEntityMethod(m => m.Delete()).UpdateColumns(it => new { it.DeleteMark, it.DeleteTime, it.DeleteUserId }).ExecuteCommandHasChangeAsync(); if (!isOk) throw Oops.Oh(ErrorCode.COM1002); } /// /// 修改. /// /// id. /// 请求参数. /// [HttpPut("{id}")] public async Task Update(string id, [FromBody] InterfaceOauthInput input) { if (await _repository.IsAnyAsync(x => x.Id != id && (x.AppId == input.appId || x.AppName == input.appName) && x.DeleteMark == null)) throw Oops.Oh(ErrorCode.COM1004); var entity = input.Adapt(); var isOk = await _repository.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).CallEntityMethod(m => m.LastModify()).ExecuteCommandHasChangeAsync(); if (input.usefulLife.IsNullOrEmpty() || input.usefulLife == "0") { await _repository.AsUpdateable().SetColumns(it => new InterfaceOauthEntity() { UsefulLife = null }).Where(it => it.Id.Equals(id)).ExecuteCommandHasChangeAsync(); } if (!isOk) throw Oops.Oh(ErrorCode.COM1001); } /// /// 授权接口. /// /// 请求参数. /// [HttpPost("saveInterfaceList")] public async Task SaveInterFaceList([FromBody] InterfaceOauthSaveInput input) { var isOk = await _repository.AsSugarClient().Updateable() .SetColumns(it => it.DataInterfaceIds == input.dataInterfaceIds).Where(x => x.Id == input.interfaceIdentId).ExecuteCommandHasChangeAsync(); if (!isOk) throw Oops.Oh(ErrorCode.COM1002); } #endregion } }