package com.palmnest.dao.hibernate;

import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.dao.OrganizationDao;
import com.palmnest.model.FloorInfo;
import com.palmnest.model.TbCompany;
import com.palmnest.model.TbDept;
import com.palmnest.model.User;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.model.property.TbOrganizationConfig;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

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

/**
 * 组织管理底层DAO实现(公司、部门、等等..)
 *
 * @author yubing
 * @since 2014-09-12
 */
@Repository("organizationDao")
public class OrganizationDaoHibernate extends GenericDaoHibernate<Object, Integer> implements OrganizationDao {

	public OrganizationDaoHibernate() {
		super(Object.class);
	}

	public TbCompany getCompanyById(int id) {
		return (TbCompany) getSession().get(TbCompany.class, id);
	}

	public TbOrganization getOrganizationById(int id) {
		return (TbOrganization) getSession().get(TbOrganization.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TbDept> getAllDept() {
		return getSession().createQuery("from TbDept").list();
	}

	@Override
	public int getMaxDeptid() {
		String  strHql = "select max(id) from TbDept";
		Integer maxId  = (Integer) getSession().createQuery(strHql).uniqueResult();
		if (maxId != null) {
			return maxId.intValue();
		} else {
			return 0;
		}
	}

	@Override
	public TbDept getDept(String treeCode, String parentCode) {
		String       strHql = "from TbDept where treecode=? and parentcode=?";
		List<TbDept> list   = getSession().createQuery(strHql).setString(0, treeCode).setString(1, parentCode).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public void deleteDept(String treeCode, String parentCode) {
		String strSql = "delete from tb_dept where TREECODE=? and PARENTCODE=?";
		getSession().createSQLQuery(strSql).setString(0, treeCode).setString(1, parentCode).executeUpdate();
	}

	@SuppressWarnings("unchecked")
	public List<TbCompany> getCompanyList(String isenabled) {
		String strHql = "from TbCompany where 1=1";
		if (StringUtils.isNotEmpty(isenabled)) {
			strHql += " and isenabled='" + isenabled + "'";
		}
		return getSession().createQuery(strHql).list();
	}

	@Override
	public TbDept getDept(int id) {
		return (TbDept) getSession().get(TbDept.class, id);
	}

	@Override
	public FloorInfo getFloor(int id) {
		return (FloorInfo) getSession().get(FloorInfo.class, id);
	}

	@Override
	public List<User> getUserList(String projectcode, String position) {
		return null;
	}

	@Override
	public Page queryCompanyList(int pageNo, int pageSize,
								 String whereHql) {
		StringBuffer strSbf = new StringBuffer("select *from tb_company");
		strSbf.append(whereHql);
		strSbf.append(" order BY createTime DESC");
		Page page = pagedQueryByJdbcTemplate(strSbf.toString(), pageNo, pageSize);
		return page;
	}

	@Override
	public TbCompany getCompanyByName(String comName) {

		String StrHql = "from TbCompany where compName='" + comName + "'";

		List<TbCompany> tbcompanys = getSession().createQuery(StrHql).list();

		if (!CollectionUtils.isEmpty(tbcompanys)) {
			return tbcompanys.get(0);
		}
		return null;
	}

	@Override
	public TbDept getDept(TbCompany company, String deptName) {
		String       strHql = "from TbDept where company.id=" + company.getId() + " and deptName='" + deptName + "'";
		List<TbDept> depts  = getSession().createQuery(strHql).list();
		if (!CollectionUtils.isEmpty(depts)) {
			return depts.get(0);
		}
		return null;
	}

	@Override
	public Page queryDeptList(int pageNo, int pageSize, String whereHql) {
		StringBuffer strSQL = new StringBuffer("SELECT t1.id,t1.`name` AS deptName,t2.`name` AS compName,t1.creator,t1.createTime FROM");
		strSQL.append(" tb_dept t1 INNER JOIN tb_company t2 ON t1.compId = t2.ID");
		strSQL.append(whereHql);
		strSQL.append(" order BY t1.createTime DESC");
		Page page = pagedQueryByJdbcTemplate(strSQL.toString(), pageNo, pageSize);
		return page;
	}

//	@Override
//	public List<Map<String, Object>> getAppUserCompIds(int userId) {
//		StringBuffer strSQL=new StringBuffer("SELECT distinct(t2.compId) FROM tr_app_user_org t1 INNER JOIN tb_dept t2 ON t1.ORG_ID = t2.id");
//		strSQL.append(" where t1.ORG_TYPE='"+ElementConstant.USER_ORG_DEPT+"' and t1.USER_ID="+userId);
//		strSQL.append(" and t2.isEnabled='"+ElementConstant.YESORNO_1+"'");
//		return getJdbcTemplate().queryForList(strSQL.toString());
//	}

	/**
	 * 根据楼宇名称获取相应的楼宇
	 */
	@Override
	public TbOrganization getOrganizationInfoByName(String name)
			throws Exception {
		String               strHql  = "from TbOrganization where name='" + name + "' and isEnabled='1' ";
		List<TbOrganization> objList = getSession().createQuery(strHql).list();
		TbOrganization       org     = new TbOrganization();
		if (objList != null && objList.size() > 0) {
			org = objList.get(0);
		} else {
			return null;
		}
		return org;
	}

	@Override
	public TbOrganizationConfig getObj(String id) {
		String                     StrHql = "from TbOrganizationConfig where id=?";
		List<TbOrganizationConfig> list   = getSession().createQuery(StrHql).setString(0, id).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public TbOrganizationConfig getDiscoverConfigByOrgId(String org) {
		String                     StrHql = "from TbOrganizationConfig where orgId=?";
		List<TbOrganizationConfig> list   = getSession().createQuery(StrHql).setString(0, org).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<TbOrganization> getAllOrganization() {
		return getSession().createQuery("from TbOrganization where isEnabled = 1 and status='1' ").list();
	}

	@Override
	public TbOrganization getByMettingClient(String mettingClient) {
		String               strHql  = "from TbOrganization where mettingClient='" + mettingClient + "' and isEnabled='1' ";
		List<TbOrganization> objList = getSession().createQuery(strHql).list();
		if (objList != null && objList.size() > 0) {
			return objList.get(0);
		} else {
			return null;
		}
	}

	@Override
	public List<Map<String, Object>> getOrgOrderbyCity(String orgIds, String city) {
		String strHql = " select cityCode,cityName,id orgId,name orgName from tb_organization where isEnabled=1 ";
		if (StringUtil.isNotEmpty(orgIds)) {
			strHql = strHql + " and id in (" + orgIds + ") ";
		}
		if (StringUtil.isNotEmpty(city)) {
			strHql = strHql + " and (cityName like '%" + city + "%' or name like '%" + city + "%') ";
		}
		strHql = strHql + " order by cityCode  ";
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(strHql);
		return list;
	}

	@Override
	public List<TbOrganization> enableOrgList(Integer enable) {
		String               hql     = "from TbOrganization where isEnabled=?";
		List<TbOrganization> orgList = getSession().createQuery(hql).setInteger(0, enable).list();
		return orgList;
	}
	
	@Override
	public List<TbOrganization> passTypeOrgList(Integer passType) {
		String               hql     = "from TbOrganization where passType like '%"+passType+"%'";
		List<TbOrganization> orgList = getSession().createQuery(hql).list();
		return orgList;
	}
}
