package com.huimv.acquisition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.acquisition.entity.*;
import com.huimv.acquisition.entity.vo.EartagParamVo;
import com.huimv.acquisition.mapper.BasePigpenDao;
import com.huimv.acquisition.mapper.CollectorRegisterDao;
import com.huimv.acquisition.mapper.EartagDataDao;
import com.huimv.acquisition.mapper.MovePigpenDao;
import com.huimv.acquisition.service.EartagDataService;
import com.huimv.acquisition.socket.eartagrecive.util.DateUtilsPublic;
import com.huimv.acquisition.socket.eartagrecive.util.StringUtilsWork;
import com.huimv.acquisition.utils.PageUtils;
import com.huimv.acquisition.utils.Query;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 
 *
 * @author yinhao
 * @version ${version}
 * @date 2021-07-24 16:29:33
 */
@Service("eartagDataService")
public class EartagDataServiceImpl extends ServiceImpl<EartagDataDao, EartagDataEntity> implements EartagDataService {

    @Autowired
    private EartagDataDao eartagDataDao;

    @Autowired
    private BasePigpenDao basePigpenDao;

    @Autowired
    private CollectorRegisterDao collectorRegisterDao ;

    @Autowired
    private MovePigpenDao movePigpenDao;


    //这个接口写的太辣鸡了
    @Override
    public PageUtils findByConditions(Map<String, Object> params) throws ParseException {

        String farmID = (String) params.get("farmID");//牧场编码
        String starttime = (String) params.get("starttime");//开始时间
        String endtime = (String) params.get("endtime");//结束时间
        String eartagNo = (String) params.get("eartagNo");//耳标号
        String dayAge = (String) params.get("dayAge");//配标日龄
        String highEartagTemp = (String) params.get("highEartagTemp");//耳标温度
        String lowEartagTemp = (String) params.get("lowEartagTemp");//耳标温度
        String highEnvTemp = (String) params.get("highEnvTemp");//环境温度
        String lowEnvTemp = (String) params.get("lowEnvTemp");//环境温度
        String deviceCode = (String) params.get("deviceCode");//采集器编码
        String bat = (String) params.get("bat");//电量
        String dayAge2 = (String) params.get("dayage2");//日龄

        System.out.println("采集器+condition---"+deviceCode+"耳标号"+eartagNo);
        //只要首页限制在第一页，其余的不需要的

        LambdaQueryWrapper<EartagDataEntity> wrapper = Wrappers.lambdaQuery();
        if(StringUtils.isNotEmpty(farmID)){  //StringUtils.isNotEmpty(orderField) && StringUtils.isNotEmpty(order)
            wrapper.eq(EartagDataEntity::getFarmCode, farmID );
        }
        if(StringUtilsWork.isNotBlank(eartagNo)){
            wrapper.eq(EartagDataEntity::getEartagNo, eartagNo );
        }
        if(StringUtilsWork.isNotBlank(deviceCode)){
            wrapper.eq(EartagDataEntity::getDeviceCode, deviceCode );
        }
        if(StringUtilsWork.isNotBlank(starttime)& StringUtilsWork.isNotBlank(endtime)) {
            System.out.println(starttime+"***shijian**"+endtime);
            DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //格式化传入的时间
            Date startTime = fmt.parse(starttime);
            Date endTime = fmt.parse(endtime);
            System.out.println("yangdi" + starttime + endtime);
            //过滤时间
            wrapper.between(EartagDataEntity::getTime, startTime, endTime);
        }

        if (StringUtilsWork.isNotBlank(dayAge)){
            wrapper.eq(EartagDataEntity::getBat, dayAge );
        }
        if (StringUtilsWork.isNotBlank(dayAge2)){
            wrapper.eq(EartagDataEntity::getDeviceCode, dayAge2 );
        }
        if (StringUtilsWork.isNotBlank(highEartagTemp)& StringUtilsWork.isNotBlank(lowEartagTemp)){
            wrapper.between(EartagDataEntity::getEarTemp, highEartagTemp, lowEartagTemp);
        }
        if (StringUtilsWork.isNotBlank(highEnvTemp)& StringUtilsWork.isNotBlank(lowEnvTemp)){
            wrapper.between(EartagDataEntity::getEnvTemp, highEnvTemp, lowEnvTemp);
        }


        List<EartagDataEntity> records;
        IPage<EartagDataEntity>  page;
        if(StringUtilsWork.isNotBlank(eartagNo)){
            System.out.println("有耳标号，展示耳标所有数据");
             page = this.page((new Query<EartagDataEntity>().getPage(params)),wrapper);
             records = page.getRecords();  //获取信息

        }else{
            System.out.println("没有耳标号，展示最新");
            wrapper.groupBy(EartagDataEntity::getEartagNo).orderByDesc( EartagDataEntity::getId );
             page = baseMapper.page(new Query<EartagDataEntity>().getPage(params), wrapper);
            records = page.getRecords();  //获取信息
        }

        //拼接栋舍信息在里面
        for (EartagDataEntity record : records) {
            //这一页的数据
            System.out.println("数据record"+records);
            LambdaQueryWrapper<MovePigpenEntity> wrapper23 = Wrappers.lambdaQuery();
            wrapper23.eq(MovePigpenEntity::getEartagNo , record.getEartagNo()).orderByAsc( MovePigpenEntity::getId ).last( "limit 1");
            MovePigpenEntity movePigpen = movePigpenDao.selectOne(wrapper23);
            System.out.println("转舍信息"+movePigpen+"转舍耳标号"+record.getEartagNo());
            //获取注册---注册信息为空
            LambdaQueryWrapper<CollectorRegisterEntity> wrapper3 = Wrappers.lambdaQuery();
            wrapper3.eq(CollectorRegisterEntity::getDeviceCode, record.getDeviceCode()  );
            CollectorRegisterEntity collectorRegisterEntity = collectorRegisterDao.selectOne(wrapper3);
            System.out.println("注册表信息"+collectorRegisterEntity);

            LambdaQueryWrapper<BasePigpenEntity> wrapper4 = Wrappers.lambdaQuery();
            wrapper4.eq(BasePigpenEntity::getId, collectorRegisterEntity.getPigpenId() );
            BasePigpenEntity basePigpenEntity = basePigpenDao.selectOne(wrapper4);
            System.out.println("栋舍信息"+basePigpenEntity);

            LambdaQueryWrapper<BasePigpenEntity> wrapper5 = Wrappers.lambdaQuery();
            wrapper5.eq(BasePigpenEntity::getId, collectorRegisterEntity.getUnitId() );
            BasePigpenEntity basePigpenEntity1 = basePigpenDao.selectOne(wrapper5);
            record.setVersion(basePigpenEntity.getPigpenName());//这个多余字段存猪舍名字
            record.setCommand(basePigpenEntity1.getPigpenName());//这个多余字段存单元名字
            //格式化传入的时---第一条不应该没有----但是新耳标上来没有存信息
            if (movePigpen!=null) {
                record.setTail(DateUtilsPublic.dateToString(movePigpen.getMoveDate()));//配标时间
            }
        }
        return  new PageUtils(page);
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<EartagDataEntity> page = this.page(
                new Query<EartagDataEntity>().getPage(params),
                new QueryWrapper<EartagDataEntity>()
        );
        return new PageUtils(page);
    }


