package com.fs.app.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.fs.app.params.SendAIParam;
import com.fs.app.websocket.service.WebSocketServer;
import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.company.service.ICompanyConfigService;
import com.fs.course.mapper.FsCourseSopLogsMapper;
import com.fs.course.mapper.FsCourseSopMapper;
import com.fs.course.mapper.FsCourseWatchLogMapper;
import com.fs.course.param.FsCourseLinkCreateParam;
import com.fs.course.service.IFsCourseLinkService;
import com.fs.crm.mapper.CrmCustomerMapper;
import com.fs.his.mapper.FsUserMapper;
import com.fs.qw.domain.QwUser;
import com.fs.qw.mapper.*;
import com.fs.qw.service.*;
import com.fs.qw.service.impl.AsyncSopService;
import com.fs.qw.vo.QwFriendWelcomeDayPart;
import com.fs.qw.vo.QwFriendWelcomeVO;
import com.fs.qwApi.domain.QwResult;
import com.fs.qwApi.domain.inner.TextMessage;
import com.fs.qwApi.param.SendWelcomeMsgParam;
import com.fs.qwApi.service.QwApiService;
import com.fs.qwHookApi.param.QwHookSendMsgParam;
import com.fs.sop.mapper.QwSopLogsMapper;
import com.fs.sop.mapper.QwSopMapper;
import com.fs.sop.mapper.SopUserLogsInfoMapper;
import com.fs.sop.service.IQwSopService;
import com.fs.sop.service.ISopUserLogsService;
import com.fs.voice.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

@RestController
@RequestMapping(value="/app/test")
public class testController {

    @Autowired
    RedisCache redisCache;
    @Autowired
    QwExternalContactMapper qwExternalContactMapper;
    @Autowired
    QwUserMapper qwUserMapper;
    @Autowired
    RedisTemplate<String, String> redisTemplate;
    @Autowired
    IQwSopService qwSopService;
    @Autowired
    QwSopMapper qwSopMapper;

    @Autowired
    ICompanyConfigService companyConfigService;

    @Autowired
    private FsCourseWatchLogMapper watchLogMapper;

    @Autowired
    private AsyncSopService asyncSopService;

    @Autowired
    QwApiService qwApiService;
    @Autowired
    QwContactWayMapper qwContactWayMapper;
    @Autowired
    private QwExternalContactTransferLogMapper qwExternalContactTransferLogMapper;
    @Autowired
    IQwContactWayService qwContactWayService;
    @Autowired
    QwContactWayUserMapper qwContactWayUserMapper;
    @Autowired
    QwContactWayLogsMapper qwContactWayLogsMapper;

    @Autowired
    QwFriendWelcomeMapper qwFriendWelcomeMapper;

    @Autowired
    QwAutoTagsMapper qwAutoTagsMapper;
    @Autowired
    CrmCustomerMapper crmCustomerMapper;
    @Autowired
    QwAutoTagsLogsMapper qwAutoTagsLogsMapper;

    @Autowired
    IQwExternalContactCrmService contactCrmService;

    @Autowired
    FsCourseSopLogsMapper fsSopLogsMapper;

    @Autowired
    FsCourseSopMapper fsSopMapper;

    @Autowired
    CrmCustomerMapper customerMapper;

    @Autowired
    FsCourseWatchLogMapper fsCourseWatchLogMapper;

    @Autowired
    private IFsCourseLinkService fsCourseLinkService;
    @Autowired
    QwAppContactWayLogsMapper qwAppContactWayLogsMapper;


    @Autowired
    private FsUserMapper fsUserMapper;
    @Autowired
    private QwSopLogsMapper qwSopLogsMapper;

    @Autowired
    private QwAppContactWayMapper  qwAppContactWayMapper;
    @Autowired
    private SopUserLogsInfoMapper sopUserLogsInfoMapper;


    @Autowired
    private ISopUserLogsService sopUserLogsService;

    @Autowired
    private IFsCourseLinkService iFsCourseLinkService;

    @Autowired
    private IQwExternalErrRetryService errRetryService;

    @Autowired
    IQwExternalContactService qwExternalContactService;

    @GetMapping("/qwHookNotify")
    public R qwHookNotify() throws ParseException {


        return  R.ok();
    }

    @GetMapping("/key/{key}")
    public R test(@PathVariable String key) {
       
//        Set<String> keys = webSocketServer.getSessionPools();
//        for (String s : keys) {
//            if (s.equals(key)){
//                return R.ok("key在线");
//            }
//        }
        return  R.ok("不在线");
    }
    @Autowired
    private IQwUserService qwUserService;
    @Autowired
    QwCompanyMapper qwCompanyMapper;
//    @GetMapping("/ren/{key}")
//    public R ren(@PathVariable String key) {
//        QwUser user=qwUserService.selectQwUserByQwHookId("1688857885596589");
//
//                QwCompany qwCompany = qwCompanyMapper.selectQwCompanyByCorpId(user.getCorpId());
//            QwSendMsgParam qwSendMsgParam = new QwSendMsgParam();
//
//            qwSendMsgParam.setAgentid(Integer.parseInt(qwCompany.getServerAgentId().trim()));
//            qwSendMsgParam.setTouser(user.getQwUserId());
//            QwSendMsgParam.Text text = new QwSendMsgParam.Text();
//            text.setContent("您的客户:"+"xxxx"+" 需要转人工,请及时回复");
//            qwSendMsgParam.setText(text);
//            qwSendMsgParam.setMsgtype("text");
//            QwSendMsgResult qwSendMsgResult1 = qwApiService.sendMsg(qwSendMsgParam, qwCompany.getCorpId());
//            System.out.println(qwSendMsgResult1);
//
//        QwSendMsgParam qwSendMsgParam2 = new QwSendMsgParam();
//        qwSendMsgParam2.setAgentid(Integer.parseInt(qwCompany.getServerAgentId().trim()));
//        qwSendMsgParam2.setTouser(user.getQwUserId());
//        QwSendMsgParam.Text text2 = new QwSendMsgParam.Text();
//        text2.setContent("xxxxx1");
//        qwSendMsgParam2.setText(text2);
//        qwSendMsgParam2.setMsgtype("text");
//        QwSendMsgResult qwSendMsgResult = qwApiService.sendMsg(qwSendMsgParam2, qwCompany.getCorpId());
//        System.out.println(qwSendMsgResult);
//
//        return  R.ok("不在线");
//    }

    @Autowired
    WebSocketServer webSocketServer;


    public  void addWebSocket(String s){
        int i=0;
        while (i<100){
            System.out.println(i);
            QwHookSendMsgParam sendMsgParam=new QwHookSendMsgParam();
            QwHookSendMsgParam.QwHookSendMsgData sendMsgData=new QwHookSendMsgParam.QwHookSendMsgData();
            sendMsgParam.setType(101003);
            sendMsgData.setMsg(i+"测试");
            sendMsgData.setOpenId("wmotXidAAAFN0SqV0Zd1vDI8OOCAmaVQ");
            sendMsgData.setSyncKey("1");
            sendMsgParam.setData(sendMsgData);
            SendAIParam sendAIParam = new SendAIParam();
            sendAIParam.setCmd("aiReplyMsg");
            sendAIParam.setData(JSONUtil.toJsonStr(sendMsgParam));
            sendAIParam.setKey("zx");
            redisTemplate.opsForList().leftPush("AiMsg:"+"zx", JSON.toJSONString(sendAIParam));

            i++;
        }
    }
}
