/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.core.jdbc;

import com.crunii.micro.common.dto.PageReq;
import com.crunii.micro.common.dto.PageRsp;
import com.crunii.micro.core.dto.CacheRefresh;
import com.crunii.micro.core.dto.CacheRefreshQ;
import com.crunii.micro.core.dto.RefreshParam;
import com.crunii.micro.domain.common.jdbc.BaseJdbcTemplate;
import com.crunii.micro.domain.common.jdbc.BaseNPJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 田平 create 2017年8月11日下午12:44:04
 */
@Repository
public class CacheRefreshJdbcDao {

	@Resource(name = "mainJT")
	protected BaseJdbcTemplate JT;

	@Resource(name = "mainNPJT")
	protected BaseNPJdbcTemplate NPJT;

	private static final String pageRefreshBaseSql = "select a.* from t_cache_refresh a where 1=1 ";

	private static final String pageRefreshCountSql = "select count(*) from t_cache_refresh a where 1=1 ";

	private static final String urlParamSql = "select * from t_cache_refresh_param where refresh_id=? order by param_id";

	public PageRsp<CacheRefresh> pageCacheRefresh(PageReq<CacheRefreshQ> req) {

		StringBuilder sb = new StringBuilder(pageRefreshBaseSql);
		StringBuilder sbCount = new StringBuilder(pageRefreshCountSql);
		List<Object> params = new ArrayList<Object>();
		if (StringUtils.hasText(req.getQuery().getGroupName())) {
			sb.append(" and a.group_name=? ");
			sbCount.append(" and a.group_name=? ");
			params.add(req.getQuery().getGroupName());
		}
		if (StringUtils.hasText(req.getQuery().getRefreshName())) {
			sb.append(" and a.refresh_name like concat('%',?,'%') ");
			sbCount.append(" and a.refresh_name like concat('%',?,'%') ");
			params.add(req.getQuery().getRefreshName());
		}
		if (StringUtils.hasText(req.getQuery().getAppName())) {
			sb.append(" and a.app_name like concat('%',?,'%') ");
			sbCount.append(" and a.app_name like concat('%',?,'%') ");
			params.add(req.getQuery().getAppName());
		}

		if (StringUtils.hasText(req.getQuery().getRefreshUrl())) {
			sb.append(" and a.refresh_url like concat('%',?,'%') ");
			sbCount.append(" and a.refresh_url like concat('%',?,'%') ");
			params.add(req.getQuery().getRefreshUrl());
		}
		sb.append(" order by a.group_name , a.app_name ");

		int total = JT.queryForInt(sbCount.toString(), params.toArray());
		List<CacheRefresh> refreshs = null;
		if (total > 0) {
			sb.append(" limit ?,? ");
			params.add(req.getMyStart());
			params.add(req.getPageSize());

			refreshs = JT.queryForBeans(CacheRefresh.class, sb.toString(), params.toArray());
		} else {
			refreshs = new ArrayList<CacheRefresh>(0);
		}

		refreshs.forEach(r -> {
			r.setParams(JT.queryForBeans(RefreshParam.class, urlParamSql, r.getRefreshId()));
		});

		PageRsp<CacheRefresh> page = new PageRsp<CacheRefresh>();
		page.setPageNo(req.getPageNo());
		page.setPageSize(req.getPageSize());
		page.setTotalNum(total);
		page.setResult(refreshs);
		// 为手机计算是否由下一页
		page.next();

		return page;
	}

}
