package com.dgtly.wxportal.service;


import com.dgtly.wxportal.domain.WxSendMessage;
import com.dgtly.wxportal.utils.BatchSendGetToUser;
import com.dgtly.wxportal.utils.BatchSendGetSendMessage;
import com.dgtly.wxportal.utils.qywxutils.QyWxSendMessageUtil;
import com.dgtly.wxportal.utils.qywxutils.messageModel.QyWxBaseMessage;
import com.dgtly.wxportal.utils.qywxutils.messageModel.QyWxTextMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class BatchSendCustomerCreditMessage<T,V> implements BatchSendQyWxMessage {
    private static final Logger log = LoggerFactory.getLogger(BatchSendCustomerCreditMessage.class);

    //组织代码set 遍历的主键
    private Set<T> orgCodeSet;
    //  获取toUser字段的类
    private BatchSendGetToUser<T> getToUser;
    //  获取toText字段的类
    private BatchSendGetSendMessage getSendMessage;

    public BatchSendCustomerCreditMessage (Set<T> orgCodeSet, BatchSendGetToUser<T> getToUser,
                                           BatchSendGetSendMessage getSendMessage) {
        this.orgCodeSet = orgCodeSet;
        this.getToUser = getToUser;
        this.getSendMessage = getSendMessage;
    }

    @Override
    public List<QyWxBaseMessage> getMessageList() {
        List<QyWxBaseMessage> res = new ArrayList<>();
        for (T code : orgCodeSet) {
            try{
                String toUser = getToUser.getToUser(code);

                if (toUser == null || toUser.trim().equals("")) {
                    log.error("code为：" + code + "发送消息失败，为查询到toUser的存在");
                    continue;
                }
                //发送的文字
                String text = getSendMessage.getSendText(code);
                QyWxTextMessage qyWxTextMessage = new QyWxTextMessage();
                qyWxTextMessage.setTouser(toUser);
                qyWxTextMessage.setText(text);
                res.add(qyWxTextMessage);
            }catch (Exception e){
                log.error("获取待发送信息发生异常",e);
            }
        }
        return res;
    }

}
