package com.gyee.frame.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.gyee.frame.common.base.BaseService;
import com.gyee.frame.common.cache.IGlobalCache;
import com.gyee.frame.common.support.Convert;
import com.gyee.frame.mapper.auto.ModelpowerRdMapper;
import com.gyee.frame.model.auto.ModelpowerRd;
import com.gyee.frame.model.auto.ModelpowerRdExample;
import com.gyee.frame.util.SnowflakeIdWorker;
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;


@Service
public class ModelpowerRdService implements BaseService<ModelpowerRd, ModelpowerRdExample> {
	@Resource
	private ModelpowerRdMapper modelpowerRdMapper;
	@Resource
	private IGlobalCache globalCache;

	@Override
	public int deleteByPrimaryKey(String ids) {
				
			List<String> lista=Convert.toListStrArray(ids);
			ModelpowerRdExample example=new ModelpowerRdExample();
			example.createCriteria().andIdIn(lista);
			return modelpowerRdMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public ModelpowerRd selectByPrimaryKey(String id) {
				
			return modelpowerRdMapper.selectByPrimaryKey(id);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(ModelpowerRd record) {
		return modelpowerRdMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(ModelpowerRd record) {
				
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
			
				
		return modelpowerRdMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(ModelpowerRd record, ModelpowerRdExample example) {
		
		return modelpowerRdMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(ModelpowerRd record, ModelpowerRdExample example) {
		
		return modelpowerRdMapper.updateByExample(record, example);
	}

	@Override
	public List<ModelpowerRd> selectByExample(ModelpowerRdExample example) {
		
		return modelpowerRdMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(ModelpowerRdExample example) {
		
		return modelpowerRdMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(ModelpowerRdExample example) {
		
		return modelpowerRdMapper.deleteByExample(example);
	}

	public Map<String, Map<Double, ModelpowerRd>> getModelpowerRdAllMap()  {


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

		String mprdString= JSON.toJSONString(globalCache.get("MPRD"));
		list= JSONObject.parseObject(mprdString,new TypeReference<List<ModelpowerRd>>(){});

		Map<String, Map<Double, ModelpowerRd>> res = new HashMap<String, Map<Double,ModelpowerRd>>();
		Map<Double, ModelpowerRd> map = null;

		if(list != null && !list.isEmpty()){
			for(ModelpowerRd mp : list){
				if(res.containsKey(mp.getModelid())){
					map = res.get(mp.getModelid());
					map.put(mp.getSpeed(), mp);
				}else{
					map = new HashMap<Double, ModelpowerRd>();
					map.put(mp.getSpeed(), mp);
					res.put(mp.getModelid(), map);
				}
			}
		}
		return res;

	}
}