    //杭电展示专用接口--耳标号目前写死  部署在35服务器 数据库为  huimv—eartag-show
    @Override
    public   Map  queryEartagDeatils(EartagParamVo eartagParamVo) throws ParseException {
        String  eartagNo1 ="133021100375854";
        String  eartagNo2 ="133021100376024";

        LambdaQueryWrapper<EartagDataEntity> wrapper1 = Wrappers.lambdaQuery();
        wrapper1.eq(EartagDataEntity::getEartagNo, eartagNo1).orderByDesc(EartagDataEntity::getId);
        if(eartagParamVo.getStarTime()!=null){
            //传了时间按照时间
            wrapper1.between(EartagDataEntity::getTime, eartagParamVo.getStarTime(), eartagParamVo.getEndTime());
        }else {
            //没有时间返回最近的500条
            wrapper1.last( "limit 50");
        }
        System.out.println("耳标号="+eartagParamVo.getEartagNo());
        List<EartagDataEntity> eartagDataEntities1 = eartagDataDao.selectList(wrapper1);


        LambdaQueryWrapper<EartagDataEntity> wrapper2 = Wrappers.lambdaQuery();
        wrapper2.eq(EartagDataEntity::getEartagNo,eartagNo2).orderByDesc(EartagDataEntity::getId);
        if(eartagParamVo.getStarTime()!=null){
            //传了时间按照时间
            wrapper2.between(EartagDataEntity::getTime, eartagParamVo.getStarTime(), eartagParamVo.getEndTime());
        }else {
            //没有时间返回最近的1000条
            wrapper2.last( "limit 50");
        }
        List<EartagDataEntity> eartagDataEntities2 = eartagDataDao.selectList(wrapper2);

        Map resultMap  = new HashMap();
        
        List<String> timeList = new ArrayList<>();
        List<Double> earTempList = new ArrayList<>();
        List<Integer> sportsList = new ArrayList<>();
        List<Integer> electricList = new ArrayList<>();
        List<Double> environmentTempList = new ArrayList<>();
        List<Object> averangeTemp = new ArrayList<>();
        List<Integer> exerciseList = new ArrayList<>();
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        

        for (EartagDataEntity eartagDataEntity : eartagDataEntities1) {
            timeList.add( fmt.format(eartagDataEntity.getTime() ));
            earTempList.add( eartagDataEntity.getEarTemp()  );
            electricList.add(eartagDataEntity.getBat());
            environmentTempList.add(eartagDataEntity.getEnvTemp());
            sportsList.add(eartagDataEntity.getSportGap());
            exerciseList.add(eartagDataEntity.getExercise());
        }


        List<Double> earTempList2 = new ArrayList<>();
        List<Integer> sportsList2 = new ArrayList<>();
        List<Integer> electricList2 = new ArrayList<>();
        List<Double> environmentTempList2 = new ArrayList<>();
        List<Object> averangeTemp2 = new ArrayList<>();
        List<Integer> exerciseList2 = new ArrayList<>();

        
        
        
        for (EartagDataEntity eartagDataEntity : eartagDataEntities2) {
            earTempList2.add( eartagDataEntity.getEarTemp()  );
            electricList2.add(eartagDataEntity.getBat());
            environmentTempList2.add(eartagDataEntity.getEnvTemp());
            sportsList2.add(eartagDataEntity.getSportGap());
            exerciseList2.add(eartagDataEntity.getExercise());
        }

      
        Collections.reverse(earTempList2);
        Collections.reverse(electricList2);
        Collections.reverse(environmentTempList2);
        Collections.reverse(sportsList2);
        Collections.reverse(exerciseList2);


        Collections.reverse(timeList);
        Collections.reverse(earTempList);
        Collections.reverse(electricList);
        Collections.reverse(environmentTempList);
        Collections.reverse(sportsList);
        Collections.reverse(exerciseList);

        resultMap.put("timeList1",timeList);
        resultMap.put("earTempList1",earTempList);
        resultMap.put("electricList1",electricList);
        resultMap.put("environmentTempList1",environmentTempList);
        resultMap.put("sportsList1",sportsList);
        resultMap.put("exerciseList1",exerciseList);
        

        resultMap.put("earTempList2",earTempList2);
        resultMap.put("electricList2",electricList2);
        resultMap.put("environmentTempList2",environmentTempList2);
        resultMap.put("sportsList2",sportsList2);
        resultMap.put("exerciseList2",exerciseList2);
        
        return resultMap;

    }






