package com.crunii.micro.service.sysmgr.jdbc;

import com.crunii.micro.common.dto.PageReq;
import com.crunii.micro.common.dto.PageRsp;
import com.crunii.micro.common.utils.SqlUtil;
import com.crunii.micro.domain.common.jdbc.BaseJdbcTemplate;
import com.crunii.micro.domain.entity.main.TsmUrl;
import com.crunii.micro.domain.entity.main.TsmUrlGroup;
import com.crunii.micro.service.sysmgr.dto.sysmgr.UrlGroupReq;
import com.crunii.micro.service.sysmgr.dto.sysmgr.UrlReq;
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 UrlJdbcDao {
	@Resource(name = "mainJT")
	protected BaseJdbcTemplate jdbcTemplate;

	private static final String PAGE_URL_BASE_URL = "select * from tsm_url a where 1=1 ";

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

	private static final String GET_URL_OF_GROUP_URL = "select a.* from tsm_url a where a.url_id in(select url_id from tsm_url_group_url where group_id=?) and state=1 order by a.url_name,a.url_path ";

	private static final String PAGE_URL_GROUP_BASE_SQL = "select * from tsm_url_group a where 1=1 ";

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

	public List<TsmUrl> getUrlsOfGroup(int groupId) {
		return jdbcTemplate.queryForBeans(TsmUrl.class, GET_URL_OF_GROUP_URL, groupId);
	}

	public PageRsp<TsmUrl> pageUrl(PageReq<UrlReq> req) {
		StringBuilder sb = new StringBuilder(PAGE_URL_BASE_URL);
		StringBuilder sbCount = new StringBuilder(PAGE_URL_COUNT_SQL);
		List<Object> params = new ArrayList<>();
		if (StringUtils.hasText(req.getQuery().getUrlName())) {
			sb.append(" and a.url_name like concat('%',?,'%') ");
			sbCount.append(" and a.url_name like concat('%',?,'%') ");
			params.add(req.getQuery().getUrlName());
		}

		if (StringUtils.hasText(req.getQuery().getUrlPath())) {
			sb.append(" and a.url_path like concat('%',?,'%') ");
			sbCount.append(" and a.url_path like concat('%',?,'%') ");
			params.add(req.getQuery().getUrlPath());
		}

		int total = jdbcTemplate.queryForInt(sbCount.toString(), params.toArray());
		List<TsmUrl> urls;
		String dbType = jdbcTemplate.getDbType();
		if (total > 0) {
			//根据数据库类型设置limit
			SqlUtil.appendLimit(dbType, params, sb, req);

			urls = jdbcTemplate.queryForBeans(TsmUrl.class, sb.toString(), params.toArray());
		} else {
			urls = new ArrayList<>(0);
		}
		PageRsp<TsmUrl> page = new PageRsp<>();
		page.setPageNo(req.getPageNo());
		page.setPageSize(req.getPageSize());
		page.setTotalNum(total);
		page.setResult(urls);
		// 为手机计算是否由下一页
		page.next();

		return page;
	}

	public PageRsp<TsmUrlGroup> pageUrlGroup(PageReq<UrlGroupReq> req) {
		StringBuilder sb = new StringBuilder(PAGE_URL_GROUP_BASE_SQL);
		StringBuilder sbCount = new StringBuilder(PAGE_URL_GROUP_COUNT_SQL);
		List<Object> params = new ArrayList<>();
		if (StringUtils.hasText(req.getQuery().getGroupName())) {
			sb.append(" and a.group_name like concat('%',?,'%') ");
			sbCount.append(" and a.group_name like concat('%',?,'%') ");
			params.add(req.getQuery().getGroupName());
		}

		int total = jdbcTemplate.queryForInt(sbCount.toString(), params.toArray());
		List<TsmUrlGroup> urlGroups;
		String dbType = jdbcTemplate.getDbType();
		if (total > 0) {
			//根据数据库类型设置limit
			SqlUtil.appendLimit(dbType, params, sb, req);

			urlGroups = jdbcTemplate.queryForBeans(TsmUrlGroup.class, sb.toString(), params.toArray());
		} else {
			urlGroups = new ArrayList<>(0);
		}
		PageRsp<TsmUrlGroup> page = new PageRsp<>();
		page.setPageNo(req.getPageNo());
		page.setPageSize(req.getPageSize());
		page.setTotalNum(total);
		page.setResult(urlGroups);
		// 为手机计算是否由下一页
		page.next();

		return page;
	}

}
