package com.goafanti.ChatAI.service.impl;

import com.goafanti.ChatAI.bo.*;
import com.goafanti.ChatAI.service.ChatAiService;
import com.goafanti.common.bo.SseEmitterUTF8;
import com.goafanti.common.dao.ErnieBotCommonDao;
import com.goafanti.common.error.BusinessException;
import com.goafanti.common.model.ErnieBotCommon;
import com.goafanti.common.utils.*;
import com.goafanti.core.mybatis.JDBCIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class ChatAiServiceImpl implements ChatAiService {


    @Autowired
    private ErnieBotCommonDao ernieBotCommonDao;


    @Autowired
    private BaiduChatUtils baiduChatUtils;
    @Autowired
    private SseEmitterUtils sseEmitterUtils;
    @Autowired
    private JDBCIdGenerator jdbcIdGenerator;


    @Override
    public SseEmitterUTF8 sendChat(InputSendChat in) {
        SseEmitterUTF8 sseEmitter = new SseEmitterUTF8(3600_000L);
//        if (clientId==null)clientId=UUID.randomUUID().toString();
        SseMap.sseEmitterMap.put(in.getUserId(), new SseResult(in.getUserId(), System.currentTimeMillis(), sseEmitter));
        try {
//            先将数据返回,异步调用返回数据
//            baiduChatUtils.sendBaiduAiStream(in);
            baiduChatUtils.sendERNIEBOT4Stream(in);
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("文言一心调用失败"+e.getLocalizedMessage());
        }
            return sseEmitter;
    }

    @Override
    public SseEmitterUTF8 sendGLM(InputsendGLM in) {
        SseEmitterUTF8 sseEmitter = new SseEmitterUTF8(3600_000L);
        String id=jdbcIdGenerator.generateId().toString();
        SseMap.sseEmitterMap.put(id, new SseResult(id, System.currentTimeMillis(), sseEmitter));
        try {
            sseEmitterUtils.sseEmitterSend(id,in);
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("ChatGLM调用失败"+e.getLocalizedMessage());
        }
        return sseEmitter;
    }

    @Override
//    @Cacheable(value = "selectErnieBotCommonAll#300",key = "'page:'+'All'")
    public List<ErnieBotCommon> selectErnieBotCommonAll(Integer count) {
        return ernieBotCommonDao.selectAll(count);
    }





}
