package com.malk.service.dingtalk.impl;

import cn.hutool.core.util.ObjectUtil;
import com.malk.server.dingtalk.DDConf;
import com.malk.server.dingtalk.DDR_New;
import com.malk.service.dingtalk.DDClient_Dedicated;
import com.malk.utils.UtilMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class DDImplClient_Dedicated implements DDClient_Dedicated {

    /**
     * 获取文件操作记录
     *
     * @apiNote https://open.dingtalk.com/document/orgapp/obtain-file-operation-records
     */
    @Override
    public List<Map> obtainFileOperationRecords(String accessToken, @NotNull long startDate, @NotNull long endDate, @NotNull int pageSize, Map preLastRecord) {

        Map params = UtilMap.map("startDate, endDate, pageSize", startDate, endDate, pageSize);
        if (ObjectUtil.isNotNull(preLastRecord)) {
            params.put("nextGmtCreate", preLastRecord.get("gmtCreate"));
            params.put("nextBizId", preLastRecord.get("bizId"));
        }
        DDR_New ddr_new = DDR_New.doGet("https://api.dingtalk.com/v1.0/exclusive/fileAuditLogs", DDConf.initTokenHeader(accessToken), params);
        return ddr_new.getList();
    }

    /**
     * 启用企业帐号
     *
     * @apiNote https://open.dingtalk.com/document/orgapp/enable-a-dedicated-account
     */
    @Override
    public boolean orgAccountsEnable(String accessToken, String userId) {
        DDR_New ddr_new = DDR_New.doPost("https://api.dingtalk.com/v1.0/contact/orgAccounts/enable", DDConf.initTokenHeader(accessToken), null, UtilMap.map("userId", userId));
        return (Boolean) ddr_new.getResult();
    }

    /**
     * 停用企业帐号
     *
     * @apiNote https://open.dingtalk.com/document/orgapp/disable-an-exclusive-account
     */
    @Override
    public boolean orgAccountsDisable(String accessToken, String userId) {
        DDR_New ddr_new = DDR_New.doPost("https://api.dingtalk.com/v1.0/contact/orgAccounts/disable", DDConf.initTokenHeader(accessToken), null, UtilMap.map("userId", userId));
        return (Boolean) ddr_new.getResult();
    }

}
