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.RecommenmainMapper;
import com.gyee.frame.model.auto.Recommenmain;
import com.gyee.frame.model.auto.RecommenmainExample;
import com.gyee.frame.model.auto.Windturbine;
import com.gyee.frame.model.custom.RecommenmainVo;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * 线路 RecommenmainService
 * @Title: RecommenmainService.java 
 * @Package com.gyee.frame.service 
 * @author gyee_自动生成
 * @email 1@qq.com
 * @date 2019-12-31 14:58:09  
 **/
@Service
public class RecommenmainService implements BaseService<Recommenmain, RecommenmainExample> {
	@Resource
	private RecommenmainMapper recommenmainMapper;

	private final String QS = "0";
	private final String FD = "-1";
	private final String GF = "-2";

	@Override
	public int deleteByPrimaryKey(String ids) {
				
			List<Integer> lista=Convert.toIntArrayList(ids);
			RecommenmainExample example=new RecommenmainExample();
			example.createCriteria().andIdIn(lista);
			return recommenmainMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public Recommenmain selectByPrimaryKey(String id) {
				
			return recommenmainMapper.selectByPrimaryKey(id);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(Recommenmain record) {
		return recommenmainMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(Recommenmain record) {
				

				
		return recommenmainMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(Recommenmain record, RecommenmainExample example) {
		
		return recommenmainMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(Recommenmain record, RecommenmainExample example) {
		
		return recommenmainMapper.updateByExample(record, example);
	}

	@Override
	public List<Recommenmain> selectByExample(RecommenmainExample example) {
		
		return recommenmainMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(RecommenmainExample example) {
		
		return recommenmainMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(RecommenmainExample example) {
		
		return recommenmainMapper.deleteByExample(example);
	}

	public List<Recommenmain> getRecommenmainList(String wtId, Date beginDate, Date endDate) {


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

		if (StringUtils.notEmp(wtId) && StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {

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

			RecommenmainExample.Criteria criteria =example.createCriteria();
			criteria.andWtidEqualTo(wtId);
			criteria.andRecodedateGreaterThanOrEqualTo(beginDate).andRecodedateLessThan(endDate);


			list= recommenmainMapper.selectByExample(example);

		}
		return list;

	}


	public List<Recommenmain> getRecommenmainListByStatus(String ismain,Date beginDate, Date endDate) {


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

		if (StringUtils.notEmp(ismain) &&StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {

			list=recommenmainMapper.getRecommenmainDay(ismain,beginDate,endDate);


		}
		return list;

	}


	public Recommenmain getRecommenmain(String rId) {

		Recommenmain po=new Recommenmain();


		if (StringUtils.notEmp(rId)) {

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

			RecommenmainExample.Criteria criteria =example.createCriteria();
			criteria.andRidEqualTo(rId);

			List<Recommenmain> list= recommenmainMapper.selectByExample(example);

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

		return po;

	}

	public Recommenmain getRecommenmain(String wtId, Date beginDate, Date endDate) {


		Recommenmain po=new Recommenmain();

		if (StringUtils.notEmp(wtId) && StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {

			PageHelper.startPage(1, 1);
			RecommenmainExample example=new RecommenmainExample();
			example.setOrderByClause("recodedate desc");

			RecommenmainExample.Criteria criteria =example.createCriteria();
			criteria.andWtidEqualTo(wtId);
			criteria.andRecodedateGreaterThanOrEqualTo(beginDate).andRecodedateLessThan(endDate);



			List<Recommenmain> list= recommenmainMapper.selectByExample(example);

			PageInfo<Recommenmain> pageInfo = new PageInfo<Recommenmain>(list);

			if(!pageInfo.getList().isEmpty())
			{
				po=pageInfo.getList().get(0);
			}


		}
		return po;

	}

	public List<RecommenmainVo> getRecommenmainTopList(String wpId, Date beginDate, Date endDate) {


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

		if (StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {

			PageHelper.startPage(1, 5);

			list= recommenmainMapper.getRecommenmainTotal(wpId, DateUtils.format(beginDate),DateUtils.format(endDate));

			PageInfo<RecommenmainVo> pageInfo = new PageInfo<RecommenmainVo>(list);

			if(!pageInfo.getList().isEmpty())
			{
				list=pageInfo.getList();
			}


		}
		return list;

	}



	public int getRecommenmainnum(String id, Date beginDate, Date endDate) {


		int rmnum=0;

		if (StringUtils.notEmp(id) && StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {


			RecommenmainExample example=new RecommenmainExample();
			example.setOrderByClause("recodedate desc");

			RecommenmainExample.Criteria criteria =example.createCriteria();


			List<String> wtidls=new ArrayList<>();
			if (id.equals(GF))
			{
				return 0;
			}else if (InitialRunner.wpmap.containsKey(id)) {
				for(Windturbine wt:InitialRunner.wp_wtmap.get(id))
				{
					wtidls.add(wt.getId());
				}
			} else if (InitialRunner.pjmap.containsKey(id)) {
				for(Windturbine wt:InitialRunner.pj_wtmap.get(id))
				{
					wtidls.add(wt.getId());
				}
			} else if (InitialRunner.lnmap.containsKey(id)) {
				for(Windturbine wt:InitialRunner.ln_wtmap.get(id))
				{
					wtidls.add(wt.getId());
				}
			}


			if(!wtidls.isEmpty())
			{
				criteria.andWtidIn(wtidls);
			}
			criteria.andRecodedateGreaterThanOrEqualTo(beginDate).andRecodedateLessThan(endDate);
			List<Recommenmain> list= recommenmainMapper.selectByExample(example);

			Set<String> set=new HashSet<>();

			if(!list.isEmpty())
			{
				for(Recommenmain rm:list)
				{
					set.add(rm.getWtid());
				}
				rmnum=set.size();
			}



		}
		return rmnum;

	}

}
