package com.njzscloud.common.sichen;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.common.sichen.support.TaskInfo;
import com.njzscloud.common.sichen.support.TaskUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 定时任务表
 */
@Slf4j
public class TaskService extends ServiceImpl<TaskMapper, TaskEntity> implements IService<TaskEntity> {

    /**
     * 新增
     */
    public void add(TaskEntity taskEntity) {
        TaskInfo taskInfo = BeanUtil.copyProperties(taskEntity, TaskInfo.class)
                .setCriticalTiming(TaskUtil.computedNextTiming(taskEntity.getScheduleType(), taskEntity.getScheduleConf()));
        this.save(taskEntity.setCriticalTiming(taskInfo.getCriticalTiming()));
    }

    /**
     * 修改
     */
    public void modify(TaskEntity taskEntity) {
        this.updateById(taskEntity);
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     */
    public TaskEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     */
    public PageResult<TaskEntity> paging(PageParam pageParam, TaskEntity taskEntity) {
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<TaskEntity>query(taskEntity)));
    }

}
