package cn.hmsoft.weixin.data.dao;

import java.util.List;

import org.springframework.stereotype.Component;

import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.jdbc.core.PlatformDaoSupport;
import cn.hmsoft.jdbc.entity.Pager;
import cn.hmsoft.jdbc.entity.QueryOrder;
import cn.hmsoft.weixin.data.model.WxTag;

@Component
public class WxTagDao extends PlatformDaoSupport<WxTag> {
	public Pager pageTag(int wx_id, Integer start, Integer limit, String query, QueryOrder order) {
		String sql = "select u.*,std_count from (select * from wx_tag where wx_id=?) u left join (select count(1) std_count,tag_id from wx_user_tag where wx_id=? group by tag_id) t on u.tag_id=t.tag_id ";
		if (StringHelper.isEmpty(query))
			return this.pageMapBySql(order, start, limit, sql, wx_id, wx_id);
		sql = sql + " where tag_name like ? ";
		return this.pageMapBySql(order, start, limit, sql, wx_id, wx_id, "%" + query + "%");
	}

	public List<WxTag> listTag(int wx_id) {
		String sql = "select * from wx_tag where wx_id=? order by tag_id";
		return this.listBySql(sql, wx_id);
	}

	private final static String UPDATE_TAG_SQL = "update wx_tag set tag_desc=?,tag_name = ? where wx_id=? and tag_id=?";

	public void updateTag(WxTag tag) {
		this.updateBySql(UPDATE_TAG_SQL, tag.getTag_desc(), tag.getTag_name(), tag.getWx_id(), tag.getTag_id());
	}

	private final static String FIND_TAG_SQL = "select *  from wx_tag  where wx_id=? and tag_id=?";

	public WxTag findTag(int wx_id, int tag_id) {
		return this.findBySql(FIND_TAG_SQL, wx_id, tag_id);
	}

	private final static String DEL_TAG_SQL = "delete from wx_tag  where wx_id=? and tag_id=?";

	public int delTag(int wx_id, int tag_id) {
		return this.updateBySql(DEL_TAG_SQL, wx_id, tag_id);
	}
}