/**
 * 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 cn.hutool.core.util.ObjectUtil;
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.TsmProperty;
import com.crunii.micro.domain.entity.main.TsmPropertyValue;
import com.crunii.micro.service.sysmgr.dto.sysmgr.PropertyReq;
import com.crunii.micro.service.sysmgr.dto.sysmgr.ValueReq;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;
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 PropertyJdbcDao {

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

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

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

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

	private static final String pageValueBaseSql = "select a.* from tsm_property_value a,tsm_property b WHERE a.del_flag=0 and a.state=1 and a.prop_id=b.prop_id AND b.prop_code=? ";

	private static final String pageValueCountSql = "select count(*) from tsm_property_value a,tsm_property b WHERE a.del_flag=0 and a.state=1 and a.prop_id=b.prop_id AND b.prop_code=? ";

	private static final String listActivityZoneSql = "select a.* from tsm_property_value a where a.state = 1 and a.prop_code in ('zone', 'school', 'city')";

	public PageRsp<TsmPropertyValue> pageValue(PageReq<ValueReq> 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<TsmPropertyValue> values = null;
		String dbType = JT.getDbType();
		if (total > 0) {
			//根据数据库类型设置limit
			SqlUtil.appendLimit(dbType, params, sb, req);

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

		return page;
	}

	public PageRsp<TsmProperty> pageProperty(PageReq<PropertyReq> req) {
		StringBuilder sb = new StringBuilder(pagePropBaseSql);
		StringBuilder sbCount = new StringBuilder(pagePropCountSql);
		List<Object> params = new ArrayList<Object>();
		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());
		}
		if (ObjectUtil.isNotEmpty(req.getQuery().getWorkerManageState())) {
			sb.append(" and a.worker_manage_state = ? ");
			sbCount.append(" and a.worker_manage_state = ? ");
			params.add(req.getQuery().getWorkerManageState());
		}

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

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

    public List<TsmPropertyValue> listActivityZone() {
		return JT.queryForBeans(TsmPropertyValue.class, listActivityZoneSql);
    }
}
