package com.yfd.platform.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yfd.platform.system.domain.QuartzJob;
import com.yfd.platform.system.domain.SysDictionary;
import com.yfd.platform.system.mapper.QuartzJobMapper;
import com.yfd.platform.system.service.IQuartzJobService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfd.platform.utils.QuartzManage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 定时任务 服务实现类
 * </p>
 *
 * @author LiMengNan
 * @since 2023-03-19
 */
@Service
public class QuartzJobServiceImpl extends ServiceImpl<QuartzJobMapper,
        QuartzJob> implements IQuartzJobService {

    @Resource
    private QuartzJobMapper quartzJobMapper;

    @Resource
    private QuartzManage quartzManage;

    /**********************************
     * 用途说明: 新增定时任务
     * 参数说明 quartzJob 定时对象
     * 返回值说明: boolean 是否成功
     ***********************************/
    @Override
    public boolean addQuartzJob(QuartzJob quartzJob) {
        // 生成序号
        int orderNo = this.count() + 1;
        quartzJob.setOrderno(orderNo);
        return this.save(quartzJob);
    }

    /**********************************
     * 用途说明: 删除定时任务
     * 参数说明 id id
     * 返回值说明: boolean 是否成功
     ***********************************/
    @Override
    public boolean deleteQuartzJob(String id) {
        String[] split = id.split(",");
        Set<String> ids = Arrays.stream(split).collect(Collectors.toSet());
        for (String s : ids) {
            QuartzJob quartzJob = this.getById(s);
            quartzManage.deleteJob(quartzJob);
            this.removeById(s);
        }

        // 查询所有定时任务
        List<QuartzJob> list =
                this.list(new LambdaQueryWrapper<QuartzJob>().orderByAsc(QuartzJob::getOrderno));
        // 更新序号
        for (int i = 0; i < list.size(); i++) {
            QuartzJob quartzJob = list.get(i);
            quartzJob.setOrderno(i + 1);
            this.updateById(quartzJob);
        }
        return true;
    }

    /**********************************
     * 用途说明: 拖动修改定时任务顺序
     * 参数说明  fromID 当前ID  toID 到达ID
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回拖动成功或者失败
     ***********************************/
    @Override
    public boolean changeDictOrder(String fromID, String toID) {
        QuartzJob fromQuartzJob =
                quartzJobMapper.selectById(fromID);
        QuartzJob toQuartzJob = quartzJobMapper.selectById(toID);
        // 如果数据字典不存在拖动失败
        if (fromQuartzJob == null || toQuartzJob == null) {
            return false;
        }
        Integer fromOrderNo = fromQuartzJob.getOrderno();
        Integer toOrderNo = toQuartzJob.getOrderno();
        // 如果数据字典的顺序号不存在拖动失败
        if (fromOrderNo == null || toOrderNo == null) {
            return false;
        }
        // 将顺序号放入字典对象中
        fromQuartzJob.setOrderno(toOrderNo);
        toQuartzJob.setOrderno(fromOrderNo);
        // 更改顺序号
        boolean fromBool = this.updateById(fromQuartzJob);
        boolean toBool = this.updateById(toQuartzJob);
        return fromBool && toBool;
    }

    /**********************************
     * 用途说明: 执行定时任务
     * 参数说明 id id
     * 返回值说明: void
     ***********************************/
    @Override
    public void execution(QuartzJob quartzJob) {
        quartzManage.runJobNow(quartzJob);
    }
}
