package com.palmnest.dao.property.impl;

import com.palmnest.application.ElementConstant;
import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.LogExceptionStackUtil;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.dao.property.PropertyDao;
import com.palmnest.model.AppUserFloorRelation;
import com.palmnest.model.AppUserOrgnizationRelation;
import com.palmnest.model.CompanyRelationOrg;
import com.palmnest.model.ScopeEntity;
import com.palmnest.model.property.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

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

/**
 * 项目组织架构数据持久层接口实现类
 *
 * @author yubing
 */
@Slf4j
@Repository("propertyDao")
public class PropertyDaoHibernate extends GenericDaoHibernate<Object, Integer> implements PropertyDao {


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

	@Override
	public TbOrganization getTbOrganizationById(Integer id) {
		return (TbOrganization) getSession().get(TbOrganization.class, id);
	}

	@Override
	public TbOrganization getTbOrganizationByNumber(String number) {
		String               strHQL = "from TbOrganization where code=? and isEnabled='1' and status='1' ";
		List<TbOrganization> list   = getSession().createQuery(strHQL).setString(0, number).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

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

	@Override
	public List<TbOrganization> queryTbOrganizationListByName(String organizationName) {
		String               strHql = "from TbOrganization where isEnabled='1' and status='1' and name like '%" + organizationName + "%'";
		List<TbOrganization> list   = getSession().createQuery(strHql).list();
		return list;
	}

	@Override
	public List<TbBuild> getTbBuildListByOrgaid(int organizationID) {
		String        strHql = "from TbBuild where isEnabled=? and organizationId=? order by name asc";
		List<TbBuild> list   = getSession().createQuery(strHql).setString(0, ElementConstant.YESORNO_1).setInteger(1, organizationID).list();
		return list;
	}

	@Override
	public List<TbOrganization> queryTbOrganizationList() {
		String               strHql = "from TbOrganization where isEnabled='" + ElementConstant.YESORNO_1 + "' and status='1' ";
		List<TbOrganization> list   = getSession().createQuery(strHql).list();
		return list;
	}

	@Override
	public List<AppUserFloorRelation> queryUserOrgFloorList() {
		String                     strHql = "from AppUserFloorRelation";
		List<AppUserFloorRelation> list   = getSession().createQuery(strHql).list();
		return list;
	}

	@Override
	public Page queryBuildListPage(int pageNo, int pageSize, String whereSQL) {
		StringBuffer strSbf = new StringBuffer();
		strSbf.append("SELECT t1.id,t1.name,t2.`name` as organizationName,t1.creator,t1.createTime FROM ");
		strSbf.append(" tb_build t1 LEFT JOIN tb_organization t2 ON t1.organizationId = t2.ID ");
		strSbf.append(whereSQL);
		strSbf.append(" order by t1.createTime desc");
		return pagedQueryByJdbcTemplate(strSbf.toString(), pageNo, pageSize);
	}

	@Override
	public Integer getMaxvirtualArchId() {
		String strSQL = "select max(id) from tb_virtualarchitecture";
		return getJdbcTemplate().queryForInt(strSQL);
	}

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

	@Override
	public List<Map<String, Object>> getVirtualListBySelectCompany(
			int organizationId) {
		//String strHQL="select id,floorIds,name from VirtualArchitecture where organizationId=? and isEnabled=?  ";
		String sql = "select id,floorIds,name from tb_virtualarchitecture where organizationId='" + organizationId + "' and isEnabled=1 order by  floorIds,id";
		//return getSession().createQuery(strHQL).setInteger(0, organizationId).setString(1, ElementConstant.YESORNO_1).list();
		return getJdbcTemplate().queryForList(sql);
	}

	@Override
	public VirtualArchitecture getVirtualArchBycode(String code,
													String parentCode) {
		String                    strHQL = "from VirtualArchitecture where code=? and parentCode=?";
		List<VirtualArchitecture> list   = getSession().createQuery(strHQL).setString(0, code).setString(1, parentCode).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<BuildOrgRelation> getBuildOrgRelationList(String type,
														  int orgId) {
		String strHQL = "from BuildOrgRelation where type=? and orgId=?";
		return getSession().createQuery(strHQL).setString(0, type).setInteger(1, orgId).list();
	}

	@Override
	public List<Map<String, Object>> getBuildList() {

		StringBuffer strSQL = new StringBuffer("SELECT t2.id as organizationId,t2.`name` as organizationName,t1.id AS buildId,t1.`name` AS buildName");
		strSQL.append(" FROM tb_build t1 INNER JOIN tb_organization t2 ON t1.organizationId = t2.ID");
		strSQL.append(" WHERE t1.isEnabled = '1' AND t2.isEnabled = '1' AND t2.status='1'");
		return getJdbcTemplate().queryForList(strSQL.toString());
	}

	@Override
	public List<Map<String, Object>> getOrgListByCompany(Integer companyId) {

		String sql = "SELECT t2.ID as organizationId,t2.name as organizationName " +
				"		FROM tr_company_org t1 " +
				"		INNER JOIN tb_organization t2 ON t1.org_id = t2.ID AND t1.company_id=" + companyId + " AND t2.isEnabled=1";
		return getJdbcTemplate().queryForList(sql);
	}

	@Override
	public List<Map<String, Object>> getOrgListByUser(Long userId) {
		String sql = "SELECT t2.ID as organizationId,t2.name as organizationName FROM tr_app_user_orgnization t1 INNER JOIN tb_organization t2 ON t1.org_id = t2.ID AND t1.user_id=" + userId + " AND t2.isEnabled=1";
		return getJdbcTemplate().queryForList(sql);
	}

	@Override
	public List<Map<String, Object>> getBuildList(int companyId) {
		StringBuffer strSQL = new StringBuffer("SELECT t1.buildId,t2.`name` AS buildName,t2.organizationId,t3.`name` AS organizationName");
		strSQL.append(" FROM tr_build_org t1 INNER JOIN tb_build t2 ON t1.buildId = t2.id AND t1.type='1' AND t1.orgId =" + companyId);
		strSQL.append(" INNER JOIN tb_organization t3 ON t2.organizationId = t3.ID");
		strSQL.append(" WHERE t2.isEnabled = '1' AND t3.isEnabled = '1' AND t3.status='1' ");
		return getJdbcTemplate().queryForList(strSQL.toString());
	}

	@Override
	public List<Map<String, Object>> getBuildListByDept(int deptId) {
		StringBuffer strSQL = new StringBuffer("SELECT t1.buildId,t2.`name` AS buildName,t2.organizationId,t3.`name` AS organizationName");
		strSQL.append(" FROM tr_build_org t1 INNER JOIN tb_build t2 ON t1.buildId = t2.id AND t1.type='2' AND t1.orgId =" + deptId);
		strSQL.append(" INNER JOIN tb_organization t3 ON t2.organizationId = t3.ID");
		strSQL.append(" WHERE t2.isEnabled = '1' AND t3.isEnabled = '1' AND t3.status='1' ");
		return getJdbcTemplate().queryForList(strSQL.toString());
	}

	@Override
	public void deleteBuildOrg(int orgId, String orgType) {
		String strHql = "delete from BuildOrgRelation where orgId=? and type=?";
		getSession().createQuery(strHql).setInteger(0, orgId).setString(1, orgType).executeUpdate();
	}

	public void deleteCompanyOrg(int orgId) {
		String strHql = "delete from CompanyRelationOrg where organizationId=?";
		getSession().createQuery(strHql).setInteger(0, orgId).executeUpdate();
	}

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

	@Override
	public List<Map<String, Object>> getAppUserOrganizationRelation(
			String companyIdStr) {
		StringBuffer strSQL = new StringBuffer("SELECT DISTINCT(t2.organizationId) as organizationId");
		strSQL.append(" FROM tr_build_org t1 INNER JOIN tb_build t2 on t1.buildId=t2.id");
		strSQL.append(" WHERE t1.type = '" + ElementConstant.USER_ORG_COMPANY + "' and t1.orgId in(" + companyIdStr + ")");
		return getJdbcTemplate().queryForList(strSQL.toString());
	}

	@Override
	public VirtualArchitecture getVirtualArchitectureById(int vaitualId) {
		return (VirtualArchitecture) getSession().get(VirtualArchitecture.class, vaitualId);
	}

	@Override
	public void delectOrgFloorRelationByUserId(String userId) {
		String strHql = "delete from AppUserFloorRelation where user_id=?";
		getSession().createQuery(strHql).setInteger(0, Integer.parseInt(userId)).executeUpdate();

	}

	@Override
	public List<Map<String, Object>> getOrganizationList(
			String organizationIdsStr) {
		StringBuffer strSQL = new StringBuffer("select id,name,cityName from tb_organization ");
		strSQL.append(" where id in(" + organizationIdsStr + ")");
		strSQL.append(" and isEnabled='1'");
		return getJdbcTemplate().queryForList(strSQL.toString());
	}

	@Override
	public List<VirtualArchitecture> getVirtualArchitectureList(
			String parentCode) {
		String                    strHQL = "from VirtualArchitecture where parentCode=? and isEnabled=?";
		List<VirtualArchitecture> list   = getSession().createQuery(strHQL).setString(0, parentCode).setInteger(1, 1).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list;
		}
		return null;
	}

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

	@Override
	public TbOrganization getOrganization(String codeNumber) {
		String               strHQL = "from TbOrganization where code=? and isEnabled=? AND status='1'";
		List<TbOrganization> list   = getSession().createQuery(strHQL).setString(0, codeNumber).setInteger(1, 1).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public VirtualArchitecture getVirtualArchitecture(String codeNumber) {
		String                    strHQL = "from VirtualArchitecture where codeNumber=? and isEnabled=?";
		List<VirtualArchitecture> list   = getSession().createQuery(strHQL).setString(0, codeNumber).setInteger(1, 1).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getAddVirtualArchitectureList(
			int organizationId, String type) {
		String strSQL = "SELECT * from tb_virtualarchitecture t1 where t1.id not in (SELECT t2.virtualArchitectureId from tb_contractiinformation t2 where t2.isEnabled=1 and t2.`status`=1)";
		strSQL += " and t1.organizationId=" + organizationId + " and t1.isEnabled=1 and t1.type=" + type;
		return getJdbcTemplate().queryForList(strSQL);
	}

	@Override
	public VirtualArchitecture getVirtualArchitectureByCustomerNumber(
			String customerNumber) {
		String                    strHQL = "from VirtualArchitecture where customerNumber=? and type='1' ";
		List<VirtualArchitecture> list   = getSession().createQuery(strHQL).setString(0, customerNumber).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getOrgExistCityList() {
		String strSQL = "select cityCode,cityName from tb_organization where isEnabled='1' GROUP BY cityCode";
		return getJdbcTemplate().queryForList(strSQL);
	}

	@Override
	public List<TbOrganization> getOrgByCityCode(String cityCode) {
		String strHQL = "from TbOrganization where cityCode=? and isEnabled='1'";
		return getSession().createQuery(strHQL).setString(0, cityCode).list();
	}

	@Override
	public List<Map<String, Object>> getVirtualarchitectureList(
			String paramValue, String organizationIds) {
		try {
			String strSQL = "SELECT  t1.organizationId ,t2.`name` from tb_virtualarchitecture t1 "
					+ " inner join tb_organization t2 on t1.organizationId=t2.ID"
					+ "  where (t1.customerNumber LIKE '%" + paramValue + "%' or t1.`name` like '%" + paramValue + "%')"
					+ " and t1.isEnabled=1 and t1.organizationId in (" + organizationIds + ") GROUP BY t1.organizationId";
			return getJdbcTemplate().queryForList(strSQL);
		} catch (Exception e) {
			LogExceptionStackUtil.LogExceptionStack(e);
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getVirtualArchitectureList(
			int organizationId, String type, String paramValue) {
		String strSQL = "select * from tb_virtualArchitecture where organizationId=" + organizationId + " and type=" + type + " and "
				+ " isEnabled=1 and (name like '%" + paramValue + "%' or customerNumber like '%" + paramValue + "%')";
		return getJdbcTemplate().queryForList(strSQL);
	}

	@Override
	public Page getVirtualArchitecturePage(int pageSize,
										   int pageNo, String wheresQL) {
		StringBuffer strSQL = new StringBuffer(" SELECT DISTINCT t1.`level`, ( SELECT GROUP_CONCAT(b.`name`) FROM tb_virtualarchitecture_agent a INNER JOIN tb_virtualarchitecture b ON a.agent_id = b.id WHERE a.id = t1.id ) AS agent_name , ( SELECT GROUP_CONCAT(b.id) FROM tb_virtualarchitecture_agent a INNER JOIN tb_virtualarchitecture b ON a.agent_id = b.id WHERE a.id = t1.id ) AS agent_id , ( SELECT GROUP_CONCAT(b.`name`) FROM tb_virtualarchitecture_agent a INNER JOIN tb_virtualarchitecture b ON a.id = b.id WHERE a.agent_id = t1.id ) AS relation_name , ( SELECT GROUP_CONCAT(b.id) FROM tb_virtualarchitecture_agent a INNER JOIN tb_virtualarchitecture b ON a.id = b.id WHERE a.agent_id = t1.id ) AS relation_id , ( SELECT GROUP_CONCAT(concat_ws(' ', d.`name`, c.`name`, b.`name`)) FROM tb_virtualarchitecture_range a INNER JOIN tb_roomno b ON a.room_id = b.id INNER JOIN tb_floor c ON b.floorId = c.id INNER JOIN tb_build d ON c.buildId = d.id WHERE a.id = t1.id ) AS range_name , ( SELECT GROUP_CONCAT(b.id) FROM tb_virtualarchitecture_range a INNER JOIN tb_roomno b ON a.room_id = b.id WHERE a.id = t1.id ) AS range_id, t1.leaseStatus, t1.contExpireTime AS contExpireTime, t3.NAME AS buildName, t1.floorNames , t1.roomNames, t1.id, t1.codeNumber, t1.customerNumber, t1.`name` , t1.phone, t1.address, t1.contractStatus, t1.customerType, t2.`name` AS organizationName , t1.creator, DATE_FORMAT(t1.createTime, '%Y-%m-%d %H:%i:%s') AS createTime, t1.modifier , DATE_FORMAT(t1.modifyTime, '%Y-%m-%d %H:%i:%s') AS modifyTime, t1.reserveBalance , t1.STATUS ");
		strSQL.append(" FROM tb_virtualarchitecture t1");
		strSQL.append(" INNER JOIN tb_organization t2 on t1.organizationId=t2.id");
		strSQL.append(" INNER JOIN tb_build t3 on t1.buildId=t3.id");
		strSQL.append(" LEFT JOIN tb_virtualarchitecture_agent t4 ON t1.id = t4.id");
		strSQL.append(" LEFT JOIN tb_virtualarchitecture t5 ON t4.agent_id = t5.id");
		strSQL.append(" WHERE t1.type = '1' AND t1.isEnabled = '1' AND t2.isEnabled='1' and t3.isEnabled='1'");
		strSQL.append(wheresQL);
		strSQL.append(" order by t1.createTime desc");
		return pagedQueryByJdbcTemplate(strSQL.toString(), pageNo, pageSize);
	}

	@Override
	public Map<String, Object> getVirtualArchitectureMap(
			String id) {
		StringBuffer strSQL = new StringBuffer("SELECT t1.id, t1.`name`, t1.`level` , ( SELECT GROUP_CONCAT(b.`name`) FROM tb_virtualarchitecture_agent a INNER JOIN tb_virtualarchitecture b ON a.agent_id = b.id WHERE a.id = t1.id ) AS agent_name , ( SELECT GROUP_CONCAT(b.id) FROM tb_virtualarchitecture_agent a INNER JOIN tb_virtualarchitecture b ON a.agent_id = b.id WHERE a.id = t1.id ) AS agent_id , ( SELECT GROUP_CONCAT(b.`name`) FROM tb_virtualarchitecture_agent a INNER JOIN tb_virtualarchitecture b ON a.id = b.id WHERE a.agent_id = t1.id ) AS relation_name , ( SELECT GROUP_CONCAT(b.id) FROM tb_virtualarchitecture_agent a INNER JOIN tb_virtualarchitecture b ON a.id = b.id WHERE a.agent_id = t1.id ) AS relation_id , ( SELECT GROUP_CONCAT(concat(d.`name`, '-', c.`name`, '楼', b.`name`)) FROM tb_virtualarchitecture_range a INNER JOIN tb_roomno b ON a.room_id = b.id INNER JOIN tb_floor c ON b.floorId = c.id INNER JOIN tb_build d ON c.buildId = d.id WHERE a.id = t1.id ) AS range_name , ( SELECT GROUP_CONCAT(b.id) FROM tb_virtualarchitecture_range a INNER JOIN tb_roomno b ON a.room_id = b.id WHERE a.id = t1.id ) AS range_id ");
		strSQL.append(" FROM tb_virtualarchitecture t1");
		strSQL.append(" INNER JOIN tb_organization t2 on t1.organizationId=t2.id");
		strSQL.append(" INNER JOIN tb_build t3 on t1.buildId=t3.id");
		strSQL.append(" WHERE t1.type = '1' AND t1.isEnabled = '1' AND t2.isEnabled='1' and t3.isEnabled='1'");
		strSQL.append(" AND t1.id=" + id);
		strSQL.append(" order by t1.createTime desc");
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(strSQL.toString());
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}


	@Override
	public String findCustomerNumberMaxId(String cityNameEng) {
		String customerNumber = "";
		String sql            = "select max(customerNumber) from tb_virtualarchitecture where customerNumber <>'' and customerNumber is not null  AND isEnabled='1' AND customerNumber like '" + cityNameEng + "%' ";
		Query  query          = getSession().createSQLQuery(sql);
		Object result         = query.uniqueResult();
		if (null != result && !result.equals("")) {
			customerNumber = String.valueOf(result);
		}
		return customerNumber;
	}

	@Override
	public VirtualArchitecture getVirtualArchitectureByCustomerNumber(
			String customerNumber, int virtualId) {
		String                    strHQL = "from VirtualArchitecture where customerNumber=? and type='1' and id!=? and isEnabled='1'";
		List<VirtualArchitecture> list   = getSession().createQuery(strHQL).setString(0, customerNumber).setInteger(1, virtualId).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getCityList() throws Exception {
		try {
			String strSQL = "SELECT t1.cityCode,t1.cityName,t2.description from tb_organization t1"
					+ " INNER JOIN tb_provincecity t2 on t1.cityCode=t2.cityCode and t2.type=2 and t1.status='1' where t1.isEnabled=1"
					+ " GROUP BY t2.cityCode ORDER BY t2.description";
			return this.getJdbcTemplate().queryForList(strSQL);
		} catch (Exception e) {
			LogExceptionStackUtil.LogExceptionStack(e);
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getVirtualarchitectureList(
			String organizationId, String type, String paramValue) {
		String strSQL = "SELECT * from tb_virtualarchitecture where organizationId=" + organizationId + " and status='1' and isEnabled='1' and type=" + type;
		if (StringUtils.isNotBlank(paramValue)) {
			strSQL += " and `name` LIKE '%" + paramValue + "%'";
		}
		strSQL += " ORDER BY convert(`name` using gbk)";
		return this.getJdbcTemplate().queryForList(strSQL);
	}

	@Override
	public List<Map<String, Object>> getOrganizationListByName(String paramValue, String cityCode, String orgIds) {
		String strSQL = "SELECT t1.ID,t1.`name` from tb_organization t1 where t1.cityCode='" + cityCode + "' and t1.isEnabled=1 and t1.status='1'";
		if (StringUtils.isNotBlank(paramValue)) {
			strSQL += " and t1.`name` like '%" + paramValue + "%'";
		}
		if (StringUtils.isNotBlank(orgIds)) {
			strSQL += " and t1.ID in (" + orgIds + ")";
		}
		strSQL += " order by  convert(t1.`name` using gbk)";
		return this.getJdbcTemplate().queryForList(strSQL);
	}

	@Override
	public List<Map<String, Object>> getProvincecityByCityList(String paramValue, String orgIds) {
		StringBuffer strBuffer = new StringBuffer();
		strBuffer.append("SELECT * from (SELECT t1.`name`,t1.cityCode,(SELECT COUNT(t2.ID) from tb_organization t2 where t2.cityCode=t1.cityCode and t2.isEnabled=1 and t2.status=1 and t2.ID in (" + orgIds + ")");
		if (StringUtils.isNotBlank(paramValue)) {
			strBuffer.append(" and t2.name like '%" + paramValue + "%'");
		}
		strBuffer.append(") organizationCount from tb_provincecity t1 WHERE t1.isEnabled=1 and t1.type=2 ORDER BY description) t3");
		strBuffer.append(" where t3.organizationCount!=0");
		log.info(strBuffer.toString());
		return this.getJdbcTemplate().queryForList(strBuffer.toString());
	}

	@Override
	public List<Map<String, Object>> getProvincecityList(String province) {
		String strSQL = "SELECT `name` text,provinceCode `value` from tb_provincecity  where `name`!='" + province + "' and type=1";
		return this.getJdbcTemplate().queryForList(strSQL);
	}

	@Override
	public Map<String, Object> getProvincecityByName(String province) {
		String                    strSQL = "SELECT `name` text,provinceCode `value` from tb_provincecity  where `name`='" + province + "' and type=1";
		List<Map<String, Object>> list   = this.getJdbcTemplate().queryForList(strSQL);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getCityList(String provinceCode) {
		String strSQL = "SELECT cityCode `value`,`name` text  from tb_provincecity where  provinceCode=" + provinceCode + " and type=2";
		return this.getJdbcTemplate().queryForList(strSQL);
	}

	@Override
	public Map<String, Object> getCityObect(String provinceCode, String city) {
		String                    strSQL = "SELECT cityCode `value`,`name` text  from tb_provincecity where `name`='" + city + "' and provinceCode=" + provinceCode + " and type=2";
		List<Map<String, Object>> list   = this.getJdbcTemplate().queryForList(strSQL);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getCityList(String provinceCode,
												 String city) {
		String strSQL = "SELECT cityCode `value`,`name` text  from tb_provincecity where `name`!='" + city + "' and  provinceCode=" + provinceCode + " and type=2";
		return this.getJdbcTemplate().queryForList(strSQL);
	}

	@Override
	public ProvinceCity findProvinceCityByName(String paramName, String type) {
		String strHQL = "from ProvinceCity where type=? and name=? and isEnabled=?";
		List<ProvinceCity> list = this.getSession().createQuery(strHQL).setString(0, type).setString(1, paramName)
				.setString(2, "1").list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<ProvinceCity> getProvinceCityList(String type, String code) {
		String strHQL = "";
		if ("1".equals(type)) {
			strHQL = "from ProvinceCity where type='" + type + "' and isEnabled='1' order by description asc";
		} else {
			strHQL = "from ProvinceCity where type='" + type + "' and provinceCode='" + code + "' and isEnabled='1' order by description asc";
		}
		return getSession().createQuery(strHQL).list();
	}

	@Override
	public List<TbBuild> getBuildByOrgIdAndBuildName(int organizationId,
													 String buildName) {
		String        strHql = "from TbBuild where isEnabled=? and organizationId=? and name=? order by name asc";
		List<TbBuild> list   = getSession().createQuery(strHql).setString(0, ElementConstant.YESORNO_1).setInteger(1, organizationId).setString(2, buildName).list();
		return list;
	}

	@Override
	public CompanyRelationOrg getCompanyByOrgId(Integer orgId) {
		String                   strHQL = "from CompanyRelationOrg where organizationId=?";
		List<CompanyRelationOrg> list   = getSession().createQuery(strHQL).setInteger(0, orgId).list();
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 企业结算方式(1现结2月结)
	 * 查询月结果or 剩余时长 >0 的企业
	 */
	@Override
	public List<Map<String, Object>> getVirtualListBySettleMonth(
			int organizationId, String companyName) {

		StringBuilder sql = new StringBuilder("").append("\n");
		sql.append("select vir.id,vir.floorIds,vir.name,vir.settleType,remainderTime,acc.id virAccountId").append("\n");
		sql.append("from tb_virtualarchitecture  vir").append("\n");
		sql.append("left join tb_vir_account acc on vir.id=acc.virId").append("\n");
		sql.append("where organizationId='" + organizationId + "' and isEnabled=1  and acc.id is not null and settleType <> 0 and  (settleType=2 or remainderTime>=1) ").append("\n");

		if (StringUtils.isNotEmpty(companyName)) {
			sql.append(" and vir.name like '%" + companyName + "%' ").append("\n");
		}

		sql.append("order by  floorIds,id").append("\n");

		//String sql="select id,floorIds,name,settleType,remainderTime from tb_virtualarchitecture    where organizationId='"+organizationId+"' and isEnabled=1  and  (settleType=2 or remainderTime>0) order by  floorIds,id";
		//return getSession().createQuery(strHQL).setInteger(0, organizationId).setString(1, ElementConstant.YESORNO_1).list();
		return getJdbcTemplate().queryForList(sql.toString());
	}

	//只查月结
	@Override
	public List<Map<String, Object>> getVirtualListBySettleMonthAir(
			int organizationId, String companyName) {

		StringBuilder sql = new StringBuilder("").append("\n");
		sql.append("select vir.id,vir.floorIds,vir.name,vir.settleType,remainderTime,acc.id virAccountId").append("\n");
		sql.append("from tb_virtualarchitecture  vir").append("\n");
		sql.append("left join tb_vir_account acc on vir.id=acc.virId").append("\n");
		sql.append("where organizationId='" + organizationId + "' and isEnabled=1 and acc.id is not null and settleType=2 ").append("\n");

		if (StringUtils.isNotEmpty(companyName)) {
			sql.append(" and vir.name like '%" + companyName + "%' ").append("\n");
		}

		sql.append("order by  floorIds,id").append("\n");

		//String sql="select id,floorIds,name,settleType,remainderTime from tb_virtualarchitecture    where organizationId='"+organizationId+"' and isEnabled=1  and  (settleType=2 or remainderTime>0) order by  floorIds,id";
		//return getSession().createQuery(strHQL).setInteger(0, organizationId).setString(1, ElementConstant.YESORNO_1).list();
		return getJdbcTemplate().queryForList(sql.toString());
	}

	@Override
	public Integer getSuperDeskOrgId(Integer id) {
		String sql = " SELECT cjqtOrgId FROM tr_llzc_cjqt_org WHERE llzcOrgId=?";
		return (Integer) getSession().createSQLQuery(sql).setInteger(0, id).uniqueResult();
	}

	@Override
	public void deleteVirtualArchAgentById(Integer id) {
		String strHql = "delete from VirtualArchitectureAgent where id=?";
		getSession().createQuery(strHql).setInteger(0, id).executeUpdate();

	}

	@Override
	public void deleteVirtualArchAgentByAgentId(Integer agentId) {
		String strHql = "delete from VirtualArchitectureAgent where agent_id=?";
		getSession().createQuery(strHql).setInteger(0, agentId).executeUpdate();

	}

	@Override
	public void deleteVirtualArchRangeById(Integer id) {
		String strHql = "delete from VirtualArchitectureRange where id=?";
		getSession().createQuery(strHql).setInteger(0, id).executeUpdate();

	}

	@Override
	public void saveVirtualArchAgent(Integer id, Integer agent_id) {
		String strHql = "INSERT INTO tb_virtualarchitecture_agent (id, agent_id) value (?, ?)";
		getSession().createQuery(strHql).setInteger(0, id).setInteger(1, agent_id).executeUpdate();

	}

	@Override
	public List<TbOrganization> getScopeOrgList(ScopeEntity scopeEntity, String developId) {
		String sql = "SELECT " +
				" * " +
				"FROM " +
				" tb_organization " +
				"WHERE " +
				" isEnabled = 1 " +
				"AND lng >=? " +
				"AND lng <=? " +
				"AND lat >=? " +
				"AND lat <=? " +
				"AND ID IN ( " +
				" SELECT " +
				" organizationId " +
				" FROM " +
				" tr_wxpublicno_org " +
				" WHERE " +
				" publicNoId = ( " +
				" SELECT " +
				" ID " +
				" FROM " +
				" tb_wxpublicno " +
				" WHERE " +
				" developId = ? " +
				" ) " +
				")";

		return getSession().createSQLQuery(sql)
				.addEntity(TbOrganization.class)
				.setDouble(0, scopeEntity.getMinlng())
				.setDouble(1, scopeEntity.getMaxlng())
				.setDouble(2, scopeEntity.getMinlat())
				.setDouble(3, scopeEntity.getMaxlat())
				.setString(4, developId)
				.list();
	}
}
