# FdProcedureCheckSchedulerController.java

    package cloud.wconnect.workperformance.controller.fdprocedurecheck;

    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.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.workperformance.entity.fdprocedurecheck.FdProcedureCheckScheduler;
    import cloud.wconnect.workperformance.pojo.dto.fdprocedurecheck.scheduler.FdSchedulerDTO;
    import cloud.wconnect.workperformance.service.fdprocedurecheck.IFdProcedureCheckSchedulerService;
    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.springframework.web.bind.annotation.*;

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

    /**
     * <p>
     * fd procedure check 功能的调度数据 前端控制器
     * </p>
     *
     * @author zc
     * @since 2024-03-15
     */
    @RestController
    @RequestMapping("/fd-procedure-check/scheduler")
    public class FdProcedureCheckSchedulerController {

        @Resource
        private IFdProcedureCheckSchedulerService schedulerService;
        @Resource
        private PropertyI18nRedisService propertyI18nRedisService;
        @Resource
        private SessionContext sessionContext;
        @Resource
        private ObjectMapper objectMapper;
        @Resource
        private BeanMapper beanMapper;

        /**
         * 查询调度任务
         *
         * @param property 资产标识
         * @param category 调度任务类型
         * @return 调度任务
         * @author zc
         * @date 2024/3/12
         */
        @ApiId(1055)
        @GetMapping("/{category:\\d+}")
        public List<FdSchedulerDTO> list(String property, @PathVariable int category) {
            int propertyId = sessionContext.getPropertyId(property);
            // 查询调度任务
            List<FdProcedureCheckScheduler> schedulerList = schedulerService.list(propertyId, category);
            if (CollectionUtils.isEmpty(schedulerList)) {
                return Collections.emptyList();
            }
            return beanMapper.mapList(schedulerList, FdSchedulerDTO.class);
        }

        /**
         * 保存调度任务
         *
         * @param property     资产标识
         * @param id           调度任务id
         * @param schedulerDTO 调度任务DTO
         * @return java.util.Map<java.lang.String, java.lang.Integer>
         * @author zc
         * @date 2024/3/13
         */
        @ApiId(1056)
        @PostMapping("/{id}")
        public Map<String, String> save(@RequestParam String property, @PathVariable String id,
                                        @RequestBody @Valid FdSchedulerDTO schedulerDTO) {
            // 检查 scheduler json 格式是否合法
            CronRuleUtil.validateCronRuleForScheduler(objectMapper, Lists.newArrayList(SchedulerCons.EVERY_DAY),
                    schedulerDTO.getScheduler());

            schedulerDTO.setParameters("{}");

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

        /**
         * 删除任务
         *
         * @param id       任务ID
         * @param property 资产标识
         * @return java.util.Map
         * @author zc
         * @date 2024/3/14
         */
        @ApiId(1057)
        @DeleteMapping("/{id}")
        public Map<Object, Object> delete(@PathVariable String id, String property) {
            int propertyId = sessionContext.getPropertyId(property);
            // 校验是否存在
            FdProcedureCheckScheduler oriSchedulerJob = schedulerService.checkExist(id);
            // 校验归属
            schedulerService.checkBelong(propertyId, oriSchedulerJob);
            // 删除中状态的数据不可删除
            if (oriSchedulerJob.getStatus() == SchedulerCons.STATUS_DELETING) {
                throw new BadRequestException(BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The deleting FdScheduler cannot delete; SchedulerJobId=" + oriSchedulerJob.getId());
            }
            // 删除任务
            schedulerService.delete(oriSchedulerJob);
            return Collections.emptyMap();
        }

    }