package com.palmnest.dao.estateserve.impl;


import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;


import com.palmnest.application.Constants;
import com.palmnest.application.bean.Page;
import com.palmnest.dao.estateserve.ServiceTypeInfoDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.AppUserOrgnizationRelation;
import com.palmnest.model.estateserve.ServiceTypeInfo;
import com.palmnest.webapp.controller.backstage.VisitorOrderInfoController;


@Repository("serviceTypeInfoDao")
public class ServiceTypeInfoDaoImpl extends GenericDaoHibernate<ServiceTypeInfo, Integer> implements ServiceTypeInfoDao {

	private Logger LOG = LoggerFactory.getLogger(ServiceTypeInfoDaoImpl.class);

	public ServiceTypeInfoDaoImpl() {
		super(ServiceTypeInfo.class);
	}

	@Override
	public Page serviceTypeInfoPage(Integer pageNo, Integer pageSize,
									String whereSql) throws Exception {
		try {
			String strSQL = "SELECT * from tb_servicetypeinfo";
			if (StringUtils.isNotBlank(whereSql)) {
				strSQL += whereSql;
			}
			return pagedQueryByJdbcTemplate(strSQL, pageNo, pageSize);
		} catch (Exception e) {
			LOG.error("dao find ServiceTypeInfoPage error{}", e);
		}
		return null;
	}

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

	@Override
	public void deleteServiceTypeInfo(String orgId) {
		String strSQL = "delete from tb_serviceTypeInfo where projectId=" + orgId;
		this.getJdbcTemplate().execute(strSQL);

	}

	@Override
	public List<ServiceTypeInfo> getServiceTypeInfoByShkipAddress(
			String shkipAddress) {
		String strHql = "from ServiceTypeInfo where skipAddress like '%" + shkipAddress + "%'";
		List<ServiceTypeInfo> list = getSession().createQuery(strHql).list();
		return list;
	}

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

	@Override
	public ServiceTypeInfo getServiceTypeInfo(String serviceCode) {
		String strHQL = "from ServiceTypeInfo where isEnabled='1' and code=? ";
		List<ServiceTypeInfo> list = this.getSession().createQuery(strHQL).setString(0, serviceCode).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public ServiceTypeInfo getServiceTypeInfoByServiceCode(String serviceCode) {
		String strHQL = "from ServiceTypeInfo where isEnabled='1' and serviceCode=? ";
		List<ServiceTypeInfo> list = this.getSession().createQuery(strHQL).setString(0, serviceCode).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

}
