package com.ruoyi.yunxin;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.yunxin.client.ImMessageClient;
import com.ruoyi.yunxin.config.YunxinProperties;
import com.ruoyi.yunxin.req.Option;
import com.ruoyi.yunxin.req.SendBatchMsgReq;
import com.ruoyi.yunxin.req.SendMsgReq;
import com.ruoyi.yunxin.req.type.YxTextData;
import com.ruoyi.yunxin.resp.SendMsgResp;
import com.ruoyi.yunxin.resp.YxCommonR;
import com.ruoyi.yunxin.resp.YxDataR;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class Yunxin {

    @Autowired
    private YunxinProperties yunxinProperties;
    @Resource
    private ImMessageClient messageClient;

    @Deprecated
    public YxDataR<SendMsgResp> sendTo(Long toUid,Long fromUid,Object data){
        SendMsgReq req = new SendMsgReq();
        req.setFrom(fromUid == null ? yunxinProperties.getDefaultFromUid() : fromUid+"");
        req.setTo(toUid+"");
        req.setBody(JSON.toJSONString(data));
        req.setOption(JSON.toJSONString(new Option()));
        return messageClient.sendMsg(req);
    }


    public YxDataR<YxCommonR> batchSendToTextMessage(Long fromUid, List<Long> toUid, String data){
        SendBatchMsgReq req = new SendBatchMsgReq();
        req.setFromAccid(fromUid+"");
        req.setToAccids(toUid.stream().map(i -> String.valueOf(toUid)).collect(Collectors.toList()));
        req.setBody(JSON.toJSONString(new YxTextData(data)));
        return messageClient.sendBatchMsg(req);
    }

}
