package cn.hmsoft.hmplatform.wxtoken.data.dao;

import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import cn.hmsoft.hmplatform.wxtoken.data.model.PWxDefine;
import cn.hmsoft.jdbc.core.PlatformDaoSupport;
import cn.hmsoft.jdbc.entity.Pager;
import cn.hmsoft.jdbc.entity.QueryOrder;

@Repository
public class PWxDefineDao extends PlatformDaoSupport<PWxDefine> {
	private final static String FIND_BY_APPID = "select * from p_wx_define where app_id=?";

	public PWxDefine findWxDefine(String app_id) {
		return this.findBySql(FIND_BY_APPID, app_id);
	}

	public Pager pageDefine(Integer start, Integer limit, String query, QueryOrder order) {
		final StringBuilder sql = new StringBuilder();
		sql.append(
				"select d.*,pd.deploy_total from p_wx_define d left join (select token_id,count(1) deploy_total from p_wx_deploy pd group by token_id) pd on d.token_id=pd.token_id where 1=1");
		if (StringUtils.isEmpty(query)) {
			return this.pageBySql(order, start, limit, sql.toString());
		}
		sql.append(" and (app_id=? or app_name like ? or wx_name like ?)");
		return super.pageMapBySql(order, start, limit, sql.toString(), query, "%" + query + "%", "%" + query + "%");
	}
}
