package com.fs.app.controller;

import com.alibaba.fastjson.JSON;
import com.fs.app.params.SendSopParam;
import com.fs.app.params.SopLogsEditParam;
import com.fs.app.websocket.service.WebSocketServer;
import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.fastGpt.param.SendHookAIParam;
import com.fs.fastGpt.service.IFastGptChatSessionService;
import com.fs.qw.param.QwLoginParam;
import com.fs.qw.param.SopMsgParam;
import com.fs.qw.result.QwExternalContactByQwResult;
import com.fs.sop.domain.QwSopLogs;
import com.fs.sop.params.GetQwSopLogsByJsApiParam;
import com.fs.sop.params.SendSopParamDetailsC;
import com.fs.sop.service.IQwSopLogsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/app/qwSop")
public class QwSopController {

    @Autowired
    WebSocketServer webSocketServer;
    @Autowired
    RedisCache redisCache;
    @Autowired
    IFastGptChatSessionService fastGptChatSessionService;
    @Autowired
    private IQwSopLogsService qwSopLogsService;

//    @Async("scheduledExecutorService")
//    @Scheduled(cron = "0/300 * * * * ? ")
//    @GetMapping("/startSopService")
//    @RepeatSubmit
//    public R startSopService() throws InterruptedException {
//
//        System.out.println("SOP任务 开始执行");
//        //webSocketServer.SelectQwSopLogsTVOList();
//        String sopFlag=redisCache.getCacheObject("sopFlag");
////        if(StringUtils.isNotEmpty(sopFlag)&&sopFlag.equals("start")){
////            return R.ok("已启动");
////        }
//        Thread t1 = new Thread() {
//            @SneakyThrows
//            @Override
//            public void run() {
//                webSocketServer.sendSopMsg();
//            }
//        };
//        t1.start();
//        return R.ok("启动成功");
//
//    }

    @GetMapping("/stopSopService")
    public R stopSopService() throws InterruptedException {
        redisCache.deleteObject("sopFlag");
        return R.ok("已关闭");
    }


    /**
     * 更新AI发送状态
     */
    @PostMapping("/updateQwSopLogs")
    public R updateCourseSopLogs(@RequestBody SopLogsEditParam param){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            QwSopLogs qwSopLogs=new QwSopLogs();
            qwSopLogs.setId(param.getId());
            qwSopLogs.setReceivingStatus(param.getReceivingStatus());
            qwSopLogs.setSendStatus(param.getSendStatus());
            qwSopLogs.setRealSendTime(sdf.format(new Date()));
            qwSopLogs.setRemark(param.getRemark());
            qwSopLogsService.updateQwSopLogsSendType(qwSopLogs);

            return R.ok("更新成功");
        }catch (Exception e){
            return R.error("更新失败");
        }

    }

    //主动获取发送信息
    @PostMapping("/getQwSopLogsByJsApi")
    public R getQwSopLogsByJsApi(@Valid @RequestBody GetQwSopLogsByJsApiParam param) {

        SendSopParamDetailsC qwSopLogsByJsApi = qwSopLogsService.getQwSopLogsByJsApi(param);

        return R.ok().put("data",qwSopLogsByJsApi);
    }

    //获取销售的某个联系人
    @GetMapping("/getExternalContactByAppKey/{appKey}")
    public R getExternalContactByAppKey(@PathVariable("appKey") String appKey) {

        QwExternalContactByQwResult result=qwSopLogsService.getExternalContactByAppKey(appKey);

        return R.ok().put("data",result);
    }

    //清除不是当前员工的 外部联系以及营期
    @PostMapping("/deleteQwSopLogsByJsApi")
    public R deleteQwSopLogsByJsApi(@Valid @RequestBody GetQwSopLogsByJsApiParam param) {

       return qwSopLogsService.deleteQwSopLogsByJsApi(param);

    }

    @GetMapping("/getQwSopLogs")
    public R getQwSopLogs(SopMsgParam param) throws Exception {
        //获取记录
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        System.out.println("传参："+param);
        List<QwSopLogs> list = qwSopLogsService.selectQwSopLogsListVO(param);
        System.out.println("参数："+list);
        PageInfo<QwSopLogs> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }


    //登录请求
    @PostMapping("/qwLogin")
    public R qwLogin(@RequestBody QwLoginParam param) {

        SendSopParam sopParam=new SendSopParam();
            sopParam.setCmd("login");
            sopParam.setKey(param.getAppKey());

        webSocketServer.sendInfo(param.getAppKey(),JSON.toJSONString(sopParam));

        return R.ok();
    }

    //全部登录
    @GetMapping("/qwLoginAll")
    public R qwLogin() {

        String value="login";

        webSocketServer.sendInfoAll(value);

        return R.ok();
    }

    //全部退出
    @GetMapping("/qwLoginOutAll")
    public R qwLoginOutAll() {


        String value="kill";
        webSocketServer.sendInfoAll(value);

        return R.ok();
    }

    @PostMapping("/qwSendMsg")
    public R qwSendMsg(@RequestBody SendHookAIParam sopParam) {

        return webSocketServer.sendInfo(sopParam.getKey(),JSON.toJSONString(sopParam));
    }


    //登录请求-刷新获取二维码
    @PostMapping("/loginQwCodeUrl")
    public R loginQwCodeUrl(@RequestBody QwLoginParam param) {

        SendSopParam sopParam=new SendSopParam();
        sopParam.setCmd("getLoginCode");
        sopParam.setKey(param.getAppKey());
        webSocketServer.sendInfo(param.getAppKey(),JSON.toJSONString(sopParam));
        return R.ok();
    }


    //退出登录请求
    @PostMapping("/qwLoginOut")
    public R qwLoginOut(@RequestBody QwLoginParam param) {
        SendSopParam sopParam=new SendSopParam();
        sopParam.setCmd("logout");
        sopParam.setKey(param.getAppKey());
        webSocketServer.sendInfo(param.getAppKey(),JSON.toJSONString(sopParam));
        return R.ok();
    }

    //输入验证码
    @PostMapping("/qwLoginInputCode")
    public R qwLoginInputCode(@RequestBody QwLoginParam param) {
        SendSopParam sopParam=new SendSopParam();
        sopParam.setCmd("inputCode");
        sopParam.setKey(param.getAppKey());
        sopParam.setData(param.getCode());
        webSocketServer.sendInfo(param.getAppKey(),JSON.toJSONString(sopParam));
        return R.ok();
    }

}
