package com.malk.service.xbongbong.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.malk.server.common.VenR;
import com.malk.server.xbongbong.DigestUtil;
import com.malk.server.xbongbong.XBBConf;
import com.malk.server.xbongbong.XBBR;
import com.malk.service.xbongbong.XBBClient;
import com.malk.utils.UtilMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class XBBImplClient implements XBBClient {

    @Autowired
    private XBBConf xbbConf;

    // 获取签名 [字符]
    public static String getDataSign(Map data, String token) {
        return getDataSign(JSON.toJSONString(data), token);
    }

    // 获取签名 [字典]
    public static String getDataSign(String data, String token) {
        return DigestUtil.Encrypt(data + token, "SHA-256");
    }

    // 获取请求签名: string/map
    public Map getHeaderSign(Object param) {
        String sign = null;
        if (param instanceof String) {
            sign = getDataSign((String) param, xbbConf.getToken());
        } else {
            sign = getDataSign((Map) param, xbbConf.getToken());
        }
        Map header = UtilMap.map("sign", sign);
        return header;
    }

    /**
     * 表单模板列表接口
     *
     * @apiNote https://profapi.xbongbong.com/#/apilist/177
     */
    @Override
    public List<Map> getFormList(String name, @NotNull int saasMark, int businessType) {
        Map body = UtilMap.map("name, saasMark, businessType, corpid, userId", name, saasMark, businessType, xbbConf.getCorpid(), xbbConf.getUserId());
        XBBR xbbr = (XBBR) XBBR.doPost("https://proapi.xbongbong.com/pro/v2/api/form/list", getHeaderSign(body), null, body, VenR.RC_XBB);
        Map<String, List<Map>> rsp = (Map<String, List<Map>>) xbbr.getResult();
        return rsp.get("formList");
    }

    /**
     * 表单模板列表接口
     *
     * @apiNote https://profapi.xbongbong.com/#/apilist/178
     */
    @Override
    public List<Map> getFormDefine(long formId, int subBusinessType) {
        Map body = UtilMap.map("formId, subBusinessType, corpid, userId", formId, subBusinessType, xbbConf.getCorpid(), xbbConf.getUserId());
        XBBR xbbr = (XBBR) XBBR.doPost("https://proapi.xbongbong.com/pro/v2/api/form/get", getHeaderSign(body), null, body, VenR.RC_XBB);
        Map<String, List<Map>> rsp = (Map<String, List<Map>>) xbbr.getResult();
        return rsp.get("explainList");
    }

    /**
     * 表单列表接口
     *
     * @apiNote https://profapi.xbongbong.com/#/apilist/181
     */
    @Override
    public Map getDataResult(String url, @NotNull long formId, List<Map> conditions, Map extInfo) {
        Map body = UtilMap.map("formId, conditions, corpid, userId", formId, conditions, xbbConf.getCorpid(), xbbConf.getUserId());
        if (ObjectUtil.isNotNull(extInfo)) {
            body.putAll(extInfo);
        } else {
            body.put("pageSize", 100);
        }
        XBBR xbbr = (XBBR) XBBR.doPost(url, getHeaderSign(body), null, body, VenR.RC_XBB);
        return (Map) xbbr.getResult();
    }

    /**
     * 表单列表接口 [返回list]
     */
    @Override
    public List<Map> getDataList(String url, @NotNull long formId, List<Map> conditions, Map extInfo) {
        return (List<Map>) getDataResult(url, formId, conditions, extInfo).get("list");
    }

    /// 获取表单以及表单定义, 查询与数据处理, 获取后可在程序内固定, 避免无效调用
    @Override
    public List<Map> testDefine(String name, @NotNull int saasMark, int businessType) {
        List<Map> rsp = this.getFormList(name, saasMark, businessType);
        long formId = UtilMap.getLong(rsp.get(0), "formId");
        return this.getFormDefine(formId, businessType); // 表单定义
    }
}
