package com.malk.service.ekuaibao.impl;

import cn.hutool.core.util.ObjectUtil;
import com.malk.server.ekuaibao.EKBConf;
import com.malk.server.ekuaibao.EKBR;
import com.malk.service.ekuaibao.EKBClient;
import com.malk.utils.UtilHttp;
import com.malk.utils.UtilMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class EKBClientImpl implements EKBClient {

    @Autowired
    private EKBConf ekbConf;

    /**
     * 根据单据编号获取单据详情
     */
    @Override
    public Map getFlowDetailsByCode(String accessToken, String code) {
        Map param = UtilMap.map("accessToken, code", accessToken, code);
        EKBR ekbr = (EKBR) UtilHttp.doGet(ekbConf.getPlatformUrl() + "api/openapi/v1.1/flowDetails/byCode", null, param, EKBR.class);
        return ekbr.getValue();
    }

    /**
     * 创建单据
     */
    @Override
    public EKBR createFormInstance(String accessToken, boolean isCommit, boolean isUpdate, Map form, Map params) {
        Map param = UtilMap.map("accessToken, isCommit, isUpdate", accessToken, isCommit, isUpdate);
        Map body = UtilMap.map("form, params", form, params);
        return (EKBR) UtilHttp.doPost(ekbConf.getPlatformUrl() + "api/openapi/v2.2/flow/data", null, param, body, EKBR.class);
    }

    /**
     * 查询员工
     */
    @Override
    public List<Map> getStaffInfo(String accessToken, String type, List conditionIds) {
        Map param = UtilMap.map("accessToken", accessToken);
        Map body = UtilMap.map("type, conditionIds", type, conditionIds);
        EKBR ekbr = (EKBR) UtilHttp.doPost(ekbConf.getPlatformUrl() + "api/openapi/v1/staffs/getStaffIds", null, param, body, EKBR.class);
        return ekbr.getItems();
    }

    /**
     * 获取当前版本单据模板列表
     */
    @Override
    public List<Map> getListTemplateId(String accessToken, String type, String specificationGroupId) {
        Map param = UtilMap.map("accessToken, type, specificationGroupId", accessToken, type, specificationGroupId);
        EKBR ekbr = (EKBR) UtilHttp.doGet(ekbConf.getPlatformUrl() + "api/openapi/v1/specifications/latestByType", null, param, EKBR.class);
        return ekbr.getItems();
    }

    /**
     * 根据模板ID获取模板信息
     */
    @Override
    public List<Map> getTemplateIdById(String accessToken, List<String> ids) {
        Map param = UtilMap.map("accessToken", accessToken);
        EKBR ekbr = (EKBR) UtilHttp.doGet(ekbConf.getPlatformUrl() + "api/openapi/v2/specifications/byIds/" + ids, null, param, EKBR.class);
        return ekbr.getItems();
    }

    /**
     * 获取自定义档案项(不带可见范围)
     */
    @Override
    public EKBR getDimensionsList(String accessToken, int start, int count, Map body_ext) {
        Map param = UtilMap.map("accessToken, start, count", accessToken, start, count);
        if (ObjectUtil.isNotNull(body_ext)) {
            param.putAll(body_ext);
        }
        return (EKBR) UtilHttp.doGet(ekbConf.getPlatformUrl() + "api/openapi/v1/dimensions/items", null, param, EKBR.class);
    }
}
