package com.sonic.frog.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.sonic.frog.dao.AiDictDao;
import com.sonic.frog.domain.entity.AiDict;
import com.sonic.frog.domain.entity.ImageStyleDict;
import com.sonic.frog.domain.output.AiDictOut;
import com.sonic.frog.domain.output.DictOutput;
import com.sonic.frog.enums.AiDictTypeEnum;
import com.sonic.frog.service.AiDictService;
import com.sonic.frog.service.ImageStyleDictService;
import com.sonic.frog.service.TimbreDictService;
import com.sonic.frog.utils.BeanConver;
import com.sonic.frog.utils.CacheUtils;
import com.sonic.frog.utils.RedisKeyUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * AI角色，性格，标签，形象风格字典表 服务实现类
 * </p>
 *
 * @author mzc
 * @since 2024-11-28
 */
@Service
@Slf4j
public class AiDictServiceImpl extends ServiceImpl<AiDictDao, AiDict> implements AiDictService {

    @Autowired
    private CacheUtils cacheUtils;
    @Autowired
    private RedisKeyUtils redisKeyUtils;
    @Autowired
    private ImageStyleDictService imageStyleDictService;
    @Autowired
    private TimbreDictService timbreDictService;

    @Override
    public AiDictOut getAiDict() {
        String aiDictCacheKey = redisKeyUtils.aiDictCacheKey();
        List<AiDict> aiDictCacheList = cacheUtils.getCacheListAndSet(aiDictCacheKey, AiDict.class, () -> {
            List<AiDict> aiDictList = list(Wrappers.<AiDict>lambdaQuery().eq(AiDict::getIsDelete, false));
            return JSON.toJSONString(aiDictList);
        }, 60 * 60 * 1);
        AiDictOut aiDictOut = new AiDictOut();
        if (CollectionUtils.isNotEmpty(aiDictCacheList)) {
            //角色字典获取
            List<DictOutput> roleDictOutputList = buildDictOutputList(AiDictTypeEnum.ROLE, aiDictCacheList);
            roleDictOutputList.forEach(e -> {
                //获取子级角色字典
                List<AiDict> childRoleDictList = aiDictCacheList.stream().filter(child -> child.getParentCode() != null && child.getParentCode().equals(e.getCode())).collect(Collectors.toList());
                e.setChildDictList(BeanConver.copeList(childRoleDictList, DictOutput.class));
            });
            //性格字典获取
            List<DictOutput> characterDictOutputList = buildDictOutputList(AiDictTypeEnum.CHARACTER, aiDictCacheList);
            characterDictOutputList.forEach(e -> {
                //获取子级性格字典，对应的标签
                List<AiDict> childCharacterDictList = aiDictCacheList.stream().filter(child -> child.getParentCode() != null && child.getParentCode().equals(e.getCode())).collect(Collectors.toList());
                e.setChildDictList(BeanConver.copeList(childCharacterDictList, DictOutput.class));
            });
            //标签字典获取
            List<DictOutput> tagDictOutputList = buildDictOutputList(AiDictTypeEnum.TAG, aiDictCacheList);
            //获取形象风格图片
            List<ImageStyleDict> allImageStyleDictList = imageStyleDictService.getAllImageStyleDictList();

            //出参
            aiDictOut.setRoleDictList(roleDictOutputList);
            aiDictOut.setCharacterDictList(characterDictOutputList);
            aiDictOut.setTagDictList(tagDictOutputList);
            aiDictOut.setImageStyleDictList(allImageStyleDictList);
        }
        //音色推荐
        aiDictOut.setTimbreDictList(timbreDictService.getAllTimbreDictList());
        return aiDictOut;
    }

    /**
     * 获取各类型字典列表
     *
     * @param type
     * @param aiDictCacheList
     * @return
     */
    private List<DictOutput> buildDictOutputList(AiDictTypeEnum type, List<AiDict> aiDictCacheList) {
        List<AiDict> aiDictList = aiDictCacheList.stream().filter(e -> type.equals(e.getType()) && StringUtils.isEmpty(e.getParentCode())).collect(Collectors.toList());
        return BeanConver.copeList(aiDictList, DictOutput.class);
    }

    @Override
    public Map<String, String> mapNameByCodeList(Set<String> codeList) {
        if (CollectionUtils.isEmpty(codeList)) {
            return Maps.newHashMap();
        }
        List<AiDict> list = list(Wrappers.<AiDict>lambdaQuery().eq(AiDict::getIsDelete, false).in(AiDict::getCode, codeList));
        if (CollectionUtils.isNotEmpty(list)) {
            return list.stream().collect(Collectors.toMap(AiDict::getCode, AiDict::getName));
        }
        return Maps.newHashMap();
    }
}

