using System; using System.Threading.Tasks; using Yitter.IdGenerator; using Znyc.Cloudcar.Admin.Commons.Entitys; using Znyc.Cloudcar.Admin.Commons.Helpers; using Znyc.Cloudcar.Admin.Commons.Mapping; using Znyc.Cloudcar.Admin.Commons.Pages; using Znyc.Cloudcar.Admin.Commons.Services; using Znyc.Cloudcar.Admin.Security.Dtos; using Znyc.Cloudcar.Admin.Security.Entitys; using Znyc.Cloudcar.Admin.Security.IRepositories; using Znyc.Cloudcar.Admin.Security.IServices; namespace Znyc.Cloudcar.Admin.Security.Services { public class StatisticalService : BaseService, IStatisticalService { private readonly IApplyJobService _applyJobService; private readonly ILoginLogsRepository _loginLogsRepository; private readonly IOrderService _orderService; private readonly ICloudcarService _CloudcarService; private readonly IStatisticalRepository _statisticalRepository; private readonly IUserService _userService; public StatisticalService(IStatisticalRepository repository, IUserService userService, ILoginLogsRepository loginLogsRepository, IOrderService orderService, IApplyJobService applyJobService, ICloudcarService CloudcarService ) : base(repository) { _statisticalRepository = repository; _userService = userService; _loginLogsRepository = loginLogsRepository; _orderService = orderService; _applyJobService = applyJobService; _CloudcarService = CloudcarService; } /// /// 异步分页查询 /// /// /// public async Task> FindWithPagerSearchAsync(SearchUserModel search) { bool order = search.Order == "asc" ? false : true; string where = GetDataPrivilege(false); if (!string.IsNullOrEmpty(search.StartTime)) { @where += " and CreatedTime >='" + search.StartTime + " 00:00:00'"; } if (!string.IsNullOrEmpty(search.EndTime)) { @where += " and CreatedTime <='" + search.EndTime + " 23:59:59'"; } PagerInfo pagerInfo = new PagerInfo { CurrenetPageIndex = search.CurrenetPageIndex, PageSize = search.PageSize }; System.Collections.Generic.List list = await _statisticalRepository.FindWithPagerAsync(where, pagerInfo, search.Sort, order); System.Collections.Generic.List listDto = list.MapTo(); PageResult pageResult = new PageResult { CurrentPage = pagerInfo.CurrenetPageIndex, Items = listDto, ItemsPerPage = pagerInfo.PageSize, TotalItems = pagerInfo.RecordCount }; return pageResult; } /// /// 异步新增数据 /// /// public async Task InsertAsync() { CommonResult result = new CommonResult(); StatisticalEntity statistical = await GetStatisticalAsync(); int userAddTotal = await _userService.GetCountByWhereAsync("date_format(CreatedTime,'%Y-%m-%d') = date_format(NOW(),'%Y-%m-%d')"); dynamic todayIncome = await _orderService.GetSumValueByFieldAsync("PaymentMoney", "OrderStatus >= 20 and date_format(CreatedTime,'%Y-%m-%d') = date_format(NOW(),'%Y-%m-%d')"); dynamic income = await _orderService.GetSumValueByFieldAsync("PaymentMoney", "OrderStatus >= 20"); StatisticalEntity info = new StatisticalEntity { Id = YitIdHelper.NextId(), Time = DateTime.Now.ToString("yyyy-MM-dd"), UserAddTotal = userAddTotal, UserSumTotal = await _userService.GetCountByWhereAsync("IsDeleted = 0"), ApplyJobsAddTotal = await _applyJobService.GetCountByWhereAsync("date_format(CreatedTime,'%Y-%m-%d') = date_format(NOW(),'%Y-%m-%d')"), CloudcarAddTotal = await _CloudcarService.GetCountByWhereAsync("date_format(CreatedTime,'%Y-%m-%d') = date_format(NOW(),'%Y-%m-%d')"), ActiveUserTotal = await _loginLogsRepository.GetActiveUserTotal(), TodayIncome = todayIncome.sumVaule, Income = income.sumVaule, ModifiedTime = DateTime.Now, CreatedTime = DateTime.Now }; if (statistical == null) { info.UserAddTotalPercent = 0; info.TodayIncomePercent = 0; } else { //UserAddTotalPercent = statistical.UserSumTotal!=0 ? (userAddTotal / statistical.UserSumTotal) * 100 : 0, info.UserAddTotalPercent = StringHelper.GetPercent(userAddTotal, statistical.UserSumTotal); //TodayIncomePercent = statistical.Income!=0 ? (todayIncome.sumVaule / statistical.Income) * 100 :0, info.TodayIncomePercent = StringHelper.GetPercent(todayIncome.sumVaule, statistical.Income); } long ln = await _statisticalRepository.InsertAsync(info).ConfigureAwait(false); result.Success = ln > 0 ? true : false; return result; } /// /// 获取最新一条数据 /// /// public async Task GetStatisticalAsync() { StatisticalEntity statistical = await _statisticalRepository.GetStatisticalAsync(); return statistical; } } }