# CbCaseSchedulerJobServiceImpl.java

    package cloud.wconnect.finance.service.chargeback.impl;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.*;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.pojo.dto.scheduler.JobBodyDTO;
    import cloud.wconnect.commonlib.pojo.dto.scheduler.JobParameterDTO;
    import cloud.wconnect.commonlib.service.SchedulerInternalService;
    import cloud.wconnect.commonlib.util.*;
    import cloud.wconnect.finance.entity.chargeback.CbCaseSchedulerJob;
    import cloud.wconnect.finance.mapper.chargeback.CbCaseSchedulerJobMapper;
    import cloud.wconnect.finance.pojo.dto.chargeback.CbCaseSchedulerDTO;
    import cloud.wconnect.finance.service.chargeback.ICbCaseSchedulerJobService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.HashMap;
    import java.util.List;
    import java.util.Objects;
    import java.util.concurrent.Executor;

    /**
     * <p>
     * 服务实现类
     * </p>
     *
     * @author yyt
     * @since 2024-03-16
     */
    @Service
    @DS("finance")
    public class CbCaseSchedulerJobServiceImpl extends ServiceImpl<CbCaseSchedulerJobMapper, CbCaseSchedulerJob> implements ICbCaseSchedulerJobService {

        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private UpdateLogUtil updateLogUtil;
        @Autowired
        private Executor executor;
        @Value("${default.timezone}")
        private String defaultTimezone;
        @Autowired
        private ObjectMapper objectMapper;
        @Autowired
        private SchedulerInternalService schedulerInternalService;

        private static final String FINANCE = "finance";

        /**
         * 保存定时任务
         *
         * @param propertyDTO  资产
         * @param schedulerDTO 定时任务
         * @param userId       操作人
         * @return 定时任务 id
         * @author yyt
         * @date 2024/3/13
         **/
        @Override
        @Transactional(rollbackFor = Exception.class)
        public String save(PropertyDTO propertyDTO, CbCaseSchedulerDTO schedulerDTO, Integer userId) {
            Integer propertyId = propertyDTO.getId();
            CbCaseSchedulerJob scheduler = beanMapper.map(schedulerDTO, CbCaseSchedulerJob.class);
            scheduler.setPropertyId(propertyId).setRunTimes(0).setSyncState(schedulerDTO.getStatus() == BaseConstant.ZERO)
                    .setUpdateLog(updateLogUtil.get(userId));
            save(scheduler);
            String schedulerId = scheduler.getId();

            // 发送定时消息
            if (!scheduler.getSyncState()) {
                TransactionUtils.afterCommitAsyncExecute(executor, () -> {
                    // 同步任务调度中心
                    try {
                        //调用内部接口保存任务
                        callInternalServiceLaunchJob(StringUtils.EMPTY, scheduler, propertyDTO, defaultTimezone);
                        //指定数据源
                        DynamicDataSourceContextHolder.push(FINANCE);
                        //更新为已同步
                        updateSyncState(schedulerId, true);
                    } catch (Exception e) {
                        log.error("Launch chargeback scheduler job[" + schedulerId + "] failed", e);
                    } finally {
                        DynamicDataSourceContextHolder.clear();
                    }
                });
            }

            return schedulerId;
        }

        /**
         * 调用内部接口保存任务
         *
         * @param originJobId     原调度任务 id
         * @param scheduler       调度任务对象
         * @param propertyDTO      资产
         * @param defaultTimezone 默认时区
         * @author yyt
         * @date 2024/3/16
         */
        private void callInternalServiceLaunchJob(String originJobId, CbCaseSchedulerJob scheduler, PropertyDTO propertyDTO, String defaultTimezone) throws JsonProcessingException {
            JobBodyDTO task = new JobBodyDTO()
                    .setJobId(scheduler.getId()).setOriginJobId(originJobId)
                    .setModuleId(MoudleIdConstant.FINANCE).setFunctionId(FunctionIdConstant.CHARGEBACK)
                    .setStartDate(scheduler.getStartDate())
                    .setCronRule(CronRuleUtil.generateCronRuleForScheduler(objectMapper.readValue(scheduler.getScheduler(), HashMap.class),
                            scheduler.getStartDate(), propertyDTO.getTimezone(), defaultTimezone));
            schedulerInternalService.launchJob(task);
        }

        /**
         * 修改同步状态
         *
         * @param scheduleId 定时任务 id
         * @param syncState  同步状态
         * @author yyt
         * @date 2024/3/16
         **/
        @Override
        public void updateSyncState(String scheduleId, boolean syncState) {
            updateById(new CbCaseSchedulerJob().setId(scheduleId).setSyncState(syncState));
        }

        /**
         * 检查存在
         *
         * @param id 定时任务 id
         * @return 定时任务
         * @author yyt
         * @date 2024/3/16
         **/
        @Override
        public CbCaseSchedulerJob checkExist(String id) {
            CbCaseSchedulerJob scheduler = getById(id);
            if (Objects.isNull(scheduler)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the cbCaseSchedulerJob is not found;id:" + id);
            }
            return scheduler;
        }

        /**
         * 修改定时任务
         *
         * @param propertyDTO  资产
         * @param scheduler    定时任务
         * @param schedulerDTO 定时任务修改内容
         * @param userId       操作人
         * @return 定时任务 id
         * @author yyt
         * @date 2024/3/13
         **/
        @Override
        @Transactional(rollbackFor = Exception.class)
        public String update(PropertyDTO propertyDTO, CbCaseSchedulerJob scheduler, CbCaseSchedulerDTO schedulerDTO, Integer userId) {
            CbCaseSchedulerJob updateScheduler = beanMapper.map(schedulerDTO, CbCaseSchedulerJob.class);
            updateScheduler.setId(scheduler.getId());
            updateScheduler.setRunTimes(BaseConstant.ZERO);
            updateScheduler.setUpdateLog(updateLogUtil.append(scheduler.getUpdateLog(), userId));
            // 设为未同步
            updateScheduler.setSyncState(false);

            String originJobId = scheduler.getId();
            // 更新调度任务的 job id
            updateAndGenerateNewJobId(updateScheduler);

            // 同步任务调度中心
            TransactionUtils.afterCommitAsyncExecute(executor, () -> {
                if (updateScheduler.getStatus() == SchedulerCons.STATUS_START) {
                    try {
                        //调用内部接口保存任务
                        callInternalServiceLaunchJob(originJobId, updateScheduler, propertyDTO, defaultTimezone);
                        //指定数据源
                        DynamicDataSourceContextHolder.push(FINANCE);
                        //更新为已同步
                        updateSyncState(updateScheduler.getId(), true);
                    } catch (Exception e) {
                        log.error("Launch chargeback scheduler job[" + updateScheduler.getId() + "] failed", e);
                    } finally {
                        DynamicDataSourceContextHolder.clear();
                    }
                } else {
                    try {
                        //调用内部接口删除任务
                        callInternalServiceDeleteJob(originJobId);
                        //指定数据源
                        DynamicDataSourceContextHolder.push(FINANCE);
                        //更新为已同步
                        updateSyncState(updateScheduler.getId(), true);
                    } catch (Exception e) {
                        log.error("Delete chargeback scheduler job[" + originJobId + "] failed", e);
                    } finally {
                        DynamicDataSourceContextHolder.clear();
                    }
                }
            });
            return updateScheduler.getId();
        }

        /**
         * 更新调度任务和生成新的 job id
         *
         * @param schedulerJob 调度任务
         * @author zc
         * @date 2024/5/27
         */
        private void updateAndGenerateNewJobId(CbCaseSchedulerJob schedulerJob) {
            String newJobId = CommonUtils.randomUuidWithoutDashes();
            update(schedulerJob, new LambdaUpdateWrapper<CbCaseSchedulerJob>()
                    .set(CbCaseSchedulerJob::getId, newJobId)
                    .eq(CbCaseSchedulerJob::getId, schedulerJob.getId()));
            schedulerJob.setId(newJobId);
        }

        /**
         * 执行内部接口更新（删除）任务
         *
         * @param schedulerId 定时任务 id
         * @author yyt
         * @date 2024/3/16
         */
        private void callInternalServiceDeleteJob(String schedulerId) {
            schedulerInternalService.deleteJob(new JobBodyDTO().setJobId(schedulerId)
                    .setModuleId(MoudleIdConstant.FINANCE).setFunctionId(FunctionIdConstant.CHARGEBACK));
        }

        /**
         * 批量刷新同步状态为已同步
         *
         * @param updateJobIds 定时任务 id
         * @author yyt
         * @date 2024/3/16
         **/
        @Override
        public void refreshSyncStateByIds(List<String> updateJobIds) {
            update(new CbCaseSchedulerJob().setSyncState(true), new QueryWrapper<CbCaseSchedulerJob>()
                    .lambda().in(CbCaseSchedulerJob::getId, updateJobIds));
        }

        /**
         * 查询指定资产、指定分类下的定时任务配置
         *
         * @param propertyId 资产 id
         * @param category   分类
         * @return 定时任务配置
         * @author yyt
         * @date 2024/3/16
         **/
        @Override
        public CbCaseSchedulerJob getByPropertyIdAndCategory(Integer propertyId, int category) {
            return getOne(new QueryWrapper<CbCaseSchedulerJob>().lambda().eq(CbCaseSchedulerJob::getPropertyId, propertyId)
                    .eq(CbCaseSchedulerJob::getCategory, category));
        }

        /**
         * 调度任务执行次数递增
         *
         * @param jobId 定时任务 id
         * @author yyt
         * @date 2024/3/12
         */
        @Override
        public void plusRunTimes(String jobId) {
            baseMapper.plusRunTimes(jobId);
        }

        /**
         * 获取定时任务配置的数据区间范围
         *
         * @param propertyDTO 资产
         * @param category    分类
         * @return 数据区间范围
         * @author yyt
         * @date 2024/3/16
         **/
        @Override
        public Long getDateRange(PropertyDTO propertyDTO, int category) {
            CbCaseSchedulerJob schedulerJob = getByPropertyIdAndCategory(propertyDTO.getId(), category);
            String parameters = schedulerJob.getParameters();
            JobParameterDTO jobParameterDTO = JsonUtils.convertStringToObject(objectMapper, parameters, JobParameterDTO.class);
            return JobParameterUtils.parseDateRange(jobParameterDTO.getDateRange(), jobParameterDTO.getDateRangeUnit(), propertyDTO.getTimezone(), null)
                    .get(JobParameterDTO.START_DATE);
        }

        /**
         * 删除定时任务
         *
         * @param scheduler  定时任务
         * @author yyt
         * @date 2024/3/16
         **/
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void delete(CbCaseSchedulerJob scheduler) {
            String schedulerId = scheduler.getId();
            updateById(new CbCaseSchedulerJob().setId(schedulerId).setStatus(SchedulerCons.STATUS_DELETING)
                    .setSyncState(false));

            // 同步任务调度中心
            TransactionUtils.afterCommitAsyncExecute(executor, () -> {
                try {
                    // 调用内部接口删除任务
                    callInternalServiceDeleteJob(schedulerId);
                    // 指定数据源
                    DynamicDataSourceContextHolder.push(FINANCE);
                    // 删除记录
                    removeById(schedulerId);
                } catch (Exception e) {
                    log.error("Delete chargeback scheduler job[" + schedulerId + "] failed", e);
                } finally {
                    DynamicDataSourceContextHolder.clear();
                }
            });
        }

        /**
         * 查询非删除中的定时任务
         *
         * @param propertyId 资产 id
         * @param category   分类（1：journal 报告缺失，2：超期通知）
         * @param id         排除自身
         * @return 定时任务
         * @author yyt
         * @date 2024/12/4
         **/
        @Override
        public List<CbCaseSchedulerJob> list(Integer propertyId, int category, String id) {
            return list(new QueryWrapper<CbCaseSchedulerJob>().lambda()
                    .eq(CbCaseSchedulerJob::getPropertyId, propertyId)
                    .eq(CbCaseSchedulerJob::getCategory, category)
                    .ne(CbCaseSchedulerJob::getStatus, SchedulerCons.STATUS_DELETING)
                    .ne(StringUtils.isNotEmpty(id) && !id.equals("0"), CbCaseSchedulerJob::getId, id));
        }

        /**
         * 校验数量超过限制
         *
         * @param propertyId 资产 id
         * @param category   分类
         * @param limitCount 限制数量
         * @author yyt
         * @date 2024/12/4
         **/
        @Override
        public void checkCountLimit(Integer propertyId, Integer category, int limitCount) {
            long totalCount = count(new QueryWrapper<CbCaseSchedulerJob>().lambda()
                    .eq(CbCaseSchedulerJob::getPropertyId, propertyId)
                    .eq(CbCaseSchedulerJob::getCategory, category)
                    .ne(CbCaseSchedulerJob::getStatus, SchedulerCons.STATUS_DELETING));
            if (totalCount == limitCount) {
                throw new BadRequestException(CommonErrorI18nConstant.SCHEDULER_COUNT_TRANSFINITE,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "Creating timing configuration rules exceeds number limit.");
            }
        }

    }