package com.palmnest.dao.dorm.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import cn.jiguang.common.utils.StringUtils;

import com.palmnest.application.bean.Page;
import com.palmnest.application.dto.input.DormRoomerListDto;
import com.palmnest.dao.dorm.DormRoomerDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.dorm.DormRoomerInfo;

/**
 * 宿舍管理信息DAO实现类
 *
 * @author longmeizhen
 */
@Repository("dormRoomerDao")
public class DormRoomerDaoImpl extends GenericDaoHibernate<DormRoomerInfo, Long> implements DormRoomerDao {
	private static Logger LOG = LoggerFactory.getLogger(DormRoomerDaoImpl.class);

	public DormRoomerDaoImpl() {
		super(DormRoomerInfo.class);
	}

	/**
	 * 获取宿舍管理信息列表
	 *
	 * @param dto界面上的参数
	 * @param pageNo
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	@Override
	public Page findList(DormRoomerListDto dto, int pageNo, int pageSize)
			throws Exception {
		StringBuilder sql = new StringBuilder("");
		sql.append(" select dr.id,dr.orgId,org.`name` orgName,dr.build ");
		sql.append(" ,dr.floor,dr.room,bedNo,dr.apartment ");
		sql.append(" ,sex,workNo,userName,IDcard,mobile ");
		sql.append("  from tb_dorm_roomerinfo dr ");
		sql.append(" left join tb_organization org on org.id=dr.orgId ");
		sql.append(" where 1=1 and dr.status='1' and dr.isEnabled='1'");
		if (StringUtils.isNotEmpty(dto.getOrgId())) {
			sql.append(" and dr.orgId in (" + dto.getOrgId() + ") ");
		}
		if (StringUtils.isNotEmpty(dto.getUserName())) {
			sql.append(" and dr.userName like '%" + dto.getUserName() + "%' ");
		}
		if (StringUtils.isNotEmpty(dto.getMobile())) {
			sql.append(" and dr.mobile like '%" + dto.getMobile() + "%' ");
		}
		if (StringUtils.isNotEmpty(dto.getApartment())) {
			sql.append(" and dr.apartment  like '%" + dto.getApartment() + "%' ");
		}
		if (StringUtils.isNotEmpty(dto.getBuild())) {
			sql.append(" and dr.build  like '%" + dto.getBuild() + "%' ");
		}
		if (StringUtils.isNotEmpty(dto.getFloor())) {
			sql.append(" and dr.floor like '%" + dto.getFloor() + "%' ");
		}
		if (StringUtils.isNotEmpty(dto.getRoom())) {
			sql.append(" and dr.room like '%" + dto.getRoom() + "%' ");
		}
		sql.append(" order by dr.createTime desc ");
		return pagedQueryByJdbcTemplate(sql.toString(), pageNo, pageSize);
	}

	/**
	 * 根据项目、身份证及手机号查找员工入宿信息
	 */
	@Override
	public DormRoomerInfo getDormInfoByIDcardOrMobile(String orgId,
													  String idCard, String mobile) {
		StringBuilder sql = new StringBuilder("");
		sql.append(" from DormRoomerInfo where isEnabled='1' and orgId=?   ");
		if (StringUtils.isNotEmpty(idCard)) {
			if (StringUtils.isNotEmpty(mobile)) {
				sql.append(" and (mobile='" + mobile + "'  or  IDcard='" + idCard + "' ) ");
			} else {
				sql.append("  and  IDcard='" + idCard + "'");
			}
		} else {
			if (StringUtils.isNotEmpty(mobile)) {
				sql.append(" and mobile='" + mobile + "'");
			}
		}
		List<DormRoomerInfo> dormList = getSession().createQuery(sql.toString()).setString(0, orgId).list();
		if (dormList != null && dormList.size() > 0) {
			return dormList.get(0);
		}
		return null;
	}

	/**
	 * 根据项目、身份证、电话号码查找入宿信息
	 *
	 * @param orgId
	 * @param idCard
	 * @param mobile
	 * @return
	 */
	@Override
	public DormRoomerInfo getDormInfoByOrdId(String orgId, String idCard,
											 String mobile) {
		StringBuilder sql = new StringBuilder("");
		sql.append(" from DormRoomerInfo where isEnabled='1' and orgId=? and IDcard=? and status='1' ");
		List<DormRoomerInfo> dormList = getSession().createQuery(sql.toString()).setString(0, orgId).setString(1, idCard).list();
		if (dormList != null && dormList.size() > 0) {
			return dormList.get(0);
		}
		return null;
	}

	/**
	 * 根据openId获取登录的员工入宿信息
	 *
	 * @param openId
	 * @return
	 */
	@Override
	public DormRoomerInfo getDormInfoByOpenId(String openId, String orgId) {
		StringBuilder sql = new StringBuilder("");
		sql.append(" from DormRoomerInfo where isEnabled='1' and openId=? and status='1' and orgId=? ");
		List<DormRoomerInfo> dormList = getSession().createQuery(sql.toString()).setString(0, openId).setString(1, orgId).list();
		if (dormList != null && dormList.size() > 0) {
			return dormList.get(0);
		}
		return null;
	}

	/**
	 * 根据公寓、楼宇、楼层、房间号以及项目获取全部舍友（排除自己）
	 * (如果维护了id之后可直接根据房间号id获取对应的数据，现无法满足2018-09-26)
	 * 备注：避免不同楼宇楼层房间名称一样的情况，因此需要这些参数一起做校验
	 *
	 * @param orgId     项目
	 * @param apartment 公寓
	 * @param build     楼宇
	 * @param floor     楼层
	 * @param room      房间
	 * @return
	 */
	@Override
	public List<DormRoomerInfo> getRoomerByRoomInfo(Long id, String orgId,
													String apartment, String build, String floor, String room) {
		StringBuilder sql = new StringBuilder("");
		sql.append(" from DormRoomerInfo where isEnabled='1' and status='1' and  orgId=? and apartment=? and build=? and floor=? and room=? and id<>?");
		List<DormRoomerInfo> dormList = getSession().createQuery(sql.toString()).setString(0, orgId).setString(1, apartment).setString(2, build).setString(3, floor).setString(4, room).setLong(5, id).list();
		return dormList;
	}

}
