package com.palmnest.dao.airconditioner.impl;

import com.palmnest.application.bean.Page;
import com.palmnest.dao.airconditioner.AirRuleDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.airconditioner.AirRule;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

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


@Repository("AirRuleDao")
public class AirRuleDaoImpl extends GenericDaoHibernate<AirRule, Long> implements AirRuleDao {

	private Logger LOG = Logger.getLogger(AirRuleDaoImpl.class);

	public AirRuleDaoImpl() {
		super(AirRule.class);
	}

	@Override
	public Page findPageBy(Map<String, Object> params, int pageSize, int pageNo) {

		LOG.info("AirRuleDaoImpl findPageBy======");

		StringBuilder sql = new StringBuilder("select").append("\n");
		sql.append("bill.id,org.name orgName,bill.ruleName,bill.priceDisplay,bill.chargeTimeDisplay,bill.applicationAirAddTime,bill.applicationMeeting,bill.isEnabled,date_format(bill.create_time, '%Y-%m-%d %H:%i') createTime").append("\n");
		sql.append("from  tb_airRule bill").append("\n");
		sql.append("left JOIN tb_organization org on org.id = bill.organizationId").append("\n");


		sql.append(" where 1=1  ").append("\n");//空调加时才显示 保存的数据不显示


		//是否启用
		if (params.get("isEnabled") != null && !params.get("isEnabled").equals("")) {
			sql.append(" and bill.isEnabled = '" + params.get("isEnabled") + "'").append("\n");
		}
		if (params.get("dataType") != null && !params.get("dataType").equals("")) {
			sql.append(" and bill.dataType = '" + params.get("dataType") + "'").append("\n");
		}

		//项目过滤
		if (params.get("organizationId") != null && !params.get("organizationId").equals("")) {
			sql.append(" and bill.organizationId = '" + params.get("organizationId") + "'").append("\n");
		}


		sql.append(" order by bill.create_time desc ").append("\n");

		LOG.info("AirRuleDaoImpl findPageBy sql" + sql.toString());

		return pagedQueryByJdbcTemplate(sql.toString(), pageNo, pageSize);
	}

	@Override
	public List<AirRule> getAirRule(Integer organizationId, Integer applicationType, Integer meetingOrAirAddTime, Integer dataType,
									String startTime, String endTime) {

		LOG.info("AirRuleDaoImpl findPageBy======");

		StringBuilder sql = new StringBuilder("select * from tb_airRule ").append("\n");

		//应用模块(1预定会议室\2空调加时
		//sql.append("where applicationType="+applicationType).append("\n");	
		if (applicationType == 1) {
			sql.append("where applicationMeeting=" + meetingOrAirAddTime).append("\n");
		} else if (applicationType == 2) {
			sql.append("where applicationAirAddTime=" + meetingOrAirAddTime).append("\n");
		}


		sql.append("   and dataType=" + dataType).append("\n");

		if (StringUtils.isNotEmpty(startTime)) {
			sql.append("   and startTime <= str_to_date('1970-01-01 " + startTime + ":00','%Y-%m-%d %H:%i:%s') ").append("\n");//空调加时才显示 保存的数据不显示
		}

		if (StringUtils.isNotEmpty(endTime)) {
			sql.append("   and endTime >= str_to_date('1970-01-01 " + endTime + ":00','%Y-%m-%d %H:%i:%s') ").append("\n");
		}

		//项目过滤
		sql.append(" and  organizationId = '" + organizationId + "'").append("\n");


		LOG.info("AirRuleDaoImpl findPageBy sql" + sql.toString());
		List<AirRule> airRules = (List<AirRule>) getSession().createSQLQuery(sql.toString()).addEntity(AirRule.class).list();

		return airRules;

	}
	
	@Override
	public AirRule getRuleByOrgId(Long orgId) {

		LOG.info("AirRuleDaoImpl getRuleByOrgId======");

		StringBuilder sql = new StringBuilder("select * from tb_airRule ").append("\n");		

		//项目过滤
		sql.append(" where  organizationId = '" + orgId + "'").append("\n");


		LOG.info("AirRuleDaoImpl getRuleByOrgId sql" + sql.toString());
		List<AirRule> airRules = (List<AirRule>) getSession().createSQLQuery(sql.toString()).addEntity(AirRule.class).list();

		if(null != airRules && airRules.size()>=1){
			return airRules.get(0);
		}else{
			return null;
		}
		

	}

}
