//package com.fs.app.controller;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import com.fs.app.service.QwDataCallbackService;
//import com.fs.common.core.domain.R;
//import com.fs.fastGpt.domain.FastGptChatSession;
//import com.fs.fastGpt.service.AiNewService;
//import com.fs.qw.domain.QwExternalContact;
//import com.fs.qw.domain.QwMessageGather;
//import com.fs.qw.domain.QwUser;
//import com.fs.qw.domain.QwUserVoiceLog;
//import com.fs.qw.mapper.QwExternalContactMapper;
//import com.fs.qw.mapper.QwUserMapper;
//import com.fs.qw.mapper.QwUserVoiceLogMapper;
//import com.fs.qw.service.IQwUserService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Service;
//
//import java.util.Date;
//import java.util.List;
//import java.util.UUID;
//
//@Service
//public class testTask {
//
//    @Autowired
//    QwDataCallbackService qwDataCallbackService;
//    @Autowired
//    private AiNewService aiService;
//
//    @Autowired
//    private QwUserMapper qwUserMapper;
//
//    @Autowired
//    private QwUserVoiceLogMapper qwUserVoiceLogMapper;
//
//    @Autowired
//    private QwExternalContactMapper qwExternalContactMapper;
//
//    //@Scheduled(fixedRate = 5000) // 每10执行一次
//    public void getMsg(){
//
//        String corpId="ww5a88c4f879f204c5";
//        // 拉取会话存档内容
//        List<String> chatContents = qwDataCallbackService.getChatData(corpId);
//        for (String chatContent : chatContents) {
//            QwMessageGather qwMessageGather = JSON.parseObject(chatContent, QwMessageGather.class);
//            if(qwMessageGather.getMsgtype().equals("voiptext")){//音视频通话获取通话时长
//                if (extracted(qwMessageGather, corpId)) {
//                    continue;
//                }
//            }
//            if (!qwMessageGather.getAction().equals("send")){
//                System.out.println("非send");
//                continue;
//            }
//            if (!qwMessageGather.getMsgtype().equals("text")){
//                System.out.println("非text");
//                continue;
//            }
//            if (qwMessageGather.getRoomid()!=null&& !qwMessageGather.getRoomid().isEmpty()){
//                System.out.println("群聊消息");
//                continue;
//            }
//
//            String from = qwMessageGather.getFrom();
//            if (from.startsWith("wo")||from.startsWith("wm")){
//                System.out.println("用户发消息");
//                aiService.qwHookNotifyAiReply(qwMessageGather,corpId);
//            }else {
//                aiService.qwHookNotifyAddMsg(qwMessageGather,corpId);
//                System.out.println("客服发送消息");
//            }
//            QwMessageGather.TextContent text = qwMessageGather.getText();
//            System.out.println(text.getContent());
//        }
//    }
//
//    /**
//     * 读取会话内容中的音视频通话时长
//     * @param qwMessageGather
//     * @param corpId
//     * @return
//     */
//    private boolean extracted(QwMessageGather qwMessageGather, String corpId) {
//        String info1 = qwMessageGather.getInfo();
//        JSONObject infoJson = JSONObject.parseObject(info1);
//        Long callDuration = infoJson.getLong("callduration");//音视频通话持续时间
//        QwUser qwUser = qwUserMapper.selectQwUserByQwUseridAndCorpId(qwMessageGather.getFrom(), corpId);
//
//        if (qwUser != null){
//            List<String> tolist = qwMessageGather.getTolist();
//            if (qwMessageGather.getTolist().isEmpty()){
//                return true;
//            }
//            String ExtId = tolist.get(0);
//            QwExternalContact qwExternalContacts = qwExternalContactMapper.selectQwExternalContactByExternalUserIdAndQwUserId(ExtId, corpId,qwUser.getQwUserId());
//            if (qwExternalContacts==null){
//                return true;
//            }
//
//            QwUserVoiceLog userVoiceLog = new QwUserVoiceLog();
//            userVoiceLog.setQwUserId(qwUser.getId());
//            userVoiceLog.setExtId(qwExternalContacts.getId());
//            userVoiceLog.setDuration(callDuration);
//            userVoiceLog.setCorpId(corpId);
//            userVoiceLog.setCompanyId(qwUser.getCompanyId());
//            userVoiceLog.setCompanyUserId(qwUser.getCompanyUserId());
//            userVoiceLog.setCreateTime(new Date());
//            userVoiceLog.setTitle("通话已接听");
//            userVoiceLog.setStatus(1L);
//            qwUserVoiceLogMapper.insertQwUserVoiceLog(userVoiceLog);
//        }
//        return false;
//    }
//
//
//    //@Scheduled(cron = "0 0/30 * * * ?")// 每10执行一次
//    public void expire(){
//
//        aiService.expireAiMsg();
//    }
//
//}
