using Furion; using Furion.DataEncryption; using Furion.DependencyInjection; using Furion.DistributedIDGenerator; using Nirvana.Common; using Nirvana.Common.Extend; using SqlSugar; using System; using System.Collections.Generic; using System.Threading.Tasks; using YBDevice.Entity; namespace YBDevice.Application.CommonInfo { public class CommonService : ICommonService, ITransient { private readonly ISqlSugarRepository repository; private readonly SqlSugarClient dbClient; public CommonService(ISqlSugarRepository sqlSugarRepository) { repository = sqlSugarRepository; dbClient = repository.Context; } /// /// 插入或者更新汇总表 /// /// public async Task InsertOrUpdateCombinedAsync() { int devcnt = await dbClient.Queryable().CountAsync(); int businesscnt = await dbClient.Queryable().CountAsync(); int todaydevactinvecnt = await dbClient.Queryable().Where(x => SqlFunc.DateIsSame(x.LastHeartTime, DateTime.Now)).CountAsync(); if (await dbClient.Queryable().AnyAsync(x => x.Id == 1)) { await dbClient.Updateable().SetColumns(x => new YB_Combined { TotalDevCnt = devcnt, BusinessCnt = businesscnt, TodayDevCnt = todaydevactinvecnt }).Where(x => x.Id == 1).ExecuteCommandAsync(); } else { await dbClient.Insertable(new YB_Combined { TodayDevCnt = todaydevactinvecnt, BusinessCnt = businesscnt, TodayIncome = 0, TodayRegCnt = 0, TodayResultCnt = 0, TotalDevCnt = devcnt, TotalIncome = 0, TotalRegCnt = 0, TotalResultCnt = 0 }).ExecuteCommandAsync(); } } /// /// 添加分配记录 /// /// /// public async Task InsertAllocAsync(YB_DeviceAlloc data) { data.CreateTime = DateTime.Now; data.Id = IDGen.NextID(); await dbClient.Insertable(data).ExecuteCommandAsync(); } /// /// 添加分配记录 /// /// 设备列表 /// 分配人 /// 接收人 /// 类型,1-出货,2-分配,3-回收,4-激活 /// public async Task InsertAllocAsync(List equids, int frombusinessid, int tobusinessid, DeviceAllocType type) { List list = new List(); equids.ForEach(x => { list.Add(new YB_DeviceAlloc { Id = IDGen.NextID(), CreateTime = DateTime.Now, EquId = x, FromBusinessId = frombusinessid, ToBusinessId = tobusinessid, Type = type }); }); await dbClient.Insertable(list).ExecuteCommandAsync(); } /// /// 更新或者插入指定商户实时数据 /// /// /// public async Task InsertOrUpdateRealDataAsync(int BusinessId) { if (BusinessId > 0) { int todaydevactivecnt = await dbClient.Queryable().Where(x => x.BusinessId == BusinessId && SqlFunc.DateIsSame(x.LastHeartTime, DateTime.Now)).CountAsync(); int devcnt = await dbClient.Queryable().Where(x => x.BusinessId == BusinessId).CountAsync(); int businesscnt = await dbClient.Queryable().Where(x => x.ParentId == BusinessId).CountAsync(); if (!await dbClient.Queryable().AnyAsync(x => x.BusinessId == BusinessId)) { await dbClient.Insertable(new YB_BusinessRealData { BusinessId = BusinessId, Balance = 0, BusinessCount = businesscnt, CreateTime = DateTime.Now, DevCount = devcnt, TodayIncome = 0, TodayResultCnt = 0, TotalIncome = 0, TotalResultCnt = 0, TotalTxAmount = 0, TodayDevCount = todaydevactivecnt, TotalRealCnt = 0, TodayRealCnt = 0 }).ExecuteCommandAsync(); } else { await dbClient.Updateable().SetColumns(x => new YB_BusinessRealData { DevCount = devcnt, TodayDevCount = todaydevactivecnt, BusinessCount = businesscnt }).Where(x => x.BusinessId == BusinessId).ExecuteCommandAsync(); } } } /// /// 更新或者插入指定商户实时数据 /// /// /// public async Task InsertOrUpdateRealDataAsync(List BusinessId) { if (BusinessId != null && BusinessId.Count > 0) { BusinessId = BusinessId.IDistinctList(); foreach (var item in BusinessId) { await InsertOrUpdateRealDataAsync(item); } } } /// /// 生成token /// /// /// public LoginInfoDto AccessToken(OperatorModel account) { string accessToken; accessToken = JWTEncryption.Encrypt(new Dictionary() { {"BusinessId",account.BusinessId}, {"BusinessCode",account.BusinessCode}, {"UserId",account.UserId}, {"RealName",account.RealName}, {"RoleId",account.RoleId}, {"LoginIPAddress",account.LoginIPAddress}, {"LoginTime",account.LoginTime}, {"AccountType",account.AccountType}, {"IsSuper",account.IsSuper}, {"Type",account.Type}, }); //获取刷新token var refreshToken = JWTEncryption.GenerateRefreshToken(accessToken, 43200); // 第二个参数是刷新 token 的有效期(分钟),默认三十天 return new LoginInfoDto { RefreshToken = refreshToken, Token = accessToken }; } } }