package com.fs.app.controller;


import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.course.mapper.FsCourseWatchLogMapper;
import com.fs.course.mapper.FsUserCourseVideoMapper;
import com.fs.fastGpt.mapper.FastgptChatVoiceHomoMapper;
import com.fs.his.domain.FsAppVersion;
import com.fs.his.service.IFsAppVersionService;
import com.fs.qw.domain.QwUser;
import com.fs.qw.mapper.QwCompanyMapper;
import com.fs.qw.mapper.QwExternalContactCrmMapper;
import com.fs.qw.mapper.QwExternalContactMapper;
import com.fs.qw.mapper.QwUserMapper;
import com.fs.qw.param.QwConfigSignatureParam;
import com.fs.qw.service.IQwJsApiService;
import com.fs.qw.service.IQwUserService;
import com.fs.qw.service.IQwUserVideoService;
import com.fs.qw.vo.QwHookAuthVO;
import com.fs.qwApi.param.QwExternalContactHParam;
import com.fs.qwApi.service.QwApiService;
import com.fs.qwHookApi.param.QwHookSendMsgParam;
import com.fs.qwHookApi.service.QwHookApiService;
import com.fs.sop.mapper.QwSopLogsMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


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

    @Autowired
    private QwHookApiService qwHookApiService;

    @Autowired
    private IQwUserService qwUserService;

    @Autowired
    QwApiService qwApiService;
    @Autowired
    QwCompanyMapper qwCompanyMapper;

    @Autowired
    FsCourseWatchLogMapper fsCourseWatchLogMapper;
    @Autowired
    FsUserCourseVideoMapper fsUserCourseVideoMapper;
    @Autowired
    FsCourseWatchLogMapper   watchLogMapper;
    @Autowired
    IQwJsApiService qwGetJsapiTicketService;

    @Autowired
    QwUserMapper qwUserMapper;
    @Autowired
    FastgptChatVoiceHomoMapper fastgptChatVoiceHomoMapper;

    @Autowired
    QwExternalContactCrmMapper qwExternalContactCrmMapper;
    @Autowired
    private IFsAppVersionService appVersionService;

    @Autowired
    RedisCache redisCache;

    @Autowired
    private QwExternalContactMapper qwExternalContactMapper;

    @Autowired
    private IQwUserVideoService qwUserVideoService;



    @Autowired
    private QwSopLogsMapper qwSopLogsMapper;


    @PostMapping("/qwHookSendMsg")
    public R qwHookSendMsg(@RequestBody QwHookSendMsgParam param ) {
        param.setClientId(2);
        return qwHookApiService.sendMsg(param);
    }

    @GetMapping("/qwHookAuth")
    public R qwHookAuth(@RequestParam(value = "key", required = false) String key) {
        QwHookAuthVO qwHookAuthVO = qwUserService.selectQwUserByAppKeyAuth(key);
        if(qwHookAuthVO!=null){
            return R.ok().put("data",qwHookAuthVO);
        }
        else {
            return R.error("查询到相关成员信息");
        }
    }

    @GetMapping("/qwHookCheck")
    public R qwHookCheckCorpId(@RequestParam(value = "key", required = false) String key,
                               @RequestParam(value = "qwHookId", required = false) String qwHookId,
                               @RequestParam(value = "corpId", required = false) String corpId) {
        QwUser user=qwUserService.selectQwUserByAppKey(key);
        if(user.getCorpId().equals(corpId)){
            if(user.getQwHookId().equals(qwHookId)){
                return R.ok();
            }
            else{
                return R.error("此帐号绑定的企业微信未授权");
            }
        }
        else{
           return R.error("此帐号绑定的企业微信未授权");
        }
    }

    //获取企业微信签名
    @PostMapping("/getConfigSignature")
    public R getConfigSignature(@RequestBody QwConfigSignatureParam qwConfigSignatureParam) throws Exception {
        return qwGetJsapiTicketService.getQwJsapiTicket(qwConfigSignatureParam);
    }

    //根据userid和外部联系人id获取到客户详情
    @PostMapping("/getQwExternalContactDetails")
    public R getQwExternalContactDetails(@RequestBody QwExternalContactHParam param){
        return qwGetJsapiTicketService.getQwExternalContactDetails(param);
    }

    @ApiOperation("获取最新版本")
    @GetMapping("/getNewAppVersion")
    public R getNewAppVersion(@RequestParam("appType")Integer appType)
    {
        FsAppVersion version=appVersionService.getNewVersion(appType,3);
        return R.ok().put("data",version);
    }

}
