package cn.com.feishu.provider;

import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.feishu.constant.FeishuConstant;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class MessageProvider {
    public static JSONObject batchSend(JSONArray departmentIds, JSONArray openIds, JSONArray userIds, String content) throws Exception {
        String tenantAccessToken = AuthProvider.getTenantAccessToken();
        JSONObject contentJson = new JSONObject();
        contentJson.put("text",content);
        JSONObject params = new JSONObject();
        if(null!=departmentIds&&!departmentIds.isEmpty()){
            params.put("department_ids",departmentIds);
        }
        if(null!=openIds&&!openIds.isEmpty()){
            params.put("open_ids",openIds);
        }
        if(null!=userIds&&!userIds.isEmpty()){
            params.put("user_ids",userIds);
        }
        params.put("msg_type","text");
        params.put("content",contentJson);
        return HttpUtils.feishuPostRequest("Bearer "+tenantAccessToken, FeishuConstant.FEISHU_API_URL+FeishuConstant.MESSAGE_BATCH_SEND,params);
    }

    public static void main(String[] args) {
        JSONArray openIds = new JSONArray();
        openIds.add("ou_e18db8d339cc33fe1446ff7bd774c074");
        openIds.add("ou_08ec7422306fa2bfd1baae9b2f724cb4");
        try {
            StringBuffer text = new StringBuffer();
            text.append("项目出价预警").append("\n\r").append("您的项目：").append( "测试项目，").append("")
                    .append("授权名称为：伊对 下的，").append("").append("")
                    .append("大于项目设置的最高出价：").append(10000 / 1000).append(" 元！ 请您及时调整。");
            batchSend(null,openIds,null,text.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
