package com.yida.data.system.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.system.EduQywxServiceProviderSchool;
import com.yida.data.system.service.EduQywxServiceProviderSchoolService;
import com.yida.data.system.service.QywxCallbackService;
import com.yida.data.system.service.QywxServiceProviderService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "企业微信同步")
@RestController
@RequiredArgsConstructor
@RequestMapping("/in/qywx")
public class InQywxController {

    private final QywxCallbackService qywxCallbackService;
    private final QywxServiceProviderService qywxServiceProviderService;
    private final EduQywxServiceProviderSchoolService eduQywxServiceProviderSchoolService;

    @GetMapping("/callback/{corpId}")
    public String get(@PathVariable String corpId,
                      @RequestParam("msg_signature") String msgSignature,
                      @RequestParam("timestamp") String timestamp,
                      @RequestParam("nonce") String nonce,
                      @RequestParam("echostr") String echostr
    ) {
        return qywxCallbackService.get(corpId, msgSignature, timestamp, nonce, echostr);
    }

    @PostMapping("/callback/{corpId}")
    public String post(@PathVariable String corpId,
                       @RequestParam("msg_signature") String msgSignature,
                       @RequestParam("timestamp") String timestamp,
                       @RequestParam("nonce") String nonce,
                       @RequestBody String body) {
        return qywxCallbackService.post(corpId, msgSignature, timestamp, nonce, body);
    }

    /**
     * 服务商代开发应用模板验证URL回调
     */
    @GetMapping("/normal/callback/{corpId}")
    public String getNormal(@PathVariable String corpId,
                            @RequestParam("msg_signature") String msgSignature,
                            @RequestParam("timestamp") String timestamp,
                            @RequestParam("nonce") String nonce,
                            @RequestParam("echostr") String echostr) {
        return qywxCallbackService.getNormal(corpId, msgSignature, timestamp, nonce, echostr);
    }

    /**
     * 服务商代开发应用模板接口回调
     */
    @PostMapping("/normal/callback/{corpId}")
    public String postNormal(@PathVariable String corpId,
                             @RequestParam("msg_signature") String msgSignature,
                             @RequestParam("timestamp") String timestamp,
                             @RequestParam("nonce") String nonce,
                             @RequestBody String body) {
        return qywxCallbackService.postNormal(corpId, msgSignature, timestamp, nonce, body);
    }

    /**
     * 服务商代开发应用客户验证URL回调
     */
    @GetMapping("/normal/customer/callback/{providerCorpId}/{corpId}")
    public String getNormalCustomer(@PathVariable String providerCorpId,
                                    @PathVariable String corpId,
                                    @RequestParam("msg_signature") String msgSignature,
                                    @RequestParam("timestamp") String timestamp,
                                    @RequestParam("nonce") String nonce,
                                    @RequestParam("echostr") String echostr) {
        return qywxCallbackService.getNormalCustomer(providerCorpId, corpId, msgSignature, timestamp, nonce, echostr);
    }

    /**
     * 服务商代开发应用客户接口回调
     * 此回调暂不做任何处理
     */
    @PostMapping("/normal/customer/callback/{providerCorpId}/{corpId}")
    public String postNormalCustomer(
            @PathVariable String providerCorpId,
            @PathVariable String corpId,
            @RequestParam("msg_signature") String msgSignature,
            @RequestParam("timestamp") String timestamp,
            @RequestParam("nonce") String nonce,
            @RequestBody String body) {
        return qywxCallbackService.postNormalCustomer(providerCorpId, corpId, msgSignature, timestamp, nonce, body);
    }

    /**
     * 服务商代开发应用验证URL回调
     */
    @GetMapping("/event/callback/{providerCorpId}/{corpId}")
    public String getEvent(@PathVariable String providerCorpId,
                           @PathVariable String corpId,
                           @RequestParam("msg_signature") String msgSignature,
                           @RequestParam("timestamp") String timestamp,
                           @RequestParam("nonce") String nonce,
                           @RequestParam("echostr") String echostr) {
        return qywxCallbackService.getEvent(providerCorpId, corpId, msgSignature, timestamp, nonce, echostr);
    }

    /**
     * 服务商代开发应用回调
     */
    @PostMapping("/event/callback/{providerCorpId}/{corpId}")
    public String postEvent(
            @PathVariable String providerCorpId,
            @PathVariable String corpId,
            @RequestParam("msg_signature") String msgSignature,
            @RequestParam("timestamp") String timestamp,
            @RequestParam("nonce") String nonce,
            @RequestBody String body) {
        return qywxCallbackService.postEvent(providerCorpId, corpId, msgSignature, timestamp, nonce, body);
    }

    /**
     * 加密对应服务商的学校corpId
     */
    @GetMapping("/changeCorpId/{providerCorpId}/{corpId}")
    public ResultBean<String> changeCorpId(@PathVariable String providerCorpId, @PathVariable String corpId) {
        qywxServiceProviderService.changeCorpId(providerCorpId, corpId);
        return ResultBean.buildSuccess();
    }

    /**
     * 服务商对应学校加密corpId数据(原始值为key)
     */
    @GetMapping("/getCorpByOriginalData")
    public ResultBean<EduQywxServiceProviderSchool> getCorpByOriginalData(@RequestParam String providerCorpId,
                                                                          @RequestParam String deptOriginalCorpId) {
        EduQywxServiceProviderSchool providerSchool = eduQywxServiceProviderSchoolService.getOne(Wrappers.lambdaQuery(new EduQywxServiceProviderSchool())
                .eq(EduQywxServiceProviderSchool::getProviderCorpId, providerCorpId)
                .eq(EduQywxServiceProviderSchool::getDeptOriginalCorpId, deptOriginalCorpId));
        return ResultBean.buildSuccess(providerSchool);
    }

    /**
     * 服务商对应学校加密corpId数据(加密值为key)
     */
    @GetMapping("/getCorpByEncryptionData")
    public ResultBean<EduQywxServiceProviderSchool> getCorpByEncryptionData(@RequestParam String providerCorpId,
                                                                            @RequestParam String deptEncryptionCorpId) {
        EduQywxServiceProviderSchool providerSchool = eduQywxServiceProviderSchoolService.getOne(Wrappers.lambdaQuery(new EduQywxServiceProviderSchool())
                .eq(EduQywxServiceProviderSchool::getProviderCorpId, providerCorpId)
                .eq(EduQywxServiceProviderSchool::getDeptEncryptionCorpId, deptEncryptionCorpId));
        return ResultBean.buildSuccess(providerSchool);
    }

}
