using System; using System.Collections.Generic; using System.Threading.Tasks; using Znyc.Admin.Commons.Entitys; using Znyc.Admin.Commons.Helpers; using Znyc.Admin.Commons.Mapping; using Znyc.Admin.Commons.Pages; using Znyc.Admin.Commons.Services; using Znyc.Admin.Security.Dtos; using Znyc.Admin.Security.Entitys; using Znyc.Admin.Security.IRepositories; using Znyc.Admin.Security.IServices; namespace Znyc.Admin.Security.Services { public class StatisticalService : BaseService, IStatisticalService { private readonly IStatisticalRepository _statisticalRepository; private readonly IUserService _userService; private readonly ICompanyService _companyService; private readonly IVehicleService _vehicleService; public StatisticalService(IStatisticalRepository repository, IUserService userService, ICompanyService companyService, IVehicleService vehicleService ) : base(repository) { _statisticalRepository = repository; _userService = userService; _companyService = companyService; _vehicleService = vehicleService; } /// /// 异步分页查询 /// /// /// 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 }; List list = await _statisticalRepository.FindWithPagerAsync(where, pagerInfo, search.Sort, order); 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(); var statistical = await _statisticalRepository.GetStatisticalAsync(); var userAddTotal = await _userService.GetCountByWhereAsync("dayofmonth(CreatedTime) = dayofmonth(now())"); Statistical info = new Statistical() { Id = Yitter.IdGenerator.YitIdHelper.NextId(), Time = DateTime.Now.ToString("yyyy-MM-dd"), UserAddTotal = userAddTotal, UserSumTotal = await _userService.GetCountByWhereAsync(" Status=1"), CompanyAddTotal = await _companyService.GetCountByWhereAsync("dayofmonth(CreatedTime) = dayofmonth(now())"), CompanySumTotal = await _companyService.GetCountByWhereAsync("Status = 1"), VehicleAddTotal = await _vehicleService.GetCountByWhereAsync("dayofmonth(CreatedTime) = dayofmonth(now())"), VehicleSumTotal = await _vehicleService.GetCountByWhereAsync("Status = 1"), ExpireAddTotal = await _vehicleService.GetCountByWhereAsync("dayofmonth(ExpireTime) = dayofmonth(now()) and Status=1"), ExpireSumTotal = await _vehicleService.GetCountByWhereAsync("dayofmonth(ExpireTime) < dayofmonth(now()) and Status=1"), ModifiedTime = DateTime.Now, CreatedTime = DateTime.Now }; if (statistical == null) { info.UserAddTotalPercent = 0; } else { info.UserAddTotalPercent = StringHelper.GetPercent(userAddTotal, statistical.UserSumTotal); } long ln = await _statisticalRepository.InsertAsync(info).ConfigureAwait(false); result.Success = ln > 0 ? true : false; return result; } /// /// 获取最新一条数据 /// /// public async Task GetStatisticalAsync() { var statistical = await _statisticalRepository.GetStatisticalAsync(); return statistical; } } }