package com.palmnest.dao.estateserve.impl;

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

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

import com.palmnest.application.ElementConstant;
import com.palmnest.dao.estateserve.FloorInfoDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.FloorInfo;
import com.palmnest.model.property.TbBuild;

/**
 * 楼层DAO实现类
 *
 * @author longmeizhen
 */
@Repository("floorInfoDao")
public class FloorInfoDaoImpl extends GenericDaoHibernate<FloorInfo, Integer> implements FloorInfoDao {

	public FloorInfoDaoImpl() {
		super(FloorInfo.class);
	}

	/**
	 * 根据楼栋
	 * 获取楼层信息集合信息
	 *
	 * @param roleType
	 * @return
	 */
	@Override
	public List<FloorInfo> getFloorInfoByBuildId(int buildId) throws Exception {
		StringBuilder strHQL = new StringBuilder("from FloorInfo where  buildId=? and isEnabled=1 ");
		List<FloorInfo> list = getSession().createQuery(strHQL.toString()).setInteger(0, buildId).list();
		return list;
	}

	/**
	 * 添加楼层
	 */
	@Override
	public FloorInfo saveFloorInfo(FloorInfo floorInfo) throws Exception {
		return super.saveObj(floorInfo);
	}

	/**
	 * 根据楼栋
	 * 获取楼层信息集合信息
	 * (如果传过来的楼层Id集合不为空，那么查出对应楼层ID)
	 *
	 * @param roleType
	 * @return
	 */
	@Override
	public List<FloorInfo> getFloorListByOrgId(int orgId, String floorIds)
			throws Exception {
		StringBuilder strHQL = new StringBuilder("from FloorInfo where  buildId=? and isEnabled=1 ");
		if (StringUtils.isNotEmpty(floorIds)) {
			strHQL.append(" and name in (" + floorIds + ") ");
		}
		List<FloorInfo> list = getSession().createQuery(strHQL.toString()).setInteger(0, orgId).list();
		return list;
	}

	@Override
	public List<FloorInfo> getFloorListByName(String name, String buildId) throws Exception {
		StringBuilder strHQL = new StringBuilder("from FloorInfo where  name=? and isEnabled=1 and buildId =? ");
		List<FloorInfo> list = getSession().createQuery(strHQL.toString()).setString(0, name).setLong(1, Long.parseLong(buildId)).list();
		return list;
	}


	@Override
	public List<FloorInfo> getFloorListByFloorIds(String floorIds) throws Exception {
		StringBuilder strHQL = new StringBuilder("from FloorInfo where  id in (" + floorIds + ") and isEnabled=1 ");
		List<FloorInfo> list = getSession().createQuery(strHQL.toString()).list();
		return list;
	}

	@Override
	public List<FloorInfo> getFloorAll() {
		List<FloorInfo> list = new ArrayList<FloorInfo>();
		StringBuilder strHQL = new StringBuilder("from FloorInfo where  isEnabled=1");
		list = getSession().createQuery(strHQL.toString()).list();
		return list;
	}

	@Override
	public List<FloorInfo> getFloorHaveMeeting(String orgId) {
		List<FloorInfo> list = new ArrayList<FloorInfo>();
		StringBuilder strHQL = new StringBuilder("from FloorInfo where  isEnabled=1  and organizationId in (" + orgId + ") and isHaveMeeting='1' ");
		list = getSession().createQuery(strHQL.toString()).list();
		return list;
	}

	@Override
	public List<TbBuild> getTbBuildListByOrgaid(String orgId) {
		StringBuilder strHql = new StringBuilder("from TbBuild where isEnabled=? ");
		if (StringUtils.isNotBlank(orgId)) {
			String[] orgStr = orgId.split(";");
			String str = "";
			if (orgStr != null && orgStr.length > 0) {
				for (int i = 0; i < orgStr.length; i++) {
					str = str + orgStr[i] + ",";
				}
			}
			if (StringUtils.isNotEmpty(str)) {
				str = str.substring(0, str.length() - 1);
				strHql.append(" and organizationId in (" + str + ") ");
			}
		}
		strHql.append(" order by name asc ");
		List<TbBuild> list = getSession().createQuery(strHql.toString()).setString(0, ElementConstant.YESORNO_1).list();
		return list;
	}

	@Override
	public TbBuild getTbBuildById(int id) {
		String strHql = "from TbBuild where isEnabled=? and id=?";
		List<TbBuild> list = getSession().createQuery(strHql).setString(0, ElementConstant.YESORNO_1).setInteger(1, id).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getFloorData(String startTime,
												  String endTime) {
		StringBuilder sql = new StringBuilder("");
		sql.append(" select flr.id floorId,flr.name floorName,org.id orgId,org.name orgName,org.mettingClient client ");
		sql.append(" ,bu.id buildId,bu.name buName,flr.isEnabled flrEn,org.isEnabled orgEn ");
		sql.append(" from tb_floor flr  ");
		sql.append(" left join tb_organization org on flr.organizationId=org.id ");
		sql.append(" left join tb_build bu on bu.id=flr.buildId ");
		sql.append(" where DATE_FORMAT(flr.modifyTime,'%Y-%m-%d %T')>='" + startTime + "'  ");
		sql.append(" and DATE_FORMAT(flr.modifyTime,'%Y-%m-%d %T')<='" + endTime + "'  ");
		sql.append(" and flr.isHaveMeeting='1' and org.mettingClient <> '' and  org.mettingClient is not null and org.isEnabled='1'  ");
		List<Map<String, Object>> mapList = getJdbcTemplate().queryForList(sql.toString());
		return mapList;
	}

	@Override
	public List<FloorInfo> getALLFloor(String orgId) {
		List<FloorInfo> list = new ArrayList<FloorInfo>();
		StringBuilder strHQL = new StringBuilder("from FloorInfo where  isEnabled=1  and organizationId in (" + orgId + ")");
		list = getSession().createQuery(strHQL.toString()).list();
		return list;
	}

}