    //想办法把平均温度计算到表里面去
    @Override
    public Map<String, Object> queryDetails(Map<String, Object> params) throws ParseException {
        System.out.println("id = "+params.get("id"));
            String id1 = String.valueOf(params.get("id"));
        String starttime = (String) params.get("startTime");
        String endtime = (String) params.get("endTime");
             int id = Integer.parseInt(id1);
        System.out.println("int id = "+id);
        //找出对应的ID
        LambdaQueryWrapper<EartagDataEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EartagDataEntity::getId, id );
        EartagDataEntity earTagData = eartagDataDao.selectOne(wrapper);
        if(earTagData==null){
            Map map = new HashMap();
            map.put("error" ,"当前数据不存在");
            return  map;
        }

        //对数据进行包装
        LambdaQueryWrapper<CollectorRegisterEntity> wrapper1 = Wrappers.lambdaQuery();
        wrapper1.eq(CollectorRegisterEntity::getDeviceCode, earTagData.getDeviceCode() );
        CollectorRegisterEntity collectorRegisterEntity = collectorRegisterDao.selectOne(wrapper1);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("Registentity",collectorRegisterEntity);//耳标所在的机器信息



        Map<String, Object> pen = new HashMap<>();


        //倒数yi条
        LambdaQueryWrapper<MovePigpenEntity> wrapper2 = Wrappers.lambdaQuery();
        wrapper2.eq(MovePigpenEntity::getEartagNo , earTagData.getEartagNo()).orderByDesc( MovePigpenEntity::getId ).last( "limit 1");
        List<MovePigpenEntity> movePigpenEntities = movePigpenDao.selectList(wrapper2);
        //时间最开始的一条
        LambdaQueryWrapper<MovePigpenEntity> wrapper23 = Wrappers.lambdaQuery();
        wrapper23.eq(MovePigpenEntity::getEartagNo , earTagData.getEartagNo()).orderByAsc( MovePigpenEntity::getId ).last( "limit 1");
        MovePigpenEntity movePigpen = movePigpenDao.selectOne(wrapper23);

