package com.malk.hake.controller;

import com.alibaba.fastjson.JSON;
import com.malk.hake.service.HKClient_ML;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.server.common.McException;
import com.malk.server.common.McR;
import com.malk.server.common.McREnum;
import com.malk.service.aliwork.YDClient;
import com.malk.utils.UtilMap;
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("ml")
public class HKController_ML {

    @Autowired
    private HKClient_ML hkClient;

    @Autowired
    private YDClient ydClient;

    /**
     * 通用流程发起
     */
    @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));
        // 拒绝审批意见必填校验 prd 4.17 取消拒绝校验, 统一返回 [审核人xxx拒绝], monitor通过活动跳转
        //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));
        if (!"Y".equals(data.get("Status"))) { // 询价\报价
            hkClient.callbackProcess(data);
        }
        return McR.success();
    }

    /**
     * 报价、询价审批, 添加标志位判定
     */
    @PostMapping("process/update")
    McR updateProcess(@RequestBody Map data) {

        McException.assertParamException_Null(data, "ddProcessId", "ddStatus");
        log.info("同步标志, {}", JSON.toJSONString(data));

        ydClient.operateData(YDParam.builder()
                .formInstanceId(UtilMap.getString(data, "ddProcessId"))
                .updateFormDataJson(JSON.toJSONString(UtilMap.map("radioField_lzkqlkgh", data.get("ddStatus"))))
                .build(), YDConf.FORM_OPERATION.update);
        return McR.success();
    }

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

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

    /**
     * 手动同步付款数据
     */
    @PostMapping("monitor/sync")
    McR pay(HttpServletRequest request) {

        Map data = UtilServlet.getParamMap(request);
        log.info("手动触发, 拉取Monitor同步, {}", data);
        hkClient.syncMonitor(UtilMap.getString(data, "code"), data);
        return McR.success();
    }
}
