package cn.hmsoft.weixin.request;

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

import cn.hmsoft.application.SpringHelper;
import cn.hmsoft.weixin.entity.response.MemberBatchTaggingResponseJson;
import cn.hmsoft.weixin.entity.response.WxResponseJson;

/*****************************
 * @note 批量为用户取消标签
 * @author revisit@126.com
 * @Company: www.hmsoft.cn
 * @create 2017-05-09 00:38:57
 * @version 4.0.0
 */
public class MemberBatchUnTaggingRequest
		extends AbstractRequest<WxResponseJson, WxResponseJson, MemberBatchTaggingResponseJson> {

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

	private final static String DEL_USER_TAG_SQL = "delete from wx_user_tag where open_id=? and tag_id=?";

	/*********************************
	 * 批量为用户取消标签
	 * 
	 * @param tag_id
	 *            标签编号
	 * @param open_id
	 *            用户的open_id
	 */
	public MemberBatchUnTaggingRequest(int tag_id, String open_id) {
		this.requestEntity = new MemberBatchTaggingResponseJson();
		this.requestEntity.setTagid(tag_id);
		List<String> list = new ArrayList<String>();
		list.add(open_id);
		this.requestEntity.setOpenid_list(list);
	}

	/*********************************
	 * 批量为用户取消标签
	 * 
	 * @param tag_id
	 *            标签编号
	 * @param open_ids
	 *            用户的open_ids列表
	 */
	public MemberBatchUnTaggingRequest(int tag_id, List<String> open_ids) {
		this.requestEntity = new MemberBatchTaggingResponseJson();
		this.requestEntity.setTagid(tag_id);
		this.requestEntity.setOpenid_list(open_ids);
	}

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

	@Override
	public void processBusiness() {
		this.returnEntity = this.jsonEntity;
		if (this.returnEntity.getErrcode() == 0) {
			for (String open_id : this.requestEntity.getOpenid_list())
				SpringHelper.AppGobalDao.updateBySql(DEL_USER_TAG_SQL, open_id, this.requestEntity.getTagid());
		}
	}

}