        movePigpenEntities.add(movePigpen);//加上去l

        System.out.println(movePigpen+"ioioio");//最开始不为空


        System.out.println(movePigpenEntities+"opop");
    if ( movePigpen!=null ) {

        List<Object> movePigpe111 = new ArrayList<>();

        for (MovePigpenEntity movePigpenEntity : movePigpenEntities) {
            Map<String, Object> c = new HashMap<>();


            LambdaQueryWrapper<CollectorRegisterEntity> wrapper3 = Wrappers.lambdaQuery();
            wrapper3.eq(CollectorRegisterEntity::getDeviceCode, movePigpenEntity.getDeviceCode());
            CollectorRegisterEntity collectorRegisterEntit = collectorRegisterDao.selectOne(wrapper3);
            System.out.println();

            LambdaQueryWrapper<BasePigpenEntity> wrapper4 = Wrappers.lambdaQuery();
            wrapper4.eq(BasePigpenEntity::getId, collectorRegisterEntit.getPigpenId());//栋舍
            BasePigpenEntity basePigpenEnti = basePigpenDao.selectOne(wrapper4);

            LambdaQueryWrapper<BasePigpenEntity> wrapper5 = Wrappers.lambdaQuery();
            wrapper5.eq(BasePigpenEntity::getId, collectorRegisterEntit.getUnitId());//单元
            BasePigpenEntity basePigpenEnt2 = basePigpenDao.selectOne(wrapper4);

            TempMovePenEntity tempMovePenEntity = new TempMovePenEntity();
            tempMovePenEntity.setDeviceCode(movePigpenEntity.getDeviceCode());
            tempMovePenEntity.setEartagNo(movePigpenEntity.getEartagNo());
            tempMovePenEntity.setPenName(basePigpenEnti.getPigpenName());
            tempMovePenEntity.setUnitname(basePigpenEnt2.getPigpenName());
            tempMovePenEntity.setMoveDate(movePigpenEntity.getMoveDate());
            tempMovePenEntity.setId(movePigpenEntity.getId());
            tempMovePenEntity.setStage(movePigpenEntity.getStage());

            movePigpe111.add(tempMovePenEntity);
        }

        map1.put("movePigPenData",movePigpe111);
         }else{
        map1.put("movePigPenData",null);}


