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

import java.util.ArrayList;
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.DayHisRecord;
import com.persagy.ems.pojo.wz.DayPredictRecord;
import com.persagy.framework.util.BeanUtils;
import com.persagy.framework.util.DateUtils;
import com.persagy.functions.weather.dto.DayPredictDTO;
import com.persagy.functions.weather.service.DayHisRecordService;

@Service("dayHisRecordService")
public class DayHisRecordServiceImpl implements DayHisRecordService {
	@Resource(name="coreService")
	private CoreService coreService;
	
	@Override
	public void save(DayHisRecord obj) throws Exception {
		obj.setBuildingForContainer(getBdForContainer(obj.getDayTime()));
	    this.coreService.save(obj);
	}

	@Override
	public void saveBatch(List<DayHisRecord> list) throws Exception {
		for (DayHisRecord obj : list) {
			obj.setBuildingForContainer(getBdForContainer(obj.getDayTime()));
		}
		this.coreService.save(list);
	}

	@Override
	public List<DayHisRecord> query(String cityId, Date startDay, Date endDay) throws Exception {
		return queryCommon(cityId, startDay, endDay);
	}

	@Override
	public List<DayHisRecord> query(Date startDay, Date endDay) throws Exception {
		return queryCommon(null, startDay, endDay);
	}

	@Override
	public List<DayHisRecord> queryOneDay(Date dayTime) throws Exception {
		DayHisRecord queryObj = new DayHisRecord();
	    queryObj.setBuildingForContainer(getBdForContainer(dayTime));
	    queryObj.setDayTime(dayTime);
	    return this.coreService.query(queryObj);
	}

	@Override
	public void updateOne(String cityId, Date dayTime, DayHisRecord updateInfo) throws Exception {
		DayHisRecord queryRecord = new DayHisRecord();
	    queryRecord.setBuildingForContainer(getBdForContainer(dayTime));
	    queryRecord.setCityId(cityId);
	    queryRecord.setDayTime(dayTime);
	    
	    updateInfo.setCityId(cityId);
	    updateInfo.setBuildingForContainer(getBdForContainer(dayTime));
	    this.coreService.update(queryRecord, updateInfo);
	}

	private List<DayHisRecord> queryCommon(String cityId, Date startDay, Date endDay) throws Exception {
		if (DateUtils.getYear(startDay) == DateUtils.getYear(endDay)) {
			DayHisRecord queryObj = new DayHisRecord();
			queryObj.setBuildingForContainer(getBdForContainer(startDay));
			queryObj.setCityId(cityId);
			queryObj.setSpecialOperation("dayTime", SpecialOperator.$gte, startDay);
			queryObj.setSpecialOperation("dayTime", SpecialOperator.$lt, endDay);
			return this.coreService.query(queryObj);
		}
		
		Date midDay = DateUtils.getShortDate(startDay, 5);
		midDay = DateUtils.getYearOff(midDay, 1);
		List<DayHisRecord> list = new ArrayList<DayHisRecord>();

		while(startDay.before(endDay)) {
			DayHisRecord queryA = new DayHisRecord();
			queryA.setBuildingForContainer(getBdForContainer(startDay));
			queryA.setCityId(cityId);
			queryA.setSpecialOperation("dayTime", SpecialOperator.$gte, startDay);
			queryA.setSpecialOperation("dayTime", SpecialOperator.$lt, midDay);
			List<DayHisRecord> listA = this.coreService.query(queryA);
			if ((listA != null) && (!listA.isEmpty())) {
				list.addAll(listA);
			}
			
			startDay = midDay;
			midDay = DateUtils.getYearOff(midDay, 1);
			if(midDay.after(endDay)) {
				midDay = endDay;
			}
		}
		return list;
	}
	
	private String getBdForContainer(Date dayTime) throws Exception {
		return DateUtils.getYear(dayTime) + "";
	}

	@Override
	public void coverPredictRecord(String cityId, List<DayPredictRecord> list) throws Exception {
		if(list != null && !list.isEmpty()) {
			DayPredictRecord delObj = new DayPredictRecord();
			delObj.setCityId(cityId);
			coreService.remove(delObj);
			
			coreService.save(list);
		}
	}

	@Override
	public List<DayPredictRecord> queryPredictData(String cityId, int limit) throws Exception {
		DayPredictRecord delObj = new DayPredictRecord();
		delObj.setCityId(cityId);
		delObj.setSort("dayTime", EMSOrder.Asc);
		List<DayPredictRecord> list = coreService.query(delObj);
		if(null != list && list.size()>limit) {
			return list.subList(0, limit);
		}
		return list;
	}

	@Override
	public void coverPredictRecordUseDTO(String cityId, List<DayPredictDTO> predicDataList) throws Exception {
		if(null != predicDataList) {
			List<DayPredictRecord> newDataList = new ArrayList<>();
			for(int i=0; i<predicDataList.size(); i++) {
				DayPredictDTO predicData = predicDataList.get(i);
				String dayTimeStr = predicData.getDate();
				predicData.setDate(null);
				DayPredictRecord saveRecord = new DayPredictRecord();
				if (predicData.getLast_update() != null) {
					saveRecord.setLastUpdate(
							DateUtils.t8DateStr2Date(predicData.getLast_update()));
					predicData.setLast_update(null);
				}
				BeanUtils.copyBean(predicData, saveRecord);
				saveRecord.setDayTime(DateUtils.str2Date(dayTimeStr, "yyyy-MM-dd"));
				saveRecord.setCityId(cityId);
				saveRecord.setUpdateTime(new Date());
				newDataList.add(saveRecord);
			}
			
			if(newDataList.size() > 0) {
				coverPredictRecord(cityId, newDataList);
			}
		}
		
	}

	@Override
	public List<DayPredictRecord> queryPredictDataOfOneDay(Date dayTime) throws Exception {
		DayPredictRecord queryObj = new DayPredictRecord();
		queryObj.setDayTime(dayTime);
		return coreService.query(queryObj);
	}

	@Override
	public List<DayPredictRecord> queryPredictData(String cityId, Date start, Date end) throws Exception {
		DayPredictRecord delObj = new DayPredictRecord();
		delObj.setCityId(cityId);
		delObj.setSort("dayTime", EMSOrder.Asc);
		delObj.setSpecialOperation("dayTime", SpecialOperator.$gte, start);
		delObj.setSpecialOperation("dayTime", SpecialOperator.$lt, end);
		List<DayPredictRecord> list = coreService.query(delObj);
		return list;
	}
}
