package cn.hmsoft.weixin.data.dao;

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.WxUser;

@Component
public class WxUserDao extends PlatformDaoSupport<WxUser> {
	public Pager pageUser(int wx_id, Integer start, Integer limit, String query, QueryOrder order) {
		String sql = "select u.*,user_tags,user_tags_ids from wx_user u left join (select open_id,t.wx_id,"
				+ this.concatValues("tag_name")+ " user_tags, " + this.concatValues("ut.tag_id") + " user_tags_ids "+
				" from wx_user_tag ut,wx_tag t where t.wx_id=ut.wx_id and ut.tag_id=t.tag_id and t.wx_id=? group by open_id,t.wx_id) t on u.open_id=t.open_id  and u.wx_id=t.wx_id where u.wx_id=?";
		if (StringHelper.isEmpty(query))
			return this.pageMapBySql(order, start, limit, sql, wx_id, wx_id);
		sql = sql + " and (nickname like ? or user_tags like ? )";
		return this.pageMapBySql(order, start, limit, sql, wx_id, wx_id, "%" + query + "%", "%" + query + "%");
	}

}