package com.malk.service.dingtalk.impl;

import com.malk.server.dingtalk.DDConf;
import com.malk.server.dingtalk.DDR;
import com.malk.service.dingtalk.DDClient_Notice;
import com.malk.utils.UtilList;
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;

@Service
@Slf4j
public class DDImplClient_Notice implements DDClient_Notice {

    @Autowired
    private DDConf ddConf;

    /**
     * 发送工作通知
     */
    @Override
    public String sendNotification(String access_token, List<String> userid_list, List<String> dept_id_list, boolean to_all_user, Map msg) {
        Map body = UtilMap.map("agent_id, to_all_user, msg", ddConf.getAgentId(), to_all_user, msg);
        if (UtilList.isNotEmpty(userid_list)) {
            body.put("userid_list", String.join(",", userid_list));
        }
        if (UtilList.isNotEmpty(dept_id_list)) {
            body.put("dept_id_list", String.join(",", dept_id_list));
        }
        DDR ddr = DDR.doPost("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2", null, DDConf.initTokenParams(access_token), body);
        return ddr.getTask_id();
    }
}
