package com.palmnest.dao.estateserve.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.palmnest.dao.estateserve.BillOperationRecodesDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.emig.BillOperationRecodes;

@Repository("billOperationRecodesDao")
public class BillOperationRecodesDaoImpl extends GenericDaoHibernate<BillOperationRecodes, Integer> implements BillOperationRecodesDao {

	public BillOperationRecodesDaoImpl() {
		super(BillOperationRecodes.class);
	}

	@Override
	public BillOperationRecodes getRecodeBySourId(Long sourId, Integer type,
												  Integer operationType) {
		StringBuilder sql = new StringBuilder("from BillOperationRecodes where sourId=? and type=? and operationType=? ");
		List<BillOperationRecodes> list = this.getSession().createQuery(sql.toString()).setLong(0, sourId).setInteger(1, type).setInteger(2, operationType).list();
		BillOperationRecodes bill = null;
		if (list != null && list.size() > 0) {
			bill = list.get(0);
		}
		return bill;
	}

	@Override
	public List<BillOperationRecodes> getRecodeBySourId(Long sourId,
														Integer type) {
		StringBuilder sql = new StringBuilder("from BillOperationRecodes where sourId=? and type=? ");
		List<BillOperationRecodes> list = this.getSession().createQuery(sql.toString()).setLong(0, sourId).setInteger(1, type).list();
		return list;
	}

	@Override
	public Integer deleteRecode(Long orderId, Integer type) throws Exception {
		String sql = "delete from BillOperationRecodes where sourId=? and type=? ";
		return this.getSession().createQuery(sql).setLong(0, orderId).setInteger(1, type).executeUpdate();
	}

	@Override
	public BillOperationRecodes getRecodeBySourId(Long sourId, Integer type,
												  String operationType) {
		StringBuilder sql = new StringBuilder("from BillOperationRecodes where sourId=? and type=? and operationType in (" + operationType + ")  ");
		List<BillOperationRecodes> list = this.getSession().createQuery(sql.toString()).setLong(0, sourId).setInteger(1, type).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}


}