        map1.put("eartagdeta",earTagData);  //点击的耳标信息

        //获取运动等信息
        String eartag = earTagData.getEartagNo();
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //格式化传入的时间
        Date startTime = fmt.parse(starttime);
        Date endTime = fmt.parse(endtime);
        System.out.println("yangdi"+starttime+endtime);

        LambdaQueryWrapper<EartagDataEntity> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(EartagDataEntity::getEartagNo,eartag);
        //过滤时间
        lambdaQuery.between(EartagDataEntity::getTime,startTime, endTime);
        List<EartagDataEntity> eartagDateEntities = baseMapper.selectList(lambdaQuery);
         //生成平均温度 -----如果刚刚转舍就没办法操作了---数据不会立马出来需要等

        LambdaQueryWrapper<EartagDataEntity> lambdaQuery1 = Wrappers.lambdaQuery();
        lambdaQuery1.eq(EartagDataEntity::getDeviceCode,earTagData.getDeviceCode());
        //过滤时间
        lambdaQuery1.between(EartagDataEntity::getTime,startTime, endTime);
        List<EartagDataEntity> eartagDateEntities1 = baseMapper.selectList(lambdaQuery1);
        Map averange = getAverange(starttime, endtime, eartagDateEntities1);

        //这样，时间轴

        List<String> timeList = new ArrayList<>();
        List<Double> tempList = new ArrayList<>();
        List<Integer> sportsList = new ArrayList<>();
        List<Integer> electricList = new ArrayList<>();
        List<Double> environmenttempList = new ArrayList<>();
        List<Object> averangeTemp = new ArrayList<>();
        for (EartagDataEntity data : eartagDateEntities) {
            timeList.add(fmt.format(data.getTime()));
            tempList.add(data.getEarTemp());
            if (data.getDeviceCode() == null) {
                sportsList.add(0);
            } else {
                sportsList.add(data.getSportGap());
            }
            electricList.add(data.getBat());
            //sleeptime.add(data.getSleepStatus());    用来存睡眠状态了
            environmenttempList.add(data.getEnvTemp());
            averangeTemp.add(averange.get(getMonthDayHour(  data.getTime()  ) ) );
            }

        map1.put("time",timeList);
        map1.put("temp",tempList);
        map1.put("sports",sportsList);
        map1.put("electric",electricList);
        map1.put("environmenttemp",environmenttempList);
        map1.put("averangeTemp",averangeTemp);

        System.out.println("changdu"+timeList.size()+"**"+averangeTemp.size()
        );
        //对time循环--用contains来处理稍微快一点
        //map1.put("averangeTemp",averange);

