package com.huimv.acquisition.socket.eartagrecive.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huimv.acquisition.entity.*;
import com.huimv.acquisition.mapper.CollectorRegisterDao;
import com.huimv.acquisition.mapper.EartagDataDao;
import com.huimv.acquisition.mapper.EartagRegisterDao;
import com.huimv.acquisition.mapper.RawDataDao;
import com.huimv.acquisition.service.*;
import com.huimv.acquisition.socket.eartagrecive.constant.ImportanteConfig;
import com.huimv.acquisition.socket.eartagrecive.util.DateUtil;
import com.huimv.acquisition.socket.eartagrecive.util.DateUtilsPublic;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;

;

/**
 * @Project : huimv.ql
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@Slf4j
@Component
public class ReceivePackageSerivce {

    //耳标数据查询
    @Autowired
    private EartagDataService eartagDataService;
    @Autowired
    private CollectorRegisterDao collectorRegisterDao  ;
    @Autowired
    private EartagDataDao eartagDataDao  ;

    @Autowired
    private MovePigpenService movePigpenService;
    @Autowired
    private EartagRegisterDao eartagRegisterDao ;
    @Autowired
    private EartagRegisterService eartagRegisterService ;

    @Autowired
    private DeviceTempService deviceTempService;

    @Autowired
    private RawDataService rawDataService  ;
    @Autowired
    private HeartBeatRecordService heartBeatRecordService ;


    public String handleEartagData(String[] clientMessageArray ,String rawdata) throws ParseException {
        //这一块应该封装起来 ，而不是直接展示
        //zj+202007239999895+133070303805451+86+0+2062+0+69+20211110104442+1.0#1.0#11567+0+end
        String head = clientMessageArray[0];               //命令头
        String device_code = clientMessageArray[1];        //采集器编码
        String eartag_no  = clientMessageArray[2];         //耳标号
        String bat = clientMessageArray[3];                //电池电量
        String useless_field = clientMessageArray[4];      //目前来说是无用字段
        String env_temp = clientMessageArray[5];           //环境温度
        int exercise = Integer.parseInt(clientMessageArray[6]);   //运动量
        String rssi  = clientMessageArray[7];             //信号强度
        String time = clientMessageArray[8];              //采集时间
        Date sendEartagTime = DateUtilsPublic.parseDateTime(DateUtilsPublic.buildDateTimeString(time));
        String prepare_field= clientMessageArray[9];      //复合字段 1.0#1.0#11567  --耳标版本--网关版本--电阻值（用来计算环境温度）
        String[] split = prepare_field.split("#");  //1.0#1.0#9314
        String check = clientMessageArray[10];            //校验码
        String tail  = clientMessageArray[11];            //命令尾
        //存放数据
        EartagDataEntity eartagDataEntity = new EartagDataEntity();
        eartagDataEntity.setHead(head);   //命令头
        eartagDataEntity.setDeviceCode(device_code); //采集器ID
        eartagDataEntity.setVersion(split[0]);  //耳标版本
        eartagDataEntity.setCommand(split[1]);  //网关版本
        eartagDataEntity.setEartagNo(eartag_no);  //耳标号
        eartagDataEntity.setBat(Integer.parseInt(bat));   //电池 电量
        DecimalFormat df = new DecimalFormat("####0.00");


        eartagDataEntity.setEarTemp(Double.parseDouble( df.format( ln(Integer.parseInt(split[2]))))) ;  //ntc转耳标温度-----
        eartagDataEntity.setEnvTemp(  Double.parseDouble(new DecimalFormat("#,##0.00").format(( Double.parseDouble(env_temp)/100))));   //环境温度--转doubbo
        System.out.println(Double.parseDouble(env_temp)/100);

        eartagDataEntity.setExercise(exercise);//运动量
        eartagDataEntity.setRssi(Integer.parseInt(rssi));//信号强度
        eartagDataEntity.setTime(sendEartagTime); //采集时间
        eartagDataEntity.setPrepareField(prepare_field); //预留字段 4
        eartagDataEntity.setChk(check); //校验码
        eartagDataEntity.setTail(tail);//命令尾

        //栋舍这个字段在注册表中没有登记，在
        LambdaQueryWrapper<CollectorRegisterEntity> wrapper1 = Wrappers.lambdaQuery();
        wrapper1.eq(CollectorRegisterEntity::getDeviceCode, device_code );
        CollectorRegisterEntity collectorRegisterEntity = collectorRegisterDao.selectOne(wrapper1);

        System.out.println("设备编码 "+device_code);
        if (collectorRegisterEntity==null){//设备没注册就不让他存，基础信息都没有
            System.out.println("设备登记表中没有此设备信息，请先注册");
            return  null;
        }

        LambdaQueryWrapper<EartagDataEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EartagDataEntity::getEartagNo, eartag_no ).orderByDesc( EartagDataEntity::getId ).last( "limit 1");
        EartagDataEntity lastEarTagDate = eartagDataDao.selectOne(wrapper);

        if (lastEarTagDate==null){//设备没注册就不让他存，基础信息都没有
            System.out.println("第一次上传————>存储转舍表转舍信息--存储耳标注册表（猪只基础信息表）");
            //存储转舍信息
            MovePigpenEntity movePigpenEntity = new MovePigpenEntity();
            movePigpenEntity.setDeviceCode(device_code);    //存储一个零
            movePigpenEntity.setEartagNo(eartag_no);
            movePigpenEntity.setMoveDate(DateUtilsPublic.parseDateTime(DateUtilsPublic.buildDateTimeString(time)));
            movePigpenEntity.setStage(collectorRegisterEntity.getStage());
            movePigpenEntity.setOrgId(collectorRegisterEntity.getOrgId());
            movePigpenService.save(movePigpenEntity);

            //存储注册信息（现在也是猪的基础信息---需要大量冗余，毕竟基础信息）
            EartagRegisterEntity eartagRegisterEntity = new EartagRegisterEntity();
            eartagRegisterEntity.setAge(0);
            eartagRegisterEntity.setDeviceCode(device_code);//给绑定的设备编码死否会改变
            eartagRegisterEntity.setEartagNo(eartag_no);
            eartagRegisterEntity.setFarmCode(collectorRegisterEntity.getFarmCode());//定时任务获取到机器ID后处理
            eartagRegisterEntity.setPigpen( String.valueOf(collectorRegisterEntity.getPigpenId()));
            eartagRegisterEntity.setRegisterDate(new Date());
            eartagRegisterEntity.setOrgId( collectorRegisterEntity.getOrgId());
            eartagRegisterEntity.setStage(collectorRegisterEntity.getStage());
            eartagRegisterEntity.setStage(collectorRegisterEntity.getUnitId());

            eartagRegisterService.save(eartagRegisterEntity);//存储一条还没有处理的数据
            //存储这一条数据
            eartagDataEntity.setFarmCode(collectorRegisterEntity.getFarmCode());
            eartagDataEntity.setStage(collectorRegisterEntity.getStage());
            eartagDataEntity.setPigpen((String.valueOf( collectorRegisterEntity.getPigpenId())));
            eartagDataEntity.setSportGap(0);//运动量
            eartagDataEntity.setOrgId(collectorRegisterEntity.getOrgId());
            eartagDataEntity.setUnitId(collectorRegisterEntity.getUnitId());
            System.out.println("存储成功------");
            eartagDataService.save(eartagDataEntity);

            //存储原始数据
            RawDataEntity rawDataEntity = new RawDataEntity();
            rawDataEntity.setDetailData(rawdata);
            rawDataEntity.setStatus(0);
            rawDataEntity.setCreateTime(new Date());
            rawDataService.save(rawDataEntity);

            //罗工要求返回空数据
            return null;
        }

        //如果第一条不为空，通过计算时间间隔来判断是否存储
        long DateGap =sendEartagTime.getTime() - lastEarTagDate.getTime().getTime();
        if( DateGap < ImportanteConfig.EARTAG_SEND_GAP_TIME){
            //更改存储形式，通过计算间隔时间来判断数据的存储
            System.out.println("间隔时间过短，抛弃");
            return  null;
        }

        eartagDataEntity.setFarmCode(collectorRegisterEntity.getFarmCode());
        eartagDataEntity.setStage(collectorRegisterEntity.getStage());
        eartagDataEntity.setPigpen((String.valueOf( collectorRegisterEntity.getPigpenId())));
        eartagDataEntity.setUnitId(collectorRegisterEntity.getUnitId());
        eartagDataEntity.setSportGap(0);
        eartagDataEntity.setOrgId(collectorRegisterEntity.getOrgId());

        eartagDataService.save(eartagDataEntity);
        System.out.println("存储成功-----");

        //存储原始数据
        RawDataEntity rawDataEntity = new RawDataEntity();
        rawDataEntity.setDetailData(rawdata);
        rawDataEntity.setStatus(0);
        rawDataEntity.setCreateTime(new Date());
        rawDataService.save(rawDataEntity);
        //罗工要求返回空数据
        return null;
    }


    public static void main(String[] args) {
        DecimalFormat df = new DecimalFormat("####0.00");
        System.out.println(df.format(23.00000));
    }

    public static  double getTwoDecimal(double num) {
        DecimalFormat dFormat = new DecimalFormat("#.00");
        String yearString = dFormat.format(num);
        return  Double.parseDouble(yearString);
    }



    public static  double  m1(double num ) {
        BigDecimal bg = new BigDecimal(num);
        return  bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }




    public static String swapHex(String src) {
        StringBuilder target = new StringBuilder();
        for (int i = src.length() - 1; i > 0; i -= 2) {
            target.append(src.substring(i - 1, i + 1));
        }
        return target.toString();
    }
    public static String numberFormater(double d) {
        DecimalFormat nf = new DecimalFormat();
        nf.setMaximumFractionDigits(2);
        return nf.format(d);
    }
    public static double A = 0.00335396319311466;
    public static double B = 0.000256376549039663;
    public static double C = 0.00000248933580817244;
    public static double D = 0.0000000753805242068686;
    public static double E = -0.0000000204758173596178;

    //保留一位小数
    public static double changeDouble(Double dou) {
        NumberFormat nf = new DecimalFormat("0.0 ");
        dou = Double.parseDouble(nf.format(dou));
        return dou;
    }




    //温度转换
    public static double ln(int rv) {
        int R_REF = 10000;
        double ln = Math.log((double) rv / R_REF);
        //保留一位小数
        return changeDouble(1 / (A + B * ln + C * Math.pow(ln, 2) + D * Math.pow(ln, 3) + E * Math.pow(ln, 4)) - 273.15)+0.001;
    }



    public String handleEartagDampness(String[] clientMessageArray) throws ParseException {
        //湿度数据还是不需要处理，返回对应的数值
  /*      String header = clientMessageArray[0];
        String deviceId = clientMessageArray[1];
        String cmd = clientMessageArray[2];
        String serialNo = clientMessageArray[3];
        String value = clientMessageArray[4];
        String timestamp = clientMessageArray[5];
        String checkCode = clientMessageArray[6];
        String tail = clientMessageArray[7];
        EartagDampness eartagDampnessPojo = new EartagDampness();
        eartagDampnessPojo.setHeader(header);
        eartagDampnessPojo.setDeviceId(Integer.parseInt(deviceId));
        eartagDampnessPojo.setCmd(Integer.parseInt(cmd));
        eartagDampnessPojo.setSerialNo(Integer.parseInt(serialNo));
        eartagDampnessPojo.setValue(Double.parseDouble(value));
        eartagDampnessPojo.setCreateDate(DateUtil.parseDateTime(DateUtil.buildDateTimeString(timestamp)));
        eartagDampnessPojo.setCheckNumber(Integer.parseInt(checkCode));
        eartagDampnessPojo.setTail(tail);
        dampnessMapper.insert(eartagDampnessPojo);
//      dampnessService.add(eartagDampnessPojo);
        log.info(Const.SAVE_DAMPNESS_SUCCESS);*/
        return "hm+5+0+8+end";
    }

    /**
     * @Method : _handleEartagTemperature
     * @Description : 处理网关温度
     * @Params : [clientMessageArray]
     * @Return : java.lang.String
     * @Author : ZhuoNing
     * @Date : 2021/1/15
     * @Time : 17:46
     */
    public String handleEartagTemperature(String[] clientMessageArray) throws ParseException {
        //设备温度
        String header = clientMessageArray[0];
        String deviceId = clientMessageArray[1];
        String cmd = clientMessageArray[2];
        String serialNo = clientMessageArray[3];
        String value = clientMessageArray[4];
        String timestamp = clientMessageArray[5];
        String checkCode = clientMessageArray[6];
        String tail = clientMessageArray[7];
        //  hm**0 +158**1  +4**2  +1**3  +289**4  +20170208152300**5  +6**6  +end**7
        LambdaQueryWrapper<CollectorRegisterEntity> wrapper1 = Wrappers.lambdaQuery();
        wrapper1.eq(CollectorRegisterEntity::getDeviceCode, deviceId );
        CollectorRegisterEntity collectorRegisterEntity = collectorRegisterDao.selectOne(wrapper1);
        if (collectorRegisterEntity==null){//设备没注册就不让他存，基础信息都没有
            System.out.println("设备登记表中没有此设备信息，请先注册");
            return "设备登记表中没有此设备信息，请先注册";
        }

        DeviceTempEntity deviceTempEntity = new DeviceTempEntity();
        deviceTempEntity.setHeader(header);
        deviceTempEntity.setDeviceCode(deviceId);
        deviceTempEntity.setCmd(Integer.parseInt(cmd));
        deviceTempEntity.setSerialNo(Integer.parseInt(serialNo));
        deviceTempEntity.setTempValue(Double.parseDouble(value) / 10);//温度。。需要除以100
        deviceTempEntity.setCreateDate(DateUtil.parseDateTime(DateUtil.buildDateTimeString(timestamp)));
        deviceTempEntity.setCheckCode(Integer.parseInt(checkCode));
        deviceTempEntity.setTail(tail);
        deviceTempEntity.setOrgId(collectorRegisterEntity.getOrgId());
        deviceTempService.save(deviceTempEntity);
        // temperatureService.add(EartagTemperaturePojo);


        //hm+202007239999856+2+0+end
        System.out.println("基站编码编码为====>"+deviceId);
        HeartBeatRecordEntity heartBeatRecordEntity = new HeartBeatRecordEntity();
        heartBeatRecordEntity.setCheckNo("0");
        heartBeatRecordEntity.setCmd(3);
        heartBeatRecordEntity.setCountyCode(collectorRegisterEntity.getCountyCode());
        heartBeatRecordEntity.setDeviceCode(deviceId);
        heartBeatRecordEntity.setFarmCode(collectorRegisterEntity.getFarmCode());
        heartBeatRecordEntity.setHead("hm");
        heartBeatRecordEntity.setPigpenId(collectorRegisterEntity.getPigpenId().toString());
        heartBeatRecordEntity.setTail("end");
        heartBeatRecordEntity.setUnitId(collectorRegisterEntity.getUnitId().toString());
        heartBeatRecordEntity.setCreateTime(new Date());
        heartBeatRecordService.save(heartBeatRecordEntity);







        return "hm+4+0+7+end";
    }




 /*
   备份-----数据可能有被需要
   public String handleEartagData(String[] clientMessageArray) throws ParseException {
        log.info("开始添加耳标数据......");
        String  head = clientMessageArray[0];           //命令头
        String device_code = clientMessageArray[1];      //采集器ID
        String eartag_no  = clientMessageArray[2];         //版本号
        String bat = clientMessageArray[3];        //耳标号
        String ear_temp = clientMessageArray[4];  //耳标帧序号
        String env_temp = clientMessageArray[5];
        int exercise = Integer.parseInt(clientMessageArray[6]);         //运动量
        //重启次数
        String rssi  = clientMessageArray[7];           //信号强度
        String  time = clientMessageArray[8];           //采集时间
        String prepare_field= clientMessageArray[9];    //耳标温度

        String[] split = prepare_field.split("#");//1.0#1.0#9314   --耳标版本--网关版本--电阻值（用来计算环境温度）
        String  check = clientMessageArray[10];          //校验码
        String  tail  = clientMessageArray[11];          //命令尾
        EartagDataEntity eartagDataEntity = new EartagDataEntity();

        eartagDataEntity.setHead(head);   //命令头
        eartagDataEntity.setDeviceCode(device_code); //采集器ID
        eartagDataEntity.setVersion(split[0]);  //耳标版本
        eartagDataEntity.setCommand(split[1]);  //网关版本

        eartagDataEntity.setEartagNo(eartag_no);  //耳标号
        // eartagDataEntity.setFrameSerialNo(Integer.parseInt(frame_serial_no));   //耳标帧序号
        eartagDataEntity.setBat(Integer.parseInt(bat));   //电池 电量
        DecimalFormat df = new DecimalFormat("####0.00");
        double setEarTemp11 = Double.parseDouble(df.format(ln(Integer.parseInt(split[2]))));
        eartagDataEntity.setEarTemp(Double.parseDouble( df.format( ln(Integer.parseInt(split[2]))))) ;  //ntc转环境温度
        eartagDataEntity.setEnvTemp(  Double.parseDouble(new DecimalFormat("#,##0.00").format(( Double.parseDouble(env_temp)/100))));   //环境温度--转doubbo
        //System.out.println( ln(Integer.parseInt(prepare_field)));//----25.001
        System.out.println(Double.parseDouble(env_temp)/100);

        eartagDataEntity.setExercise(exercise);//运动量
        //eartagDataEntity.setRebootTimes(Integer.parseInt(reboot_times));  //重启次数
        eartagDataEntity.setRssi(Integer.parseInt(rssi));//信号强度
        eartagDataEntity.setTime( DateUtilsPublic.parseDateTime(DateUtilsPublic.buildDateTimeString(time)) ); //采集时间
        eartagDataEntity.setPrepareField(prepare_field); //预留字段 4
        eartagDataEntity.setChk(check); //校验码
        eartagDataEntity.setTail(tail);//命令尾
        //判断年猪表中是否有这一只耳标的数据   //栋舍这个字段在注册表中没有登记，在
        LambdaQueryWrapper<CollectorRegisterEntity> wrapper1 = Wrappers.lambdaQuery();
        wrapper1.eq(CollectorRegisterEntity::getDeviceCode, device_code );
        CollectorRegisterEntity collectorRegisterEntity = collectorRegisterDao.selectOne(wrapper1);
        if (collectorRegisterEntity==null){//设备没注册就不让他存，基础信息都没有
            System.out.println("设备登记表中没有此设备信息，请先注册");
            return "设备登记表中没有此设备信息，请先注册";
        }

        //查看这个耳标绑定的基站编码
        LambdaQueryWrapper<EartagRegisterEntity> wrapper3 = Wrappers.lambdaQuery();
        wrapper3.eq(EartagRegisterEntity::getEartagNo, eartag_no );
        EartagRegisterEntity eartagRegisterEntity = eartagRegisterDao.selectOne(wrapper3);
        System.out.println( "zhece xinxi "+eartagRegisterEntity+"ooo"+eartag_no+"ooo"+device_code);

        // String deviceCode = eartagRegisterEntity.getDeviceCode();//---获取这个基站的编码
        // System.out.println(deviceCode);

        //wendu cha dayu 8
      *//*  if(Double.parseDouble(env_temp)/100==0){
            //与注册表不同，，不存
            System.out.println("丢弃");
            return "hm+6+0+6+end";
        }*//*
        System.out.println("存储");
        //日龄
        eartagDataEntity.setDayAge2(eartagRegisterEntity.getAge());
        //上传设备发生了变化，      转舍信息进行存储
        //在接受数据的时候加一条记录迁移信息的数据到一个新表中，方便查找
        eartagDataEntity.setStage(collectorRegisterEntity.getStage()); //获取阶段
        eartagDataEntity.setPigpen(String.valueOf(collectorRegisterEntity.getPigpenId())); //获取猪舍ID
        eartagDataEntity.setFarmCode(collectorRegisterEntity.getFarmCode());//获取牧场编码
        //关于两个日龄的处理--不应该是直接增加，应该是判断上一条的“日”和当前是否相同 ，相同不增加，不相同增加


        LambdaQueryWrapper<EartagDataEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EartagDataEntity::getEartagNo, eartag_no ).orderByDesc( EartagDataEntity::getId ).last( "limit 1");
        EartagDataEntity   lastEarTagDate = eartagDataDao.selectOne(wrapper);
        //返回的数据在这里为空--说明这个是第一次上传--需要存储一条device_code 为空的注册表
        if(lastEarTagDate==null){//上一条为空，存转舍
            LambdaQueryWrapper<EartagRegisterEntity> wrapper4 = Wrappers.lambdaQuery();
            wrapper4.eq(EartagRegisterEntity::getEartagNo, eartag_no );
            EartagRegisterEntity eartagRegisterEntity1 = eartagRegisterDao.selectOne(wrapper4);

            MovePigpenEntity movePigpenEntity = new MovePigpenEntity();
            movePigpenEntity.setDeviceCode(device_code);
            movePigpenEntity.setEartagNo(eartag_no);
            movePigpenEntity.setMoveDate(DateUtilsPublic.parseDateTime(DateUtilsPublic.buildDateTimeString(time)));
            movePigpenEntity.setStage(collectorRegisterEntity.getStage());
            movePigpenService.save(movePigpenEntity);
            if(eartagRegisterEntity1==null){
                //说明之前没有注册过--现在注册一下--设备ID的设置留给定时任务
                EartagRegisterEntity eartagRegisterEntity2 = new EartagRegisterEntity();
                eartagRegisterEntity2.setAge(0);
                eartagRegisterEntity2.setDeviceCode(null);
                eartagRegisterEntity2.setEartagNo(eartag_no);
                eartagRegisterEntity2.setFarmCode(null);//定时任务获取到机器ID后处理
                eartagRegisterEntity2.setPigpen( null);
                eartagRegisterEntity2.setRegisterDate(new Date());
                eartagRegisterEntity2.setStage(null);
                eartagRegisterService.save(eartagRegisterEntity2);//存储一条还没有处理的数据
            }

            //第一次上传的数据----注册表肯定没有这只耳标的信息
         *//*  eartagDataEntity.setDayAge (0);  //佩标日龄----默认值，第一次有数据上传就是默认值
           eartagDataEntity.setDayAge2(50); //日龄----默认第50天佩戴耳标*//*
            eartagDataEntity.setExercise(exercise);
            eartagDataEntity.setSportGap(2);
            eartagDataService.save(eartagDataEntity);
            return "hm+6+0+6+end";
        }

        if(!eartagRegisterEntity.getDeviceCode().equals(device_code) ){
            //与注册表不同，，不存
            System.out.println("丢弃");
            return "hm+6+0+6+end";

        }


        if( Double.parseDouble(new DecimalFormat("#,##0.00").format(( Double.parseDouble(env_temp)/100))) <1.0){
            //与注册表不同，，不存
            System.out.println("丢弃环境温度为0");
            return "hm+6+0+6+end";
        }


        if( device_code.equals("112007239999996")){
            //这个基站的数据不要了
            System.out.println("丢弃环境温度为0");
            return "hm+6+0+6+end";
        }

//202007239999123

        //上一条不为空---而且判断是存储注册表device-code
        //存储转舍信息--还要判断新的设备阶段和老的设备阶段是否相同，如果相同就不存储转舍信息
        if(!lastEarTagDate.getDeviceCode().equals(device_code)){
           *//* LambdaQueryWrapper<EartagRegisterEntity> wrapper4 = Wrappers.lambdaQuery();
            wrapper4.eq(EartagRegisterEntity::getEartagNo, eartag_no );
            EartagRegisterEntity eartagRegisterEntity1 = eartagRegisterDao.selectOne(wrapper4);//查到注册信息--有可能为空的
            String deviceCode = eartagRegisterEntity1.getDeviceCode();
            if ( StringUtilsWork.isNotBlank(deviceCode) ) {//如果耳标注册表的绑定机器ID为空就不存储转舍信息
                System.out.println("之前=" + lastEarTagDate.getDeviceCode() + "现在" + device_code);
                System.out.println(lastEarTagDate.getDeviceCode().equals(device_code));
                 //上一阶段的
                if(!lastEarTagDate.getStage().equals(collectorRegisterEntity.getStage())) {  //不相等，---判断阶段是否相同--阶段也不相同--增加转舍信息

                    MovePigpenEntity movePigpenEntity = new MovePigpenEntity();
                    movePigpenEntity.setDeviceCode(device_code);
                    movePigpenEntity.setEartagNo(eartag_no);
                    movePigpenEntity.setMoveDate(DateUtilsPublic.parseDateTime(DateUtilsPublic.buildDateTimeString(time)));
                    movePigpenEntity.setStage(collectorRegisterEntity.getStage());
                    movePigpenService.save(movePigpenEntity);
                }
            }*//*
        }
        Integer exercise1 = lastEarTagDate.getExercise();
        eartagDataEntity.setSportGap(exercise - exercise1);//存储区间运动量
        //获取当前时间的 “天”---判断天的情况
        int dayNow= Integer.parseInt(String.format("%td", DateUtilsPublic.parseDateTime(DateUtilsPublic.buildDateTimeString(time))));   //当前上传数据的天
        int dayLastData= Integer.parseInt(String.format("%td",lastEarTagDate.getTime()));     //当前的天
        System.out.println("当天时间="+dayNow+"----上一条时间"+dayLastData);


        if( dayNow == dayLastData){
            //若相等，沿用上一条的日龄和配标日龄
            eartagDataEntity.setDayAge (lastEarTagDate.getDayAge());  //佩标日龄
            eartagDataEntity.setDayAge2(lastEarTagDate.getDayAge2()); //日龄
        }else {
            //有数据，若 “日” 不相等是否应该增加一天
            eartagDataEntity.setDayAge (lastEarTagDate.getDayAge()+1 );  //佩标日龄
            eartagDataEntity.setDayAge2(lastEarTagDate.getDayAge2()+1 ); //日龄
        }
        //设置最后一次上传时间
        collectorRegisterEntity.setLastTime(new Date());
        collectorRegisterService.updateById(collectorRegisterEntity);

        eartagDataService.save(eartagDataEntity);
        return "hm+6+0+6+end";
    }*/










}
