package cn.iocoder.yudao.module.system.service.cycle;

import cn.iocoder.yudao.module.system.controller.admin.cycle.vo.TaskCyclePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.cycle.vo.TaskCycleSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.cycle.TaskCycleDO;
import cn.iocoder.yudao.module.system.dal.mysql.cycle.TaskCycleMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.TASK_CYCLE_NOT_EXISTS;

/**
 * 循环任务点位 Service 实现类
 *
 * @author 陈宾顺
 */
@Service
@Validated
public class TaskCycleServiceImpl implements TaskCycleService {

    @Resource
    private TaskCycleMapper taskCycleMapper;

    @Override
    public Long createTaskCycle(TaskCycleSaveReqVO createReqVO) {
        // 插入
        TaskCycleDO taskCycle = BeanUtils.toBean(createReqVO, TaskCycleDO.class);
        taskCycleMapper.insert(taskCycle);
        // 返回
        return taskCycle.getId();
    }

    @Override
    public void updateTaskCycle(TaskCycleSaveReqVO updateReqVO) {
        // 校验存在
        validateTaskCycleExists(updateReqVO.getId());
        // 更新
        TaskCycleDO updateObj = BeanUtils.toBean(updateReqVO, TaskCycleDO.class);
        taskCycleMapper.updateById(updateObj);
    }

    @Override
    public void deleteTaskCycle(Long id) {
        // 校验存在
        validateTaskCycleExists(id);
        // 删除
        taskCycleMapper.deleteById(id);
    }

    private void validateTaskCycleExists(Long id) {
        if (taskCycleMapper.selectById(id) == null) {
            throw exception(TASK_CYCLE_NOT_EXISTS);
        }
    }

    @Override
    public TaskCycleDO getTaskCycle(Long id) {
        return taskCycleMapper.selectById(id);
    }

    @Override
    public PageResult<TaskCycleDO> getTaskCyclePage(TaskCyclePageReqVO pageReqVO) {
        return taskCycleMapper.selectPage(pageReqVO);
    }

}