package com.malk.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.malk.server.common.McR;
import com.malk.server.dingtalk.DDConf;
import com.malk.server.dingtalk.crypto.DingCallbackCrypto;
import com.malk.service.dingtalk.DDClient_Event;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 钉钉事件回调 3_1
 * -
 * [子项目直接继承即可有调用, 无需实现]
 * -
 * 注解 @RequestMapping 路径不能重复 [主子项目属同一个项目];
 * 获取项目回调请求地址, https://mc.cloudpure.cn/frp/mc/dd/callback [调试代理: frp + nginx]
 */
@Slf4j
@RestController
@RequestMapping("/mc/dd")
public class DDCallbackController {

    @Autowired
    private DDConf ddConf;

    @Autowired
    private DDClient_Event ddClient_event;

    /**
     * 钉钉审批回调: [依赖包方案已弃用]
     * -
     * DingCallbackCrypto 方案: 官网案例 DingCallbackCrypto 不在钉钉架包, 需要单独引用
     * 在钉钉开放平台重新保存回调地址后, 所有的注册事件会被关闭:: 通过代码注册不成功; 官方回复, 要么使用调用注册的方式 要么是后台的方式, 二选一
     */
    @SneakyThrows
    @RequestMapping(value = "/callback", method = RequestMethod.POST)
    public Map<String, String> invokeCallback(@RequestParam(value = "signature", required = false) String signature,
                                              @RequestParam(value = "timestamp", required = false) String timestamp,
                                              @RequestParam(value = "nonce", required = false) String nonce,
                                              @RequestBody(required = false) JSONObject json) {

        DingCallbackCrypto callbackCrypto = new DingCallbackCrypto(ddConf.getToken(), ddConf.getAesKey(), ddConf.getAppKey());
        final String decryptMsg = callbackCrypto.getDecryptMsg(signature, timestamp, nonce, json.getString("encrypt"));
        JSONObject eventJson = JSON.parseObject(decryptMsg);
        Map success = callbackCrypto.getEncryptedMap(DDConf.CALLBACK_RESPONSE, System.currentTimeMillis(), DingCallbackCrypto.Utils.getRandomStr(8));
        String eventType = eventJson.getString("EventType");
        if (DDConf.CALLBACK_CHECK.equals(eventType)) {
            log.info("----- [DD]验证注册 -----");
            return success;
        }
        // [回调任务执行逻辑: 异步] 钉钉超时3s未返回会被记录为失败, 可通过失败接口获取记录
        if (Arrays.asList(DDConf.BPMS_INSTANCE_CHANGE, DDConf.BPMS_TASK_CHANGE).contains(eventType)) {
            log.info("[DD]审批回调, {}", eventJson);
            ddClient_event.callBackEvent_Workflow(eventJson);
            return success;
        }

        if (Arrays.asList(DDConf.ATTENDANCE_CHECK_RECORD).contains(eventType)) {
            log.info("[DD]考勤回调, {}", eventJson);
            ddClient_event.callBackEvent_Attendance(eventJson);
            return success;
        }

        log.info("----- [DD]已注册, 未处理的其它回调 -----, {}", eventJson);
        return success;
    }

    @PostMapping("robot")
    McR robot(@RequestBody Map data) {
        log.info("xxx, {}", data);
        return McR.success();
    }
}
