ABP集成Quartz

using Abp.AutoMapper;
using Abp.Modules;
using Abp.Quartz;
using Abp.Reflection.Extensions;
using Abp.Threading.BackgroundWorkers;
using EvaluationSystem.Authorization;
using EvaluationSystem.Pc;
using EvaluationSystem.TimmerManager;
using Quartz;

namespace EvaluationSystem
{
    [DependsOn(
        typeof(AbpQuartzModule),
        typeof(EvaluationSystemCoreModule),
        typeof(AbpAutoMapperModule))]
    public class EvaluationSystemApplicationModule : AbpModule
    {
      
        public override void PreInitialize()
        {
            Configuration.Authorization.Providers.Add<EvaluationSystemAuthorizationProvider>();
           
        }
        //LearningMpaAbpApplicationModule.cs
        public override void PostInitialize()
        {
            //注册后台工作者 循环算分
            var workManager = IocManager.Resolve<IBackgroundWorkerManager>();
            workManager.Add(IocManager.Resolve<MakeInactiveUsersPassiveWorker>());
            
            workManager.Start();
            var _jobManager = IocManager.Resolve<IQuartzScheduleJobManager>();
            _jobManager.ScheduleAsync<TimePcSpaceShowJob>(
                job =>
                {
                    job.WithIdentity("TimePcSpaceShowJob", "TimmerManager")
                        .WithDescription("A job to simply write logs.");
                }, 
                trigger =>
                {
                    trigger.StartNow().WithCronSchedule("0 0 1 * * ?");
                });

           

        }
         public override void Initialize()
        {
            var thisAssembly = typeof(EvaluationSystemApplicationModule).GetAssembly();

            IocManager.RegisterAssemblyByConvention(thisAssembly);

            Configuration.Modules.AbpAutoMapper().Configurators.Add(




                cfg =>
                {

                    cfg.CreateMap<Abp.Authorization.Permission, Authorization.Permissions.Dto.FlatPermissionWithLevelDto
                    >();
                    cfg.CreateMap<EvaluationSystem.ClassInfo.Dto.CreateOrUpdateClassInput, EntityDesign.ClassManage>();
                    cfg.CreateMap<EntityDesign.ClassManage, EvaluationSystem.ClassInfo.Dto.ClassOutput>();
                }
            );

        }
    }
}

任务

TimePcSpaceShowJob

using Abp.Dependency;
using Abp.Quartz;
using Quartz;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace EvaluationSystem.TimmerManager
{
    public class TimePcSpaceShowJob : JobBase, ITransientDependency
    {
        public override Task Execute(IJobExecutionContext context)
        {

            return Task.CompletedTask;
        }
    }
}

 另一种定时任务

MakeInactiveUsersPassiveWorker

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Abp.MultiTenancy;
using Abp.Runtime.Caching;
using Abp.Runtime.Session;
using Abp.Threading.BackgroundWorkers;
using Abp.Threading.Timers;
using Abp.Timing;
using Abp.UI;
using EvaluationSystem.EntityDesign;
using EvaluationSystem.MultiTenancy;
using EvaluationSystem.SchoolTerm;
using Microsoft.EntityFrameworkCore;

namespace EvaluationSystem.Pc
{

