package com.gyee.frame.service;

import com.gyee.frame.common.base.BaseService;
import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.mapper.auto.LossPowerRateMapper;
import com.gyee.frame.model.auto.LossPowerRate;
import com.gyee.frame.model.auto.LossPowerRateExample;
import com.gyee.frame.model.auto.Windturbineinfoday3;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 线路 LossPowerRateService
 *
 * @author gyee_自动生成
 * @Title: LossPowerRateService.java 
 * @Package com.gyee.frame.service 
 * @email 1@qq.com
 * @date 2019-12-31 14:58:09  
 **/
@Service
public class LossPowerRateService implements BaseService<LossPowerRate, LossPowerRateExample> {
    @Resource
    private LossPowerRateMapper lossPowerRateMapper;
	@Resource
	private Windturbineinfoday3Service windturbineinfoday3Service;

    @Override
    public int deleteByPrimaryKey(String ids) {

        return -1;

    }


    @Override
    public LossPowerRate selectByPrimaryKey(String id) {

        return new LossPowerRate();

    }


    @Override
    public int updateByPrimaryKeySelective(LossPowerRate record) {
        return -1;
    }


    /**
     * 添加
     */
    @Override
    public int insertSelective(LossPowerRate record) {


        return lossPowerRateMapper.insertSelective(record);
    }


    @Override
    public int updateByExampleSelective(LossPowerRate record, LossPowerRateExample example) {

        return lossPowerRateMapper.updateByExampleSelective(record, example);
    }


    @Override
    public int updateByExample(LossPowerRate record, LossPowerRateExample example) {

        return lossPowerRateMapper.updateByExample(record, example);
    }

    @Override
    public List<LossPowerRate> selectByExample(LossPowerRateExample example) {

        return lossPowerRateMapper.selectByExample(example);
    }


    @Override
    public long countByExample(LossPowerRateExample example) {

        return lossPowerRateMapper.countByExample(example);
    }


    @Override
    public int deleteByExample(LossPowerRateExample example) {

        return lossPowerRateMapper.deleteByExample(example);
    }

	public List<LossPowerRate> getLossPowerRateListBywtId(String wtId,Date beginDate,Date endDate) throws ParseException {


		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

		List<Windturbineinfoday3> list=windturbineinfoday3Service.getWindturbineinfoday3ListBywtId(wtId, beginDate, endDate);
		List<List<Object>> llit=new ArrayList<>();
		List<LossPowerRate> listlosspowerrate=new ArrayList<>();
		for(Windturbineinfoday3 w3:list){
			if(w3.getDaydl2()==null||w3.getDaynhqfdl2()==null){
				continue;
			}
			LossPowerRate losspowerrate =new LossPowerRate();

			double dou=0.0;
			if((w3.getDaydl2()+w3.getDaynhqfdl2())!=0.0){
				dou=StringUtils.round(w3.getDaynhqfdl2()/(w3.getDaydl2()+w3.getDaynhqfdl2()),4)*100;
			}

			losspowerrate.setRealpower(w3.getDaydl2());
			losspowerrate.setLosspower(w3.getDaynhqfdl2());
			losspowerrate.setRate(StringUtils.round(dou,2));
			losspowerrate.setTime(sdf.format(w3.getRecorddate().getTime()));
			losspowerrate.setWtid(wtId);
			listlosspowerrate.add(losspowerrate);
		}


		return listlosspowerrate;

	}


	public List<List<Object>> getLossPowerRateListBywtId(List<List<Object>> lli,String wtId) {


		List<List<Object>> ls=new ArrayList<>();

		if (StringUtils.notEmp(wtId) ) {

			LossPowerRateExample example=new LossPowerRateExample();
			example.setOrderByClause("wtid asc");

			LossPowerRateExample.Criteria criteria =example.createCriteria();

			criteria.andWtidEqualTo(wtId);
			List<LossPowerRate>  list= lossPowerRateMapper.selectByExample(example);

			if(!list.isEmpty())
			{
				double rate= list.get(0).getRate();

				for(List<Object> li:lli){
					List<Object> lo=new ArrayList<>();
					lo.add(li.get(0));
					lo.add(rate);
					ls.add(lo);
				}
			}

		}
		return ls;

	}


	public List<List<Object>> getHistorySpline(List<List<Object>> lli,String wtid,String isinsert) throws Exception{
		SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date begin=DateUtils.addMonths(sdf.parse(DateUtils.getCurrtenFirstDay()),-1);
		Date end=sdf.parse(DateUtils.getCurrtenFirstDay());
		List<Windturbineinfoday3> list=windturbineinfoday3Service.getWindturbineinfoday3ListBywtId(wtid,begin,end);
		double sumxn=0.0;
		double sumsj=0.0;
		for(Windturbineinfoday3 w3:list){
			if(w3.getDaydl2()==null||w3.getDaynhqfdl2()==null){
				continue;
			}
			sumxn+=w3.getDaynhqfdl2();
			sumsj+=w3.getDaydl2();
		}
		Double dou=0.0;
		if((sumxn+sumsj)!=0.0){
			dou= StringUtils.round(sumxn/(sumxn+sumsj)*100,2);
		}
		List<List<Object>> llo=new ArrayList<>();
		for(List<Object> li:lli){
			List<Object> lo=new ArrayList<>();
			lo.add(li.get(0));
			lo.add(dou);
			llo.add(lo);
		}
		String time=sdf1.format(begin);
		String wpid= InitialRunner.wtmap.get(wtid).getWindpowerstationid();
//		if(isinsert.equalsIgnoreCase("1")){
//			if(wpid.split("_")[1].equalsIgnoreCase("FDC")){
//				String sql="insert into losspowerrate values('"+time+"','"+wtid+"','"+sumsj+"','"+sumxn+"','"+dou+"','"+wpid+"')";
//				System.out.println(sql);
//				operate.update(sql);
//			}
//		}
		return llo;
	}


	public List<List<List<Object>>>  getChartlist(String wtId,Date beginDate,Date endDate) throws Exception{
		List<List<List<Object>>> llld=new ArrayList<>();

		if(StringUtils.notEmp(wtId) && StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate))
		{
			List<List<Object>> real=windturbineinfoday3Service.getWtDay3ListBywtId(wtId,beginDate,endDate);
			List<List<Object>> nihe=getLossPowerRateListBywtId(real,wtId);

			llld.add(real);
			llld.add(nihe);
		}

		return llld;
	}
}
