# GmwrSchedulerController.java

    package cloud.wconnect.workperformance.controller.gmwr;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.cons.CommonFieldI18nConstant;
    import cloud.wconnect.commonlib.cons.SchedulerCons;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.util.CronRuleUtil;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.workperformance.cons.gmwr.GmwrNotifyTypeEnum;
    import cloud.wconnect.workperformance.entity.gmwr.GmwrScheduler;
    import cloud.wconnect.workperformance.pojo.dto.gmwr.scheduler.GmwrSchedulerDTO;
    import cloud.wconnect.workperformance.service.gmwr.IGmwrSchedulerService;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.google.common.collect.ImmutableMap;
    import com.google.common.collect.Lists;
    import jakarta.annotation.Resource;
    import jakarta.validation.Valid;
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.web.bind.annotation.*;

    import java.text.MessageFormat;
    import java.util.Collections;
    import java.util.List;
    import java.util.Map;

    /**
     * <p>
     * hk efficiency 功能的调度数据 前端控制器
     * </p>
     *
     * @author zc
     * @since 2025-01-24
     */
    @RestController
    @RequestMapping("/gm-weekly-report/scheduler")
    public class GmwrSchedulerController {
        @Resource
        private IGmwrSchedulerService schedulerService;
        @Resource
        private PropertyI18nRedisService propertyI18nRedisService;
        @Resource
        private SessionContext sessionContext;
        @Resource
        private ObjectMapper objectMapper;
        @Resource
        private BeanMapper beanMapper;
        @Resource
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 查询调度任务
         *
         * @param property 资产标识
         * @param category 调度任务类型
         * @return 调度任务
         * @author zc
         * @date 2025/1/24
         */
        @ApiId(1334)
        @GetMapping("/{category:\\d+}")
        public List<GmwrSchedulerDTO> list(String property, @PathVariable int category) {
            int propertyId = sessionContext.getPropertyId(property);
            // 校验支持的定时任务类型
            checkSchedulerCategory(category);
            // 查询调度任务
            List<GmwrScheduler> schedulerList = schedulerService.list(propertyId, category);
            if (CollectionUtils.isEmpty(schedulerList)) {
                return Collections.emptyList();
            }
            return beanMapper.mapList(schedulerList, GmwrSchedulerDTO.class);
        }

        /**
         * 保存调度任务
         *
         * @param property     资产标识
         * @param id           调度任务id
         * @param schedulerDTO 调度任务DTO
         * @return job id
         * @author zc
         * @date 2025/1/24
         */
        @ApiId(1335)
        @PostMapping("/{id}")
        public Map<String, String> save(@RequestParam String property, @PathVariable String id,
                                        @RequestBody @Valid GmwrSchedulerDTO schedulerDTO) {
            // 校验支持的定时任务类型
            checkSchedulerCategory(schedulerDTO.getCategory());
            // 校验入参
            checkParameters(schedulerDTO);

            int propertyId = sessionContext.getPropertyId(property);
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);
            schedulerDTO.setPropertyId(propertyDTO.getId());
            if (!id.equals("0")) {
                // 校验调度任务是否存在
                GmwrScheduler scheduler = schedulerService.checkExist(id);
                // 校验归属
                schedulerService.checkBelong(propertyId, scheduler);
                if (scheduler.getStatus() == SchedulerCons.STATUS_DELETING) {
                    throw new BadRequestException(CommonErrorI18nConstant.SCHEDULER_CANNOT_UPDATE_DELETING,
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "The deleting GmwrScheduler cannot update, SchedulerJobId=" + scheduler.getId());
                }
                if (!scheduler.getCategory().equals(schedulerDTO.getCategory())) {
                    throw new BadRequestException(CommonErrorI18nConstant.SCHEDULER_CANNOT_CHANGE_CATEGORY,
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "The GmwrScheduler category cannot update, SchedulerJobId=" + scheduler.getId());
                }
                // 修改调度任务
                schedulerDTO.setId(id);
                return ImmutableMap.of("id", schedulerService.update(propertyDTO, scheduler, schedulerDTO, sessionContext.getLoginUserId()));
            }
            // 限制同一个资产下同一个 category 只允许有一条状态为未删除中的 scheduler 记录
            schedulerService.checkMaxNum(propertyId, schedulerDTO.getCategory(), 1);
            // 新增调度任务
            return ImmutableMap.of("id", schedulerService.save(schedulerDTO, propertyDTO, sessionContext.getLoginUserId()));
        }

        /**
         * 校验支持的定时任务类型
         *
         * @param schedulerType 定时任务类型
         * @author zc
         * @date 2026/3/6
         */
        private void checkSchedulerCategory(int schedulerType) {
            List<Integer> effectiveSchedulerTypeList = List.of(GmwrNotifyTypeEnum.DRAFT_DATA_PROCESS.getType());
            if (!effectiveSchedulerTypeList.contains(schedulerType)) {
                throw new BadRequestException(CommonErrorI18nConstant.NOTIFY_TYPE_NOT_SUPPORTED,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        MessageFormat.format("The currently selected scheduler type is not supported, " +
                                "schedulerCategory must be in {0}", effectiveSchedulerTypeList));
            }
        }

        /**
         * 校验入参 for category: 1、Draft data process
         *
         * @param schedulerDTO 调度任务DTO
         * @author zc
         * @date 2025/1/24
         */
        private void checkParameters(GmwrSchedulerDTO schedulerDTO) {
            // 检查 scheduler json 格式是否合法
            CronRuleUtil.validateCronRuleForScheduler(objectMapper, Lists.newArrayList(SchedulerCons.EVERY_WEEK),
                    schedulerDTO.getScheduler());
            // 校验调度任务参数
            if (StringUtils.isBlank(schedulerDTO.getParameters())) {
                throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                        i18nMessageUtils.getErrorParamMapNeedTranslate(CommonFieldI18nConstant.SCHEDULER_EXEC_PARAMETERS),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The parameters cannot be blank");
            }
        }

    }