package com.palmnest.dao.estateserve.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.palmnest.application.bean.Page;
import com.palmnest.dao.estateserve.PushBillDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.PushBill;
import com.palmnest.model.PushModel;

/**
 * 账单推送的DAO实现类
 *
 * @author longmeizhen
 */
@Repository("pushBillDao")
public class PushBillDaoImpl extends GenericDaoHibernate<PushBill, Integer> implements PushBillDao {

	private static Logger LOG = LoggerFactory.getLogger(PushBillDaoImpl.class);

	public PushBillDaoImpl() {
		super(PushBill.class);
	}

	/**
	 * 获取账单推送列表信息
	 */
	@Transactional
	@Override
	public List<Map<String, Object>> getPushBillList(Map<String, Object> map)
			throws Exception {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		StringBuffer sbf = new StringBuffer();
		try {
			sbf.append(" select * from tb_billpush where 1=1 ");
			String beginTime = map.get("beginTime").toString();//开始时间
			String endTime = map.get("endTime").toString();//结束时间
			String billType = map.get("billType").toString();//账单类型
			String build = map.get("build").toString();//楼宇
			String floors = map.get("floors").toString();//楼层数组
			Query query = getSession().createSQLQuery(sbf.toString()).addEntity(PushBill.class);
			list = query.list();
		} catch (Exception e) {
			LOG.error(e.getMessage());
			throw new Exception(e);
		}
		return list;
	}

	/**
	 * 新增账单推送
	 */
	@Override
	public PushBill addPushBill(PushBill push) throws Exception {
		return super.save(push);
	}

	@Override
	public Page queryBillPushList(String proId, String beginTime, String endTime, String billType,
								  String buildId, String floorIds, String virName, int pageNo, int pageSize)
			throws Exception {
		StringBuffer strSQL = new StringBuffer("  select IFNULL(bill.buildName,'') buildName,bill.id,bill.monthDate monthDate,DATE_FORMAT(bill.beginTime,'%Y-%m-%d') beginTime,DATE_FORMAT(bill.endTime,'%Y-%m-%d') endTime,DATE_FORMAT(bill.billUpDate,'%Y-%m-%d') billUpDate,DATE_FORMAT(bill.createTime,'%Y-%m-%d %T') createTime,org.`name` orgName,bill.roomName,bill.virNumber");
		strSQL.append("    ,bill.virName virName,bill.billType,bill.pushUser1,bill.pushUser2,bill.pushAmount,REPLACE(ifnull(bill.wechatData,''),',','，') wechatData ");
		strSQL.append("    ,bill.wechatStatus,REPLACE(ifnull(bill.messageData,''),',','，') messageData ,bill.messageStatus,REPLACE(ifnull(bill.emailData,''),',','，') emailData,bill.emailStatus from tb_billpush bill ");
//		strSQL.append("    left join tb_virtualarchitecture vir on bill.virtId=vir.id ");
		strSQL.append("    left join tb_organization org on bill.orgId=org.id");
//		strSQL.append("    left join tb_build build on build.id=vir.buildId");
		strSQL.append("    where 1=1");
		if (StringUtils.isNotBlank(beginTime)) {
			beginTime = beginTime + " 00:00:00";
			if (StringUtils.isNotBlank(endTime)) {
				endTime = endTime + " 23:59:59";
				strSQL.append("    and bill.createTime BETWEEN '" + beginTime + "' and '" + endTime + "' ");
			} else {
				strSQL.append("    and bill.createTime >='" + beginTime + "'");
			}
		} else {
			if (StringUtils.isNotBlank(endTime)) {
				strSQL.append("    and bill.createTime<='" + endTime + "' ");
			}
		}
		if (StringUtils.isNotBlank(billType)) {
			strSQL.append("    and bill.billType=" + billType);
		}
//		if(StringUtils.isNotBlank(buildId)&&!"0".equals(buildId)){
//			strSQL.append("    and build.id="+buildId);
//		}
		if (StringUtils.isNotBlank(proId) && !"0".equals(proId)) {
			String[] orgStr = proId.split(";");
			String str = "";
			if (orgStr != null && orgStr.length > 0) {
				for (int i = 0; i < orgStr.length; i++) {
					str = str + orgStr[i] + ",";
				}
			}
			if (StringUtils.isNotEmpty(str)) {
				str = str.substring(0, str.length() - 1);
				strSQL.append(" and org.id in (" + str + ") ");
			}

		}
//		if(StringUtils.isNotBlank(floorIds)){
//			floorIds=floorIds.replace(";", ",");
//			String[] floor=floorIds.split(",");
//			strSQL.append("    and ( ");
//			for(int i=0;i<floor.length;i++){
//				if(i==0){
//					strSQL.append("  vir.floorIds like '%"+floor[i]+"%' ");
//				}else{
//					strSQL.append("  or vir.floorIds like '%"+floor[i]+"%' ");
//				}
//			}
//			strSQL.append(" ) ");
//		}
//		
		if (StringUtils.isNotBlank(virName)) {
			strSQL.append("   and bill.virName like '%" + virName + "%' ");
		}
		strSQL.append("    order by bill.createTime desc");
		return pagedQueryByJdbcTemplate(strSQL.toString(), pageNo, pageSize);
	}

	@Override
	public List<Map<String, Object>> getWechatPushBillList(String openId) throws Exception {
//		StringBuilder strHQL=new StringBuilder("from PushBill where  (openId1='"+openId+"' or openId2='"+openId+"')  and wechatStatus<>0 order by createTime desc ");
//		List<PushBill> list=getSession().createQuery(strHQL.toString()).list();
		StringBuilder sql = new StringBuilder("select bill.id id,DATE_FORMAT(bill.billUpDate,'%Y-%m-%d') billUpDate,DATE_FORMAT(bill.createTime,'%Y-%m-%d') createTime,bill.pushAmount,bill.billType,vir.name virName ");
		sql.append("from tb_billpush bill left join tb_virtualarchitecture vir on vir.id=bill.virtId");
		sql.append(" where (openId1='" + openId + "' or openId2='" + openId + "')  and wechatStatus<>0 order by bill.createTime desc");
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
		return list;
	}
}
