package com.malk.service.h3yun.impl;

import com.malk.server.common.FilePath;
import com.malk.server.dingtalk.DDConf;
import com.malk.server.dingtalk.DDR_New;
import com.malk.service.h3yun.CYClient;
import com.malk.utils.UtilFile;
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.io.File;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class CYImplClient implements CYClient {

    /// 请求路径
    private String getRequestUrl(String path) {
        return "https://api.dingtalk.com/v1.0/h3yun" + path;
    }

    /**
     * 获取附件临时免登地址
     *
     * @apiNote https://open.dingtalk.com/document/orgapp/obtain-the-temporary-attachment-free-address
     */
    @Override
    public String getTemporaryUrls(String accessToken, String attachmentId) {
        Map param = UtilMap.map("attachmentId", attachmentId);
        DDR_New cyr = DDR_New.doGet(getRequestUrl("/attachments/temporaryUrls"), DDConf.initTokenHeader(accessToken), param);
        return UtilMap.getString(((Map) cyr.getData()), "attachmentUrl");
    }
    @Override
    public Map updatetForm(String accessToken,String schemaCode, String bizObjectId, String bizObjectJson) {
        Map param = UtilMap.map("schemaCode, bizObjectId, bizObjectJson", schemaCode,bizObjectId, bizObjectJson);
        DDR_New cyr = DDR_New.doPut(getRequestUrl("/forms/instances"), DDConf.initTokenHeader(accessToken),null, param);
        return UtilMap.map("code, message", cyr.getCode(), cyr.getMessage());
    }
    @Autowired
    private FilePath filePath;

    /**
     * 储存氚云附件到本地, 保留文件名称与格式
     */
    @Override
    public File saveFromTemporaryUrl(String accessToken, Map attachment) {
        String fileUrl = getTemporaryUrls(accessToken, UtilMap.getString(attachment, "FileId"));
        String fileName = UtilMap.getString(attachment, "FileName");
        File file = UtilFile.mkdirIfNot(fileName, filePath.getPath().getFile());
        UtilHttp.doDownload(fileUrl, file);
        return file;
    }

    /**
     * 查询表单数据
     */
    @Override
    public List<Map> queryData(String accessToken, String schemaCode, Map extInfo) {

        extInfo = UtilMap.empty(extInfo);
        UtilMap.put(extInfo, "pageSize", 500);
        UtilMap.put(extInfo, "pageNumber", 1);
        extInfo.put("schemaCode", schemaCode);
        DDR_New cyr = DDR_New.doPost(getRequestUrl("/forms/instances/search"), DDConf.initTokenHeader(accessToken), null, extInfo);
        return (List<Map>) ((Map) cyr.getData()).get("bizObjects");
    }

    /**
     * 创建流程数据
     */
    @Override
    public Map operateData(String accessToken, String schemaCode, Map extInfo) {

        Map body = UtilMap.map("schemaCode", schemaCode);

        String path = "/forms/instances";
        if (extInfo.containsKey("bizObjectId")) {
            path = "/processes/instances";
        }
        UtilMap.putAll(body, extInfo);
        DDR_New cyr = DDR_New.doPost(getRequestUrl(path), DDConf.initTokenHeader(accessToken), null, body);
        return (Map) cyr.getData();
    }

    /**
     * 获取用户数据
     *
     * @apiNote https://open.dingtalk.com/document/orgapp/obtain-user-data
     */
    @Override
    public List<Map> getUserInfoList(String accessToken, String departmentId, boolean isRecursive) {
        Map param = UtilMap.map("departmentId, isRecursive", departmentId, isRecursive);
        DDR_New cyr = DDR_New.doGet(getRequestUrl("/users"), DDConf.initTokenHeader(accessToken), param);
        return (List<Map>) cyr.getData();
    }

    /**
     * 获取组织数据
     */
    @Override
    public List<Map> getDepartmentInfoList(String accessToken, String departmentId) {
        Map param = UtilMap.putNotEmpty(UtilMap.empty(), "departmentId", departmentId);
        DDR_New cyr = DDR_New.doGet(getRequestUrl("/departments"), DDConf.initTokenHeader(accessToken), param);
        return (List<Map>) cyr.getData();
    }
}
