package com.fs.fastGpt.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.fs.common.annotation.Excel;
import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.course.domain.FsUserCourseVideo;
import com.fs.course.mapper.FsCourseWatchLogMapper;
import com.fs.course.mapper.FsUserCourseVideoMapper;
import com.fs.course.param.FsCourseLinkCreateParam;
import com.fs.course.service.IFsCourseLinkService;
import com.fs.course.vo.FsCourseWatchLogVO;
import com.fs.fastGpt.config.ModeConfig;
import com.fs.fastGpt.domain.*;
import com.fs.fastGpt.mapper.FastGptChatReplaceWordsMapper;
import com.fs.fastGpt.mapper.FastGptChatSessionMapper;
import com.fs.fastGpt.mapper.FastgptChatVoiceHomoMapper;
import com.fs.fastGpt.param.SendAIParam;
import com.fs.fastGpt.service.AiService;
import com.fs.fastGpt.service.IFastGptChatMsgService;
import com.fs.fastGpt.service.IFastGptRoleService;
import com.fs.fastgptApi.param.ChatParam;
import com.fs.fastgptApi.result.ChatDetailTStreamFResult;
import com.fs.fastgptApi.service.ChatService;
import com.fs.fastgptApi.service.Impl.AudioServiceImpl;
import com.fs.fastgptApi.vo.AudioVO;
import com.fs.qw.domain.*;
import com.fs.qw.mapper.QwCompanyMapper;
import com.fs.qw.mapper.QwExternalContactInfoMapper;
import com.fs.qw.mapper.QwExternalContactMapper;
import com.fs.qw.mapper.QwUserMapper;
import com.fs.qw.service.*;
import com.fs.qwApi.param.QwSendMsgParam;
import com.fs.qwApi.service.QwApiService;
import com.fs.qwHookApi.param.QwHookSendMsgParam;
import com.fs.qwHookApi.vo.QwHookMsgVO;
import com.fs.qwHookApi.vo.QwHookVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
@Service
public class AiServiceImpl implements AiService {
    @Autowired
    private FastGptChatSessionMapper fastGptChatSessionMapper;
    @Autowired
    private ChatService chatService;
    @Autowired
    private QwUserMapper qwUserMapper;
    @Autowired
    private IFastGptRoleService roleService;
    @Autowired
    private QwExternalContactInfoMapper qwExternalContactInfoMapper;
    @Autowired
    private IFastGptChatMsgService fastGptChatMsgService;
    @Autowired
    private IQwContactWayService qwContactWayService;
    @Autowired
    QwApiService qwApiService;
    @Autowired
    QwCompanyMapper qwCompanyMapper;
    @Autowired
    FsCourseWatchLogMapper fsCourseWatchLogMapper;
    @Autowired
    FsUserCourseVideoMapper fsUserCourseVideoMapper;
    @Autowired
    FsCourseWatchLogMapper   watchLogMapper;
    @Autowired
    IQwJsApiService qwGetJsapiTicketService;
    @Autowired
    private IFsCourseLinkService iFsCourseLinkService;
    @Autowired
    FastgptChatVoiceHomoMapper fastgptChatVoiceHomoMapper;
    @Autowired
    RedisCache redisCache;
    @Autowired
    private IQwUserService qwUserService;
    @Autowired
    private QwExternalContactMapper qwExternalContactMapper;
    @Autowired
    private IQwMsgService qwMsgService;
    @Autowired
    private IQwTagGroupService qwTagGroupService;
    @Autowired
    private FastGptChatReplaceWordsMapper fastGptChatReplaceWordsMapper;
    @Autowired
    RedisTemplate<String, String> redisTemplate;


