package com.fs.app.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.ad.service.IBdReportDataService;
import com.fs.app.annotation.Login;
import com.fs.app.param.SendSopParam;
import com.fs.app.utils.JwtUtils;
import com.fs.app.websocket.bean.MsgBean;
import com.fs.app.websocket.service.WebSocketServer;
import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.service.ISmsService;
import com.fs.company.domain.*;
import com.fs.company.mapper.CompanyVoiceRoboticCalleesMapper;
import com.fs.company.service.ICompanyConfigService;
import com.fs.company.service.ICompanySmsTempService;
import com.fs.company.service.ICompanyWxChatService;
import com.fs.company.service.ICompanyWxClientService;
import com.fs.company.service.impl.CompanyWxClientServiceImpl;
import com.fs.crm.domain.CrmCustomer;
import com.fs.crm.mapper.CrmCustomerMapper;
import com.fs.crm.param.SmsSendParam;
import com.fs.his.service.IFsAppVersionService;
import com.fs.his.service.IFsCityService;
import com.fs.his.service.IFsInquiryOrderMsgService;
import com.fs.his.utils.ConfigUtil;
import com.fs.sop.domain.QwSopLogs;
import com.fs.sop.mapper.QwSopLogsMapper;
import com.fs.system.service.ISysConfigService;
import com.fs.system.service.ISysDictDataService;
import com.fs.wx.kf.dto.WeixinKfMsgDTO;
import com.fs.wx.kf.dto.WeixinKfMsgSendDTO;
import com.fs.wx.kf.dto.WeixinKfTextMsgDTO;
import com.fs.wx.kf.service.IWeixinKfService;
import com.fs.wx.kf.vo.WeixinKfMsgItemVO;
import com.fs.wx.kf.vo.WeixinKfMsgVO;
import com.fs.wxUser.service.ICompanyWxUserService;
import com.google.code.kaptcha.Producer;
import com.qq.weixin.mp.aes.AesException;
import com.qq.weixin.mp.aes.WXBizMsgCrypt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.Element;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.logging.Logger;

import static com.fs.common.constant.FsConstants.REDIS_CHAT_NEXTCURSOR;


@Api("公共接口")
@Slf4j
@RestController
@RequestMapping(value="/app/common")
@AllArgsConstructor
public class CommonController {

    private final WebSocketServer webSocketServer;
    private final ICompanyWxUserService companyWxUserService;
    private final ICompanyWxChatService companyWxChatService;
    private final QwSopLogsMapper qwSopLogsMapper;
    private final RedisCache redisCache;
    private final JwtUtils jwtUtils;
    private final IBdReportDataService bdReportDataService;
    private final ICompanyWxClientService companyWxClientService;
//    private final WebSocketServer webSocketServer;
    private final CrmCustomerMapper crmCustomerMapper;
    private final ISmsService smsService;
    private final ICompanySmsTempService smsTempService;
    private final CompanyVoiceRoboticCalleesMapper companyVoiceRoboticCalleesMapper;
    private final CompanyWxClientServiceImpl companyWxClientServiceImpl;

    @GetMapping("/testSend")
    public R testSend( ) throws Exception
    {
        String json="{\"cmd\":\"sendMsg\",\"msg\":\"test\",\"nickName\":\"123\"}";
//        String json="{\"cmd\":\"listenMsg\",\"users\":[\"123\"]}";
        webSocketServer.sendInfo("2020", json);
        return R.ok();
    }

    @GetMapping("/testListen")
    public R testListen( ) throws Exception
    {
//        String json="{\"cmd\":\"sendMsg\",\"msg\":\"test\",\"nickName\":\"123\"}";
        String json="{\"cmd\":\"listenMsg\",\"users\":[\"123\"]}";
        webSocketServer.sendInfo("2020", json);
        return R.ok();
    }
    @GetMapping("/testClickHouse")
    public R testClickHouse( ) throws Exception{
        QwSopLogs qwSopLogs = qwSopLogsMapper.selectById("2c42bded-085b-452c-8166-7ed2b070a016");
        qwSopLogs.setSopId("2324dcea-0a4c-4688-b43d-2d9691ede6b4");
        qwSopLogsMapper.updateById(qwSopLogs);
        return R.ok();
    }

    @ApiOperation("获取发送信息")
    @GetMapping("/getMsgList")
    public R getMsg(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time){
        return R.ok().put("data", companyWxUserService.getMsg(1, time));
    }


    @ApiOperation("获取发送信息")
    @GetMapping("/syncReportData")
    public R syncReportData(){
        bdReportDataService.syncCreativeStatistics(LocalDate.now());
        return R.ok();
    }
    @ApiOperation("获取发送信息")
    @GetMapping("/getAddWx")
    public R getAddWx(Long userId, Long accountId){
        return R.ok().put("data", companyWxClientService.getAddWx(userId, accountId, 0));
    }
    @ApiOperation("获取发送信息")
    @GetMapping("/sms")
    public R sms(Long id){
        CompanyVoiceRoboticCallees callee = companyVoiceRoboticCalleesMapper.selectById(id);
        CrmCustomer crmCustomer = crmCustomerMapper.selectCrmCustomerById(callee.getUserId());
        SmsSendParam param = new SmsSendParam();
        param.setCustomerId(crmCustomer.getCustomerId());
        param.setTempCode("aiob-wok-link");
        param.setCompanyId(crmCustomer.getCompanyId());
        param.setCompanyUserId(crmCustomer.getCustomerUserId());
        param.setSmsType(2);
        param.setMobile(callee.getPhone());
        CompanySmsTemp temp=smsTempService.selectCompanySmsTempByCode(param.getTempCode());
        param.setContent(temp.getContent());
        CompanyWxClient one = companyWxClientServiceImpl.getOne(new QueryWrapper<CompanyWxClient>().eq("add_type", 2).eq("robotic_id", callee.getRoboticId()).eq("customer_id", callee.getUserId()));
        param.setWorkUrl(one.getWorkUrl());
        return smsService.sendSms(param);
    }

}