        //关于耳标存在的位置的信息，这里需要做记录，如果发生位置迁移那么需要将位置迁移信息记录到一张表中
        return map1;
    }


    //想办法把平均温度计算到表里面去
    @Override
    public Map<String, Object> queryDetailsByEarTag(Map<String, Object> params) throws ParseException {
        System.out.println("eartagno = "+params.get("eartagno"));
        String eartagno1 = String.valueOf(params.get("eartagno"));
        String starttime = (String) params.get("startTime");
        String endtime = (String) params.get("endTime");

        //找出对应的ID
        LambdaQueryWrapper<EartagDataEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EartagDataEntity::getEartagNo, eartagno1 ).orderByDesc( EartagDataEntity::getId ).last( "limit 1");
        EartagDataEntity earTagData = eartagDataDao.selectOne(wrapper);
        if(earTagData==null){
            Map map = new HashMap();
            map.put("error" ,"当前数据不存在");
            return  map;
        }

        //对数据进行包装
        LambdaQueryWrapper<CollectorRegisterEntity> wrapper1 = Wrappers.lambdaQuery();
        wrapper1.eq(CollectorRegisterEntity::getDeviceCode, earTagData.getDeviceCode() );
        CollectorRegisterEntity collectorRegisterEntity = collectorRegisterDao.selectOne(wrapper1);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("Registentity",collectorRegisterEntity);//耳标所在的机器信息

        Map<String, Object> pen = new HashMap<>();


        //倒数yi条
        LambdaQueryWrapper<MovePigpenEntity> wrapper2 = Wrappers.lambdaQuery();
        wrapper2.eq(MovePigpenEntity::getEartagNo , earTagData.getEartagNo()).orderByDesc( MovePigpenEntity::getId ).last( "limit 1");
        List<MovePigpenEntity> movePigpenEntities = movePigpenDao.selectList(wrapper2);
        //时间最开始的一条
        LambdaQueryWrapper<MovePigpenEntity> wrapper23 = Wrappers.lambdaQuery();
        wrapper23.eq(MovePigpenEntity::getEartagNo , earTagData.getEartagNo()).orderByAsc( MovePigpenEntity::getId ).last( "limit 1");
        MovePigpenEntity movePigpen = movePigpenDao.selectOne(wrapper23);

        movePigpenEntities.add(movePigpen);//加上去l

        System.out.println(movePigpen+"ioioio");//最开始不为空


        System.out.println(movePigpenEntities+"opop");
        if ( movePigpen!=null ) {

            List<Object> movePigpe111 = new ArrayList<>();

            for (MovePigpenEntity movePigpenEntity : movePigpenEntities) {
                Map<String, Object> c = new HashMap<>();


                LambdaQueryWrapper<CollectorRegisterEntity> wrapper3 = Wrappers.lambdaQuery();
                wrapper3.eq(CollectorRegisterEntity::getDeviceCode, movePigpenEntity.getDeviceCode());
                CollectorRegisterEntity collectorRegisterEntit = collectorRegisterDao.selectOne(wrapper3);
                System.out.println();

                LambdaQueryWrapper<BasePigpenEntity> wrapper4 = Wrappers.lambdaQuery();
                wrapper4.eq(BasePigpenEntity::getId, collectorRegisterEntit.getPigpenId());//栋舍
                BasePigpenEntity basePigpenEnti = basePigpenDao.selectOne(wrapper4);

                LambdaQueryWrapper<BasePigpenEntity> wrapper5 = Wrappers.lambdaQuery();
                wrapper5.eq(BasePigpenEntity::getId, collectorRegisterEntit.getUnitId());//单元
                BasePigpenEntity basePigpenEnt2 = basePigpenDao.selectOne(wrapper4);

                TempMovePenEntity tempMovePenEntity = new TempMovePenEntity();
                tempMovePenEntity.setDeviceCode(movePigpenEntity.getDeviceCode());
                tempMovePenEntity.setEartagNo(movePigpenEntity.getEartagNo());
                tempMovePenEntity.setPenName(basePigpenEnti.getPigpenName());
                tempMovePenEntity.setUnitname(basePigpenEnt2.getPigpenName());
                tempMovePenEntity.setMoveDate(movePigpenEntity.getMoveDate());
                tempMovePenEntity.setId(movePigpenEntity.getId());
                tempMovePenEntity.setStage(movePigpenEntity.getStage());

                movePigpe111.add(tempMovePenEntity);
            }

            map1.put("movePigPenData",movePigpe111);
        }else{
            map1.put("movePigPenData",null);}


        map1.put("eartagdeta",earTagData);  //点击的耳标信息

        //获取运动等信息
        String eartag = earTagData.getEartagNo();
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //格式化传入的时间
        Date startTime = fmt.parse(starttime);
        Date endTime = fmt.parse(endtime);
        System.out.println("yangdi"+starttime+endtime);

        LambdaQueryWrapper<EartagDataEntity> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(EartagDataEntity::getEartagNo,eartag);
        //过滤时间
        lambdaQuery.between(EartagDataEntity::getTime,startTime, endTime);
        List<EartagDataEntity> eartagDateEntities = baseMapper.selectList(lambdaQuery);
        //生成平均温度 -----如果刚刚转舍就没办法操作了---数据不会立马出来需要等

        LambdaQueryWrapper<EartagDataEntity> lambdaQuery1 = Wrappers.lambdaQuery();
        lambdaQuery1.eq(EartagDataEntity::getDeviceCode,earTagData.getDeviceCode());
        //过滤时间
        lambdaQuery1.between(EartagDataEntity::getTime,startTime, endTime);
        List<EartagDataEntity> eartagDateEntities1 = baseMapper.selectList(lambdaQuery1);
        Map averange = getAverange(starttime, endtime, eartagDateEntities1);

        //这样，时间轴

        List<String> timeList = new ArrayList<>();
        List<Double> tempList = new ArrayList<>();
        List<Integer> sportsList = new ArrayList<>();
        List<Integer> electricList = new ArrayList<>();
        List<Double> environmenttempList = new ArrayList<>();
        List<Object> averangeTemp = new ArrayList<>();
        for (EartagDataEntity data : eartagDateEntities) {
            timeList.add(fmt.format(data.getTime()));
            tempList.add(data.getEarTemp());
            if (data.getDeviceCode() == null) {
                sportsList.add(0);
            } else {
                sportsList.add(data.getSportGap());
            }
            electricList.add(data.getBat());
            //sleeptime.add(data.getSleepStatus());    用来存睡眠状态了
            environmenttempList.add(data.getEnvTemp());
            averangeTemp.add(averange.get(getMonthDayHour(  data.getTime()  ) ) );
        }

        map1.put("time",timeList);
        map1.put("temp",tempList);
        map1.put("sports",sportsList);
        map1.put("electric",electricList);
        map1.put("environmenttemp",environmenttempList);
        map1.put("averangeTemp",averangeTemp);

        System.out.println("changdu"+timeList.size()+"**"+averangeTemp.size()
        );
        //对time循环--用contains来处理稍微快一点
        //map1.put("averangeTemp",averange);

        //关于耳标存在的位置的信息，这里需要做记录，如果发生位置迁移那么需要将位置迁移信息记录到一张表中
        return map1;
    }



    public String getMonthDayHour(Date date  ){

        String year= String.format("%tY", date);
        String mon= String.format("%tm", date);
        String day= String.format("%td", date);
        String h= String.format("%tH", date);
        String m= String.format("%tM", date);
        String s= String.format("%tS", date);

return mon+day+h;

    }


       public Map<String, String> getAverange(String starttime , String endtime  , List<EartagDataEntity> entityList     ) throws ParseException {
            DecimalFormat df = new DecimalFormat("####0.00");
         DateFormat fmt1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

         //格式化传入的时间
         Date startTime = fmt1.parse(starttime);
         Date endTime  =    fmt1.parse(endtime);
         Map<String, String> map = new HashMap<>();
         while(startTime.before(endTime)){
             double totlatemp = 0.0;
             double avreange = 0.0;
             System.out.println("开始时间"+startTime+"----"+"结束时间"+endTime);
             int a =0;
             for (EartagDataEntity data : entityList) {
                 //这个操作占用时间太多
                 if(data.getTime().after(startTime)&data.getTime().before(DateUtilsPublic.addDateHour(startTime,1))){
                     a++;
                     totlatemp += data.getEarTemp();
                     System.out.println(data.getEarTemp());
                 }
             }

             System.out.println("结果"+totlatemp+a+totlatemp/a);
             //直接三元
             if(totlatemp/a>0.01){
                 avreange  =  totlatemp/a;
             }
             //这个时间有点麻烦，需要对应进去
               // map.put(format.format(startTime),df.format(avreange));
                 //时间变为年+天+小时
                map.put(getMonthDayHour(startTime),df.format(avreange));

             startTime =DateUtilsPublic.addDateHour(startTime,1);
           }

       return  map;
    }
}
