    /** Ai半小时未回复提醒 **/
    @Override
    public void AiRemind() {
        List<FastGptChatSession> fastGptChatSessions = fastGptChatSessionMapper.selectFastGptChatSessionByRemind();
        for (FastGptChatSession fastGptChatSession : fastGptChatSessions) {
            FastGptRole role=roleService.selectFastGptRoleTypeByRoleId(Long.parseLong(fastGptChatSession.getKfId()) );
            if (role!=null){
                String modeConfig=role.getModeConfigJson();
                if (modeConfig==null|| modeConfig.isEmpty()){
                    return;
                }
                ModeConfig config= JSONUtil.toBean(modeConfig,ModeConfig.class);
                String timeCount="用户未回复";
                ChatParam param=new ChatParam();
                param.setChatId(fastGptChatSession.getChatId());
                param.setStream(false);
                param.setDetail(true);
                ChatParam.Variables variables=new ChatParam.Variables();
                variables.setUid(fastGptChatSession.getKfId());
                variables.setName("test");
                param.setVariables(variables);
                List<ChatParam.Message> messageList=new ArrayList<ChatParam.Message>();
                param.setMessages(messageList);
                addPromptWord(messageList,timeCount,fastGptChatSession.getQwExtId(),role.getReminderWords(), role.getContactInfo(),fastGptChatSession.getSessionId());
                R r = chatService.initiatingTakeChat(param,"http://154.8.194.176:3000/api",config.getAPPKey());
                if(r.get("code").equals(200)){
                    ChatDetailTStreamFResult result=(ChatDetailTStreamFResult)r.get("data");
                    String content = result.getChoices().get(0).getMessage().getContent();
                    String count = replace(result.getChoices().get(0).getMessage().getContent()).trim();
                    QwUser qwUser = qwUserMapper.selectQwUserById(fastGptChatSession.getQwUserId());
                    if(count!=null&& !count.isEmpty()) {
                        QwHookSendMsgParam sendMsgParam = new QwHookSendMsgParam();
                        QwHookSendMsgParam.QwHookSendMsgData sendMsgData = new QwHookSendMsgParam.QwHookSendMsgData();
                        sendMsgParam.setType(101003);
                        sendMsgData.setMsg(count);
                        sendMsgData.setSendId(fastGptChatSession.getUserId());
                        sendMsgData.setSyncKey("1");
                        sendMsgParam.setData(sendMsgData);
                        SendAIParam sendAIParam = new SendAIParam();
                        sendAIParam.setCmd("aiReplyMsg");
                        sendAIParam.setData(JSONUtil.toJsonStr(sendMsgParam));
                        sendAIParam.setKey(qwUser.getAppKey());
                        redisTemplate.opsForList().leftPush("AiMsg:" + qwUser.getAppKey(), JSON.toJSONString(sendAIParam));
                    }
                    FastGptChatMsg fastGptChatMsgAi = new FastGptChatMsg();
                    fastGptChatMsgAi.setContent(result.getChoices().get(0).getMessage().getContent());
                    fastGptChatMsgAi.setSessionId(fastGptChatSession.getSessionId());
                    fastGptChatMsgAi.setRoleId(role.getRoleId());
                    fastGptChatMsgAi.setSendType(2);
                    fastGptChatMsgAi.setCompanyId(qwUser.getCompanyId());
                    fastGptChatMsgAi.setUserId(fastGptChatSession.getQwExtId().toString());
                    fastGptChatMsgAi.setCompanyUserId(qwUser.getCompanyUserId());
                    fastGptChatMsgAi.setUserType(1);
                    fastGptChatMsgAi.setMsgType(1);
                    fastGptChatMsgAi.setStatus(0);
                    fastGptChatMsgAi.setAvatar(fastGptChatSession.getAvatar());
                    fastGptChatMsgAi.setNickName(fastGptChatSession.getNickName());
                    fastGptChatMsgAi.setCreateTime(new Date());
                    fastGptChatMsgAi.setPromptTokens(result.getUsage().prompt_tokens);
                    fastGptChatMsgAi.setCompletionTokens(result.getUsage().completion_tokens);
                    List<ChatDetailTStreamFResult.ResponseNode> responseData = result.getResponseData();
                    int token=0;
                    for (ChatDetailTStreamFResult.ResponseNode responseDatum : responseData) {
                        int tokens = responseDatum.getTokens();
                        token+=tokens;
                    }
                    fastGptChatMsgAi.setTotalTokens(token);
                    fastGptChatMsgService.insertFastGptChatMsg(fastGptChatMsgAi);
                    addUserInfo(result.getChoices().get(0).getMessage().getContent(), fastGptChatSession.getQwExtId(),fastGptChatSession);
                    addUserInfo(content, fastGptChatSession.getQwExtId(),fastGptChatSession);
                    sendTaskUrlLink(content,fastGptChatSession.getUserId(),qwUser,fastGptChatSession);
                }
            }
            FastGptChatSession ss = new FastGptChatSession();
            ss.setSessionId(fastGptChatSession.getSessionId());
            ss.setRemindStatus(0);
            ss.setRemindCount(0);
            fastGptChatSessionMapper.updateFastGptChatSession(ss);
        }

    }
    /** Ai回复 **/
    @Async
    @Override
    public R qwHookNotifyAiReply(QwHookMsgVO msgVo,Integer type) {
        //是否是群聊
        if (msgVo.getIs_room()!=null&&msgVo.getIs_room()==0){
            QwSession session = new QwSession();
            R qwSession = qwMsgService.addQwMsg(msgVo);
            if (qwSession.get("code").equals(200)){
                session = (QwSession)qwSession.get("data");
            }
            if (msgVo.getContent().contains("我已经添加了你")){
                return R.ok();
            }
            QwUser user = qwUserService.selectQwUserByAppKey(msgVo.getKey());
            //查询接收人
            if(user==null||user.getFastGptRoleId()==null){
                return R.ok();
            }
            FastGptRole role=roleService.selectFastGptRoleTypeByRoleId(user.getFastGptRoleId());
            //没用ai角色跳过
            if(role==null){
                return R.ok();
            }
            String modeConfig=role.getModeConfigJson();
            //key不为空
            if(StringUtils.isEmpty(modeConfig)){
                return R.ok();
            }
            ModeConfig config=JSONUtil.toBean(modeConfig,ModeConfig.class);
            if(StringUtils.isEmpty(config.getAPPKey())){
                return R.ok();
            }

            QwExternalContact qwExternalContacts = qwExternalContactMapper.selectQwExternalContactByExternalUserIdAndQwUserId(msgVo.getSender_openid(), user.getCorpId(),user.getQwUserId());
            if (qwExternalContacts==null){
                FastGptChatSession fastGptChatSession = fastGptChatSessionMapper.selectFastGptChatSessionByUserIdAndKfId(msgVo.getReceiver(), user.getId());
                if (fastGptChatSession!=null){
                    saveQwUserMsg(fastGptChatSession,2,msgVo.getContent());
                }
                if (fastGptChatSession!=null&&fastGptChatSession.getIsArtificial()==1&&fastGptChatSession.getIsReply()==0){
                    FastGptChatSession fs = new FastGptChatSession();
                    fs.setSessionId(fastGptChatSession.getSessionId());
                    fs.setIsReply(1);
                    fs.setLastTime(new Date());
                    fastGptChatSessionMapper.updateFastGptChatSession(fs);
                }
//                else if (fastGptChatSession!=null&&fastGptChatSession.getIsArtificial()==0){
//
//                    FastGptChatSession fs = new FastGptChatSession();
//                    fs.setSessionId(fastGptChatSession.getSessionId());
//                    fs.setIsArtificial(1);
//                    fs.setIsReply(1);
//                    fs.setLastTime(new Date());
//                    fastGptChatSessionMapper.updateFastGptChatSession(fs);
//                }
                return R.ok();
            }
            if(qwExternalContacts.getType()!=null&&qwExternalContacts.getType()==1){
                FastGptChatSession fastGptChatSession= getFastGptSession(qwExternalContacts,msgVo,user);
                String contentEmj = replaceWxEmo(msgVo.getContent());
                if(!contentEmj.isEmpty()){
                    addSaveAiMsg(1,1,contentEmj,user,fastGptChatSession.getSessionId(),role.getRoleId(),qwExternalContacts,fastGptChatSession.getUserId(),null,null,null);
                }
                //判断是否转人工
                if (fastGptChatSession.getIsArtificial()==1){
                    return R.ok();
                }
                if (contentEmj.isEmpty()){
                    contentEmj ="用户发送表情："+msgVo.getContent();
                }
                //获取是用户是否发送消息
                Integer reply = redisCache.getCacheObject("reply:" + fastGptChatSession.getSessionId());
                Integer replyI=1;
                //用户正在发送消息 不发
                if (reply!=null&&reply!=0){
                    //更新用户发送消息次数
                    redisCache.setCacheObject("reply:" + fastGptChatSession.getSessionId(),reply+1,5, TimeUnit.MINUTES);
                    //获取用户之前发送的消息
                    String msg = redisCache.getCacheObject("msg:" + fastGptChatSession.getSessionId());
                    if (!msg.isEmpty()){
                        //更新用户发送消息内容
                        redisCache.setCacheObject("msg:" + fastGptChatSession.getSessionId(),msg+","+contentEmj,5,TimeUnit.MINUTES);
                    }
                    //本次跳过
                    System.out.println("正在对话");
                    return R.ok();
                }
                //用户首次发送消息
                redisCache.setCacheObject("reply:" + fastGptChatSession.getSessionId(),1,5,TimeUnit.MINUTES);
                redisCache.setCacheObject("msg:" + fastGptChatSession.getSessionId(),contentEmj,5,TimeUnit.MINUTES);
                System.out.println("等待");
                R r= sendAiMsg(replyI,fastGptChatSession,role,user,qwExternalContacts.getId(),config.getAPPKey(),qwExternalContacts);
                //完成对话 删除消息记录
                redisCache.deleteObject("reply:" + fastGptChatSession.getSessionId());
                redisCache.deleteObject("msg:" + fastGptChatSession.getSessionId());
                if(!r.get("code").equals(200)){
                    log.info("ai报错转人工:"+role.getRoleId()+":"+qwExternalContacts.getName());
                    notifyArtificial(fastGptChatSession.getSessionId(),user,qwExternalContacts.getName()," ai报错转人工");
                    return R.ok();
                }
                ChatDetailTStreamFResult result=(ChatDetailTStreamFResult)r.get("data");
                //是否转人工
                if(result.getChoices().isEmpty()){
                    return R.ok();
                }
                String contentKh = result.getChoices().get(0).getMessage().getContent();
                String content = replace(result.getChoices().get(0).getMessage().getContent()).trim();
                //计算token
                List<ChatDetailTStreamFResult.ResponseNode> responseData = result.getResponseData();
                int token=0;
                for (ChatDetailTStreamFResult.ResponseNode responseDatum : responseData) {
                    int tokens = responseDatum.getTokens();
                    token+=tokens;
                }
                //存聊天记录
                addSaveAiMsg(2,2,contentKh,user,fastGptChatSession.getSessionId(),role.getRoleId(),qwExternalContacts,fastGptChatSession.getUserId(),result.getUsage().prompt_tokens,result.getUsage().completion_tokens,token);
                if (!content.isEmpty()){
                    addSaveAiMsg(1,2,content,user,fastGptChatSession.getSessionId(),role.getRoleId(),qwExternalContacts,fastGptChatSession.getUserId(),null,null,null);
                    if (contentKh.contains("FunctionCallBegin")||contentKh.contains("FunctionCallEnd")||contentKh.contains("不清楚")||contentKh.contains("对不起")||contentKh.contains("客服")||contentKh.contains("不太清楚")||contentKh.contains("不明白")||contentKh.contains("不太明白")||contentKh.contains("http")){
                        notifyArtificial(fastGptChatSession.getSessionId(),user,qwExternalContacts.getName()," 触发关键词");
                        return R.ok();
                    }
                    //是否长对话不是就分段
                    if (result.isLongText()){
                        sendWebSocketMsg(content,msgVo,user,session);
                    }else {
                        String sa = contentKh.replaceAll("】\n", "】").replaceAll("\n【", "【");
                        String nr = replace(sa);
                        String[] split = nr.split("\n");
                        if (split.length>6){
                            notifyArtificial(fastGptChatSession.getSessionId(),user,qwExternalContacts.getName(),"回复长度异常");
                            return R.ok();
                        }
                        List<String> countList = countString(content, !type.equals(104014));
                        for (String msg : countList) {
//                          if (vo.getType().equals(104014)){
//                            sendWebSocketVoiceMsg(msg,msgVo,user,session,user.getCompanyUserId());
//                               }else {
                            sendWebSocketMsg(msg,msgVo,user,session);
//                               }
                        }
                    }
                }
                //新增用户信息
                addUserInfo(contentKh, qwExternalContacts.getId(),fastGptChatSession);
                //发送短链
                sendUrlLink(contentKh,msgVo,user,session);
                if (result.isArtificial()){
                    notifyArtificial(fastGptChatSession.getSessionId(),user,qwExternalContacts.getName()," ai请求人工协助");
                }
            }
        }
        return R.ok();
    }
    /** 回调转人工  **/
    private void notifyArtificial(Long sessionId, QwUser user, String name,String reason) {
        FastGptChatSession s = new FastGptChatSession();
        s.setIsArtificial(1);
        s.setRemindStatus(0);
        s.setIsReply(0);
        s.setSessionId(sessionId);
        s.setLastTime(new Date());
        fastGptChatSessionMapper.updateFastGptChatSession(s);
        log.info("转人工:"+name);
        QwCompany qwCompany = qwCompanyMapper.selectQwCompanyByCorpId(user.getCorpId());
        sendQwAppMsg(user.getCorpId(),Integer.parseInt(qwCompany.getServerAgentId().trim()),user.getQwUserId(),"您的客户:"+name+"因"+reason+"  需要转人工,请及时回复");
        sendQwAppMsg(user.getCorpId(),Integer.parseInt(qwCompany.getServerAgentId().trim()),user.getQwUserId(),name);
    }

