package com.sckw.car.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sckw.car.entity.KwCarOrderDict;
import com.sckw.car.entity.KwOrderCarDictReq;
import com.sckw.car.entity.KwOrderCarDictRes;
import com.sckw.car.mapper.KwOrderCarDictMapper;
import com.sckw.core.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @desc:
 * @author: lt
 * @date: 2024/4/3 17:40
 */
@Service
public class OrderCarDictService {

    @Autowired
    KwOrderCarDictMapper kwOrderDictMapper;

    /**
     * @desc: 字典新增
     * @author: lt
     * @date: 2024/4/3 17:50
     */
    public String dictInsert(KwOrderCarDictReq kwOrderDictReq)
    {
        // 创建查询条件包装器
        QueryWrapper<KwCarOrderDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_group", kwOrderDictReq.getDictGroup())
                .eq("dict_value", kwOrderDictReq.getDictValue())
                .eq("del_flag", 0);

        // 查询是否已存在相同的字典数据
        KwCarOrderDict existingDict = kwOrderDictMapper.selectOne(queryWrapper);

        // 如果存在相同的字典数据，则抛出异常
        if (existingDict != null) {
            throw new RuntimeException("当前组已存在");
        }

        // 如果不存在相同的字典数据，则进行插入操作
        KwCarOrderDict kwOrderDict = new KwCarOrderDict();
        // 将请求对象的属性复制到字典对象中
        BeanUtils.copyProperties(kwOrderDictReq, kwOrderDict);

        // 进行插入操作
        int rows = kwOrderDictMapper.insert(kwOrderDict);
        if (rows > 0) {
            return "插入成功";
        } else {
            throw new RuntimeException("插入失败");
        }

    }


    public List<KwOrderCarDictRes> getDictData(String group) {
        QueryWrapper<KwCarOrderDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_group", group)
                .eq("del_flag", 0);

        List<KwCarOrderDict> dictList = kwOrderDictMapper.selectList(queryWrapper);

        // 将查询到的 KwOrderDict 转换为 KwOrderDictRes

        return dictFormat(dictList);
    }

    private List<KwOrderCarDictRes> dictFormat(List<KwCarOrderDict> list)
    {
        List<KwOrderCarDictRes> resList = new ArrayList<>();
        for (KwCarOrderDict dict : list) {
            KwOrderCarDictRes res = new KwOrderCarDictRes();
            res.setLabel(dict.getDictLabel());
            res.setValue(dict.getDictValue());
            res.setGroup(dict.getDictGroup());
            resList.add(res);
        }
        return resList;
    }

    public Map<String, Map<String, String>> queryDictByType(List<String> list) {
        if (list.isEmpty()) {
            return Collections.emptyMap();
        }

        Map<String, Map<String, String>> map = new HashMap<>(16);
        for (String type : list) {
            List<KwOrderCarDictRes> sysDictResDtos = queryDictByType(type);
            if (sysDictResDtos.isEmpty()) {
                continue;
            }

            Map<String, String> collect = sysDictResDtos.stream().collect(Collectors.toMap(KwOrderCarDictRes::getValue, KwOrderCarDictRes::getLabel, (n, o) -> n));
            map.put(type, collect);
        }
        return map;
    }

    public List<KwOrderCarDictRes> queryDictByType(String type) {
        System.out.println(type);
        if (StringUtils.isBlank(type)) {
            return Collections.emptyList();
        }
        //String key = Global.REDIS_SYS_DICT_TYPE_PREFIX + type;
        //String dictCache = RedissonUtils.getString(key);
        //从redis查，查不到从数据库查，并写入redis
        //if (StringUtils.isBlank(dictCache)) {
        List<KwCarOrderDict> sysDictList = kwOrderDictMapper.queryByType(type);
        if (CollectionUtils.isEmpty(sysDictList)) {
            return Collections.emptyList();
        }
            //List<KwOrderDictRes> sysDictResDtos = BeanUtils.copyProperties(sysDictList, KwOrderDictRes.class);
            //RedissonUtils.putString(key, JSON.toJSONString(sysDictResDtos), Global.COMMON_EXPIRE);
            //return sysDictResDtos;
        //}
        return dictFormat(sysDictList);
        //RedissonUtils.putString(key, dictCache, Global.COMMON_EXPIRE);
    }


}
