package cn.momia.service.im.push.impl;

import cn.momia.common.webapp.config.Configuration;
import cn.momia.service.im.push.PushMsg;
import cn.momia.service.im.rongcloud.RongCloudUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class PushServiceRongCloudImpl extends AbstractPushService {
    private static final Logger LOGGER = LoggerFactory.getLogger(PushServiceRongCloudImpl.class);

    @Override
    protected boolean doPush(Collection<Long> userIds, PushMsg msg) {
        boolean successful = true;

        List<Long> subUserIds = new ArrayList<Long>();
        for (long userId : userIds) {
            if (subUserIds.size() < 1000) {
                subUserIds.add(userId);
            } else {
                try {
                    HttpPost httpPost = RongCloudUtil.createHttpPost(Configuration.getString("Im.RongCloud.Service.SystemPush"));

                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("fromUserId", String.valueOf(SYSTEM_PUSH_USERID)));
                    for (long subUserId : subUserIds) {
                        params.add(new BasicNameValuePair("toUserId", String.valueOf(subUserId)));
                    }
                    params.add(new BasicNameValuePair("objectName", "RC:TxtMsg"));
                    params.add(new BasicNameValuePair("content", msg.toString()));
                    HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
                    httpPost.setEntity(entity);

                    JSONObject responseJson = RongCloudUtil.executeRequest(httpPost);
                    if (responseJson.getInteger("code") != 200) {
                        LOGGER.error("push failed: {}/{}", subUserIds, msg);
                        successful = false;
                    }
                } catch (Exception e) {
                    LOGGER.error("publish msg exception: {}/{}", new Object[] { subUserIds, msg, e });
                    successful = false;
                } finally {
                    subUserIds.clear();
                }
            }
        }

        return successful;
    }
}

@@ -835,24 +835,96 @@
 hMsg msg) %7B%0A
+        LOGGER.info(%22start push: %7B%7D to %7B%7D users%22, msg, userIds.size());%0A
         bool
@@ -947,16 +947,39 @@
  true;%0A%0A
+        int count = 0;%0A
         
@@ -1075,20 +1075,16 @@
         
-if (
 subUserI
@@ -1090,83 +1090,111 @@
 Ids.
-size() %3C 1000) %7B%0A                subUserIds.add(userId);%0A            %7D else
+add(userId);%0A            count++;%0A            if (subUserIds.size() == 1000 %7C%7C count == userIds.size())
  %7B%0A 

fix bug