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 SysTaskService extends ServiceImpl<SysTaskMapper, SysTaskEntity> implements IService<SysTaskEntity> {

    /**
     * 新增
     *
     * @param sysTaskEntity 数据
     */
    public void add(SysTaskEntity sysTaskEntity) {
        TaskInfo taskInfo = BeanUtil.copyProperties(sysTaskEntity, TaskInfo.class)
                .setCriticalTiming(TaskUtil.computedNextTiming(sysTaskEntity.getScheduleType(), sysTaskEntity.getScheduleConf()));
        this.save(sysTaskEntity.setCriticalTiming(taskInfo.getCriticalTiming()));
    }

    /**
     * 修改
     *
     * @param sysTaskEntity 数据
     */
    public void modify(SysTaskEntity sysTaskEntity) {
        this.updateById(sysTaskEntity);
    }

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

    /**
     * 详情
     *
     * @param id Id
     * @return SysTaskEntity 结果
     */
    public SysTaskEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     *
     * @param sysTaskEntity 筛选条件
     * @param pageParam     分页参数
     * @return PageResult&lt;SysTaskEntity&gt; 分页结果
     */
    public PageResult<SysTaskEntity> paging(PageParam pageParam, SysTaskEntity sysTaskEntity) {
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<SysTaskEntity>query(sysTaskEntity)));
    }

}
