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.Constant;
import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.common.support.Convert;
import com.gyee.frame.mapper.auto.WindPowerStationTestingPointMapper;
import com.gyee.frame.model.auto.WindPowerStationTestingPoint;
import com.gyee.frame.model.auto.WindPowerStationTestingPointExample;
import com.gyee.frame.model.auto.Windpowerstation;
import com.gyee.frame.model.custom.SimpleVo;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.util.SnowflakeIdWorker;
import com.gyee.frame.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 风场测点 windPowerstationTestingPoint2Service
 * @Title: windPowerstationTestingPoint2Service.java 
 * @Package com.gyee.frame.service 
 * @author gyee_自动生成
 * @email 1@qq.com
 * @date 2019-12-31 14:58:09  
 **/
@Service
public class WindPowerstationTestingPoint2Service implements BaseService<WindPowerStationTestingPoint, WindPowerStationTestingPointExample> {
	@Resource
	private WindPowerStationTestingPointMapper windPowerStationTestingPoint2Mapper;
	
      	   	      	      	      	      	      	      	      	      	      	
	/**
	 * 分页查询
	 * @param tablepar
	 * @param name
	 * @return
	 */
	 public PageInfo<WindPowerStationTestingPoint> list(Tablepar tablepar, String name){
	        WindPowerStationTestingPointExample testExample=new WindPowerStationTestingPointExample();
	        testExample.setOrderByClause("id ASC");
	        if(name!=null&&!"".equals(name)){
	        	testExample.createCriteria().andCodeLike("%"+name+"%");
	        }

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

	@Override
	public int deleteByPrimaryKey(String ids) {
				
			List<String> lista=Convert.toListStrArray(ids);
			WindPowerStationTestingPointExample example=new WindPowerStationTestingPointExample();
			example.createCriteria().andCodeIn(lista);
			return windPowerStationTestingPoint2Mapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public WindPowerStationTestingPoint selectByPrimaryKey(String id) {
				
			return windPowerStationTestingPoint2Mapper.selectByPrimaryKey(id);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(WindPowerStationTestingPoint record) {
		return windPowerStationTestingPoint2Mapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(WindPowerStationTestingPoint record) {
				
		//添加雪花主键id
		record.setCode(SnowflakeIdWorker.getUUID());
			
				
		return windPowerStationTestingPoint2Mapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(WindPowerStationTestingPoint record, WindPowerStationTestingPointExample example) {
		
		return windPowerStationTestingPoint2Mapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(WindPowerStationTestingPoint record, WindPowerStationTestingPointExample example) {
		
		return windPowerStationTestingPoint2Mapper.updateByExample(record, example);
	}

	@Override
	public List<WindPowerStationTestingPoint> selectByExample(WindPowerStationTestingPointExample example) {
		
		return windPowerStationTestingPoint2Mapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(WindPowerStationTestingPointExample example) {
		
		return windPowerStationTestingPoint2Mapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(WindPowerStationTestingPointExample example) {
		
		return windPowerStationTestingPoint2Mapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param windPowerstationTestingPoint
	 * @return
	 */
	public int checkNameUnique(WindPowerStationTestingPoint windPowerstationTestingPoint){
		WindPowerStationTestingPointExample example=new WindPowerStationTestingPointExample();
		example.createCriteria().andCodeEqualTo(windPowerstationTestingPoint.getCode());
		List<WindPowerStationTestingPoint> list=windPowerStationTestingPoint2Mapper.selectByExample(example);
		return list.size();
	}

	public WindPowerStationTestingPoint getWindPowerStationTestingPoint2(String wpId, String unicode)  {
		WindPowerStationTestingPoint point=new WindPowerStationTestingPoint();

		if(StringUtils.notEmp(wpId) && StringUtils.notEmp(unicode) )
		{
			if(wpId.equals("0") || wpId.equals("-1") ||wpId.equals("-2") || InitialRunner.wpmap.containsKey(wpId))
			{
				if(InitialRunner.wpPointmap.containsKey(wpId))
				{
					Map<String, WindPowerStationTestingPoint> map=InitialRunner.wpPointmap.get(wpId);
					if(map.containsKey(unicode))
					{
						point=map.get(unicode);
					}
				}
			}else if(InitialRunner.pjmap.containsKey(wpId))
			{
				if(InitialRunner.pjPointmap.containsKey(wpId))
				{
					Map<String, WindPowerStationTestingPoint> map=InitialRunner.pjPointmap.get(wpId);
					if(map.containsKey(unicode))
					{
						point=map.get(unicode);
					}
				}
			}else if(InitialRunner.lnmap.containsKey(wpId))
			{
				if(InitialRunner.lnPointmap.containsKey(wpId))
				{
					Map<String, WindPowerStationTestingPoint> map=InitialRunner.lnPointmap.get(wpId);
					if(map.containsKey(unicode))
					{
						point=map.get(unicode);
					}
				}
			}
		}


		return point;
	}

	public Map<String,Integer> getWpPointTotal(){
		Map<String,Integer> map=new HashMap<>();
		for(Windpowerstation wp:InitialRunner.wpallls)
		{
			String str=wp.getId().substring(0,wp.getId().indexOf("_"));
			List<SimpleVo> vos=windPowerStationTestingPoint2Mapper.getWpPointTotal(str,null);
			if(!vos.isEmpty())
			{
				SimpleVo vo=vos.get(0);
				map.put(wp.getId(),vo.getValue());
			}

		}
		List<SimpleVo> vos=windPowerStationTestingPoint2Mapper.getWpPointTotal(null,"jsfw");
		if(!vos.isEmpty())
		{
			SimpleVo vo=vos.get(0);
			map.put("JSFW",vo.getValue());
		}
		return map;
	}


	public List<WindPowerStationTestingPoint> getJsztList(String wpId){

		List<WindPowerStationTestingPoint> wpoints=new ArrayList<>();
		if(StringUtils.notEmp(wpId) )
		{
			WindPowerStationTestingPointExample example=new WindPowerStationTestingPointExample();
			WindPowerStationTestingPointExample.Criteria criteria =example.createCriteria();
			criteria.andWindpowerstationidEqualTo(wpId);
			criteria.andUniformcodeEqualTo(Constant.JSZT);
			wpoints=windPowerStationTestingPoint2Mapper.selectByExample(example);
		}

		return wpoints;
	}
}
