package com.css.simulation.resource.server.app.service;

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.param.RedisParameter;
import api.common.pojo.param.system.DictParam;
import api.common.pojo.po.system.DictPO;
import api.common.pojo.vo.system.DictVO;
import api.common.util.JsonUtil;
import api.common.util.ObjectUtil;
import api.common.util.StringUtil;
import com.css.simulation.resource.server.infra.db.mysql.mapper.DictMapper;
import com.css.simulation.resource.server.infra.feign.service.RedisService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DictService {

    @Resource
    private DictMapper dictMapper;

    @Resource
    private RedisService redisService;

    @SneakyThrows
    public String refreshDicts(DictParam param) {
        String dictTypes = param.getDictTypes();
        List<String> dictTypeList = Arrays.asList(dictTypes.split(","));
        if (dictTypeList.size() == 0) {//防止参数为","
            dictTypeList = new ArrayList<>();
            dictTypeList.add("xxx");
        }
        if ("all".equals(dictTypes)) {//关键字all时刷新全部字典
            dictTypeList = new ArrayList<>();
        }
        //数据库取值
        List<DictVO> DictVOlist = dictMapper.getDicts(dictTypeList);
        //分组转换
        Map<String, List<DictVO>> DictVOMap = DictVOlist.stream().collect(Collectors.groupingBy(DictVO::getDictType));
        //解析重构
        List<RedisParameter> dictLists = new ArrayList<>();
        List<RedisParameter> dictMaps = new ArrayList<>();
        Set<String> keySet = DictVOMap.keySet();
        for (String key : keySet) {
            List<DictVO> dictVOS = DictVOMap.get(key);
            //list 字典
            String dictList = JsonUtil.listToJson(dictVOS);
            //放入列表
            RedisParameter listParameter = new RedisParameter();
            listParameter.setKey(key);
            listParameter.setValue(dictList);
            dictLists.add(listParameter);
            //map 字典
            Map<String, String> map = new HashMap<>();
            for (DictVO dictVO : dictVOS) {
                String code = dictVO.getDictCode();
                String name = dictVO.getDictName();
                map.put(code, name);
            }
            String dictMap = JsonUtil.beanToJson(map);
            //放入列表
            RedisParameter mapParameter = new RedisParameter();
            mapParameter.setKey(key);
            mapParameter.setValue(dictMap);
            dictMaps.add(mapParameter);
        }
        //放入缓存
        redisService.setDictLists(dictLists);//前端下拉框使用
        redisService.setDictMaps(dictMaps);//后端字典翻译使用
        //返回刷新成功的字典名称
        return keySet.toString();
    }

    public Map<String, List<DictVO>> getDictListsByTypes(DictParam param) {
        //解析参数进行查询
        String dictTypes = param.getDictTypes();
        List<String> dictTypeList = Arrays.asList(dictTypes.split(","));
        ResponseBodyVO<Map<String, String>> responseBodyVO = redisService.getDictLists(dictTypeList);
        //查询结果转换至java对象
        Map<String, String> map = responseBodyVO.getInfo();
        Map<String, List<DictVO>> dictLists = new HashMap<>();
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            String val = map.get(key);
            //json转javaBean
            List<DictVO> DictVOS = JsonUtil.jsonToList(val, DictVO.class);
            dictLists.put(key, DictVOS);
        }
        return dictLists;
    }

    public Map<String, List<DictVO>> getDictTreesByTypes(DictParam param) {
        //解析参数进行查询
        String dictTypes = param.getDictTypes();
        List<String> dictTypeList = Arrays.asList(dictTypes.split(","));
        ResponseBodyVO<Map<String, String>> responseBodyVO = redisService.getDictLists(dictTypeList);
        //查询结果转换至java对象
        Map<String, String> map = responseBodyVO.getInfo();
        Map<String, List<DictVO>> dictTrees = new HashMap<>();
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            String val = map.get(key);
            //json转javaBean
            List<DictVO> dictVOS = JsonUtil.jsonToList(val, DictVO.class);
            //list转tree
            List<DictVO> tree = ObjectUtil.DictListToTree(dictVOS);
            dictTrees.put(key, tree);
        }
        return dictTrees;
    }

    public Map<String, Map<String, String>> getDictMapsByTypes(DictParam param) {
        //解析参数进行查询
        String dictTypes = param.getDictTypes();
        List<String> dictTypeList = Arrays.asList(dictTypes.split(","));
        ResponseBodyVO<Map<String, String>> responseBodyVO = redisService.getDictMaps(dictTypeList);
        //查询结果转换至map对象
        Map<String, String> map = responseBodyVO.getInfo();
        Map<String, Map<String, String>> dictMaps = new HashMap<>();
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            String val = map.get(key);
            //json转map
            Map<String, String> hashMap = JsonUtil.jsonToMap(val);
            dictMaps.put(key, hashMap);
        }
        return dictMaps;
    }

    public Map<String, Map<String, String>> getDictMapsByTypes(String param) {
        DictParam dictParam = new DictParam();
        dictParam.setDictTypes(param);
        return getDictMapsByTypes(dictParam);
    }

    public Map<String, Map<String, String>> getDictMapsByTypes(String param, String... arg) {
        StringBuilder sb = new StringBuilder(param);
        if (arg != null) {
            for (String s : arg) {
                if (s != null && !s.equals("")) {
                    sb.append(",").append(s);
                }
            }
        }
        DictParam dictParam = new DictParam();
        dictParam.setDictTypes(sb.toString());
        return getDictMapsByTypes(dictParam);
    }

    public Map<String, String> getDictMapByType(String param) {
        DictParam dictParam = new DictParam();
        dictParam.setDictTypes(param);
        Map<String, Map<String, String>> dictMaps = getDictMapsByTypes(dictParam);
        Map<String, String> dictMap = dictMaps.get(param);
        if (ObjectUtil.isNull(dictMap)) {
            new HashMap<>();
        }
        return dictMap;
    }

    @Transactional
    public ResponseBodyVO<String> createDictByType(DictPO dictPO) {
        dictPO.setId(StringUtil.getRandomUUID());
        DictPO tempPo = dictMapper.getDictByDictType(dictPO);
        int tempOrder = 1;
        if (tempPo != null) {
            tempOrder = tempPo.getDictOrder() + 1;
        }
        dictPO.setDictOrder(tempOrder);
        dictPO.setIsDeleted("0");
        dictMapper.createSystemDict(dictPO);
        DictParam param = new DictParam();
        param.setDictTypes(dictPO.getDictType());
        refreshDicts(param);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }

    public ResponseBodyVO<String> deleteDictById(DictPO param) {
        dictMapper.deleteDictById(param);
        DictParam deleteParam = new DictParam();
        deleteParam.setDictTypes(param.getDictType());
        refreshDicts(deleteParam);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }
}
