/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.cqcis.com</a>
 */
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.common.jdbc.BaseNPJdbcTemplate;
import com.crunii.micro.domain.entity.main.TfmProperty;
import com.crunii.micro.domain.entity.main.TfmPropertyValue;
import com.crunii.micro.service.sysmgr.dto.fm.FmPropertyReq;
import com.crunii.micro.service.sysmgr.dto.fm.FmValueReq;
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 FmPropertyJdbcDao {

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

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

	private static final String pagePropBaseSql = "select * from tfm_property a where 1=1 ";

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

	private static final String pageValueBaseSql = "select a.* from tfm_property_value a,tfm_property b WHERE a.prop_id=b.prop_id AND b.prop_code=? ";

	private static final String pageValueCountSql = "select count(*) from tfm_property_value a,tfm_property b WHERE a.prop_id=b.prop_id AND b.prop_code=? ";

	public PageRsp<TfmPropertyValue> pageValue(PageReq<FmValueReq> req) {
		StringBuilder sb = new StringBuilder(pageValueBaseSql);
		StringBuilder sbCount = new StringBuilder(pageValueCountSql);
		List<Object> params = new ArrayList<>();
		params.add(req.getQuery().getPropCode());

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

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

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

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

		return page;
	}

	public PageRsp<TfmProperty> pageProperty(PageReq<FmPropertyReq> req) {
		StringBuilder sb = new StringBuilder(pagePropBaseSql);
		StringBuilder sbCount = new StringBuilder(pagePropCountSql);
		List<Object> params = new ArrayList<>();
		if (StringUtils.hasText(req.getQuery().getPropName())) {
			sb.append(" and a.prop_name like concat('%',?,'%') ");
			sbCount.append(" and a.prop_name like concat('%',?,'%') ");
			params.add(req.getQuery().getPropName());
		}

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

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

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

}
