package com.gyee.frame.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gyee.frame.common.base.BaseService;
import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.common.support.Convert;
import com.gyee.frame.mapper.auto.WindturbineinfodaytopMapper;
import com.gyee.frame.model.auto.*;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 风机信息表 WindturbineinfodaytopService
 * @Title: WindturbineinfodaytopService.java 
 * @Package com.gyee.frame.service 
 * @author gyee_自动生成
 * @email 1@qq.com
 * @date 2019-12-30 17:00:04  
 **/
@Service
public class WindturbineinfodaytopService implements BaseService<Windturbineinfodaytop, WindturbineinfodaytopExample> {
	@Resource
	private WindturbineinfodaytopMapper windturbineinfodaytopMapper;
	@Resource
	private WindturbineinfodayService windturbineinfodayService;

	/**
	 * 分页查询
	 * @param tablepar
	 * @param name
	 * @return
	 */
	 public PageInfo<Windturbineinfodaytop> list(Tablepar tablepar, String name){
	        WindturbineinfodaytopExample testExample=new WindturbineinfodaytopExample();
	        testExample.setOrderByClause("id ASC");
	        if(name!=null&&!"".equals(name)){
//	        	testExample.createCriteria().and.andCodeLike("%"+name+"%");
	        }

	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<Windturbineinfodaytop> list= windturbineinfodaytopMapper.selectByExample(testExample);
	        PageInfo<Windturbineinfodaytop> pageInfo = new PageInfo<Windturbineinfodaytop>(list);
	        return  pageInfo;
	 }

	@Override
	public int deleteByPrimaryKey(String ids) {

			List<Integer> lista=Convert.toIntArrayList(ids);
			WindturbineinfodaytopExample example=new WindturbineinfodaytopExample();
			example.createCriteria().andIdIn(lista);
			return windturbineinfodaytopMapper.deleteByExample(example);


	}


	@Override
	public Windturbineinfodaytop selectByPrimaryKey(String id) {

	 	if(StringUtils.isNotEmpty(id))
		{
			return windturbineinfodaytopMapper.selectByPrimaryKey(Integer.valueOf(id));

		}
	 	return new Windturbineinfodaytop();

	}


	@Override
	public int updateByPrimaryKeySelective(Windturbineinfodaytop record) {
		return windturbineinfodaytopMapper.updateByPrimaryKeySelective(record);
	}


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

//		//添加雪花主键id
//		record.setId(SnowflakeIdWorker.getUUID());


