package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.SysOaAiConversationBo;
import com.ruoyi.oa.domain.vo.SysOaAiConversationVo;
import com.ruoyi.oa.domain.SysOaAiConversation;
import com.ruoyi.oa.mapper.SysOaAiConversationMapper;
import com.ruoyi.oa.service.ISysOaAiConversationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * AI对话历史Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-19
 */
@RequiredArgsConstructor
@Service
public class SysOaAiConversationServiceImpl implements ISysOaAiConversationService {

    private final SysOaAiConversationMapper baseMapper;

    /**
     * 查询AI对话历史
     */
    @Override
    public SysOaAiConversationVo queryById(Long conversationId) {
        return baseMapper.selectVoById(conversationId);
    }

    /**
     * 查询AI对话历史列表
     */
    @Override
    public TableDataInfo<SysOaAiConversationVo> queryPageList(SysOaAiConversationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysOaAiConversation> lqw = buildQueryWrapper(bo);
        Page<SysOaAiConversationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询AI对话历史列表
     */
    @Override
    public List<SysOaAiConversationVo> queryList(SysOaAiConversationBo bo) {
        LambdaQueryWrapper<SysOaAiConversation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysOaAiConversation> buildQueryWrapper(SysOaAiConversationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysOaAiConversation> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getConversationTitle()), SysOaAiConversation::getConversationTitle, bo.getConversationTitle());
        lqw.eq(bo.getUserId() != null, SysOaAiConversation::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getModelName()), SysOaAiConversation::getModelName, bo.getModelName());
        lqw.eq(bo.getStatus() != null, SysOaAiConversation::getStatus, bo.getStatus());
        lqw.eq(bo.getTotalTokens() != null, SysOaAiConversation::getTotalTokens, bo.getTotalTokens());
        lqw.eq(bo.getTotalCost() != null, SysOaAiConversation::getTotalCost, bo.getTotalCost());
        return lqw;
    }

    /**
     * 新增AI对话历史
     */
    @Override
    public Boolean insertByBo(SysOaAiConversationBo bo) {
        SysOaAiConversation add = BeanUtil.toBean(bo, SysOaAiConversation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setConversationId(add.getConversationId());
        }
        return flag;
    }

    /**
     * 修改AI对话历史
     */
    @Override
    public Boolean updateByBo(SysOaAiConversationBo bo) {
        SysOaAiConversation update = BeanUtil.toBean(bo, SysOaAiConversation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysOaAiConversation entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除AI对话历史
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据用户ID查询对话列表
     */
    @Override
    public List<SysOaAiConversationVo> queryByUserId(Long userId) {
        LambdaQueryWrapper<SysOaAiConversation> lqw = Wrappers.lambdaQuery();
        lqw.eq(SysOaAiConversation::getUserId, userId);
        lqw.orderByDesc(SysOaAiConversation::getCreateTime);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 创建新对话
     */
    @Override
    public Long createConversation(Long userId, String title) {
        SysOaAiConversation conversation = new SysOaAiConversation();
        conversation.setUserId(userId);
        conversation.setConversationTitle(title);
        conversation.setModelName("deepseek-chat");
        conversation.setStatus(1); // 进行中
        conversation.setTotalTokens(0);
        conversation.setTotalCost(java.math.BigDecimal.ZERO);
        baseMapper.insert(conversation);
        return conversation.getConversationId();
    }

    /**
     * 结束对话
     */
    @Override
    public Boolean endConversation(Long conversationId) {
        SysOaAiConversation conversation = new SysOaAiConversation();
        conversation.setConversationId(conversationId);
        conversation.setStatus(0); // 已结束
        return baseMapper.updateById(conversation) > 0;
    }
} 