package admin.modules.chatgpt.service.impl;

import admin.base.PageInfo;
import admin.base.QueryHelpMybatisPlus;
import admin.base.impl.CommonServiceImpl;
import admin.modules.chatgpt.domain.ChatgptRole;
import admin.modules.chatgpt.domain.ChatgptRoleDesc;
import admin.modules.chatgpt.domain.ChatgptRoleType;
import admin.modules.chatgpt.service.ChatgptRoleDescService;
import admin.modules.chatgpt.service.dto.ChatgptRoleDescDto;
import admin.modules.chatgpt.service.dto.ChatgptRoleDescQueryCriteria;
import admin.modules.chatgpt.service.mapper.ChatgptRoleDescMapper;
import admin.modules.chatgpt.service.mapper.ChatgptRoleMapper;

import admin.util.ConvertUtil;
import admin.util.PageUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
* @author wk
* @date 2023-11-27
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = ChatgptRoleDescService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ChatgptRoleDescServiceImpl extends CommonServiceImpl<ChatgptRoleDescMapper, ChatgptRoleDesc> implements ChatgptRoleDescService {

    // private final RedisUtils redisUtils;
    private final ChatgptRoleDescMapper chatgptRoleDescMapper;
    private final ChatgptRoleMapper chatgptRoleMapper;

    @Override
    public PageInfo<ChatgptRoleDescDto> queryAll(ChatgptRoleDescQueryCriteria query, Pageable pageable) {
        IPage<ChatgptRoleDescDto> queryPage = PageUtil.toMybatisPage(pageable);
        MPJLambdaWrapper<ChatgptRoleDesc> mpjLambdaWrapper = QueryHelpMybatisPlus.getPredicateMPJ(query);
        mpjLambdaWrapper
                //查询表1的全部字段
                .selectAll(ChatgptRoleDesc.class)
                //查询表2的全部字段
                .selectAll(ChatgptRole.class)
                //左查询表2条件为表二的productId=表一的id
                .innerJoin(ChatgptRole.class, ChatgptRole::getId, ChatgptRoleDesc::getRoleId);
        IPage<ChatgptRoleDescDto> page = chatgptRoleDescMapper.selectJoinPage(queryPage,
                ChatgptRoleDescDto.class, mpjLambdaWrapper
        );
        return ConvertUtil.convertPage(page, ChatgptRoleDescDto.class);
    }

    @Override
    public List<ChatgptRoleDescDto> queryAll(ChatgptRoleDescQueryCriteria query){
        return ConvertUtil.convertList(chatgptRoleDescMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), ChatgptRoleDescDto.class);
    }

    @Override
    public ChatgptRoleDesc getById(Integer id) {
        return chatgptRoleDescMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public ChatgptRoleDescDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), ChatgptRoleDescDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(ChatgptRoleDescDto resources) {
        ChatgptRoleDesc entity = ConvertUtil.convert(resources, ChatgptRoleDesc.class);
        return chatgptRoleDescMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(ChatgptRoleDescDto resources){
        ChatgptRoleDesc entity = ConvertUtil.convert(resources, ChatgptRoleDesc.class);
        int ret = chatgptRoleDescMapper.updateById(entity);
        ChatgptRole chatgptRole = ConvertUtil.convert(resources, ChatgptRole.class);
        chatgptRole.setId(resources.getRoleId());
        chatgptRoleMapper.updateById(chatgptRole);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return chatgptRoleDescMapper.deleteBatchIds(ids);
    }

    @Override
    public List<Map<String, Object>> queryIndexList() {
        ChatgptRoleDescQueryCriteria query = new ChatgptRoleDescQueryCriteria();
        query.setClassify(3);
        query.setPostStatus(0);
        MPJLambdaWrapper<ChatgptRoleDesc> mpjLambdaWrapper = QueryHelpMybatisPlus.getPredicateMPJ(query);
        mpjLambdaWrapper
                //查询表1的全部字段
                .selectAll(ChatgptRoleDesc.class)
                //查询表2的全部字段
                .selectAll(ChatgptRole.class)
                //查询类别表类别字段
                .select(ChatgptRoleType::getRoleTypeName)
                //左查询表2条件为表二的productId=表一的id
                .innerJoin(ChatgptRole.class, ChatgptRole::getId, ChatgptRoleDesc::getRoleId)
                .innerJoin(ChatgptRoleType.class, ChatgptRoleType::getId, ChatgptRoleDesc::getRoleTypeId)
                .orderByAsc(ChatgptRoleType::getSortNum);
        List<ChatgptRoleDescDto> chatgptRoleDescDtos = chatgptRoleDescMapper.selectJoinList(ChatgptRoleDescDto.class, mpjLambdaWrapper);
        Map<String, List<ChatgptRoleDescDto>> collect =
                chatgptRoleDescDtos.stream().collect(Collectors.groupingBy(ChatgptRoleDescDto::getRoleTypeName));
        List<Map<String, Object>> result = new ArrayList<>();
        collect.forEach((k,v)->{
            Map<String, Object> map = new HashMap<>();
            map.put("roleTypeName", k);
            map.put("list", v);
            result.add(map);
        });
        Collections.shuffle(result);
        return result;
    }

    @Override
    public List<Map<String, Object>> queryAIList() {
        ChatgptRoleDescQueryCriteria query = new ChatgptRoleDescQueryCriteria();
        query.setPostStatus(0);
        query.setClassify(2);
        MPJLambdaWrapper<ChatgptRoleDesc> mpjLambdaWrapper = QueryHelpMybatisPlus.getPredicateMPJ(query);
        mpjLambdaWrapper
                //查询表1的全部字段
                .selectAll(ChatgptRoleDesc.class)
                //查询表2的全部字段
                .selectAll(ChatgptRole.class)
                .selectAll(ChatgptRoleType.class)
                //左查询表2条件为表二的productId=表一的id
                .innerJoin(ChatgptRole.class, ChatgptRole::getId, ChatgptRoleDesc::getRoleId)
                .innerJoin(ChatgptRoleType.class, ChatgptRoleType::getId, ChatgptRoleDesc::getRoleTypeId);
        List<ChatgptRoleDescDto> list = chatgptRoleDescMapper.selectJoinList(
                ChatgptRoleDescDto.class, mpjLambdaWrapper
        );
        Map<String, List<ChatgptRoleDescDto>> collect = list.stream().collect(Collectors.groupingBy(ChatgptRoleDescDto::getRoleTypeName));
        List<Map<String,Object>> result = new ArrayList<>();
        Map<String, Object> all = new HashMap<>();
        all.put("name","全部");
        all.put("value",list);
        result.add(all);
        for (Map.Entry<String,List<ChatgptRoleDescDto>> map : collect.entrySet()){
            String key = map.getKey();
            List<ChatgptRoleDescDto> value = map.getValue();
            Map<String,Object> tempMap = new HashMap<>();
            tempMap.put("name",key);
            tempMap.put("value",value);
            result.add(tempMap);
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<ChatgptRoleDescDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (ChatgptRoleDescDto chatgptRoleDesc : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("标题", chatgptRoleDesc.getTitle());
              map.put("内容", chatgptRoleDesc.getDescription());
              map.put("进入聊天提示内容", chatgptRoleDesc.getChat());
              map.put("图片", chatgptRoleDesc.getImage());
              map.put("角色id", chatgptRoleDesc.getRoleId());
              map.put("排序序号", chatgptRoleDesc.getSortNum());
              map.put("上线状态", chatgptRoleDesc.getPostStatus());
              map.put("创建时间", chatgptRoleDesc.getCreateDate());
              map.put("更新时间", chatgptRoleDesc.getUpdateDate());
              map.put("角色类型", chatgptRoleDesc.getRoleTypeId());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}