package com.palmnest.dao.dorm.impl;

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

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.DormCheckOutDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.dorm.DormCheckOut;

/**
 * 退宿申请单DAO实现类
 *
 * @author longmeizhen
 */
@Repository("dormCheckOutDao")
public class DormCheckOutDaoImpl extends GenericDaoHibernate<DormCheckOut, Long> implements DormCheckOutDao {
	private static Logger LOG = LoggerFactory.getLogger(DormCheckOutDaoImpl.class);

	public DormCheckOutDaoImpl() {
		super(DormCheckOut.class);
	}

	@Override
	public Page getDormCheckOutList(DormRoomerListDto dto, int pageNo,
									int pageSize) {
		StringBuilder sql = new StringBuilder("");
		sql.append(" select dc.id,dc.dormId,dc.number,dr.userName,dr.workNo,dr.sex,dr.IDcard,dr.mobile ");
		sql.append(" ,dr.apartment,dr.floor,dr.build,dr.room,dr.bedNo,dc.`status` ");
		sql.append(" ,date_format(dc.createTime,'%Y-%m-%d %T') createTime ");
		sql.append(" from tb_dorm_checkout dc ");
		sql.append(" left join tb_dorm_roomerinfo dr on dc.dormId=dr.id ");
		sql.append(" where 1=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() + "%' ");
		}
		if (StringUtils.isNotEmpty(dto.getStatus())) {
			if ("1".equals(dto.getStatus())) {//待审核
				sql.append(" and dc.status='1' ");
			} else if ("2".equals(dto.getStatus())) {//待放行
				sql.append(" and dc.status='2' ");
			} else if ("3".equals(dto.getStatus())) {//已放行
				sql.append(" and dc.status='3' ");
			} else if ("4".equals(dto.getStatus())) {//已拒绝(审核不通过，放行不通过)
				sql.append(" and dc.status in ('4','5') ");
			} else if ("6".equals(dto.getStatus())) {//已拒绝(放行)
				sql.append(" and dc.status in ('5') ");
			} else if ("7".equals(dto.getStatus())) {//已拒绝(审核不通过)
				sql.append(" and dc.status in ('4') ");
			}
		}
		sql.append(" order by  dc.status,dc.createTime desc");
		return pagedQueryByJdbcTemplate(sql.toString(), pageNo, pageSize);
	}

	@Override
	public DormCheckOut get(Long id) {
		return super.get(id);
	}

	/**
	 * 获取微信端的退宿申请列表
	 *
	 * @param openId
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getCheckOutListByCreator(String orgId, String idCard, String mobile) {
		StringBuilder sql = new StringBuilder("");
		sql.append(" select dc.id,dc.goodsInfo,date_format(dc.createTime,'%Y-%m-%d %T') createTime,dc.number,dc.roomieNo,dc.status from tb_dorm_checkout dc  ");
		sql.append(" left join tb_dorm_roomerinfo dr on dr.id=dc.dormId ");
		sql.append(" where  dc.orgId='" + orgId + "'  ");
		sql.append(" and dr.IDcard='" + idCard + "'  ");
		sql.append(" order by dc.status,dc.createTime desc ");
//		sql.append("from DormCheckOut where creator=? and orgId=? order by createTime desc ");
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
		return list;
	}

	/**
	 * 判断该项目下该用户是否还存在未处理完的退房信息
	 *
	 * @param openId
	 * @param orgId
	 * @return
	 */
	@Override
	public int hasCheckOutByStatus(String orgId, String idCard, String mobile) {
		StringBuilder sql = new StringBuilder("");
		sql.append(" select dc.id from tb_dorm_checkout dc  ");
		sql.append(" left join tb_dorm_roomerinfo dr on dr.id=dc.dormId ");
		sql.append(" where  dc.orgId='" + orgId + "' and (dc.status ='1' or dc.status='2' )  ");
		sql.append(" and dr.IDcard='" + idCard + "'   ");
		sql.append(" order by dc.createTime desc ");
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
		if (list != null && list.size() > 0) {
			return 1;
		}
		return 0;
	}

	/**
	 * 获取退宿申请status状态下的数量
	 */
	@Override
	public int getDormCheckOutIndex(String status, String orgIds) {
		StringBuilder sql = new StringBuilder("");
		if (StringUtils.isNotEmpty(orgIds)) {
			sql.append(" select COUNT(id) from tb_dorm_checkout  where orgId in (" + orgIds + ") and status=" + status);
			String count = getSession().createSQLQuery(sql.toString()).uniqueResult().toString();
			return Integer.parseInt(count);
		}
		return 0;
	}
}