    void sendQwAppMsg(String corpId, Integer agentId,String qwUserId,String content){
        QwSendMsgParam qwSendMsgParam = new QwSendMsgParam();
        qwSendMsgParam.setAgentid(agentId);
        qwSendMsgParam.setTouser(qwUserId);
        QwSendMsgParam.Text text = new QwSendMsgParam.Text();
        text.setContent(content);
        qwSendMsgParam.setText(text);
        qwSendMsgParam.setMsgtype("text");
        qwApiService.sendMsg(qwSendMsgParam, corpId);
    }


    private void saveQwUserMsg(FastGptChatSession fastGptChatSession,Integer sendType,String content) {
        content = replaceWxEmo(content);
        if(content.isEmpty()){
            return;
        }
        FastGptChatMsg fastGptChatMsgAi = new FastGptChatMsg();
        fastGptChatMsgAi.setContent(content);
        fastGptChatMsgAi.setSessionId(fastGptChatSession.getSessionId());
        fastGptChatMsgAi.setRoleId(Long.parseLong(fastGptChatSession.getKfId()));
        fastGptChatMsgAi.setSendType(sendType);
        fastGptChatMsgAi.setCompanyId(fastGptChatSession.getCompanyId());
        fastGptChatMsgAi.setUserId(fastGptChatSession.getUserId());
        fastGptChatMsgAi.setUserType(1);
        fastGptChatMsgAi.setMsgType(1);
        fastGptChatMsgAi.setStatus(0);
        fastGptChatMsgAi.setAvatar(fastGptChatSession.getAvatar());
        fastGptChatMsgAi.setNickName(fastGptChatSession.getNickName());
        fastGptChatMsgAi.setCreateTime(new Date());
        fastGptChatMsgAi.setExtId(fastGptChatSession.getQwExtId()+"");
        fastGptChatMsgService.insertFastGptChatMsg(fastGptChatMsgAi);
        log.info("新增消息："+fastGptChatMsgAi);
    }
    /** 存聊天记录  **/
    private void addSaveAiMsg(Integer msgType,Integer sendType,String content, QwUser user, Long sessionId, Long roleId, QwExternalContact qwExternalContacts, String userId, Integer promptTokens, Integer completionTokens, Integer token) {

        FastGptChatMsg fastGptChatMsgAi = new FastGptChatMsg();
        fastGptChatMsgAi.setContent(content);
        fastGptChatMsgAi.setSessionId(sessionId);
        fastGptChatMsgAi.setRoleId(roleId);
        fastGptChatMsgAi.setSendType(sendType);
        fastGptChatMsgAi.setCompanyId(user.getCompanyId());
        fastGptChatMsgAi.setUserId(userId);
        fastGptChatMsgAi.setCompanyUserId(user.getCompanyUserId());
        fastGptChatMsgAi.setUserType(1);
        fastGptChatMsgAi.setMsgType(msgType);
        fastGptChatMsgAi.setStatus(0);
        fastGptChatMsgAi.setAvatar(qwExternalContacts.getAvatar());
        fastGptChatMsgAi.setNickName(qwExternalContacts.getName());
        fastGptChatMsgAi.setCreateTime(new Date());
        fastGptChatMsgAi.setExtId(qwExternalContacts.getId()+"");
        fastGptChatMsgAi.setPromptTokens(promptTokens);
        fastGptChatMsgAi.setCompletionTokens(completionTokens);
        fastGptChatMsgAi.setTotalTokens(token);
        fastGptChatMsgService.insertFastGptChatMsg(fastGptChatMsgAi);
    }
    /** 获取会话  **/
    private FastGptChatSession getFastGptSession(QwExternalContact qwExternalContacts, QwHookMsgVO msgVo, QwUser user) {
        FastGptChatSession fastGptChatSession = fastGptChatSessionMapper.selectFastGptChatSessionByUserIdAndKfId(msgVo.getSender(), user.getId());
        if (fastGptChatSession==null){
            fastGptChatSession = new FastGptChatSession();
            String chatId = UUID.randomUUID().toString();
            fastGptChatSession.setChatId(chatId);
            fastGptChatSession.setUserId(msgVo.getSender());
            fastGptChatSession.setKfId(user.getFastGptRoleId().toString());
            fastGptChatSession.setStatus(1);
            fastGptChatSession.setRemindCount(0);
            fastGptChatSession.setRemindStatus(0);
            fastGptChatSession.setCreateTime(new Date());
            fastGptChatSession.setQwExtId(qwExternalContacts.getId());
            fastGptChatSession.setQwUserId(user.getId());
            fastGptChatSession.setIsArtificial(0);
            fastGptChatSession.setAvatar(qwExternalContacts.getAvatar());
            fastGptChatSession.setNickName(qwExternalContacts.getName());
            fastGptChatSession.setCompanyId(user.getCompanyId());
            fastGptChatSession.setLastTime(new Date());
            fastGptChatSession.setIsReply(0);
            fastGptChatSessionMapper.insertFastGptChatSession(fastGptChatSession);
            addUserSex(qwExternalContacts);
        }else {
            if (fastGptChatSession.getRemindStatus()==1){
                FastGptChatSession ss = new FastGptChatSession();
                ss.setSessionId(fastGptChatSession.getSessionId());
                ss.setRemindStatus(0);
                ss.setIsReply(0);
                fastGptChatSessionMapper.updateFastGptChatSession(ss);
            }
        }
        return fastGptChatSession;
    }
    /** 特殊转人工 **/
    @Override
    public void artificial(QwHookVO vo) {
        QwHookMsgVO msgVo= JSONUtil.toBean(vo.getData(),QwHookMsgVO.class);
        if (msgVo.getIs_room()!=null&&msgVo.getIs_room()==0){
            QwUser user=qwUserService.selectQwUserByAppKey(msgVo.getKey());
            if(user!=null&&user.getFastGptRoleId()!=null) {
                FastGptChatSession fastGptChatSession = fastGptChatSessionMapper.selectFastGptChatSessionByUserIdAndKfId(msgVo.getSender(), user.getId());
                if (fastGptChatSession!=null&&fastGptChatSession.getIsArtificial()!=1){
                    QwExternalContact qwExternalContacts = qwExternalContactMapper.selectQwExternalContactByExternalUserIdAndQwUserId(msgVo.getSender_openid(), user.getCorpId(),user.getQwUserId());
                    if (qwExternalContacts!=null){
                        FastGptChatSession s = new FastGptChatSession();
                        s.setIsArtificial(1);
                        s.setSessionId(fastGptChatSession.getSessionId());
                        fastGptChatSessionMapper.updateFastGptChatSession(s);
                        QwCompany qwCompany = qwCompanyMapper.selectQwCompanyByCorpId(user.getCorpId());
                        qwSendAppMsg(qwCompany.getCorpId(),Integer.parseInt(qwCompany.getServerAgentId().trim()),user.getQwUserId(),"您的客户:"+qwExternalContacts.getName()+" 因发送图片或视频,需要转人工,请及时回复");
                        qwSendAppMsg(qwCompany.getCorpId(),Integer.parseInt(qwCompany.getServerAgentId().trim()),user.getQwUserId(),qwExternalContacts.getName());
                        if (fastGptChatSession.getRemindStatus()==1){
                            FastGptChatSession ss = new FastGptChatSession();
                            ss.setSessionId(fastGptChatSession.getSessionId());
                            ss.setRemindStatus(0);
                            ss.setIsReply(0);
                            fastGptChatSessionMapper.updateFastGptChatSession(ss);
                        }
                    }
                }
            }
        }
    }

