using BBWYB.Common.Log; using BBWYB.Common.Models; using BBWYB.Server.Model; using BBWYB.Server.Model.Db; using BBWYB.Server.Model.Dto; using Microsoft.Extensions.DependencyInjection; using Yitter.IdGenerator; namespace BBWYB.Server.Business { public class TimeLimitTaskBusiness : BaseBusiness, IDenpendency { private List hgzTaskTypeList; private TimeLimitRules timeLimitRules; private IServiceProvider serviceProvider; private Lazy optimizationBusinessLazy; private Lazy venderBusinessLazy; private Lazy taskSchedulerManagerLazy; private OptimizationBusiness optimizationBusiness => optimizationBusinessLazy.Value; private VenderBusiness venderBusiness => venderBusinessLazy.Value; private TaskSchedulerManager taskSchedulerManager => taskSchedulerManagerLazy.Value; public TimeLimitTaskBusiness(IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator, VenderBusiness venderBusiness, TimeLimitRules timeLimitRules, IServiceProvider serviceProvider) : base(fsql, nLogManager, idGenerator) { this.serviceProvider = serviceProvider; this.optimizationBusinessLazy = new Lazy(() => serviceProvider.GetRequiredService()); this.venderBusinessLazy = new Lazy(() => serviceProvider.GetRequiredService()); this.taskSchedulerManagerLazy = new Lazy(() => serviceProvider.GetRequiredService()); hgzTaskTypeList = new List() { Enums.TimeLimitTaskType.合格证拟定任务, Enums.TimeLimitTaskType.合格证补充任务 }; this.timeLimitRules = timeLimitRules; } public void CheckTask() { //fsql.Update().Set(t => t.IsTimely, false) // .Where(t => t.CompletionTime == null) // .Where(t => t.ExpirationTime < DateTime.Now) // .ExecuteAffrows(); var timeoutTaskList = fsql.Select().Where(t => t.CompletionTime == null && t.ExpirationTime < DateTime.Now).ToList(); if (timeoutTaskList.Count() > 0) { var timeoutTaskIdList = timeoutTaskList.Select(t => t.Id).ToList(); fsql.Update(timeoutTaskIdList).Set(t => t.IsTimely, false).ExecuteAffrows(); } var optimizationTaskList = timeoutTaskList.Where(t => t.TaskType == Enums.TimeLimitTaskType.待议价任务).ToList(); if (optimizationTaskList.Count() > 0) { //待议价任务超时检查 foreach (var task in optimizationTaskList) { Task.Factory.StartNew(() => optimizationBusiness.TimeLimitTaskTimeOutCallBack(task.TaskId), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.OptimizationTaskScheduler); } } } /// /// 修复订单待核算任务 /// public void RepairOrderComputationTask() { var childSelect = fsql.Select().As("t").Where(t => t.TaskType == Enums.TimeLimitTaskType.待核算任务); var noComputationTaskOrderList = fsql.Select().Where(o => o.OrderState == Enums.OrderState.待核算 && !childSelect.Where(t => t.OrderId == o.Id).Any()) .ToList(o => new { o.Id, o.OrderSn, o.ShopId, }); var insertComputationTaskList = noComputationTaskOrderList.Select(o => new TimeLimitTask() { Id = idGenerator.NewLong(), CreateTme = DateTime.Now, OrderId = o.Id, OrderSn = o.OrderSn, ShopId = o.ShopId, TaskType = Enums.TimeLimitTaskType.待核算任务, ExpirationTime = timeLimitRules.CalculateExpirationTime(Enums.TimeLimitTaskType.待核算任务, DateTime.Now), //ExpirationTime = DateTime.Now.AddDays(1), Remark = "RepairOrderComputationTask" }).ToList(); if (insertComputationTaskList.Count() > 0) { fsql.Transaction(() => { fsql.Insert(insertComputationTaskList).ExecuteAffrows(); }); } } public TimeLimitTaskListResponse QueryTimeLimitTask(QueryTimeLimitTaskRequest request) { if (request.StartDate > request.EndDate) throw new BusinessException("开始时间不能大于结束时间"); if (request.PageSize > 100) request.PageSize = 100; var shopList = venderBusiness.GetShopList(request.ShopId, Model.Enums.Platform.拳探); request.EndDate = request.EndDate.Date.AddDays(1).AddSeconds(-1); var list = fsql.Select().WhereIf(request.ShopId != null, t => t.ShopId == request.ShopId) .Where(t => t.CreateTme >= request.StartDate && t.CreateTme <= request.EndDate) .WhereIf(request.IsCompleted != null && request.IsCompleted == 1, t => t.CompletionTime != null) .WhereIf(request.IsCompleted != null && request.IsCompleted == 0, t => t.CompletionTime == null) .WhereIf(request.IsTimeOut != null && request.IsTimeOut == 1, t => t.IsTimely == false) .WhereIf(request.IsTimeOut != null && request.IsTimeOut == 0, t => t.IsTimely == true || t.IsTimely == null) .WhereIf(request.TimeLimitTaskType != null, t => t.TaskType == request.TimeLimitTaskType) .OrderByDescending(t => t.CreateTme) .Count(out var count) .Page(request.PageIndex, request.PageSize) .ToList(); foreach (var task in list) { var shopId = task.ShopId.ToString(); task.ShopName = shopList.FirstOrDefault(s => s.ShopId == shopId)?.ShopName; if (task.CompletionTime == null) task.RemainingTime = timeLimitRules.CalculateLessTimeForWorkHour(task.ExpirationTime.Value); } return new TimeLimitTaskListResponse() { Count = count, ItemList = list }; } public IList TimeOutStatistics(QueryTimeOutRequest request) { if (request.StartDate > request.EndDate) throw new BusinessException("开始时间不能大于结束时间"); request.EndDate = request.EndDate.Date.AddDays(1).AddSeconds(-1); var shopList = venderBusiness.GetShopList(request.ShopId, Model.Enums.Platform.拳探); var list = new List(); var shopIdList = new List(); #region 采购超时率 var purchaseTaskCountGroups = fsql.Select().WhereIf(request.ShopId != null, t => t.ShopId == request.ShopId) .Where(t => t.CreateTme >= request.StartDate && t.CreateTme <= request.EndDate) .Where(t => t.TaskType == Model.Enums.TimeLimitTaskType.采购任务) .GroupBy(t => t.ShopId) .ToList(g => new { ShopId = g.Key, TaskCount = g.Count() }); var purchaseTaskTimOutCountGroups = fsql.Select().WhereIf(request.ShopId != null, t => t.ShopId == request.ShopId) .Where(t => t.CreateTme >= request.StartDate && t.CreateTme <= request.EndDate) .Where(t => t.IsTimely == false) .Where(t => t.TaskType == Model.Enums.TimeLimitTaskType.采购任务) .GroupBy(t => t.ShopId) .ToList(g => new { ShopId = g.Key, TaskCount = g.Count() }); #endregion #region 合格证补充/拟定超时量 var cerTimeOutCountGroups = fsql.Select().WhereIf(request.ShopId != null, t => t.ShopId == request.ShopId) .Where(t => t.CreateTme >= request.StartDate && t.CreateTme <= request.EndDate) .Where(t => t.IsTimely == false) .Where(t => hgzTaskTypeList.Contains(t.TaskType)) .GroupBy(t => new { t.ShopId, t.TaskType }) .ToList(g => new { ShopId = g.Key.ShopId, TaskType = g.Key.TaskType, TaskCount = g.Count() }); #endregion shopIdList.AddRange(purchaseTaskCountGroups.Select(g => g.ShopId.Value).ToList()); foreach (var shopId in shopIdList) { var sid = shopId.ToString(); var response = new TimeLimitTaskStatisticsResponse(); response.ShopId = shopId; response.ShopName = shopList.FirstOrDefault(s => s.ShopId == sid)?.ShopName; var purchaseTaskCount = purchaseTaskCountGroups.FirstOrDefault(x => x.ShopId == response.ShopId)?.TaskCount ?? 0; var purchaseTaskTimeOutCount = purchaseTaskTimOutCountGroups.FirstOrDefault(x => x.ShopId == response.ShopId)?.TaskCount ?? 0; response.PurchaseTimeOutPercent = purchaseTaskCount == 0 ? 0 : Math.Round(1.0 * purchaseTaskTimeOutCount / purchaseTaskCount * 100, 2); response.CerEditTimeOutCount = cerTimeOutCountGroups.FirstOrDefault(x => x.ShopId == response.ShopId && x.TaskType == Enums.TimeLimitTaskType.合格证补充任务)?.TaskCount ?? 0; response.CerConfigTimeOutCount = cerTimeOutCountGroups.FirstOrDefault(x => x.ShopId == response.ShopId && x.TaskType == Enums.TimeLimitTaskType.合格证拟定任务)?.TaskCount ?? 0; list.Add(response); } return list; } public void EditTimeLimitTaskRemark(EditTimeLimitTaskRemarkRequest request) { fsql.Update(request.TaskId).Set(t => t.Remark, request.Remark).ExecuteAffrows(); } public DateTime TestExpireTime(Enums.TimeLimitTaskType timeLimitTaskType, DateTime startTime) { return timeLimitRules.CalculateExpirationTime(timeLimitTaskType, startTime); } public (long, TimeSpan) TextLessTime(DateTime expireTime) { var time = timeLimitRules.CalculateLessTimeForWorkHour(expireTime); return (time, new TimeSpan(0, 0, (int)time)); } } }