package com.backendsys.service.Ai;

import com.backendsys.entity.Ai.AiChatHistoryDTO;
import com.backendsys.mapper.Ai.AiChatHistoryMapper;
import com.backendsys.utils.response.PageInfoResult;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class AiChatHistoryServiceImpl implements AiChatHistoryService {

    @Autowired
    private AiChatHistoryMapper aiChatHistoryMapper;

    /**
     * 查询 列表
     */
    public Map<String, Object> queryAiChatHistoryList(Integer pageNum, Integer pageSize, AiChatHistoryDTO aiChatHistoryDTO) {
        // 分页查询
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
            PageHelper.getLocalPage().setPageSizeZero(true);
        }
        // 分页输出 (自定义)
        List<Map<String, Object>> list = aiChatHistoryMapper.queryAiChatHistory(aiChatHistoryDTO);
        PageInfoResult pageInfoResult = new PageInfoResult(list);
        return pageInfoResult.toMap();
    }


    /**
     * 获得历史记录详情 (适用于 @QueryNullCheck 查询)
     */
    @Override
    public Map<String, Object> queryAiChatHistoryByCode(String history_code) {
        Map<String, Object> response = aiChatHistoryMapper.queryAiChatHistoryByCode(history_code);
        if (response == null) { return null; }
        return response;
    }

    /**
     * 创建
     */
    @Override
    @Transactional
    public Map<String, Object> insertAiChatHistory(AiChatHistoryDTO aiChatHistoryDTO) {
        aiChatHistoryMapper.insertAiChatHistory(aiChatHistoryDTO);
        return Map.of("history_code", aiChatHistoryDTO.getHistory_code());
    }

    // /**
    //  * 更新
    //  */
    // @Override
    // @Transactional
    // public Map<String, Object> updateAiChatHistory(AiChatHistoryDTO aiChatHistoryDTO) {
    //     aiChatHistoryMapper.updateAiChatHistory(aiChatHistoryDTO);
    //     return Map.of("history_code", aiChatHistoryDTO.getHistory_code());
    // }

    /**
     * 删除
     */
    @Override
    @Transactional
    public Map<String, Object> deleteAiChatHistory(AiChatHistoryDTO aiChatHistoryDTO) {
        aiChatHistoryMapper.deleteAiChatHistory(aiChatHistoryDTO);
        return Map.of("history_code", aiChatHistoryDTO.getHistory_code());
    }
}
