# HkInventorySchedulerController.java

    package cloud.wconnect.workperformance.controller.hkinventory;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.cons.SchedulerCons;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.util.CronRuleUtil;
    import cloud.wconnect.workperformance.cons.hkinventory.HkInventoryNotifyConfigTypeEnum;
    import cloud.wconnect.workperformance.entity.hkinventory.HkInventoryScheduler;
    import cloud.wconnect.workperformance.pojo.dto.hkinventory.HkInventorySchedulerDTO;
    import cloud.wconnect.workperformance.service.hkinventory.IHkInventorySchedulerService;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import jakarta.validation.Valid;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

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

    /**
     * Housekeeping Inventory定时任务控制层
     *
     * @author lichen
     * @date 2025/4/26
     */
    @RestController
    @RequestMapping("/hk-inventory/scheduler")
    public class HkInventorySchedulerController {

        @Autowired
        private IHkInventorySchedulerService schedulerService;
        @Autowired
        private ObjectMapper objectMapper;
        @Autowired
        private SessionContext sessionContext;

        /**
         * 查询调度任务
         *
         * @param property 资产标识
         * @param category 调度任务类型
         * @return java.util.List<cloud.wconnect.workperformance.entity.hkinventory.HkInventoryScheduler>
         * @author lichen
         * @date 2025/4/26
         */
        @ApiId(1411)
        @GetMapping("{category:\\d+}")
        public List<HkInventoryScheduler> list(String property, @PathVariable int category) {
            int propertyId = sessionContext.getPropertyId(property);
            //校验分类是否合法
            HkInventoryNotifyConfigTypeEnum.checkValid(category);
            //查询调度任务
            return schedulerService.listByCategory(propertyId, category);
        }

        /**
         * 保存调度任务
         *
         * @param property     资产标识
         * @param id           调度任务主键ID
         * @param schedulerDTO 调度任务DTO
         * @return java.util.Map<java.lang.String, java.lang.String>
         * @author lichen
         * @date 2025/4/26
         */
        @ApiId(1412)
        @PostMapping("{id}")
        public Map<String, String> save(String property, @PathVariable String id,
                                        @RequestBody @Valid HkInventorySchedulerDTO schedulerDTO) {
            int propertyId = sessionContext.getPropertyId(property);

            // 检查 scheduler json格式是否合法
            CronRuleUtil.validateCronRuleForScheduler(objectMapper, List.of(SchedulerCons.EVERY_MONTH), schedulerDTO.getScheduler());
            // 参数设置为空
            schedulerDTO.setParameters("{}");
            //校验定时任务执行时间唯一
            schedulerService.checkCornRuleSole(propertyId, schedulerDTO.getCategory(), schedulerDTO.getScheduler(), id);

            if (!id.equals("0")) {
                //校验调度任务是否存在
                HkInventoryScheduler oriScheduler = schedulerService.checkExist(id);
                //校验是否归属于资产
                schedulerService.checkBelong(oriScheduler, propertyId);
                //校验状态不能等于删除中
                if (oriScheduler.getStatus() == SchedulerCons.STATUS_DELETING) {
                    throw new BadRequestException(CommonErrorI18nConstant.SCHEDULER_CANNOT_UPDATE_DELETING,
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "deleting SchedulerJob cannot update, SchedulerJobId=" + oriScheduler.getId());
                }
                if (!oriScheduler.getCategory().equals(schedulerDTO.getCategory())) {
                    throw new BadRequestException(CommonErrorI18nConstant.SCHEDULER_CANNOT_CHANGE_CATEGORY,
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "The scheduler category cannot update, SchedulerJobId=" + oriScheduler.getId());
                }
                schedulerDTO.setId(id);
                //修改
                return Map.of("id", schedulerService.update(propertyId, oriScheduler, schedulerDTO, sessionContext.getLoginUserId()));
            } else {
                //校验同一个category下最大可配置的数量
                schedulerService.checkMaxNum(propertyId, schedulerDTO.getCategory(), 5);
                //新增
                return Map.of("id", schedulerService.createScheduler(propertyId, schedulerDTO, sessionContext.getLoginUserId()));
            }
        }

        /**
         * 删除任务
         *
         * @param property 资产标识
         * @param id       任务ID
         * @return java.util.Map
         * @author lichen
         * @date 2025/4/26
         */
        @ApiId(1413)
        @DeleteMapping("{id}")
        public Map<String, String> delete(String property, @PathVariable String id) {
            int propertyId = sessionContext.getPropertyId(property);
            //校验是否存在
            HkInventoryScheduler scheduler = schedulerService.checkExist(id);
            //校验归属
            schedulerService.checkBelong(scheduler, propertyId);
            //删除任务
            schedulerService.delete(scheduler);
            return Collections.emptyMap();
        }

    }