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;

@Service
public class QuartzBaseServiceImpl extends ServiceImpl<QuartzBaseMapper, QuartzBase> implements QuartzBaseService {

    @Override
    public QuartzBase findByExecuteClass(String executeClass) {
        LambdaQueryWrapper<QuartzBase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QuartzBase::getExecuteClass,executeClass);
        return this.getOne(lambdaQueryWrapper);
    }

    @Override
    public QuartzBase findByCode(String code) {
        LambdaQueryWrapper<QuartzBase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QuartzBase::getCode,code);
        return this.getOne(lambdaQueryWrapper);
    }

    @Override
    public List<QuartzBase> findByIncode(String inCode) {
        LambdaQueryWrapper<QuartzBase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QuartzBase::getInCode,inCode);
        return this.list(lambdaQueryWrapper);
    }

    @Override
    public List<QuartzBase> findByType(String type) {
        LambdaQueryWrapper<QuartzBase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QuartzBase::getType, CommonStant.JOB_TYPE_PUBLIC);
        return this.list(lambdaQueryWrapper);

    }

    @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;
    }
}
