using Furion.DependencyInjection; using Furion.DistributedIDGenerator; using Nirvana.Common; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Waste.Domain; namespace Waste.Application { public class BusinessApiService : BaseInfoService, IBusinessApiService, ITransient { private readonly ISqlSugarRepository repository; private readonly SqlSugarClient dbClient; public BusinessApiService(ISqlSugarRepository sqlSugarRepository) { repository = sqlSugarRepository; dbClient = repository.Context; } /// /// 删除 /// /// /// public async Task DeleteFormAsync(Guid keyValue) { if (!await dbClient.Queryable().AnyAsync(x => x.Id == keyValue)) { return new ResultInfo(ResultState.FAIL, "记录未找到"); } await dbClient.Deleteable().Where(x => x.Id == keyValue).ExecuteCommandAsync(); return new ResultInfo(ResultState.SUCCESS, "删除成功"); } /// /// 详情 /// /// /// public async Task DetailAsync(Guid id) { return await dbClient.Queryable().FirstAsync(x => x.Id == id); } /// /// 授权列表 /// /// /// public async Task> GetListAsync(QueryParams param) { RefAsync totalnum = 0; var temquery = dbClient.Queryable(); if (param.queryParam != null && param.queryParam.Count > 0) { List conModels = new List(); param.queryParam.ForEach(x => { if (!string.IsNullOrEmpty(x.Value)) { conModels.Add(new ConditionalModel() { FieldName = x.Name, ConditionalType = (ConditionalType)x.Type, FieldValue = x.Value.Trim() }); } }); if (conModels.Count > 0) { temquery = temquery.Where(conModels); } } string sorts = string.Format("{0} {1}", param.sort, param.order); var query = await temquery.OrderBy(sorts) .Select(x => new BusinessApiInfo { Id = x.Id, AppId = x.AppId, Status = x.Status, AppSecret = x.AppSecret, CreateTime = x.CreateTime, IPList = x.IPList, PushUrl = x.PushUrl }) .Mapper((it, cache) => { var allbus = cache.Get(list => { var ids = list.Select(x => x.Id).ToList(); return repository.Change().Context.Queryable().Where(x => ids.Contains(x.Id)).ToList(); }); var bus = allbus.FirstOrDefault(x => x.Id == it.Id); it.BusinessName = bus != null ? bus.Name : ""; }) .ToPageListAsync(param.offset, param.limit, totalnum); return new PageParms { page = param.offset, Items = query, totalnum = totalnum, limit = param.limit }; } /// /// 信息提交 /// /// /// public async Task SubmitFormAsync(W_BusinessAppApi role) { role.IPList = role.IPList.ToStr(); role.PushUrl = role.PushUrl.ToStr(); if (string.IsNullOrEmpty(role.PushUrl)) { return new ResultInfo(ResultState.FAIL, "推送地址不可为空"); } if (!role.PushUrl.ToLower().StartsWith("http")) { return new ResultInfo(ResultState.FAIL, "推送地址格式不正确"); } if (await dbClient.Queryable().AnyAsync(x => x.Id == role.Id)) { await dbClient.Updateable().SetColumns(x => new W_BusinessAppApi { PushUrl = role.PushUrl, IPList = role.IPList }).Where(x => x.Id == role.Id).ExecuteCommandAsync(); return new ResultInfo(ResultState.SUCCESS, "授权更新成功"); } else { role.AppId = $"jt_{IDGen.NextID().ToString("N")}"; string secret = Md5.md5(role.AppId, 32); role.AppSecret = Convert.ToBase64String(Encoding.UTF8.GetBytes(secret)); role.Status = (int)StatusType.Enabled; role.CreateTime = DateTime.Now; await dbClient.Insertable(role).ExecuteCommandAsync(); return new ResultInfo(ResultState.SUCCESS, "授权分配成功"); } } /// /// 检查是否在IP白名单中 /// /// /// /// public async Task IsWhiteIP(string appid, string ip) { var data = await dbClient.Queryable().FirstAsync(x => x.AppId == appid); if (data == null) { return new ResultInfo(ApiResultState.NONAPPID, "appid无效"); } if (!string.IsNullOrEmpty(data.IPList) && !data.IPList.Contains(ip)) { return new ResultInfo(ApiResultState.IPNOALLOW, "IP不合法"); } return new ResultInfo(ApiResultState.SUCCESS, "success"); } /// /// 根据商户ID获取详情 /// /// public async Task> GetPushListAsync() { var query = await dbClient.Ado.UseStoredProcedure().GetDataTableAsync("proc_getthirdpush", new { }); var list = query.ToList(); return list; } /// /// 增加推送消息 /// /// /// public async Task InsertPushInfoAsync(W_BusinessPush data) { await dbClient.Insertable(data).ExecuteCommandAsync(); } } }