/*
 * Powered By [柳丰]
 * Web Site: http://www.trend.com.cn
 */

package com.trend.hbny.service;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import javacommon.base.*;
import javacommon.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import com.trend.hbny.model.*;
import com.trend.hbny.Helper.Neo;
import com.trend.hbny.dao.*;
import com.trend.hbny.service.*;

/**
 * @author neolf email:neolf(a)foxmail.com
 * @version 1.0
 * @since 1.0
 */

@Component
@Transactional
public class ERealtimePowerShowManager extends BaseManager<ERealtimePowerPlantBar,java.lang.Long>{

	private ERealtimePowerDao eRealtimePowerDao;
	private EMaxminPowerDao eMaxminPowerDao;
	private ELineBasicinfoManager eLineBasicinfoManager;
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setERealtimePowerDao(ERealtimePowerDao dao) {
		this.eRealtimePowerDao = dao;
	}	 
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setEMaxminPowerDao(EMaxminPowerDao dao) {
		this.eMaxminPowerDao = dao;
	}	
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setELineBasicinfoManager(ELineBasicinfoManager dao) {
		this.eLineBasicinfoManager = dao;
	}	

	
	
	@Transactional(readOnly=true)
	public Page findByPageRequest(PageRequest<Map> pageRequest) {
		String hashcode = "ERealtimePowerShow-"+getPageRequestHashCode(pageRequest);
		Page cachePage = getCacheManager().getObject(hashcode);
		if(cachePage != null){
			return cachePage;			
		}
		
		Page page = findDatabaseByPageRequest(pageRequest);
		
		getCacheManager().putObject(hashcode, page);
		
		return page;
	}
	
	@Transactional(readOnly=true)
	public Page findDatabaseByPageRequest(PageRequest<Map> pageRequest) {
		
		
		
		Map<String, ERealtimePowerPlantBar> eRealtimePowerPlantBarList = new HashMap<String, ERealtimePowerPlantBar>();
		
		//取实时出力
		Page eRealtimePowerPage = eRealtimePowerDao.findByPageRequest(pageRequest);
		for (Object item : eRealtimePowerPage.getResult()) {
			ERealtimePower temp = (ERealtimePower) item;
			ERealtimePowerPlantBar t = eRealtimePowerPlantBarList.get(Neo.getCoNoDC(temp.getCoNo()) );
			if(t == null){
				t = new ERealtimePowerPlantBar();
				t.setCoNo(Neo.getCoNoDC(temp.getCoNo()) );
				t.setlGFlag(temp.getlGFlag());
				t.setCapacity(0.0);
				t.setRealPower(temp.getP());
				t.setRealPowerTime(temp.getUpdateTime());				
				eRealtimePowerPlantBarList.put(Neo.getCoNoDC(temp.getCoNo()) , t);
			}else{				
				t.setRealPower(t.getRealPower() + temp.getP());
				//t.setRealPowerTime(temp.getUpdateTime());	
			}
	    } 
		
		//取额定功率
		Page eLineBasicinfoManagerPage = eLineBasicinfoManager.findByPageRequest(pageRequest);
		for (Object item : eLineBasicinfoManagerPage.getResult()) {
			ELineBasicinfo temp = (ELineBasicinfo) item;
			ERealtimePowerPlantBar t = eRealtimePowerPlantBarList.get(temp.getCoNo());
			if(t != null){
				t.setName(temp.getName());		
				t.setCompany(temp.getCompany());
				t.setPowerStation(temp.getPowerStation());
				t.setCapacity(temp.getCapacity());
			}
	    } 
		
		
		
		
		
		List <ERealtimePowerPlantBar> list = new ArrayList(eRealtimePowerPlantBarList.values());
		
		Collections.sort(list,new Comparator<ERealtimePowerPlantBar>(){   
            public int compare(ERealtimePowerPlantBar arg0, ERealtimePowerPlantBar arg1) {   
                return arg0.getCoNo().compareTo(arg1.getCoNo());   
             }   
			}); 
		
		Page page = new Page(0,list.size(),list.size());
		page.setResult(list);
		
		
		return page;
	}

	@Override
	protected EntityDao getEntityDao() {		
		return null;
	}

}
