SqlExecuteJob.cs 1.3 KB
using Hh.Mes.POJO.Entity;
using System;

namespace Quartz.Job.Jobs
{
    /// <summary>
    /// Sql执行Job
    /// </summary>
    public class SqlExecuteJob : JobBase
    {
        public override void ExecuteJob(IJobExecutionContext context)
        {
            var sqlJobs = _dbContext.Queryable<sys_job>().Where(x => x.status == "0" && string.IsNullOrEmpty(x.jobSql)).ToList();
            foreach (var jobItem in sqlJobs)
            {
                try
                {
                    //示例SQL
                    //"declare @deleteTime datetime;
                    //set @deleteTime=dateadd(month,-2,getdate());
                    // select   @deleteTime
                    //delete  from sys_job_log where createTime <= @deleteTime
                    //delete  from sys_interface_log where createTime <= @deleteTime
                    //delete  from sys_oper_log where createTime <= @deleteTime
                    //delete  from sys_login_log where createTime <= @deleteTime";
                    _dbContext.Ado.ExecuteCommand(jobItem.jobSql);
                }
                catch (Exception ex)
                {
                    _log.EnqueueJobLog($"{nameof(SqlExecuteJob)}", jobItem.methodName, jobItem.methodParams, "定时器", ex.Message, "");
                }
            }
        }
    }
}