    void qwSendAppMsg(String corpId,Integer agentId, String qwUserId,String content){
        QwCompany qwCompany = qwCompanyMapper.selectQwCompanyByCorpId(corpId);
        QwSendMsgParam qwSendMsgParam2 = new QwSendMsgParam();
        qwSendMsgParam2.setAgentid(agentId);
        qwSendMsgParam2.setTouser(qwUserId);
        QwSendMsgParam.Text text2 = new QwSendMsgParam.Text();
        text2.setContent(content);
        qwSendMsgParam2.setText(text2);
        qwSendMsgParam2.setMsgtype("text");
        qwApiService.sendMsg(qwSendMsgParam2, qwCompany.getCorpId());
    }
    /** 添加企微性别 **/
    private void addUserSex(QwExternalContact qwExternalContacts) {

        QwExternalContactInfo info = qwExternalContactInfoMapper.selectQwExternalContactInfoByExternalContactId(qwExternalContacts.getId());
        if (qwExternalContacts.getGender()!=0){
            if (info!=null){
                info.setSex(qwExternalContacts.getGender()==1?"男":"女");
                qwExternalContactInfoMapper.updateQwExternalContactInfo(info);

            }else {
                QwExternalContactInfo qwExternalContactInfo = new QwExternalContactInfo();
                qwExternalContactInfo.setExternalContactId(qwExternalContacts.getId());
                qwExternalContactInfo.setSex(qwExternalContacts.getGender()==1?"男":"女");
                qwExternalContactInfo.setCreateTime(new Date());
                qwExternalContactInfoMapper.insertQwExternalContactInfo(qwExternalContactInfo);

            }
        }



    }
    /** Ai发送课程链接 **/
    private void sendUrlLink(String content, QwHookMsgVO msgVo, QwUser user, QwSession session) {
        if (content.contains("【发送课程：当天课程】")){
            FsCourseWatchLogVO fsCourseWatchLogVO = watchLogMapper.selectFsCourseWatchLogByExtIdAndQwUserId(session.getQwExtId(), user.getId());
            if (fsCourseWatchLogVO!=null){
                FsCourseLinkCreateParam param = new FsCourseLinkCreateParam();
                param.setVideoId(fsCourseWatchLogVO.getVideoId());
                param.setQwUserId(String.valueOf(user.getId()));
                param.setDays(1);
                param.setCorpId(user.getCorpId());
                param.setCourseId(fsCourseWatchLogVO.getCourseId());
                param.setCompanyUserId(user.getCompanyUserId());
                param.setCompanyId(user.getCompanyId());
                param.setQwExternalId(Long.parseLong(session.getQwExtId()));
                param.setSendTime(new Date());
                R linkUrl = iFsCourseLinkService.createLinkUrlWc(param);
                qwContactWayService.addWatchLogIfNeeded(Integer.valueOf(fsCourseWatchLogVO.getVideoId()+""),
                        Integer.valueOf(fsCourseWatchLogVO.getCourseId()+""),
                        String.valueOf(user.getId()),
                        String.valueOf(user.getCompanyUserId()),
                        String.valueOf(user.getCompanyId()),
                        String.valueOf(session.getQwExtId()));
                if (linkUrl != null && linkUrl.get("url") != null) {
                    String s = (String)linkUrl.get("url");
                    sendWebSocketMsg(s,msgVo,user,session);
                }
            }
        }
        else if (content.contains("【发送课程")){
            Pattern c = Pattern.compile("【发送课程：(.*?)】", Pattern.DOTALL);
            Matcher cMatcher = c.matcher(content);
            while  (cMatcher.find()) {
                String trim = cMatcher.group(1).trim();
                if(trim!=null&&!trim.equals("")){
                    FsUserCourseVideo fsUserCourseVideo = fsUserCourseVideoMapper.selectFsUserCourseVideoByVideoStringId(trim);
                    if (fsUserCourseVideo==null){
                        FsCourseLinkCreateParam param = new FsCourseLinkCreateParam();
                        param.setVideoId(fsUserCourseVideo.getVideoId());
                        param.setQwUserId(String.valueOf(user.getId()));
                        param.setDays(1);
                        param.setCorpId(user.getCorpId());
                        param.setCourseId(fsUserCourseVideo.getCourseId());
                        param.setCompanyUserId(user.getCompanyUserId());
                        param.setCompanyId(user.getCompanyId());
                        param.setQwExternalId(Long.parseLong(session.getQwExtId()));
                        param.setSendTime(new Date());
                        R linkUrl = iFsCourseLinkService.createLinkUrlWc(param);
                        qwContactWayService.addWatchLogIfNeeded(Integer.valueOf(fsUserCourseVideo.getVideoId()+""),
                                Integer.valueOf(fsUserCourseVideo.getCourseId()+""),
                                String.valueOf(user.getId()),
                                String.valueOf(user.getCompanyUserId()),
                                String.valueOf(user.getCompanyId()),
                                String.valueOf(session.getQwExtId()));
                        if (linkUrl != null && linkUrl.get("url") != null) {
                            String s = (String)linkUrl.get("url");
                            sendWebSocketMsg(s,msgVo,user,session);
                        }
                    }

                }else {
                    log.error("发送课程不存在:"+trim);
                }

            }
        }
    }
    /** 发送Ai消息 **/
    private R  sendAiMsg(Integer i,FastGptChatSession fastGptChatSession, FastGptRole role,QwUser user,Long qwExternalContactsId,String appKey,QwExternalContact qwExternalContacts){
        //等待5秒
        try {
            Thread.sleep(5000); // 5000 毫秒 = 5 秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //获取现在的次数
        Integer reply = (Integer)redisCache.getCacheObject("reply:" + fastGptChatSession.getSessionId());
        if (reply!=i){
            //次数变动 重新等待5秒
            return   sendAiMsg(reply,fastGptChatSession,role,user,qwExternalContactsId,appKey,qwExternalContacts);
        }else {
            System.out.println("开始ai回答");
            ChatParam param=new ChatParam();
            param.setChatId(fastGptChatSession.getChatId());
            param.setStream(false);
            param.setDetail(true);
            ChatParam.Variables variables=new ChatParam.Variables();
            variables.setUid(user.getFastGptRoleId().toString());
            variables.setName("test");
            param.setVariables(variables);
            List<ChatParam.Message> messageList=new ArrayList<ChatParam.Message>();
            param.setMessages(messageList);
            //添加看客记录
            addCourseWatchLog(qwExternalContactsId);
            String msgC = (String)redisCache.getCacheObject("msg:" + fastGptChatSession.getSessionId());
            //添加关键词
            addPromptWord(messageList,msgC,qwExternalContactsId,role.getReminderWords(), role.getContactInfo(),fastGptChatSession.getSessionId());
            R r = chatService.initiatingTakeChat(param,"http://154.8.194.176:3000/api",appKey);
            Integer reply2 = (Integer)redisCache.getCacheObject("reply:" + fastGptChatSession.getSessionId());
            //次数变动 重新等待5秒
            if (reply2!=i){
                System.out.println("等待");
                return   sendAiMsg(reply,fastGptChatSession,role,user,qwExternalContactsId,appKey,qwExternalContacts);
            }
            addSaveAiMsg(2,1,messageList.get(0).getContent(),user,fastGptChatSession.getSessionId(),role.getRoleId(),qwExternalContacts,fastGptChatSession.getUserId(),null,null,null);
            return r;
        }

    }
    /** 增加课程信息 **/
    private void addCourseWatchLog(Long id) {
        FsCourseWatchLogVO log = fsCourseWatchLogMapper.selectFsCourseWatchLogByExtId(id);
        if (log!=null){
            QwExternalContactInfo qwExternalContactInfo = qwExternalContactInfoMapper.selectQwExternalContactInfoByExternalContactId(id);
            QwExternalContactInfo info = new QwExternalContactInfo();
//            Date dateToCheck = log.getCreateTime(); // 假设这是你要检查的日期
//            Date today = new Date(); // 获取当前日期
           // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 格式化日期
            String name = log.getCourseName() + log.getTitle();
            name = name.replaceAll("[【】]", "");
//            if (sdf.format(dateToCheck).equals(sdf.format(today))) {
                info.setStudy(name);
                info.setCourseStatus(log.getLogType()==3?"待看课":log.getLogType()==1?"已完课":log.getLogType()==2?"已完课":"看课中断");

//            }else {
//                info.setStudy(name);
//                info.setCourseStatus("待看课");
//
//
//            }
            if(qwExternalContactInfo!=null){
                info.setId(qwExternalContactInfo.getId());
                qwExternalContactInfoMapper.updateQwExternalContactInfo(info);
            }else {
                info.setExternalContactId(id);
                info.setCreateTime(new Date());
                qwExternalContactInfoMapper.insertQwExternalContactInfo(info);

            }
        }
    }
    /** 组装发送AI内容 **/
    private void addPromptWord(List<ChatParam.Message> messageList,String count,Long extId,String words,String countInfo,Long sessionId){

        String  str="";
        List<FastGptChatMsg> msgs=fastGptChatMsgService.selectFastGptChatMsgByMsgSessionId(sessionId);
        if (!msgs.isEmpty()){
            Collections.reverse(msgs);
            str="【历史聊天内容：\n";
            for (FastGptChatMsg msg : msgs) {
                Integer sendType = msg.getSendType();
                String content = msg.getContent();
                str +=(sendType==1?"用户:":"AI:")+content+"\n";
            }
            str+="】\n";
        }

        // 这里获取后台的提示词进行匹配
        QwExternalContactInfo info = qwExternalContactInfoMapper.selectQwExternalContactInfoByExternalContactId(extId);
        if(info==null){
            info=new QwExternalContactInfo();
        }
        if (info!=null){
            str+="【用户状态信息\n";
            Field[] fields = info.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                Excel annotation = field.getAnnotation(Excel.class);
                if (annotation != null) {
                    String name = field.getName();
                    String fieldName = annotation.name();
                    String[] split = countInfo.split(",");
                    for (String zName : split) {
                        if (zName.equals(name)) {
                            Object value = null;
                            try {
                                value = field.get(info);
                            } catch (IllegalAccessException e) {
                            }
                            if (value != null) {
                                str += fieldName + ": " + value.toString() + "\n";
                            }else {
                                str += fieldName + ":  \n";
                            }
                        }
                    }
                }
            }
        }
        str+="】\n";
        if (words!=null&&!"".equals(words)){
            str+="【你的角色信息：以下内容为你的信息状态的补充而非用户信息，相当于放在角色任务里面，问到了需要知晓，但是如果无关的时候请无视此段内容 "+"\""+words+"\""+"】\n";
        }
        if (count!=null&&!"".equals(count)){
            str+="【用户说的话内容（之前的内容仅仅为背景，你知道即可，以下才是用户真实说的话的内容）\n" +
                    "\""+count+"\""+"\n" +
                    "】";
        }


        ChatParam.Message message1=new ChatParam.Message();
        message1.setRole("user");
        message1.setContent(str);
        messageList.add(message1);
    }
    /** 组装表情 **/
    public static List<String> countString(String input,Boolean isEmoji) {

        List<String> stringList = new ArrayList<>();
        // 所有的回车都分段发送
        String[] split = input.split("\n");
        for (String s : split) {
            List<String> sList = subCount(s);
            stringList.addAll(sList);
        }
//        if (isEmoji){
//            Random random = new Random();
//            String[] emojiMorning = new String[] {
//                    "😊",  // 微笑
//                    "☀️",  // 太阳
//                    "🌹",  // 玫瑰
//                    "☕️",  // 茶杯
//                    "💪",  // 强壮
//                    "❤️"   // 爱心
//            };
//            String[] emojiEvening = new String[] {
//                    "😊",  // 微笑
//                    "🌟",  // 星星
//                    "🌹",  // 玫瑰
//                    "☕️",  // 茶杯
//                    "💪",  // 强壮
//                    "❤️"   // 爱心
//            };
//            List<String> sebdList = new ArrayList<>();
//            for (String segment : stringList) {
//                if(!segment.trim().equals("")){
//                    LocalTime currentTime = LocalTime.now();
//                    LocalTime startTime = LocalTime.of(5, 0);
//                    LocalTime endTime   = LocalTime.of(18, 0);
//                    int sj = random.nextInt(2);
//                    // 判断当前时间是否在5点到18点之间
//                    String emj="";
//                    if (sj==0){
//                        if (currentTime.isAfter(startTime) && currentTime.isBefore(endTime)) {
//                            int i = random.nextInt(1);
//                            emj=emojiMorning[random.nextInt(emojiMorning.length-1)];
//                        } else {
//                            emj=emojiEvening[random.nextInt(emojiEvening.length-1)];
//                        }
//                    }
//                    sebdList.add(segment+emj) ;
//                }
//
//            }
//            return sebdList;
//        }


        return stringList;
    }
    /** 内容分段 **/
    static List<String> subCount(String s){
        ArrayList<String> a = new ArrayList<>();
        if (s.length()>30){
            String substring = s.substring(30);
            Pattern pattern = Pattern.compile("([~。!?]+)");
            String[] segments = pattern.split(substring);
            Matcher matcher = pattern.matcher(substring);
            if (matcher.find()&&segments.length>1) {
                int dh=0;
                String group = matcher.group();
                if (group.equals("。")){
                    group="";
                    dh=1;
                }
                String s1 = s.substring(0, 30) + segments[0] + group;
                a.addAll(Arrays.asList(s1));
                if (s.substring(s1.length())!=null&&!s.substring(s1.length()+dh).equals("")){
                    List<String> add = subCount(s.substring(s1.length()+dh));
                    a.addAll(add);
                }
            }else {
                a.add(s);
                return  a;
            }
        }else {
            a.add(s);
        }
        return  a;
    }
    /** 增加用户信息以及打标签 **/
    private void addUserInfo(String word,Long extId,FastGptChatSession fastGptChatSession)  {
        Pattern pattern = Pattern.compile("【用户状态信息(.*?)】", Pattern.DOTALL);
        Matcher matcher = pattern.matcher(word);
        while  (matcher.find()) {
            String trim = matcher.group(1).trim();
            String[] zd = trim.split("\n");
            boolean b=false;
            QwExternalContactInfo info = qwExternalContactInfoMapper.selectQwExternalContactInfoByExternalContactId(extId);
            if (info==null){
                info=new QwExternalContactInfo();
                b=true;
            }
            Field[] fields = info.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                Excel annotation = field.getAnnotation(Excel.class);
                if (annotation != null) {
                    //中文名称
                    String fieldName = annotation.name();
                    String valueName ="";
                    Object value = null;
                    try {
                        value = field.get(info);
                    } catch (IllegalAccessException e) {
                    }
                    if (value != null) {
                        valueName= value.toString();
                    }
                    for (String s : zd) {
                        String[] zdName=null;
                        if (s.contains(":")){
                            zdName = s.split(":");
                        }
                        if (s.contains("：")){
                            zdName = s.split("：");
                        }
                        if (zdName!=null&&zdName.length==2){
                            String name1 = zdName[0];
                            String name2 = zdName[1];
                            if (name1.trim().equals(fieldName)){
                                String name2Trim = name2.trim();
                                if (!name2Trim.isEmpty()){
                                    if(name1.equals("交流状态")){
                                        if (name2Trim.equals("非首次交流")){
                                            if (fastGptChatSession.getRemindStatus()!=null&&fastGptChatSession.getRemindStatus()==1){
                                                FastGptChatSession s1 = new FastGptChatSession();
                                                s1.setSessionId(fastGptChatSession.getSessionId());
                                                s1.setRemindStatus(0);
                                                s1.setRemindCount(0);
                                                fastGptChatSessionMapper.updateFastGptChatSession(s1);
                                            }
                                        }
                                    }
                                    if (!valueName.trim().equals(name2Trim)){
                                        if(name1.equals("学习到的章节")||name1.equals("今日课程完成情况")){
                                            continue;
                                        }

                                        if (name2Trim.contains("delete")&&name2Trim.contains("；")){
                                            name2Trim = name2Trim.replaceAll("delete.*?；", "");
                                        }else if (name2Trim.contains("delete")){
                                            name2Trim=" ";
                                        }
                                        try {
                                            // 允许修改私有属性
                                            field.setAccessible(true);
                                            // 修改 name 属性的值
                                            field.set(info, name2Trim);
                                            b=true;
                                        } catch (Exception e) {
                                            System.out.println("修改错误");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (b){
                if (info.getId()!=null){
                    qwExternalContactInfoMapper.updateQwExternalContactInfo(info);
                }else {
                    info.setExternalContactId(extId);
                    info.setCreateTime(new Date());
                    qwExternalContactInfoMapper.insertQwExternalContactInfo(info);
                }
            }
        }
        Pattern tag = Pattern.compile("【标签：(.*?)】", Pattern.DOTALL);
        Matcher TagMatcher = tag.matcher(word);
        while  (TagMatcher.find()) {
            String trimTag = TagMatcher.group(1).trim();
            if(trimTag!=null&&!trimTag.equals("")){
                qwTagGroupService.addQwTagByAi(trimTag,extId);
            }

        }

        Pattern delTag = Pattern.compile("【移除标签：(.*?)】", Pattern.DOTALL);
        Matcher delTagMatcher = delTag.matcher(word);
        while  (delTagMatcher.find()) {
            String deTag = delTagMatcher.group(1).trim();
            if(deTag!=null&&!deTag.equals("")){
                qwTagGroupService.delQwTagByAi(deTag,extId);
            }

        }

        Pattern addTime = Pattern.compile("【开始计时：(.*?)】", Pattern.DOTALL);
        Matcher addTimeMather = addTime.matcher(word);
        while  (addTimeMather.find()) {
            try {
                String time = addTimeMather.group(1).trim();
                int t = Integer.parseInt(time);
                    FastGptChatSession s1 = new FastGptChatSession();
                    s1.setSessionId(fastGptChatSession.getSessionId());
                    Calendar calendar = Calendar.getInstance(); // 获取当前时间
                    calendar.add(Calendar.MINUTE, t); // 增加t分钟
                    s1.setRemindTime(calendar.getTime());
                    s1.setRemindStatus(1);
                    s1.setRemindCount(0);
                    fastGptChatSessionMapper.updateFastGptChatSession(s1);

            }catch (Exception e){
                log.info("ai计时错误"+word);
            }

        }
    }
    /** 过滤[] **/
    private static String replace(String s){

        if(org.springframework.util.StringUtils.isEmpty(s)) return "";

        String regex1 = "<llnnerThoughtBeginl>[\\s\\S]*?<lnnerThoughtEnd|>";
        // 创建 Pattern 对象
        Pattern pattern1 = Pattern.compile(regex1);
        // 创建 Matcher 对象
        Matcher matcher1 = pattern1.matcher(s);
        s = matcher1.replaceAll("");

        String regex = "【[\\s\\S]*?】";
        // 创建 Pattern 对象
        Pattern pattern = Pattern.compile(regex);
        // 创建 Matcher 对象
        Matcher matcher = pattern.matcher(s);
        // 替换匹配到的内容
        return matcher.replaceAll("");
    }
    /** 过滤[] **/
    private static String replaceWxEmo(String s){

        if(org.springframework.util.StringUtils.isEmpty(s)) return "";
        // 替换匹配到的内容
        return s.replaceAll("\\[.*?]", "").trim();
    }
    /** 发送语音过滤 */
    private  String voiceHomo(String content){
        List<FastgptChatVoiceHomo> homos = fastgptChatVoiceHomoMapper.selectFastgptChatVoiceHomoList(new FastgptChatVoiceHomo());
        for (FastgptChatVoiceHomo homo : homos) {
            if (content.contains(homo.getContent())) {
                // 如果包含目标字段，则替换
                content= content.replace(homo.getContent(), homo.getChangeCount());
            } else {
            }
        }
        return content;
    }
    /** 替换违禁词 **/
    private  String replaceWords(String content){
        List<FastGptChatReplaceWords> words = fastGptChatReplaceWordsMapper.selectAllFastGptChatReplaceWords();
        for (FastGptChatReplaceWords word : words) {
            if (content.contains(word.getContent())) {
                // 如果包含目标字段，则替换
                content= content.replace(word.getContent(), word.getChangeCount());
            } else {
            }
        }
        return content;
    }
    /** 发送语音回复 */
    private static AudioVO getSilk(String count,Long userId) {
        AudioServiceImpl audioService = new AudioServiceImpl();
        AudioVO Silk = audioService.TextToVoice(count,userId);
        return Silk;
    }
    /** 发送普通消息 */
    private void sendWebSocketMsg(String count, QwHookMsgVO msgVo, QwUser user,QwSession session) {
        //发送socket
        if (count!=null&& !count.trim().isEmpty()){
            qwMsgService.addAiMsg(session,count,1,user);
            QwHookSendMsgParam sendMsgParam=new QwHookSendMsgParam();
            QwHookSendMsgParam.QwHookSendMsgData sendMsgData=new QwHookSendMsgParam.QwHookSendMsgData();
            sendMsgParam.setType(101003);
            sendMsgData.setMsg(replaceWords(count));
            sendMsgData.setSendId(msgVo.getSender());
            sendMsgData.setSyncKey("1");
            sendMsgParam.setData(sendMsgData);
            SendAIParam sendAIParam = new SendAIParam();
            sendAIParam.setCmd("aiReplyMsg");
            sendAIParam.setData(JSONUtil.toJsonStr(sendMsgParam));
            sendAIParam.setKey(user.getAppKey());
            redisTemplate.opsForList().leftPush("AiMsg:"+user.getAppKey(), JSON.toJSONString(sendAIParam));
        }


    }
    /** 发送语音回复 */
    private void sendWebSocketVoiceMsg(String count, QwHookMsgVO msgVo, QwUser user,QwSession session,Long companyUserId) {
        //发送socket
        qwMsgService.addAiMsg(session,count,2,user);
        AudioVO skl = getSilk(voiceHomo(count),companyUserId);
        QwHookSendMsgParam sendMsgParam=new QwHookSendMsgParam();
        QwHookSendMsgParam.QwHookSendMsgData sendMsgData=new QwHookSendMsgParam.QwHookSendMsgData();
        sendMsgParam.setType(101019);
        sendMsgData.setVoiceDuration(skl.getDuration());
        sendMsgData.setVoiceUrl(skl.getUrl());
        sendMsgData.setSendId(msgVo.getSender());
        sendMsgData.setSyncKey("1");
        sendMsgParam.setData(sendMsgData);
        SendAIParam sendAIParam = new SendAIParam();
        sendAIParam.setCmd("aiReplyMsg");
        sendAIParam.setData(JSONUtil.toJsonStr(sendMsgParam));
        sendAIParam.setKey(user.getAppKey());
        redisTemplate.opsForList().leftPush("AiMsg:"+user.getAppKey(), JSON.toJSONString(sendAIParam));

    }
    /** 发送定时任务课程链接 */
    private void sendTaskUrlLink(String content, String sendId, QwUser user, FastGptChatSession session) {
        if (content.contains("【发送课程：当天课程】")){
            FsCourseWatchLogVO fsCourseWatchLogVO = watchLogMapper.selectFsCourseWatchLogByExtIdAndQwUserId(session.getQwExtId().toString(), user.getId());
            if (fsCourseWatchLogVO!=null){
                FsCourseLinkCreateParam param = new FsCourseLinkCreateParam();
                param.setVideoId(fsCourseWatchLogVO.getVideoId());
                param.setQwUserId(String.valueOf(user.getId()));
                param.setDays(1);
                param.setCorpId(user.getCorpId());
                param.setCourseId(fsCourseWatchLogVO.getCourseId());
                param.setCompanyUserId(user.getCompanyUserId());
                param.setCompanyId(user.getCompanyId());
                param.setQwExternalId(session.getQwExtId());
                param.setSendTime(new Date());
                R linkUrl = iFsCourseLinkService.createLinkUrlWc(param);
                qwContactWayService.addWatchLogIfNeeded(Integer.valueOf(fsCourseWatchLogVO.getVideoId()+""),
                        Integer.valueOf(fsCourseWatchLogVO.getCourseId()+""),
                        String.valueOf(user.getId()),
                        String.valueOf(user.getCompanyUserId()),
                        String.valueOf(user.getCompanyId()),
                        String.valueOf(session.getQwExtId()));
                if (linkUrl != null && linkUrl.get("url") != null) {
                    String s = (String)linkUrl.get("url");
                    sendWebTaskSocketMsg(s,sendId,user);
                }
            }
        }
        else if (content.contains("【发送课程")){
            Pattern c = Pattern.compile("【发送课程：(.*?)】", Pattern.DOTALL);
            Matcher cMatcher = c.matcher(content);
            while  (cMatcher.find()) {
                String trim = cMatcher.group(1).trim();
                if(trim!=null&&!trim.equals("")){
                    FsUserCourseVideo fsUserCourseVideo = fsUserCourseVideoMapper.selectFsUserCourseVideoByVideoStringId(trim);
                    System.out.println("课程："+fsUserCourseVideo);
                    FsCourseLinkCreateParam param = new FsCourseLinkCreateParam();
                    param.setVideoId(fsUserCourseVideo.getVideoId());
                    param.setQwUserId(String.valueOf(user.getId()));
                    param.setDays(1);
                    param.setCorpId(user.getCorpId());
                    param.setCourseId(fsUserCourseVideo.getCourseId());
                    param.setCompanyUserId(user.getCompanyUserId());
                    param.setCompanyId(user.getCompanyId());
                    param.setQwExternalId(session.getQwExtId());
                    param.setSendTime(new Date());
                    R linkUrl = iFsCourseLinkService.createLinkUrlWc(param);
                    qwContactWayService.addWatchLogIfNeeded(Integer.valueOf(fsUserCourseVideo.getVideoId()+""),
                            Integer.valueOf(fsUserCourseVideo.getCourseId()+""),
                            String.valueOf(user.getId()),
                            String.valueOf(user.getCompanyUserId()),
                            String.valueOf(user.getCompanyId()),
                            String.valueOf(session.getQwExtId()));
                    if (linkUrl != null && linkUrl.get("url") != null) {
                        String s = (String)linkUrl.get("url");
                        sendWebTaskSocketMsg(s,sendId,user);
                    }
                }

            }
        }



    }
    /** 发送定时任务消息 */
    private void sendWebTaskSocketMsg(String count, String sendId, QwUser user) {
        //发送socket
        //文本消息
        QwHookSendMsgParam sendMsgParam=new QwHookSendMsgParam();
        QwHookSendMsgParam.QwHookSendMsgData sendMsgData=new QwHookSendMsgParam.QwHookSendMsgData();
        sendMsgParam.setType(101003);
        sendMsgData.setMsg(count);
        sendMsgData.setSendId(sendId);
        sendMsgData.setSyncKey("1");
        sendMsgParam.setData(sendMsgData);
        SendAIParam sendAIParam = new SendAIParam();
        sendAIParam.setCmd("aiReplyMsg");
        sendAIParam.setData(JSONUtil.toJsonStr(sendMsgParam));
        sendAIParam.setKey(user.getAppKey());
        redisTemplate.opsForList().leftPush("AiMsg:"+user.getAppKey(), JSON.toJSONString(sendAIParam));
    }
    @Override
    public R qwHookNotifyAddMsg(QwHookMsgVO vo) {
        QwUser sendUser=qwUserService.selectQwUserByAppKey(vo.getKey());
        if (sendUser!=null){
            FastGptChatSession fastGptChatSession = fastGptChatSessionMapper.selectFastGptChatSessionByUserIdAndKfId(vo.getReceiver(), sendUser.getId());
            if (fastGptChatSession!=null){
                saveQwUserMsg(fastGptChatSession,2,vo.getContent());
            }else {
                QwExternalContact qwExternalContacts = qwExternalContactMapper.selectQwExternalContactByExternalUserIdAndQwUserId(vo.getReceiver_openid(), sendUser.getCorpId(),sendUser.getQwUserId());
                if(qwExternalContacts!=null&&qwExternalContacts.getType()!=null&&qwExternalContacts.getType()==1){
                    if(sendUser.getFastGptRoleId()!=null){
                        fastGptChatSession = new FastGptChatSession();
                        String chatId = UUID.randomUUID().toString();
                        fastGptChatSession.setChatId(chatId);
                        fastGptChatSession.setUserId(vo.getReceiver());
                        fastGptChatSession.setKfId(sendUser.getFastGptRoleId().toString());
                        fastGptChatSession.setStatus(1);
                        fastGptChatSession.setRemindCount(0);
                        fastGptChatSession.setRemindStatus(0);
                        fastGptChatSession.setCreateTime(new Date());
                        fastGptChatSession.setQwExtId(qwExternalContacts.getId());
                        fastGptChatSession.setQwUserId(sendUser.getId());
                        fastGptChatSession.setIsArtificial(0);
                        fastGptChatSession.setAvatar(qwExternalContacts.getAvatar());
                        fastGptChatSession.setNickName(qwExternalContacts.getName());
                        fastGptChatSession.setCompanyId(sendUser.getCompanyId());
                        fastGptChatSession.setLastTime(new Date());
                        fastGptChatSession.setIsReply(0);
                        fastGptChatSessionMapper.insertFastGptChatSession(fastGptChatSession);
                        addUserSex(qwExternalContacts);
                        saveQwUserMsg(fastGptChatSession,2,vo.getContent());
                    }
                }
            }
        }
        return R.ok();
    }
}
