package com.energy.manage.service.service.dict.impl;

import com.alibaba.fastjson.JSON;
import com.energy.manage.common.constant.redis.ManagerRedisKeyConstant;
import com.energy.manage.common.po.dict.DictConstantsPO;
import com.energy.manage.service.domain.vo.dict.DictParentVo;
import com.energy.manage.service.mappers.dict.DictConstantsMapper;
import com.energy.manage.service.service.cache.CacheService;
import com.energy.manage.service.service.dict.DictConstantsService;
import com.energy.manage.service.util.BeanMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tongzw
 * @since 2022-07-04
 */
@Service
public class DictConstantsServiceImpl implements DictConstantsService {


    @Autowired
    DictConstantsMapper mapper;


    @Autowired
    CacheService cacheService;

    /*
     * 批量写入
     *@param data
     */
    @Override
    @Transactional
    public void batchSave(List<DictConstantsPO> data) {
        mapper.batchSave(data);
    }

    /*
     * 批量更新
     * @param data
     */
    @Override
    @Transactional
    public void batchUpdate(List<DictConstantsPO> data) {
        mapper.batchUpdate(data);
    }

    @Override
    public List<DictConstantsPO> getDictListByType(int dictType) {

//        if (!DictConstantsTypeEnum.ifContains(dictType)) {
//            return null;
//        }
        initDictListAllCache();
        String constantStr = cacheService.get(ManagerRedisKeyConstant.DICT_CONSTANTS_KEY);
        List<DictConstantsPO> DictConstantsList = JSON.parseArray(constantStr, DictConstantsPO.class);
        return DictConstantsList.stream().filter(t -> t.getContentsType().equals(dictType)).sorted(Comparator.comparingInt(DictConstantsPO::getContentsValue)).collect(Collectors.toList());

    }


    @Override
    public List<DictConstantsPO> getDictListAll() {
        return mapper.selectAll();
    }


    @Override
    @Transactional
    public void refreshDictListAllCache() {
        cacheService.delete(ManagerRedisKeyConstant.DICT_CONSTANTS_KEY);
        List<DictConstantsPO> dataList = mapper.selectAll();
        cacheService.add(ManagerRedisKeyConstant.DICT_CONSTANTS_KEY, JSON.toJSONString(dataList));
    }


    @Override
    @Transactional
    public void initDictListAllCache() {
        if (!cacheService.exists(ManagerRedisKeyConstant.DICT_CONSTANTS_KEY)) {
            List<DictConstantsPO> dataList = mapper.selectAll();
            cacheService.add(ManagerRedisKeyConstant.DICT_CONSTANTS_KEY, JSON.toJSONString(dataList));
        }
    }

    @Override
    public List<DictParentVo> getDictReachChain(Integer type) {
        return mapper.getDictReachChain(type);
    }


    @Override
    public DictConstantsPO getByTypeAndValue(int i, Integer ruleId) {
        DictConstantsPO dictConstantsPo = new DictConstantsPO();
        dictConstantsPo.setContentsType(i);
        dictConstantsPo.setContentsValue(ruleId);
        return mapper.selectOne(dictConstantsPo);
    }

    @Override
    public Map<Integer, String> getDictConstantsMap(Integer type) {
        List<DictConstantsPO> dictConstantsPOList = getDictListByType(type);
        Map<Integer, String> dictMap = dictConstantsPOList.stream().collect(Collectors.toMap(DictConstantsPO::getContentsValue, DictConstantsPO::getContentsName));
        return dictMap;
    }


}
