package cn.hmsoft.weixin.request;

import cn.hmsoft.application.SpringHelper;
import cn.hmsoft.weixin.data.model.WxUserTag;
import cn.hmsoft.weixin.entity.request.UserTagGetRequestJson;
import cn.hmsoft.weixin.entity.response.UserTagGetResponseJson;

/*******************************
 * @note 获取用户的标签
 * @author revisit@126.com
 * @Company: www.hmsoft.cn
 * @create 2017-05-09 01:14:42
 * @version 4.0.0
 */
public class UserTagGetRequest
		extends AbstractRequest<UserTagGetResponseJson, UserTagGetResponseJson, UserTagGetRequestJson> {

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

	public UserTagGetRequest(int tag_id, String next_openid) {
		this.requestEntity = new UserTagGetRequestJson();
		this.requestEntity.setTagid(tag_id);
		this.requestEntity.setNext_openid(next_openid);
	}

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

	@Override
	public void processBusiness() {
		this.returnEntity = this.jsonEntity;
		if (this.returnEntity.getErrcode() == 0) {
			// 移除所有的tag
			SpringHelper.AppGobalDao.delete(WxUserTag.class, "tag_id", this.requestEntity.getTagid());
			if (this.returnEntity.getData() != null && this.returnEntity.getData().getOpenid() != null) {
				for (String open_id : this.returnEntity.getData().getOpenid()) {
					WxUserTag ug = new WxUserTag();
					ug.setOpen_id(open_id);
					ug.setWx_id(this.wxDefine.getWx_id());
					ug.setTag_id(this.requestEntity.getTagid());
					try {
						SpringHelper.AppGobalDao.insert(ug);
					} catch (Exception e) {

					}
				}
			}
		}
	}
}
