package com.sckw.goods.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.StringUtils;
import com.sckw.goods.entity.KwBusinessGoodsDict;
import com.sckw.goods.entity.req.KwGoodsDictReq;
import com.sckw.goods.entity.res.KwGoodsDictRes;
import com.sckw.goods.mapper.KwGoodsDictMapper;
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 GoodsDictService {

    @Autowired
    KwGoodsDictMapper kwGoodsDictMapper;

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

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

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

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

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

    }


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

        List<KwBusinessGoodsDict> dictList = kwGoodsDictMapper.selectList(queryWrapper);

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

        return dictFormat(dictList);
    }

    private List<KwGoodsDictRes> dictFormat(List<KwBusinessGoodsDict> list)
    {
        List<KwGoodsDictRes> resList = new ArrayList<>();
        for (KwBusinessGoodsDict dict : list) {
            KwGoodsDictRes res = new KwGoodsDictRes();
            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<KwGoodsDictRes> sysDictResDtos = queryDictByType(type);
            if (sysDictResDtos.isEmpty()) {
                continue;
            }

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

    public List<KwGoodsDictRes> queryDictByType(String 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<KwBusinessGoodsDict> sysDictList = kwGoodsDictMapper.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);
    }


}
