package com.easyproject.system.service.aichat.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.easyproject.common.core.domain.AjaxResult;
import com.easyproject.service.domain.aichat.BotConversation;
import com.easyproject.service.domain.aichat.BotConversationChat;
import com.easyproject.service.mapper.aichat.BotConversationChatMapper;
import com.easyproject.service.mapper.aichat.BotConversationMapper;
import com.easyproject.system.domain.vo.BotConversationChatVo;
import com.easyproject.system.service.aichat.IBotConversationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 会话列表Service业务层处理
 *
 * @author easyproject
 * @date 2025-04-10
 */
@Service
public class BotConversationServiceImpl implements IBotConversationService {
    @Autowired
    private BotConversationMapper botConversationMapper;
    @Autowired
    private BotConversationChatMapper botConversationChatMapper;

    /**
     * 查询会话列表
     *
     * @param id 会话列表主键
     * @return 会话列表
     */
    @Override
    public BotConversation selectBotConversationById(Long id) {
        return botConversationMapper.selectBotConversationById(id);
    }

    /**
     * 查询会话列表列表
     *
     * @param botConversation 会话列表
     * @return 会话列表
     */
    @Override
    public List<BotConversation> selectBotConversationList(BotConversation botConversation) {
        List<BotConversation> list = botConversationMapper.selectBotConversationList(botConversation);
        return list;
    }

    /**
     * 方法说明: 设置备注
     *
     * @param
     * @return
     * @author java-101
     * @date 2025-04-10 16:27:25
     */
    @Override
    public AjaxResult updateBotConversation(BotConversation botConversation) {
        LambdaUpdateWrapper<BotConversation> updateWrapper = Wrappers.<BotConversation>lambdaUpdate();
        updateWrapper.eq(BotConversation::getId, botConversation.getId());
        updateWrapper.set(BotConversation::getRemark, botConversation.getRemark());
        updateWrapper.set(BotConversation::getUpdateTime, DateUtil.date());
        int result = botConversationMapper.update(null, updateWrapper);
        if (result > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 方法说明: 查询会话的对话列表（对话详情）
     * @param
     * @return
     * @author java-101
     * @date 2025-04-10 17:17:20
     */
    @Override
    public AjaxResult getConversationChatList(Long conversationId) {
        BotConversation botConversation = botConversationMapper.selectById(conversationId);
        if(ObjectUtil.isEmpty(botConversation)){
            return AjaxResult.error("会话不存在");
        }
        //查询对话列表
        LambdaQueryWrapper<BotConversationChat> queryWrapper = Wrappers.<BotConversationChat>lambdaQuery();
        queryWrapper.eq(BotConversationChat::getConversationId, botConversation.getConversationId());
        queryWrapper.eq(BotConversationChat::getBotId, botConversation.getBotId());
        queryWrapper.orderByAsc(BotConversationChat::getId);
        List<BotConversationChat> list = botConversationChatMapper.selectList(queryWrapper);

        List<BotConversationChatVo> chatVoList = null;
        if(ObjectUtil.isNotEmpty(list)){
            chatVoList = new ArrayList<>();
            BotConversationChatVo chatVo = null;
            for(BotConversationChat item: list){
                //处理用户发的数据
                chatVo = new BotConversationChatVo();
                chatVo.setContent(item.getMsgContent());
                chatVo.setRole("user");
                chatVoList.add(chatVo);

                //处理智能体发的数据
                chatVo = new BotConversationChatVo();
                chatVo.setContent(item.getBackMsgContent());
                chatVo.setRole("assistant");
                chatVoList.add(chatVo);
            }
        }

        return AjaxResult.success(chatVoList);
    }
}
