package cn.hmsoft.weixin.request;

import cn.hmsoft.application.SpringHelper;
import cn.hmsoft.weixin.data.model.WxUser;
import cn.hmsoft.weixin.entity.response.UserGetResponseJson;

/********************************************
 * @note 获取用户列表，公众号可通过本接口来获取帐号的关注者列表，关注者列表由一串OpenID（加密后的微信号，每个用户对每个公众号的OpenID是唯一的）组成。一次拉取调用最多拉取10000个关注者的OpenID，可以通过多次拉取的方式来满足需求
 * @author revisit@126.com
 * @Company: www.hmsoft.cn
 * @create 2017-05-09 01:10:24
 * @version 4.0.0
 */
public class UserGetRequest extends AbstractRequest<UserGetResponseJson, UserGetResponseJson, String> {

	private final static String url = "https://api.weixin.qq.com/cgi-bin/user/get?";

	private String next_openid;

	public UserGetRequest(String next_openid) {
		this.next_openid = next_openid;
	}

	@Override
	protected String makeUrl() {
		return url + "access_token=" + this.wxDefine.getApp_accesstoken() + "&next_openid=" + next_openid;
	}

	@Override
	public void processBusiness() {
		if (this.jsonEntity.getCount() > 0) {
			for (String open_id : this.jsonEntity.getData().getOpenid()) {
				WxUser user = SpringHelper.AppGobalDao.find(WxUser.class, "open_id", open_id);
				if (user == null) {
					user = new WxUser();
					user.setOpen_id(open_id);
					user.setWx_id(this.wxDefine.getWx_id());
					SpringHelper.AppGobalDao.insert(user);
				}

			}
		}
		this.returnEntity = jsonEntity;
	}
}