    public class MakeInactiveUsersPassiveWorker : PeriodicBackgroundWorkerBase, ISingletonDependency
    {
        private readonly IRepository<Tenant, int> _tenantRepository;
        private readonly ISchoolTermAppService _iSchoolTermAppService;
        private readonly ICacheManager _cacheManager;
        private readonly Abp.RealTime.IOnlineClientManager _iOnlineClientManager;
        private readonly IRepository<EntityDesign.ImmeEvalBaseSetting, Guid> _immeEvalBaseSettingRepository;
        private readonly IRepository<EntityDesign.StudentGetBadge, Guid> _studentGetBadgeRepository;
        private readonly IRepository<EntityDesign.Canton, Guid> _cantonRepository;
        private readonly IRepository<EntityDesign.SchoolInformation, Guid> _schoolInformationRepository;
        private readonly IRepository<EntityDesign.Student, Guid> _studentRepository;
        private readonly IRepository<EntityDesign.Teacher, Guid> _teacherRepository;
        private readonly IRepository<EntityDesign.SchoolDepart, Guid> _schoolDepartRepository;
        private readonly IRepository<EntityDesign.SpaceShow, Guid> _spaceShowRepository;
        private readonly IRepository<EntityDesign.SpaceShowType, Guid> _spaceShowTypeRepository;
        private readonly IRepository<EntityDesign.AdminUser, Guid> _adminUserRepository;
        private readonly IRepository<EntityDesign.DictionaryData, Guid> _dictionaryDataRepository;
        private readonly IRepository<EntityDesign.PersonQulitativeScore, Guid> _personQulitativeScoreRepository;
        private readonly IRepository<EntityDesign.ClassManage, Guid> _classManageRepository;
        private readonly IRepository<EvaluationSystem.EntityDesign.PersonEvaluateTask, Guid> _personEvaluateTaskRepository;
        private readonly IRepository<EvaluationSystem.EntityDesign.AppraiseLevel, Guid> _appraiseLevelRepository;
        private readonly IRepository<EvaluationSystem.EntityDesign.Appraise, Guid> _appraiseRepository;
        private readonly IRepository<EvaluationSystem.EntityDesign.QuotaClass, Guid> _quotaClassRepository;
        private readonly IRepository<EvaluationSystem.EntityDesign.EvaluateReportArea, Guid> _evaluateReportAreaRepository;
        private readonly IRepository<EvaluationSystem.EntityDesign.EvaluateReportSchool, Guid> _evaluateReportSchoolRepository;
        public MakeInactiveUsersPassiveWorker(AbpTimer timer,
            ICacheManager cacheManager,
        Abp.RealTime.IOnlineClientManager iOnlineClientManager,
            IRepository<EntityDesign.SchoolInformation, Guid> schoolInformationRepository,
        IRepository<EntityDesign.Student, Guid> studentRepository,
            ISchoolTermAppService iSchoolTermAppService,
            IRepository<EntityDesign.Teacher, Guid> teacherRepository,
        IRepository<EntityDesign.DictionaryData, Guid> dictionaryDataRepository,
            IRepository<EvaluationSystem.EntityDesign.QuotaClass, Guid> quotaClassRepository,
            IRepository<EvaluationSystem.EntityDesign.AppraiseLevel, Guid> appraiseLevelRepository,
        IRepository<EvaluationSystem.EntityDesign.Appraise, Guid> appraiseRepository,
            IRepository<EntityDesign.Canton, Guid> cantonRepository,
            IRepository<EntityDesign.ImmeEvalBaseSetting, Guid> immeEvalBaseSettingRepository,
            IRepository<EntityDesign.PersonQulitativeScore, Guid> personQulitativeScoreRepository,
        IRepository<EntityDesign.SpaceShow, Guid> spaceShowRepository,
            IRepository<EntityDesign.ClassManage, Guid> classManageRepository,
        IRepository<EntityDesign.SpaceShowType, Guid> spaceShowTypeRepository,
            IRepository<Tenant, int> tenantRepository,
            IRepository<EvaluationSystem.EntityDesign.EvaluateReportArea, Guid> evaluateReportAreaRepository,
        IRepository<EvaluationSystem.EntityDesign.EvaluateReportSchool, Guid> evaluateReportSchoolRepository,
            IRepository<EntityDesign.SchoolDepart, Guid> schoolDepartRepository,
            IRepository<EntityDesign.StudentGetBadge, Guid> studentGetBadgeRepository,
        IRepository<EvaluationSystem.EntityDesign.PersonEvaluateTask, Guid> personEvaluateTaskRepository,
            IRepository<EntityDesign.AdminUser, Guid> adminUserRepository
       )
            : base(timer)
        {

            Timer.Period = 1000 * 60 * 60 * 24; //5 seconds (good for tests, but normally will be more)

            Start();
            _cacheManager = cacheManager;
            _iOnlineClientManager = iOnlineClientManager;
            _schoolInformationRepository = schoolInformationRepository;
            _studentRepository = studentRepository;
            _evaluateReportAreaRepository = evaluateReportAreaRepository;
            _evaluateReportSchoolRepository = evaluateReportSchoolRepository;
            _teacherRepository = teacherRepository;
            _iSchoolTermAppService = iSchoolTermAppService;
            _personEvaluateTaskRepository = personEvaluateTaskRepository;
            _dictionaryDataRepository = dictionaryDataRepository;
            _immeEvalBaseSettingRepository = immeEvalBaseSettingRepository;
            _personQulitativeScoreRepository = personQulitativeScoreRepository;
            _cantonRepository = cantonRepository;
            _classManageRepository = classManageRepository;
            _spaceShowRepository = spaceShowRepository;
            _spaceShowTypeRepository = spaceShowTypeRepository;
            _adminUserRepository = adminUserRepository;
            _schoolDepartRepository = schoolDepartRepository;
            _tenantRepository = tenantRepository;
            _quotaClassRepository = quotaClassRepository;
            _appraiseRepository = appraiseRepository;
            _studentGetBadgeRepository = studentGetBadgeRepository;
            _appraiseLevelRepository = appraiseLevelRepository;
        }
        [UnitOfWork]
        protected override void DoWork()
        {

            var tenants = _tenantRepository.GetAll().Where(x => x.IsActive);
            foreach (var tenant in tenants)
            {
                using (CurrentUnitOfWork.SetTenantId(tenant.Id))
                {
                    var immeEvalBaseSetting = _immeEvalBaseSettingRepository.GetAll().FirstOrDefault();
                    if (immeEvalBaseSetting == null)
                    {
                        throw new UserFriendlyException("请先维护[即时评价基础设置]");
                    }

                    var team = _iSchoolTermAppService.GetCurrent().Result;
                    var quotaClass = _quotaClassRepository.GetAll().Where(x => x.PId == Guid.Empty).ToList();
                    //所有学校
                    var schools = _schoolInformationRepository.GetAll();

                    //所有区域分
                    var cantons = _cantonRepository.GetAll();
                    //先清空
                    _evaluateReportAreaRepository.Delete(x => x.TermId == team.Id);
                    _evaluateReportSchoolRepository.Delete(x => x.TermId == team.Id);

                    foreach (var canton in cantons)
                    {

                        var schoolsTemp = schools.Where(x => x.CanTonId == canton.Id);

                        var query2 = from p in _personQulitativeScoreRepository.GetAll()
                                     join c in _classManageRepository.GetAll() on p.ClassId equals c.Id
                                     join schoolInformation in schoolsTemp on c.SchoolId equals schoolInformation.Id
                                     join task in _personEvaluateTaskRepository.GetAll() on p.EvaluationTaskId equals task.Id
                                     select new { p, c, schoolInformation, task };
                        foreach (var v in quotaClass)
                        {
                            decimal lastScore = 0;
                            lastScore = lastScore + immeEvalBaseSetting.StudentBaseScore.GetValueOrDefault();
                            int score_1 = 0;
                            int count_1 = 0;
                            int score_2 = 0;
                            int count_2 = 0;
                            var listTemp = query2.Where(x => x.p.FirstLevelQuotaId == v.Id).ToList();
                            foreach (var item2 in listTemp)
                            {
                                //定量
                                if (item2.p.TaskType == 1)
                                {
                                    score_1 = score_1 + item2.p.Score;
                                    count_1++;
                                }//定性
                                else if (item2.p.TaskType == 2)
                                {
                                    if (item2.p.Level == 1)
                                    {
                                        score_2 = score_2 + item2.task.ExcellentValue;
                                    }
                                    else if (item2.p.Level == 2)
                                    {
                                        score_2 = score_2 + item2.task.GoodValue;
                                    }
                                    else if (item2.p.Level == 3)
                                    {
                                        score_2 = score_2 + item2.task.StandardValue;
                                    }
                                    else if (item2.p.Level == 4)
                                    {
                                        score_2 = score_2 + item2.task.NeedHelpValue;
                                    }
                                    count_2++;
                                }

                            }
                            if (count_1 > 0)
                            {
                                lastScore = lastScore + (score_1 / count_1) * immeEvalBaseSetting.QuantifyWeight.GetValueOrDefault();
                            }

                            if (count_2 > 0)
                            {
                                lastScore = lastScore + (score_2 / count_2);
                            }
                            var studentGetBadge = _studentGetBadgeRepository.GetAll().Count(x =>
                                 x.TermId == team.Id && x.FirstLevelQuatoId == v.Id);
                            var studentGetBadgeScore = Convert.ToInt32(studentGetBadge * immeEvalBaseSetting.BadgeScore);
                            //如果大于每类徽章最高得分,则直接取最高得分
                            if (studentGetBadgeScore > immeEvalBaseSetting.HighestScore)
                            {
                                studentGetBadgeScore = (int)immeEvalBaseSetting.HighestScore;
                            }
                            lastScore = lastScore + studentGetBadgeScore;
                            _evaluateReportAreaRepository.Insert(new EvaluateReportArea()
                            {
                                AreaId = canton.Id,
                                TermId = team.Id,
                                FirstLevelQuotaId = v.Id,
                                Score = lastScore
                            });
                        }
                    }
                
                    //所有学校分
                    foreach (var school in schools)
                    {
                        var schoolsTemp = schools.Where(x => x.Id == school.Id);

                        var query2 = from p in _personQulitativeScoreRepository.GetAll()
                                     join c in _classManageRepository.GetAll() on p.ClassId equals c.Id
                                     join schoolInformation in schoolsTemp on c.SchoolId equals schoolInformation.Id
                                     join task in _personEvaluateTaskRepository.GetAll() on p.EvaluationTaskId equals task.Id
                                     select new { p, c, schoolInformation, task };
                        foreach (var v in quotaClass)
                        {
                            decimal lastScore = 0;
                            lastScore = lastScore + immeEvalBaseSetting.StudentBaseScore.GetValueOrDefault();
                            int score_1 = 0;
                            int count_1 = 0;
                            int score_2 = 0;
                            int count_2 = 0;
                            var listTemp = query2.Where(x => x.p.FirstLevelQuotaId == v.Id).ToList();
                            foreach (var item2 in listTemp)
                            {
                                //定量
                                if (item2.p.TaskType == 1)
                                {
                                    score_1 = score_1 + item2.p.Score;
                                    count_1++;
                                }//定性
                                else if (item2.p.TaskType == 2)
                                {
                                    if (item2.p.Level == 1)
                                    {
                                        score_2 = score_2 + item2.task.ExcellentValue;
                                    }
                                    else if (item2.p.Level == 2)
                                    {
                                        score_2 = score_2 + item2.task.GoodValue;
                                    }
                                    else if (item2.p.Level == 3)
                                    {
                                        score_2 = score_2 + item2.task.StandardValue;
                                    }
                                    else if (item2.p.Level == 4)
                                    {
                                        score_2 = score_2 + item2.task.NeedHelpValue;
                                    }
                                    count_2++;
                                }

                            }
                            if (count_1 > 0)
                            {
                                lastScore = lastScore + (score_1 / count_1) * immeEvalBaseSetting.QuantifyWeight.GetValueOrDefault();
                            }

                            if (count_2 > 0)
                            {
                                lastScore = lastScore + (score_2 / count_2);
                            }
                            var studentGetBadge = _studentGetBadgeRepository.GetAll().Count(x =>
                                 x.TermId == team.Id && x.FirstLevelQuatoId == v.Id);
                            var studentGetBadgeScore = Convert.ToInt32(studentGetBadge * immeEvalBaseSetting.BadgeScore);
                            //如果大于每类徽章最高得分,则直接取最高得分
                            if (studentGetBadgeScore > immeEvalBaseSetting.HighestScore)
                            {
                                studentGetBadgeScore = (int)immeEvalBaseSetting.HighestScore;
                            }
                            lastScore = lastScore + studentGetBadgeScore;
                            _evaluateReportSchoolRepository.Insert(new EvaluateReportSchool()
                            {
                                SchoolId = school.Id,
                                TermId = team.Id,
                                FirstLevelQuotaId = v.Id,
                                Score = lastScore
                            });
                        }
                    }
                    CurrentUnitOfWork.SaveChanges();
                }
            }

        }
    }
}
原文地址:https://www.cnblogs.com/wangyinlon/p/13953414.html