package com.palmnest.dao.estateserve.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

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

import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.LogExceptionStackUtil;
import com.palmnest.dao.estateserve.GeneralBillsDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.bill.GeneralBillOrders;
import com.palmnest.model.bill.GeneralBills;
import com.palmnest.model.bill.GeneralbillService;

@Repository("generalBillsDao")
public class GeneralBillsDaoImpl extends GenericDaoHibernate<Object, Integer> implements GeneralBillsDao {

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

	@Override
	public Page generalBillsPage(Integer pageSzie, Integer pageNo,
								 Integer virtualId, String dateTime) throws Exception {
		try {
			String strSQL = "SELECT t1.outMonth,t1.totalFee,if(t1.payFee is NULL, 0,t1.payFee) payFee,if(t1.dedFee is NULL,0,t1.dedFee) dedFee,"
					+ "t1.ID,DATE_FORMAT(t1.createTime,'%Y-%m-%d') createTime,t1.creator,t1.payStatus,DATE_FORMAT(t1.modifyTime,'%Y-%m-%d') modifyTime,t1.modifier modifier from tb_generalbills t1 where 1=1";
			if (virtualId != null) {
				strSQL += "  and t1.virtualId=" + virtualId;
			}
			if (StringUtils.isNotBlank(dateTime)) {
				strSQL += "  and t1.outMonth='" + dateTime + "'";
			}
			return pagedQueryByJdbcTemplate(strSQL, pageNo, pageSzie);
		} catch (Exception e) {
			LogExceptionStackUtil.LogExceptionStack(e);
		}
		return null;
	}

	@Override
	public Page getDepositRecordPage(Integer pageSzie, Integer pageNo,
									 Integer virtualId) throws Exception {
		try {
			String strSQL = "SELECT DATE_FORMAT(t1.createTime,'%Y-%m-%d %k:%i:%s') createTime,t1.`status`,t1.amountFee,"
					+ "t1.remark,t1.creator from tb_deposit_record t1 where t1.virtualId=" + virtualId + ""
					+ " ORDER BY t1.createTime desc";
			return pagedQueryByJdbcTemplate(strSQL, pageNo, pageSzie);
		} catch (Exception e) {
			LogExceptionStackUtil.LogExceptionStack(e);
		}
		return null;
	}

	@Override
	public BigDecimal getArrearsRecord(String mothTime,
									   Integer virtualId) throws Exception {
		try {
			String strSQL = "SELECT if(SUM(totalFee) is null,0,SUM(totalFee)) totalFee from tb_generalbills t1 where t1.virtualId=" + virtualId + " and t1.outMonth<='" + mothTime + "' and t1.payStatus=0";
			BigDecimal totalFee = this.getJdbcTemplate().queryForObject(strSQL, BigDecimal.class);
			return totalFee;
		} catch (Exception e) {
			LogExceptionStackUtil.LogExceptionStack(e);
		}
		return null;
	}

	@Override
	public GeneralBills getGeneralBillsById(Integer id) throws Exception {
		try {
			String strHQL = "from GeneralBills where isEnabled=? and id=?";
			List<GeneralBills> list = this.getSession().createQuery(strHQL).setString(0, "1").setInteger(1, id).list();
			if (list != null && list.size() > 0) {
				return list.get(0);
			}
		} catch (Exception e) {
			LogExceptionStackUtil.LogExceptionStack(e);
		}
		return null;
	}

	@Override
	public List<GeneralbillService> getGeneralbillServiceList(Integer billId)
			throws Exception {
		try {
			String strHQL = "from GeneralbillService where billId=?";
			return this.getSession().createQuery(strHQL).setInteger(0, billId).list();
		} catch (Exception e) {
			LogExceptionStackUtil.LogExceptionStack(e);
		}
		return null;
	}

	@Override
	public List<GeneralBillOrders> geteneralBillOrdersList(Integer billId,
														   String serviceCode) throws Exception {
		try {
			String strHQL = "from GeneralBillOrders where serviceCode=? and billId=? and isEnabled=?";
			return this.getSession().createQuery(strHQL).setString(0, serviceCode).setInteger(1, billId)
					.setString(2, "1").list();
		} catch (Exception e) {
			LogExceptionStackUtil.LogExceptionStack(e);
		}
		return null;
	}

	@Override
	public GeneralbillService getGeneralbillServiceById(Integer id)
			throws Exception {
		try {
			String strHQL = "from GeneralbillService where  id=?";
			List<GeneralbillService> list = this.getSession().createQuery(strHQL).setInteger(0, id).list();
			if (list != null && list.size() > 0) {
				return list.get(0);
			}
		} catch (Exception e) {
			LogExceptionStackUtil.LogExceptionStack(e);
		}
		return null;
	}

	@Override
	public GeneralBillOrders geteneralBillOrdersById(Integer id) throws Exception {
		try {
			String strHQL = "from GeneralBillOrders where  id=?";
			List<GeneralBillOrders> list = this.getSession().createQuery(strHQL).setInteger(0, id).list();
			if (list != null && list.size() > 0) {
				return list.get(0);
			}
		} catch (Exception e) {
			LogExceptionStackUtil.LogExceptionStack(e);
		}
		return null;
	}

	@Override
	public void deleteGeneralbillService(Integer id) throws Exception {
		String strHQL = "DELETE from GeneralbillService where id=?";
		this.getSession().createQuery(strHQL).setInteger(0, id).executeUpdate();

	}

	@Override
	public void deleteGeneralBillOrders(Integer id) throws Exception {
		String strHQL = "DELETE from GeneralBillOrders where id=?";
		this.getSession().createQuery(strHQL).setInteger(0, id).executeUpdate();

	}

	@Override
	public List<GeneralBills> getGeneralBillsList(int virtualId,
												  String payStatus) throws Exception {
		String strHQL = "from GeneralBills where virtualId=? and payStatus=? order by outMonth desc";
		List<GeneralBills> list = getSession().createQuery(strHQL).setInteger(0, virtualId).setString(1, payStatus).list();
		return list;
	}

	@Override
	public List<Map<String, Object>> getHistoryBill(Integer generalBillsId, String starTime,
													String endTime, String organizationId, String virtualId) throws Exception {
		String strSQL = "SELECT ID,outMonth,totalFee,payStatus,DATE_FORMAT(payTime,'%Y-%m-%d') payTime FROM tb_generalbills WHERE"
				+ " outMonth BETWEEN '" + starTime + "' and '" + endTime + "' and outMonth!='" + endTime + "' and organizationId=" + organizationId + " and virtualId=" + virtualId
				+ " order by outMonth desc";
		return this.getJdbcTemplate().queryForList(strSQL);
	}
}
