package com.palmnest.dao.estateserve.impl;

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

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

import com.palmnest.application.dto.CommonDto;
import com.palmnest.dao.estateserve.EmigrationDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.EnterpriseInfo;
import com.palmnest.model.emig.EmigrationSettle;
import com.palmnest.model.property.VirtualArchitecture;

@Repository("emigrationDao")
public class EmigrationDaoImpl extends GenericDaoHibernate<EnterpriseInfo, Integer> implements EmigrationDao {

	public EmigrationDaoImpl() {
		super(EnterpriseInfo.class);
	}

	@Override
	public List<CommonDto> getFloorVirList(String floorId)
			throws Exception {
		List<CommonDto> list = new ArrayList<CommonDto>();
		List<VirtualArchitecture> userList = new ArrayList<VirtualArchitecture>();
		if (StringUtils.isNotEmpty(floorId)) {
			String userHql = "from VirtualArchitecture where floorIds like '%" + floorId + ";%' and isEnabled='1' and leaseStatus=1 ";
			userList = getSession().createQuery(userHql).list();
			for (int i = 0; i < userList.size(); i++) {
				VirtualArchitecture virInfo = userList.get(i);
				String floor = virInfo.getFloorIds();
				if (StringUtils.isNotEmpty(floor)) {
					String[] str = floor.split(";");
					for (int k = 0; k < str.length; k++) {
						if (floorId.equals(str[k])) {
							CommonDto dto = new CommonDto();
							dto.setId(Long.parseLong(String.valueOf(virInfo.getId())));
							dto.setName(virInfo.getName());
							list.add(dto);
						}
					}
				}

			}
		}
		return list;
	}

	@Override
	public EmigrationSettle getEmigrationSettle(int emigId) throws Exception {
		List<EmigrationSettle> list = new ArrayList<EmigrationSettle>();
		String userHql = "from EmigrationSettle where isEnabled='1' and enterId=" + emigId;
		list = getSession().createQuery(userHql).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
}
