package com.ruoyi.cms.service.impl;


import com.ruoyi.cms.domain.BussinessDictData;
import com.ruoyi.cms.mapper.BussinessDictDataMapper;
import com.ruoyi.cms.service.IBussinessDictDataService;
import com.ruoyi.cms.util.DictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 字典 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class BussinessDictDataServiceImpl implements IBussinessDictDataService
{
    @Autowired
    private BussinessDictDataMapper dictDataMapper;

    /**
     * 根据条件分页查询字典数据
     * 
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<BussinessDictData> selectDictDataList(BussinessDictData dictData)
    {
        return dictDataMapper.selectDictDataList(dictData);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     * 
     * @param dictType 字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue)
    {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     * 
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public BussinessDictData selectDictDataById(Long dictCode)
    {
        return dictDataMapper.selectDictDataById(dictCode);
    }

    /**
     * 批量删除字典数据信息
     * 
     * @param dictCodes 需要删除的字典数据ID
     */
    @Override
    public void deleteDictDataByIds(Long[] dictCodes)
    {
        for (Long dictCode : dictCodes)
        {
            BussinessDictData data = selectDictDataById(dictCode);
            dictDataMapper.deleteDictDataById(dictCode);
            List<BussinessDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
    }

    /**
     * 新增保存字典数据信息
     * 
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(BussinessDictData data)
    {
        int row = dictDataMapper.insertDictData(data);
        if (row > 0)
        {
            List<BussinessDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
        return row;
    }

    /**
     * 修改保存字典数据信息
     * 
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(BussinessDictData data)
    {
        int row = dictDataMapper.updateDictData(data);
        if (row > 0)
        {
            List<BussinessDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
        return row;
    }
    @Override
    public List<Long> getSalary(String salaryDict)
    {
        ArrayList<Long> longs = new ArrayList<>();
        switch (salaryDict){
            case "0":
                longs.add(0l);
                longs.add(99999l);
                return longs;
            case "1":
                longs.add(0l);
                longs.add(0l);
                return longs;
            case "2":
                longs.add(2000l);
                longs.add(5000l);
                return longs;
            case "3":
                longs.add(5000l);
                longs.add(8000l);
                return longs;
            case "4":
                longs.add(8000l);
                longs.add(10000l);
                return longs;
            case "5":
                longs.add(10000l);
                longs.add(15000l);
                return longs;
            case "6":
                longs.add(15000l);
                longs.add(20000l);
                return longs;
            case "7":
                longs.add(20000l);
                longs.add(25000l);
                return longs;
            case "8":
                longs.add(25000l);
                longs.add(30000l);
                return longs;
            case "9":
                longs.add(30000l);
                longs.add(99999l);
                return longs;
        }
        longs.add(0l);
        longs.add(99999l);
        return longs;
    }
    @Override
    public String findCode(List<BussinessDictData> dictData, String type){
        if(!Objects.equals(type, "不限学历")&&!Objects.equals(type, "学历不限")){
            type.replace("学历","");
        }
        if(Objects.equals(type, "不限学历")||Objects.equals(type, "学历不限")){
            return "-1";
        }
        if(Objects.equals(type, "中专")){
            return "1";
        }
        if(Objects.equals(type, "中技")){
            return "1";
        }
        if(Objects.equals(type, "中技/中专")){
            return "1";
        }
        if(Objects.equals(type, "技校")){
            return "1";
        }
        if(Objects.equals(type, "本科及以上")){
            return "4";
        }
        for (BussinessDictData bussinessDictData:dictData){
            if (bussinessDictData.getDictLabel().equals(type)){
                return bussinessDictData.getDictValue();
            }
        }
        return null;
    }
}
