package com.huimv.modular.timer.tasks;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huimv.modular.collectorregister.entity.CollectorRegister;
import com.huimv.modular.collectorregister.mapper.CollectorRegisterMapper;
import com.huimv.modular.eartagdata.entity.EartagData;
import com.huimv.modular.eartagdata.mapper.EartagDataMapper;
import com.huimv.modular.eartagdata.service.EartagDataService;
import com.huimv.modular.eartagregister.entity.EartagRegister;
import com.huimv.modular.eartagregister.mapper.EartagRegisterMapper;
import com.huimv.modular.eartagregister.service.EartagRegisterService;
import com.huimv.modular.heartbeatrecord.entity.HeartBeatRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.tags.form.ErrorsTag;

import java.util.Date;
import java.util.List;


@Component
public class UpdateEartagRegirterDetailsInfo {


    @Autowired
    private EartagRegisterMapper eartagRegisterMapper;


    @Autowired
    private EartagRegisterService eartagRegisterService;


    @Autowired
    private EartagDataMapper eartagDataMapper;


    @Autowired
    private CollectorRegisterMapper collectorRegisterMapper;

    //基站手动添加之后   信息并没有补充完整  这里补充完整  补充完整基站信息
    @Scheduled(cron = "0/50 * * * * ?")
    public void updateInfo() {




/*
       for (int i = 21; i <= 30; i++) {

            for (int j = 1; j <= 100; j++) {
                EartagRegister eartagRegister1 = new EartagRegister();
                eartagRegister1.setLocation(i + "--" + j);
                eartagRegister1.setEartagNo("13308312361");
                eartagRegister1.setOrgId(123456124L);
                eartagRegister1.setRegisterDate(new Date());
                eartagRegisterService.save(eartagRegister1);
                System.out.println("存储" + i + "--" + j);
            }
        }*/


        LambdaQueryWrapper<EartagRegister> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EartagRegister::getDeviceCode, null);
        List<EartagRegister> list = eartagRegisterMapper.selectList(wrapper);

        for (EartagRegister eartagRegister : list) {
            //找最新上传的一条信息
            LambdaQueryWrapper<EartagData> wrapper1 = Wrappers.lambdaQuery();
            wrapper1.eq(EartagData::getEartagNo, eartagRegister.getEartagNo()).orderByDesc(EartagData::getId).last("limit 1");
            EartagData eartagData = eartagDataMapper.selectOne(wrapper1);


            if (eartagData != null) {   //如果有数据上传了
                LambdaQueryWrapper<CollectorRegister> wrapper12 = Wrappers.lambdaQuery();
                wrapper12.eq(CollectorRegister::getDeviceCode, eartagData.getDeviceCode());
                CollectorRegister collectorRegister = collectorRegisterMapper.selectOne(wrapper12);


                eartagRegister.setDeviceCode(collectorRegister.getDeviceCode());
                eartagRegister.setPigpen((String.valueOf(collectorRegister.getPigpenId())));
                eartagRegister.setFarmCode(collectorRegister.getFarmCode());
                eartagRegister.setStage(collectorRegister.getStage());

                eartagRegisterService.updateById(eartagRegister); //更新基站信息


            }


        }


    }

    public static void main(String[] args) {
        String ss  = "1--56";
        String[] as = ss.split("--");
        System.out.println(as[0]);
        System.out.println(as[1]);
    }


}

