package com.gyee.frame.service;

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.WtReportMainMapper;
import com.gyee.frame.model.auto.Windturbine;
import com.gyee.frame.model.auto.WtReportMain;
import com.gyee.frame.model.auto.WtReportMainExample;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.SnowflakeIdWorker;
import com.gyee.frame.util.StringUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

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


@Service
public class WtReportMainService implements BaseService<WtReportMain, WtReportMainExample> {
	@Resource
	private WtReportMainMapper wtReportMainMapper;


	@Override
	public int deleteByPrimaryKey(String ids) {

			List<String> lista=Convert.toListStrArray(ids);
			WtReportMainExample example=new WtReportMainExample();
			example.createCriteria().andIdIn(lista);
			return wtReportMainMapper.deleteByExample(example);


	}


	@Override
	public WtReportMain selectByPrimaryKey(String id) {

			return wtReportMainMapper.selectByPrimaryKey(id);

	}


	@Override
	public int updateByPrimaryKeySelective(WtReportMain record) {
		return wtReportMainMapper.updateByPrimaryKeySelective(record);
	}


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

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


		return wtReportMainMapper.insertSelective(record);
	}


	@Override
	public int updateByExampleSelective(WtReportMain record, WtReportMainExample example) {

		return wtReportMainMapper.updateByExampleSelective(record, example);
	}


	@Override
	public int updateByExample(WtReportMain record, WtReportMainExample example) {

		return wtReportMainMapper.updateByExample(record, example);
	}

	@Override
	public List<WtReportMain> selectByExample(WtReportMainExample example) {

		return wtReportMainMapper.selectByExample(example);
	}


	@Override
	public long countByExample(WtReportMainExample example) {

		return wtReportMainMapper.countByExample(example);
	}


	@Override
	public int deleteByExample(WtReportMainExample example) {

		return wtReportMainMapper.deleteByExample(example);
	}

	public List<WtReportMain> healthReport(String wtId, Date recorddate) {


		List<WtReportMain> list=new ArrayList<>();

		WtReportMainExample example=new WtReportMainExample();
		example.setOrderByClause("recodedate asc");

		WtReportMainExample.Criteria criteria =example.createCriteria();

		criteria.andWtidEqualTo(wtId);

		Date endDate= DateUtils.addDays(recorddate,1);
		criteria.andRecodedateGreaterThanOrEqualTo(recorddate).andRecodedateLessThan(endDate);


		list= wtReportMainMapper.selectByExample(example);

		return list;

	}

	public List<WtReportMain> getReportList(String wpId,String wtId, Date beginDate,Date endDate) {


		List<WtReportMain> list=new ArrayList<>();

		WtReportMainExample example=new WtReportMainExample();
		example.setOrderByClause("recodedate asc");

		WtReportMainExample.Criteria criteria =example.createCriteria();

		if(StringUtils.notEmp(wpId))
		{
			if(InitialRunner.wp_wtmap.containsKey(wpId))
			{
				List<Windturbine> wtls=InitialRunner.wp_wtmap.get(wpId);
				List<String> ids=new ArrayList<>();
				for(Windturbine wt:wtls)
				{
					ids.add(wt.getId());
				}
				criteria.andWtidIn(ids);
			}

		}
		if(StringUtils.notEmp(wtId))
		{
			criteria.andWtidEqualTo(wtId);
		}


		criteria.andRecodedateGreaterThanOrEqualTo(beginDate).andRecodedateLessThan(endDate);


		list= wtReportMainMapper.selectByExample(example);

		return list;

	}

	public WtReportMain findWtReportMainById(String wtId, Date recorddate) {

		WtReportMain po=new WtReportMain();


		WtReportMainExample example=new WtReportMainExample();
		example.setOrderByClause("recodedate asc");

		WtReportMainExample.Criteria criteria =example.createCriteria();

		criteria.andWtidEqualTo(wtId);

		Date endDate= DateUtils.addDays(recorddate,1);
		criteria.andRecodedateGreaterThanOrEqualTo(recorddate).andRecodedateLessThan(endDate);


		List<WtReportMain>  list= wtReportMainMapper.selectByExample(example);

		if(!list.isEmpty())
		{
			po=list.get(0);
		}
		return po;

	}

}
