package com.palmnest.dao.estateserve.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.palmnest.application.ElementConstant;
import com.palmnest.application.bean.Page;
import com.palmnest.dao.estateserve.ContractIinformationOrderDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.ContractIinformation;
import com.palmnest.model.ContractIinformationOrder;

@Repository("contractIinformationOrderDao")
public class ContractIinformationOrderDaoImpl extends
		GenericDaoHibernate<ContractIinformationOrder, Integer> implements ContractIinformationOrderDao {

	public ContractIinformationOrderDaoImpl() {
		super(ContractIinformationOrder.class);
	}

	@Override
	public Page ContractIinformationOrderPage(Integer pageSize, Integer pageNo,
											  String orgId, String virtualArchitectureId) throws Exception {
		String strSQL = "SELECT t1.userId,t1.orderType,t1.id,t1.organizationId,t1.virtualArchitectureId,DATE_FORMAT(t1.createTime,'%Y-%m-%d') createTime,t3.`name` organizationName,t2.type,"
				+ " if(t2.`status` is NULL,0,t2.`status`) `status`,t4.userMobile,t4.userName,t5.`name` virtualarchitectureName,t5.phone,t5.address,t5.customerNumber"
				+ " from tb_contractiinformationorder t1 "
				+ " LEFT JOIN tb_contractiinformation t2 on t2.organizationId=t1.organizationId and t2.virtualArchitectureId=t1.virtualArchitectureId and t2.isEnabled=1"
				+ " LEFT JOIN tb_organization t3 on t3.ID=t1.organizationId"
				+ " LEFT JOIN tb_user t4 on t4.ID=t1.userId"
				+ " LEFT JOIN tb_virtualarchitecture t5 on t5.id=t1.virtualArchitectureId where t1.organizationId in (" + orgId + ")";
		if (StringUtils.isNotBlank(virtualArchitectureId)) {
			strSQL += " and t1.virtualArchitectureId=" + virtualArchitectureId;
		}
		strSQL += " GROUP BY t1.userId";
		return pagedQueryByJdbcTemplate(strSQL, pageNo, pageSize);
	}

	@SuppressWarnings("unchecked")
	@Override
	public ContractIinformation findContractIinformationObj(
			Integer id) {
		String strHQL = "from ContractIinformation where id=?   and isEnabled=?";
		List<ContractIinformation> list = this.getSession().createQuery(strHQL).setInteger(0, id)
				.setString(1, "1").list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public Page ContractIinformationPage(Integer pageSize, Integer pageNo,
										 String orgId, String virtualArchitectureId) throws Exception {
		String strSQL = "SELECT t1.*,t2.`name` virtualarchitectureName,t2.customerNumber,t2.address,t2.phone,t3.`name` organizationName"
				+ " from tb_contractiinformation t1"
				+ " LEFT JOIN tb_virtualarchitecture t2 on t1.virtualArchitectureId=t2.id and t2.isEnabled=1"
				+ " LEFT JOIN tb_organization t3 on t3.ID=t1.organizationId and t3.isEnabled=1"
				+ " where t1.isEnabled=1";
		if (StringUtils.isNotBlank(orgId)) {
			strSQL += " and t1.organizationId in (" + orgId + ")";
		}
		if (StringUtils.isNotBlank(virtualArchitectureId)) {
			strSQL += " and t1.virtualArchitectureId =" + virtualArchitectureId;
		}
		strSQL += " order by t1.createTime desc";
		return pagedQueryByJdbcTemplate(strSQL, pageNo, pageSize);
	}

	@Override
	public Map<String, Object> businessRecordDetails(Integer id) {
		String strSQL = "SELECT DATE_FORMAT(t1.createTime,'%Y-%m-%d') createTimeFormat,t1.*,t2.`name` virtualarchitectureName,t2.customerNumber,t2.address,t2.phone,t3.`name` organizationName"
				+ " from tb_contractiinformation t1"
				+ " LEFT JOIN tb_virtualarchitecture t2 on t1.virtualArchitectureId=t2.id and t2.isEnabled=1"
				+ " LEFT JOIN tb_organization t3 on t3.ID=t1.organizationId and t3.isEnabled=1"
				+ " where t1.isEnabled=1 and t1.id=" + id;
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(strSQL);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getBusinessRecordBuyUser(String userId) {
		String strSQL = "SELECT t1.orderType from tb_contractiinformationorder t1 where t1.userId=" + userId + " GROUP BY t1.orderType";
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(strSQL);
		return list;
	}

	@Override
	public ContractIinformation getContractIinformation(
			Integer virtualArchitectureId) {
		String strHQL = "from ContractIinformation where virtualArchitectureId=? and status=? and isEnabled=?";
		List<ContractIinformation> list = this.getSession().createQuery(strHQL).setInteger(0, virtualArchitectureId).setString(1, "1").setString(2, "1").list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<ContractIinformationOrder> getBusinessRecordBuyUser(
			Integer userId) {
		String strHQL = "from ContractIinformationOrder where userId=?";
		List<ContractIinformationOrder> list = getSession().createQuery(strHQL).setInteger(0, userId).list();
		return list;
	}

	@Override
	public ContractIinformationOrder getContractIinformationOrder(int userId,
																  String orderType) {
		String strHQL = "from ContractIinformationOrder where userId=? and orderType=?";
		List<ContractIinformationOrder> list = getSession().createQuery(strHQL).setInteger(0, userId).setString(1, orderType).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<ContractIinformation> getContractIinformationByOrgId(
			int organizationId) {
		String strHQL = "from ContractIinformation where organizationId=? and isEnabled=?";
		return getSession().createQuery(strHQL).setInteger(0, organizationId).setString(1, ElementConstant.YESORNO_1).list();
	}

	@Override
	public Integer getContractIinformationCount() {
		try {
			String strSQL = "SELECT COUNT(DISTINCT(virtualArchitectureId)) from tb_ContractIinformation where isEnabled=1";
			return this.getJdbcTemplate().queryForInt(strSQL);
		} catch (Exception e) {

		}
		return 0;
	}

	@Override
	public List<ContractIinformation> getContractIinformationList() {
		String strHQL = "from ContractIinformation where isEnabled='1' ";
		return getSession().createQuery(strHQL).list();
	}
}
