package cn.com.ctop.common.module.utils;

import cn.com.ctop.common.module.entity.WexinConfig;
import cn.com.ctop.common.module.service.IWexinConfigService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CorpWexinUtils {
    @Autowired
    private IWexinConfigService wexinConfigService;
    private static Map<String, WexinConfig> configMap = new HashMap<>();

    private Map<String, WexinConfig> getConfigMap() {
        QueryWrapper<WexinConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        List<WexinConfig> list = wexinConfigService.list(queryWrapper);
        for (WexinConfig config : list) {
            WxCpService wxCpService = new WxCpServiceImpl();
            val configStorage = new WxCpDefaultConfigImpl();
            configStorage.setCorpId(config.getCorpId());
            configStorage.setAgentId(config.getAgentId());
            configStorage.setCorpSecret(config.getCorpSecret());
            configStorage.setToken(config.getToken());
            configStorage.setAesKey(config.getAesKey());
            wxCpService.setWxCpConfigStorage(configStorage);
            config.setWxCpService(wxCpService);
            configMap.put(config.getCorpId(), config);
        }
        return configMap;
    }

    public void sendMessage(List<JSONObject> wxUserList, String content) {
        if (Check.isNullMap(configMap)) {
            getConfigMap();
        }
        if (wxUserList != null && wxUserList.size() > 0) {
            for (JSONObject userInfo : wxUserList) {
                WxCpMessage message = new WxCpMessage();
                message.setMsgType("text");
                message.setContent(content);
                message.setAgentId(1000002);
                message.setToUser(userInfo.getString("wexin_id"));
                try {
                    WexinConfig config = configMap.get(userInfo.getString("corp_id"));
                    WxCpService wxCpService = config.getWxCpService();
                    wxCpService.messageSend(message);
                } catch (WxErrorException e) {
                    log.error("发送信息失败，userId:{}", userInfo.getString("wexin_id"));
                    e.printStackTrace();
                }
            }
        }
    }
    public void sendMessageByWeChatId(JSONObject weChatInfo, String content) {
        if (Check.isNullMap(configMap)) {
            this.getConfigMap();
        }
        if (!Check.isNull(weChatInfo)) {
            WxCpMessage message = new WxCpMessage();
            message.setMsgType("text");
            message.setContent(content);
            message.setAgentId(1000002);
            message.setToUser(weChatInfo.getString("wexin_id"));
            try {
                WexinConfig config = configMap.get(weChatInfo.getString("corp_id"));
                WxCpService wxCpService = config.getWxCpService();
                wxCpService.messageSend(message);
            } catch (WxErrorException e) {
                log.error("发送信息失败，userId:{}", weChatInfo.getString("wexin_id"));
                e.printStackTrace();
            }
        }
    }
}
