package com.jiayue.insu.incloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiayue.insu.incloud.constants.CommonStant;
import com.jiayue.insu.incloud.entity.QuartzBase;
import com.jiayue.insu.incloud.mapper.QuartzBaseMapper;
import com.jiayue.insu.incloud.service.QuartzBaseService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
 * 定时任务初始业务实现
 *
 * @author yh
 * @version 1.0
 * @since 2022/5/22 11:29
 */
@Service
public class QuartzBaseServiceImpl extends ServiceImpl<QuartzBaseMapper, QuartzBase> implements QuartzBaseService {


    /**
     * 通过执行类查询
     * @param executeClass 执行类路径
     * @return 结果
     */
    @Override
    public QuartzBase findByExecuteClass(String executeClass) {
        LambdaQueryWrapper<QuartzBase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QuartzBase::getExecuteClass,executeClass);
        return this.getOne(lambdaQueryWrapper);
    }


    /**
     * 通过定时任务编码查询
     * @param code 定时任务编码
     * @return 结果
     */
    @Override
    public QuartzBase findByCode(String code) {
        LambdaQueryWrapper<QuartzBase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QuartzBase::getCode,code);
        return this.getOne(lambdaQueryWrapper);
    }

    /**
     * 通过公司编码查询
     * @param inCode 公司编码
     * @return 结果
     */
    @Override
    public List<QuartzBase> findByIncode(String inCode) {
        LambdaQueryWrapper<QuartzBase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QuartzBase::getInCode,inCode);
        return this.list(lambdaQueryWrapper);
    }

    /**
     * 通过类型查询
     * @param type 类型
     * @return 结果
     */
    @Override
    public List<QuartzBase> findByType(String type) {
        LambdaQueryWrapper<QuartzBase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QuartzBase::getType, CommonStant.JOB_TYPE_PUBLIC);
        return this.list(lambdaQueryWrapper);

    }

    /**
     * 返回场站下可选择定时任务 （所属公司私有类型定时任务和公共类型定时任务）
     * @param inCode 公司编码
     * @return 结果
     */
    @Override
    public List<QuartzBase> getChooseQuartz(String inCode){
        List<QuartzBase> result = new ArrayList<>();
        LambdaQueryWrapper<QuartzBase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QuartzBase::getType, CommonStant.JOB_TYPE_PUBLIC);
        List<QuartzBase> list = this.list(lambdaQueryWrapper);
        Collections.sort(list, Comparator.comparing(QuartzBase::getSor));
        result.addAll(list);
        List<QuartzBase> byIncode = findByIncode(inCode);
        Collections.sort(byIncode, Comparator.comparing(QuartzBase::getSor));
        result.addAll(byIncode);
        return result;
    }
}
