package com.malk.hake.controller;

import com.alibaba.fastjson.JSON;
import com.malk.hake.service.HKClient;
import com.malk.server.common.McException;
import com.malk.server.common.McR;
import com.malk.server.common.McREnum;
import com.malk.utils.UtilServlet;
import lombok.SneakyThrows;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 错误抛出与拦截详见 CatchException
 */
@Slf4j
@RestController
@RequestMapping
public class HKController {

    @Autowired
    private HKClient hkClient;

    /**
     * 通用流程发起
     */
    @SneakyThrows
    @PostMapping("process/start")
    McR startProcess(@RequestBody Map data, HttpServletRequest request, @RequestParam String code) {

        Map header = UtilServlet.getHeaders(request);
        log.info("流程发起, {}, {}, {}", code, data, header);
        McException.assertException(!"dinge61fe69900ea236b35c2f4657eb6378f".equals(header.get("authorization")), McREnum.NOT_AUTHORIZED);
        Map result = hkClient.startProcess(code, data);
        return McR.success(result);
    }

    /**
     * 通用审批校验 [区分类型, 宜搭提示不同报错信息]
     */
    @PostMapping("process/validate")
    McR validateProcess(HttpServletRequest request) {

        Map data = UtilServlet.getParamMap(request);
        log.info("审批校验, {}", JSON.toJSONString(data));
        // 拒绝审批意见必填校验
        McException.assertParamException_Null(data, "Remark");
        return McR.success();
    }

    /**
     * 通用审批回调 [区分类型, 宜搭提示不同报错信息]
     */
    @PostMapping("process/callback")
    McR callbackProcess(HttpServletRequest request) {

        Map data = UtilServlet.getParamMap(request);
        log.info("审批回调, {}", JSON.toJSONString(data));
        hkClient.callbackProcess(data);
        return McR.success();
    }

    /**
     * 通讯录同步
     */
    @Synchronized
    @PostMapping("contact/sync")
    McR syncContact() {

        log.info("手动触发, 通讯录同步");
        hkClient.syncContact();
        return McR.success();
    }

    @PostMapping("monitor/sync")
    McR pay() {

        log.info("手动触发, 拉取Monitor同步");
        hkClient.syncMonitor("FKSQ");
        return McR.success();
    }
}
