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.FaultEliminateDefectsMapper;
import com.gyee.frame.model.auto.FaultEliminateDefects;
import com.gyee.frame.model.auto.FaultEliminateDefectsExample;
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.*;

/**
 * 风机信息表 FaultEliminateDefectsService
 * @Title: FaultEliminateDefectsService.java 
 * @Package com.gyee.frame.service 
 **/
@Service
public class FaultEliminateDefectsService implements BaseService<FaultEliminateDefects, FaultEliminateDefectsExample> {
	@Resource
	private FaultEliminateDefectsMapper faultEliminateDefectsMapper;

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

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

	@Override
	public int deleteByPrimaryKey(String ids) {

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


	}


	@Override
	public FaultEliminateDefects selectByPrimaryKey(String id) {

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

		}
	 	return new FaultEliminateDefects();

	}


	@Override
	public int updateByPrimaryKeySelective(FaultEliminateDefects record) {
		return faultEliminateDefectsMapper.updateByPrimaryKeySelective(record);
	}


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

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


		return faultEliminateDefectsMapper.insertSelective(record);
	}


	@Override
	public int updateByExampleSelective(FaultEliminateDefects record, FaultEliminateDefectsExample example) {

		return faultEliminateDefectsMapper.updateByExampleSelective(record, example);
	}


	@Override
	public int updateByExample(FaultEliminateDefects record, FaultEliminateDefectsExample example) {

		return faultEliminateDefectsMapper.updateByExample(record, example);
	}

	@Override
	public List<FaultEliminateDefects> selectByExample(FaultEliminateDefectsExample example) {

		return faultEliminateDefectsMapper.selectByExample(example);
	}


	@Override
	public long countByExample(FaultEliminateDefectsExample example) {

		return faultEliminateDefectsMapper.countByExample(example);
	}


	@Override
	public int deleteByExample(FaultEliminateDefectsExample example) {

		return faultEliminateDefectsMapper.deleteByExample(example);
	}

	public Map<String, FaultEliminateDefects> getFaultEliminateDefectsByWpTotal(String wpId, Date beginDate, Date endDate){

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

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

		FaultEliminateDefectsExample example=new FaultEliminateDefectsExample();
		example.setOrderByClause("windpowerstationid ASC");

		FaultEliminateDefectsExample.Criteria criteria =example.createCriteria();

		if(StringUtils.isNotEmpty(wpId))
		{
			criteria.andWindpowerstationidEqualTo(wpId);
		}

		if(beginDate!=null && endDate!=null ){


			criteria.andDatetimeGreaterThanOrEqualTo(beginDate).andDatetimeLessThan(endDate);
			list= faultEliminateDefectsMapper.selectByExample(example);
		}
		if(list!=null)
		{
			for(FaultEliminateDefects st:list)
			{

				if(stmap.containsKey(st.getWindpowerstationid()))
				{
					FaultEliminateDefects vo=stmap.get(st.getWindpowerstationid());
					vo.setWindpowerstationid(st.getWindpowerstationid());
					vo.setFaultcount(vo.getFaultcount()+st.getFaultcount());
					vo.setRightcount(vo.getRightcount()+st.getRightcount());
					stmap.put(vo.getWindpowerstationid(), vo);
				}else
				{
					FaultEliminateDefects vo=new FaultEliminateDefects();
					vo.setWindpowerstationid(st.getWindpowerstationid());
					vo.setFaultcount(st.getFaultcount());
					vo.setRightcount(st.getRightcount());
					stmap.put(vo.getWindpowerstationid(), vo);
				}

			}
		}
		return stmap;
	}



	public Map<String, Map<String, FaultEliminateDefects>> getFaultEliminateDefectsByWt(String wpId, Date beginDate, Date endDate){

		Map<String, Map<String, FaultEliminateDefects>> stmap = new HashMap<String, Map<String, FaultEliminateDefects>>();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

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

		FaultEliminateDefectsExample example=new FaultEliminateDefectsExample();
		example.setOrderByClause("windpowerstationid ASC");

		FaultEliminateDefectsExample.Criteria criteria =example.createCriteria();

		if(StringUtils.isNotEmpty(wpId))
		{
			criteria.andWindpowerstationidEqualTo(wpId);
		}

		if(beginDate!=null && endDate!=null ){


			criteria.andDatetimeGreaterThanOrEqualTo(beginDate).andDatetimeLessThan(endDate);
			list= faultEliminateDefectsMapper.selectByExample(example);
		}
		if(list!=null)
		{
			for(FaultEliminateDefects st:list)
			{
				String day = df.format(st.getDatetime());
				if(stmap.containsKey(st.getWindpowerstationid()))
				{
					Map<String, FaultEliminateDefects> map=stmap.get(st.getWindpowerstationid());
					map.put(day, st);
					stmap.put(st.getWindpowerstationid(),map);
				}else
				{
					Map<String, FaultEliminateDefects> map=new HashMap<String, FaultEliminateDefects>();
					map.put(day, st);
					stmap.put(st.getWindpowerstationid(),map);
				}
			}
		}
		return stmap;
	}

	public Map<String, FaultEliminateDefects> getFaultEliminateDefectsByWtTotal(String id, Date beginDate, Date endDate,int type){

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

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

		FaultEliminateDefectsExample example=new FaultEliminateDefectsExample();
		example.setOrderByClause("windpowerstationid ASC");

		FaultEliminateDefectsExample.Criteria criteria =example.createCriteria();

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

					if(StringUtils.notEmp(id)){
						criteria.andWindpowerstationidEqualTo(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.andDatetimeGreaterThanOrEqualTo(beginDate).andDatetimeLessThan(endDate);
			list= faultEliminateDefectsMapper.selectByExample(example);
		}
		if(list!=null)
		{
			for(FaultEliminateDefects st:list)
			{

				if(stmap.containsKey(st.getWindpowerstationid()))
				{
					FaultEliminateDefects vo=stmap.get(st.getWindpowerstationid());
					vo.setWindpowerstationid(st.getWindpowerstationid());
					vo.setFaultcount(vo.getFaultcount()+st.getFaultcount());
					vo.setRightcount(vo.getRightcount()+st.getRightcount());
					stmap.put(vo.getWindpowerstationid(), vo);
				}else
				{
					FaultEliminateDefects vo=new FaultEliminateDefects();
					vo.setWindpowerstationid(st.getWindpowerstationid());
					vo.setFaultcount(st.getFaultcount());
					vo.setRightcount(st.getRightcount());
					stmap.put(vo.getWindpowerstationid(), vo);
				}

			}
		}
		return stmap;
	}
}
