package com.persagy.functions.weather.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.persagy.core.enumeration.EMSOrder;
import com.persagy.core.enumeration.SpecialOperator;
import com.persagy.core.mvc.service.CoreService;
import com.persagy.ems.pojo.wz.HourRecordData;
import com.persagy.framework.dto.TimeSpanCondition;
import com.persagy.framework.util.ConfigUtil;
import com.persagy.framework.util.DateUtils;
import com.persagy.functions.weather.service.HourRecordService;

@Service("hourRecordService")
public class HourRecordServiceImpl implements HourRecordService {
	@Resource(name="coreService")
	private CoreService coreService;
	
	@Override
	public void save(HourRecordData obj) throws Exception {
		obj.setBuildingForContainer(ConfigUtil.getComputeStr(obj.getCityId()));
	    this.coreService.save(obj);
	}
	
	@Override
	public void saveBatch(List<HourRecordData> list) throws Exception {
		for(HourRecordData obj : list) {
			obj.setBuildingForContainer(ConfigUtil.getComputeStr(obj.getCityId()));
		}
		this.coreService.save(list);
	}

	@Override
	public List<HourRecordData> query(String cityId, Date start, Date end) throws Exception {
		HourRecordData queryObj = new HourRecordData();
	    String splitTag = ConfigUtil.getComputeStr(cityId);
	    queryObj.setBuildingForContainer(splitTag);
	    queryObj.setCityId(cityId);
	    queryObj.setSort("hourTime", EMSOrder.Asc);
	    queryObj.setSpecialOperation("hourTime", SpecialOperator.$gte, start);
	    queryObj.setSpecialOperation("hourTime", SpecialOperator.$lt, end);
	    return this.coreService.query(queryObj);
	}

	@Override
	public List<HourRecordData> queryForRightHour(String cityId, Date hourTime) throws Exception {
		HourRecordData queryObj = new HourRecordData();
	    queryObj.setCityId(cityId);
	    queryObj.setBuildingForContainer(ConfigUtil.getComputeStr(cityId));
	    queryObj.setHourTime(hourTime);
	    return this.coreService.query(queryObj);
	}

	@Override
	public void deleteOneDayOfCity(String cityId, Date dayDate) throws Exception {
		HourRecordData queryObj = new HourRecordData();
	    queryObj.setCityId(cityId);
	    queryObj.setBuildingForContainer(ConfigUtil.getComputeStr(cityId));
	    
	    Date dayEnd = DateUtils.getDayOff(dayDate, 1);
	    queryObj.setSpecialOperation("hourTime", SpecialOperator.$gte, dayDate);
	    queryObj.setSpecialOperation("hourTime", SpecialOperator.$lt, dayEnd);
	    
	    this.coreService.remove(queryObj);
	}

	@Override
	public HourRecordData getPresent(String cityId) throws Exception {
		Date end = new Date();
		Date start = DateUtils.getDayOff(end, -1);
		HourRecordData queryObj = new HourRecordData();
	    String splitTag = ConfigUtil.getComputeStr(cityId);
	    queryObj.setBuildingForContainer(splitTag);
	    queryObj.setCityId(cityId);
	    queryObj.setSort("hourTime", EMSOrder.Desc);
	    queryObj.setLimit(1L);
	    queryObj.setSpecialOperation("hourTime", SpecialOperator.$gte, start);
	    queryObj.setSpecialOperation("hourTime", SpecialOperator.$lt, end);
	    List<HourRecordData> list = this.coreService.query(queryObj);
	    if(null != list && !list.isEmpty()) {
	    	return list.get(0);
	    }
	    return null;
	}

	@Override
	public List<HourRecordData> query(String cityId, TimeSpanCondition timeCondition) throws Exception {
		HourRecordData queryObj = new HourRecordData();
	    queryObj.setCityId(cityId);
	    queryObj.setBuildingForContainer(ConfigUtil.getComputeStr(cityId));
	    
	    queryObj.setSpecialOperation("hourTime", timeCondition.optLeft, timeCondition.timeLeft);
	    queryObj.setSpecialOperation("hourTime", timeCondition.optRight, timeCondition.timeRight);
	    
	    return this.coreService.query(queryObj);
	}


}
