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.support.Convert;
import com.gyee.frame.mapper.auto.StatetransitionratesMapper;
import com.gyee.frame.model.auto.Statetransitionrates;
import com.gyee.frame.model.auto.StatetransitionratesExample;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.util.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * 风机信息表 StatetransitionratesService
 * @Title: StatetransitionratesService.java 
 * @Package com.gyee.frame.service 
 **/
@Service
public class StatetransitionratesService implements BaseService<Statetransitionrates, StatetransitionratesExample> {
	@Resource
	private StatetransitionratesMapper statetransitionratesMapper;

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

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

	@Override
	public int deleteByPrimaryKey(String ids) {

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


	}


	@Override
	public Statetransitionrates selectByPrimaryKey(String id) {

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

		}
	 	return new Statetransitionrates();

	}


	@Override
	public int updateByPrimaryKeySelective(Statetransitionrates record) {
		return statetransitionratesMapper.updateByPrimaryKeySelective(record);
	}


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

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


		return statetransitionratesMapper.insertSelective(record);
	}


	@Override
	public int updateByExampleSelective(Statetransitionrates record, StatetransitionratesExample example) {

		return statetransitionratesMapper.updateByExampleSelective(record, example);
	}


	@Override
	public int updateByExample(Statetransitionrates record, StatetransitionratesExample example) {

		return statetransitionratesMapper.updateByExample(record, example);
	}

	@Override
	public List<Statetransitionrates> selectByExample(StatetransitionratesExample example) {

		return statetransitionratesMapper.selectByExample(example);
	}


	@Override
	public long countByExample(StatetransitionratesExample example) {

		return statetransitionratesMapper.countByExample(example);
	}


	@Override
	public int deleteByExample(StatetransitionratesExample example) {

		return statetransitionratesMapper.deleteByExample(example);
	}

	public Map<String, Statetransitionrates>  getStatetransitionratesByWpTotal(String wpId, Date beginDate, Date endDate){

		Map<String, Statetransitionrates> stmap = new HashMap<String, Statetransitionrates>();

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

		StatetransitionratesExample example=new StatetransitionratesExample();
		example.setOrderByClause("windpwoerid ASC");

		StatetransitionratesExample.Criteria criteria =example.createCriteria();

		if(StringUtils.isNotEmpty(wpId))
		{
			criteria.andWindpwoeridEqualTo(wpId);
		}
		if(beginDate!=null && endDate!=null){


			criteria.andRecorddateGreaterThanOrEqualTo(beginDate).andRecorddateLessThan(endDate);
			list= statetransitionratesMapper.selectByExample(example);
		}
		if(list!=null)
		{
			for(Statetransitionrates st:list)
			{

				if(stmap.containsKey(st.getWindpwoerid()))
				{
					Statetransitionrates vo=stmap.get(st.getWindpwoerid());
					vo.setWindpwoerid(st.getWindpwoerid());
					vo.setTimerate1(vo.getTimerate1()+st.getTimerate1());
					vo.setTimerate2(vo.getTimerate2()+st.getTimerate2());
					vo.setTimerate3(vo.getTimerate3()+st.getTimerate3());
					vo.setTimerate4(vo.getTimerate4()+st.getTimerate4());
					vo.setTimerate5(vo.getTimerate5()+st.getTimerate5());
					vo.setTimerate6(vo.getTimerate6()+st.getTimerate6());
					vo.setTimerate7(vo.getTimerate7()+st.getTimerate7());
					vo.setTimerate8(vo.getTimerate8()+st.getTimerate8());
					vo.setTimerate9(vo.getTimerate9()+st.getTimerate9());
					vo.setTimerate10(vo.getTimerate10()+st.getTimerate10());
					vo.setTimerate11(vo.getTimerate11()+st.getTimerate11());
					vo.setTimerate12(vo.getTimerate12()+st.getTimerate12());
					vo.setTimerate13(vo.getTimerate13()+st.getTimerate13());
					vo.setTimerate14(vo.getTimerate14()+st.getTimerate14());
					vo.setTimerate15(vo.getTimerate15()+st.getTimerate15());
					vo.setTimerate16(vo.getTimerate16()+st.getTimerate16());
					vo.setTimerate17(vo.getTimerate17()+st.getTimerate17());
					vo.setTimerate18(vo.getTimerate18()+st.getTimerate18());
					vo.setTimerate19(vo.getTimerate19()+st.getTimerate19());
					vo.setTimerate20(vo.getTimerate20()+st.getTimerate20());
					stmap.put(vo.getWindpwoerid(), vo);
				}else
				{
					Statetransitionrates vo=new Statetransitionrates();
					vo.setWindpwoerid(st.getWindpwoerid());
					vo.setTimerate1(st.getTimerate1());
					vo.setTimerate2(st.getTimerate2());
					vo.setTimerate3(st.getTimerate3());
					vo.setTimerate4(st.getTimerate4());
					vo.setTimerate5(st.getTimerate5());
					vo.setTimerate6(st.getTimerate6());
					vo.setTimerate7(st.getTimerate7());
					vo.setTimerate8(st.getTimerate8());
					vo.setTimerate9(st.getTimerate9());
					vo.setTimerate10(st.getTimerate10());
					vo.setTimerate11(st.getTimerate11());
					vo.setTimerate12(st.getTimerate12());
					vo.setTimerate13(st.getTimerate13());
					vo.setTimerate14(st.getTimerate14());
					vo.setTimerate15(st.getTimerate15());
					vo.setTimerate16(st.getTimerate16());
					vo.setTimerate17(st.getTimerate17());
					vo.setTimerate18(st.getTimerate18());
					vo.setTimerate19(st.getTimerate19());
					vo.setTimerate20(st.getTimerate20());
					stmap.put(vo.getWindpwoerid(), vo);
				}

			}
		}
		return stmap;
	}

	public Map<String, Statetransitionrates>  getStatetransitionratesByWtTotal(String id, Date beginDate, Date endDate,int type){

		Map<String, Statetransitionrates> stmap = new HashMap<String, Statetransitionrates>();

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

		StatetransitionratesExample example=new StatetransitionratesExample();
		example.setOrderByClause("windpwoerid ASC");

		StatetransitionratesExample.Criteria criteria =example.createCriteria();



		if(StringUtils.notEmp(type))
		{
			switch (type) {
				case 1:

					if(StringUtils.notEmp(id)){
						criteria.andWindpwoeridEqualTo(id);
					}
					break;
				case 2:
					if(StringUtils.notEmp(id)){
						criteria.andProjectidEqualTo(id);
					}
					break;
				case 3:
					if(StringUtils.notEmp(id)){
						criteria.andLineidEqualTo(id);
					}
					break;
				default:
					break;
			}
		}
		if(beginDate!=null && endDate!=null){


			criteria.andRecorddateGreaterThanOrEqualTo(beginDate).andRecorddateLessThan(endDate);
			list= statetransitionratesMapper.selectByExample(example);
		}
		if(list!=null)
		{
			for(Statetransitionrates st:list)
			{

				if(stmap.containsKey(st.getWindpwoerid()))
				{
					Statetransitionrates vo=stmap.get(st.getWindpwoerid());
					vo.setWindpwoerid(st.getWindpwoerid());
					vo.setTimerate1(vo.getTimerate1()+st.getTimerate1());
					vo.setTimerate2(vo.getTimerate2()+st.getTimerate2());
					vo.setTimerate3(vo.getTimerate3()+st.getTimerate3());
					vo.setTimerate4(vo.getTimerate4()+st.getTimerate4());
					vo.setTimerate5(vo.getTimerate5()+st.getTimerate5());
					vo.setTimerate6(vo.getTimerate6()+st.getTimerate6());
					vo.setTimerate7(vo.getTimerate7()+st.getTimerate7());
					vo.setTimerate8(vo.getTimerate8()+st.getTimerate8());
					vo.setTimerate9(vo.getTimerate9()+st.getTimerate9());
					vo.setTimerate10(vo.getTimerate10()+st.getTimerate10());
					vo.setTimerate11(vo.getTimerate11()+st.getTimerate11());
					vo.setTimerate12(vo.getTimerate12()+st.getTimerate12());
					vo.setTimerate13(vo.getTimerate13()+st.getTimerate13());
					vo.setTimerate14(vo.getTimerate14()+st.getTimerate14());
					vo.setTimerate15(vo.getTimerate15()+st.getTimerate15());
					vo.setTimerate16(vo.getTimerate16()+st.getTimerate16());
					vo.setTimerate17(vo.getTimerate17()+st.getTimerate17());
					vo.setTimerate18(vo.getTimerate18()+st.getTimerate18());
					vo.setTimerate19(vo.getTimerate19()+st.getTimerate19());
					vo.setTimerate20(vo.getTimerate20()+st.getTimerate20());
					stmap.put(vo.getWindpwoerid(), vo);
				}else
				{
					Statetransitionrates vo=new Statetransitionrates();
					vo.setWindpwoerid(st.getWindpwoerid());
					vo.setTimerate1(st.getTimerate1());
					vo.setTimerate2(st.getTimerate2());
					vo.setTimerate3(st.getTimerate3());
					vo.setTimerate4(st.getTimerate4());
					vo.setTimerate5(st.getTimerate5());
					vo.setTimerate6(st.getTimerate6());
					vo.setTimerate7(st.getTimerate7());
					vo.setTimerate8(st.getTimerate8());
					vo.setTimerate9(st.getTimerate9());
					vo.setTimerate10(st.getTimerate10());
					vo.setTimerate11(st.getTimerate11());
					vo.setTimerate12(st.getTimerate12());
					vo.setTimerate13(st.getTimerate13());
					vo.setTimerate14(st.getTimerate14());
					vo.setTimerate15(st.getTimerate15());
					vo.setTimerate16(st.getTimerate16());
					vo.setTimerate17(st.getTimerate17());
					vo.setTimerate18(st.getTimerate18());
					vo.setTimerate19(st.getTimerate19());
					vo.setTimerate20(st.getTimerate20());
					stmap.put(vo.getWindpwoerid(), vo);
				}

			}
		}
		return stmap;
	}


	public Map<String, Map<String, Statetransitionrates>>  getStatetransitionratesByWt(String id, Date beginDate, Date endDate){

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

		Map<String, Map<String, Statetransitionrates>> stmap = new HashMap<String, Map<String, Statetransitionrates>>();

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

		StatetransitionratesExample example=new StatetransitionratesExample();
		example.setOrderByClause("windpwoerid ASC");

		StatetransitionratesExample.Criteria criteria =example.createCriteria();

		if(StringUtils.notEmp(id)){
			criteria.andWindpwoeridEqualTo(id);
		}
		if(beginDate!=null && endDate!=null){


			criteria.andRecorddateGreaterThanOrEqualTo(beginDate).andRecorddateLessThan(endDate);
			list= statetransitionratesMapper.selectByExample(example);
		}

		if(list!=null)
		{

			for(Statetransitionrates st:list)
			{
				String day = df.format(st.getRecorddate());
				if(stmap.containsKey(st.getWindpwoerid()))
				{
					Map<String, Statetransitionrates> map=stmap.get(st.getWindpwoerid());

					map.put(day, st);
					stmap.put(st.getWindpwoerid(),map);
				}else
				{
					Map<String, Statetransitionrates> map=new HashMap<String, Statetransitionrates>();
					map.put(day, st);
					stmap.put(st.getWindpwoerid(),map);
				}
			}
		}
		return stmap;
	}
}