		return windturbineinfodaytopMapper.insertSelective(record);
	}


	@Override
	public int updateByExampleSelective(Windturbineinfodaytop record, WindturbineinfodaytopExample example) {

		return windturbineinfodaytopMapper.updateByExampleSelective(record, example);
	}


	@Override
	public int updateByExample(Windturbineinfodaytop record, WindturbineinfodaytopExample example) {

		return windturbineinfodaytopMapper.updateByExample(record, example);
	}

	@Override
	public List<Windturbineinfodaytop> selectByExample(WindturbineinfodaytopExample example) {

		return windturbineinfodaytopMapper.selectByExample(example);
	}


	@Override
	public long countByExample(WindturbineinfodaytopExample example) {

		return windturbineinfodaytopMapper.countByExample(example);
	}


	@Override
	public int deleteByExample(WindturbineinfodaytopExample example) {

		return windturbineinfodaytopMapper.deleteByExample(example);
	}


	/******************************************************************************************************************/
	public List<Windturbineinfodaytop> gadayhistlist(String wtId, Date recorddate)  {
		List<Windturbineinfodaytop> ls=null;

		WindturbineinfodaytopExample example=new WindturbineinfodaytopExample();
		example.setOrderByClause("windturbineid ASC");

		if(StringUtils.notEmp(wtId) &&  StringUtils.notEmp(recorddate)){


			example.createCriteria().andWindturbineidEqualTo(wtId).andRecorddateGreaterThanOrEqualTo(recorddate).andRecorddateLessThan(DateUtils.addDays(recorddate,1));
			ls= windturbineinfodaytopMapper.selectByExample(example);

			Map<String, Windturbine> wtmap = InitialRunner.wtmap;

			if (!ls.isEmpty()) {
				for (Windturbineinfodaytop wtop : ls) {
					if (wtmap.containsKey(wtop.getWindturbineid())) {
						Windturbine wt = wtmap.get(wtop.getWindturbineid());
						wtop.setWindturbinename(wt.getName());
					}
					List<WindTurbineInfoDay> wtdls = windturbineinfodayService.getWindturbineinfo(wtop.getWindturbineid(),recorddate);
					if (null != wtdls && !wtdls.isEmpty()) {
						WindTurbineInfoDay wt = wtdls.get(0);
						double fdl = null != wt.getGeneratingcapacity2() ? wt.getGeneratingcapacity2() : 0.0;
						double gz = null != wt.getLossofpower2() ? wt.getLossofpower2() : 0.0;
						double jx = null != wt.getLossofpower3() ? wt.getLossofpower3() : 0.0;
						double qf = null != wt.getLossofpower4() ? wt.getLossofpower4() : 0.0;
						double xd = null != wt.getLossofpower1() ? wt.getLossofpower1() : 0.0;
						double sl1 = null != wt.getMainlossin() ? wt.getMainlossin() : 0.0;
						double sl2 = null != wt.getFaultlossin() ? wt.getFaultlossin() : 0.0;
						double sl3 = null != wt.getElectriclossout() ? wt.getElectriclossout() : 0.0;
						double sl4 = null != wt.getWeatherlossout() ? wt.getWeatherlossout() : 0.0;

						wtop.setDayllfdl(StringUtils.round(fdl + gz + jx + qf + xd + sl1 + sl2 + sl3 + sl4, 2));
					}
				}
			}
		}else
		{
			ls=new ArrayList<>();
		}


		return ls;
	}


	public List<Windturbineinfodaytop> gamonthhistlist(String wtId, String  year,String month)  {
		List<Windturbineinfodaytop> ls=null;

		WindturbineinfodaytopExample example=new WindturbineinfodaytopExample();
		example.setOrderByClause("windturbineid ASC");

		if(StringUtils.notEmp(wtId) &&  StringUtils.notEmp(year) &&  StringUtils.notEmp(month)){

			Calendar c = Calendar.getInstance();
			c.set(Calendar.YEAR, Integer.valueOf(year));
			c.set(Calendar.MONTH, Integer.valueOf(month) - 1);
			c.set(Calendar.DAY_OF_MONTH, 1);

			Date beginDate=DateUtils.truncate(c.getTime());
			c.add(Calendar.MONTH,1);
			Date endDate=c.getTime();

			example.createCriteria().andWindturbineidEqualTo(wtId).andRecorddateGreaterThanOrEqualTo(beginDate).andRecorddateLessThan(endDate);
			ls= windturbineinfodaytopMapper.selectByExample(example);


			if (!ls.isEmpty()) {
				List<WindTurbineInfoDay> wtdls = windturbineinfodayService.getWindturbineinfo(beginDate,endDate,wtId);
				Map<String, Double> map = new HashMap<String, Double>();
				if (null != wtdls && !wtdls.isEmpty()) {
					for (WindTurbineInfoDay wt : wtdls) {
						double result = getllfdlValue(wt);
						map.put(DateUtils.format(wt.getRecorddate()), result);

					}
				}

				for (Windturbineinfodaytop w : ls) {
					if (map.containsKey(DateUtils.format(w.getRecorddate()))) {
						double llfdl = map.get(DateUtils.format(w.getRecorddate()));
						w.setDayllfdl(StringUtils.round(llfdl, 2));
					}
				}
			}
		}else
		{
			ls=new ArrayList<>();
		}


		return ls;
	}

	public List<Windturbineinfodaytop> gayearhistlist(String wtId, String  year)  {
		List<Windturbineinfodaytop> ls=null;

		WindturbineinfodaytopExample example=new WindturbineinfodaytopExample();
		example.setOrderByClause("windturbineid ASC");

		if(StringUtils.notEmp(wtId) &&  StringUtils.notEmp(year) ){

			Calendar c = Calendar.getInstance();
			c.set(Calendar.YEAR, Integer.valueOf(year));
			c.set(Calendar.MONTH, 0);
			c.set(Calendar.DAY_OF_MONTH, 1);

			Date beginDate=DateUtils.truncate(c.getTime());
			c.add(Calendar.YEAR,1);
			Date endDate=c.getTime();

			example.createCriteria().andWindturbineidEqualTo(wtId).andRecorddateGreaterThanOrEqualTo(beginDate).andRecorddateLessThan(endDate);
			ls= windturbineinfodaytopMapper.selectByExample(example);


			if (!ls.isEmpty()) {
				List<WindTurbineInfoDay> wtdls = windturbineinfodayService.getWindturbineinfo(beginDate,endDate,wtId);
				Map<String, Double> map = new HashMap<String, Double>();
				if (null != wtdls && !wtdls.isEmpty()) {
					for (WindTurbineInfoDay wt : wtdls) {
						double result = getllfdlValue(wt);
						map.put(DateUtils.format(wt.getRecorddate()), result);

					}
				}

				for (Windturbineinfodaytop w : ls) {
					if (map.containsKey(DateUtils.format(w.getRecorddate()))) {
						double llfdl = map.get(DateUtils.format(w.getRecorddate()));
						w.setDayllfdl(StringUtils.round(llfdl, 2));
					}
				}

			}
		}else
		{
			ls=new ArrayList<>();
		}


		return ls;
	}

	private double getllfdlValue(WindTurbineInfoDay wt) {
		double fdl = null != wt.getGeneratingcapacity2() ? wt.getGeneratingcapacity2() : 0.0;
		double gz = null != wt.getLossofpower2() ? wt.getLossofpower2() : 0.0;
		double jx = null != wt.getLossofpower3() ? wt.getLossofpower3() : 0.0;
		double qf = null != wt.getLossofpower4() ? wt.getLossofpower4() : 0.0;
		double xd = null != wt.getLossofpower1() ? wt.getLossofpower1() : 0.0;
		double sl1 = null != wt.getMainlossin() ? wt.getMainlossin() : 0.0;
		double sl2 = null != wt.getFaultlossin() ? wt.getFaultlossin() : 0.0;
		double sl3 = null != wt.getElectriclossout() ? wt.getElectriclossout() : 0.0;
		double sl4 = null != wt.getWeatherlossout() ? wt.getWeatherlossout() : 0.0;
		return StringUtils.round(fdl + gz + jx + qf + xd + sl1 + sl2 + sl3 + sl4, 2);
	}
}
