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.CollectorRegisterEntity;
import com.huimv.acquisition.entity.HeartBeatRecordEntity;
import com.huimv.acquisition.mapper.CollectorRegisterDao;
import com.huimv.acquisition.mapper.HeartBeatRecordDao;
import com.huimv.acquisition.service.HeartBeatRecordService;
import com.huimv.acquisition.socket.eartagrecive.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@Component
public class SendPackageService {
    @Autowired
    private CollectorRegisterDao collectorRegisterDao;

    @Autowired
    private HeartBeatRecordService heartBeatRecordService;


    /**
     * @Method      : _handleGetDeviceId
     * @Description : 获取设备id
     * @Params      : [clientMessageArray]
     * @Return      : java.lang.String
     * @Author      : ZhuoNing
     * @Date        : 2021/1/15
     * @Time        : 17:41
     */
    public String getDeviceId(String[] clientMessageArray) {
        String serialNo = clientMessageArray[1];
        System.out.println("serialNo>>"+serialNo);
        // 查询机器id 没有查询了，取消
        // String jqid = jqidService.findDeviceIdByJQcode(serialNo);
        // hm+202007239999123+4+1+243+20210729161101+0+end
        LambdaQueryWrapper<CollectorRegisterEntity> wrapper1 = Wrappers.lambdaQuery();
        wrapper1.eq(CollectorRegisterEntity::getRegisteCode, serialNo );
        CollectorRegisterEntity collectorRegisterEntity = collectorRegisterDao.selectOne(wrapper1);
        System.out.println("芯片编码为====>"+serialNo);
        //520---站点ID--现在随便写
         if(collectorRegisterEntity==null){
             //注册失败的标志
             return    "hm+1+1+158+123+8+end";
         }
        return "hm+1+0+"+collectorRegisterEntity.getDeviceCode()+"+520+8+end";
    }


    //0号命令  请求机器的IP
    public String getDeviceIp(String[] clientMessageArray) {
        //盒子的芯片编码为
        String serialNo = clientMessageArray[1];
        System.out.println("芯片编码为====>"+serialNo);
        LambdaQueryWrapper<CollectorRegisterEntity> wrapper1 = Wrappers.lambdaQuery();
        wrapper1.eq(CollectorRegisterEntity::getRegisteCode, serialNo );
        CollectorRegisterEntity collectorRegisterEntity = collectorRegisterDao.selectOne(wrapper1);
        //520---站点ID--现在随便写
        if(collectorRegisterEntity==null) {
            //注册失败的标志
            return "hm+1+1+158+123+8+end";
        }
        //返回IP和端口
        System.out.println("返回IP成功");
        return "hm+0+0+"+collectorRegisterEntity.getServerIp()+"+"+collectorRegisterEntity.getPort()+"+8+end";
    }



    /**
     * @Method      : _handleGetServerDateTime
     * @Description : 获取获取服务器日期时间
     * @Params      : [clientMessageArray]
     * @Return      : java.lang.Strin
     * @Author      : ZhuoNing
     * @Date        : 2021/1/15
     * @Time        : 17:38
     */
    public String getServerDateTime(String[] clientMessageArray) {
        //hm+202007239999856+2+0+end
        String device_code = clientMessageArray[1];
        System.out.println("基站编码编码为====>"+device_code);
        LambdaQueryWrapper<CollectorRegisterEntity> wrapper1 = Wrappers.lambdaQuery();
        wrapper1.eq(CollectorRegisterEntity::getDeviceCode, device_code );
        CollectorRegisterEntity collectorRegisterEntity = collectorRegisterDao.selectOne(wrapper1);
        HeartBeatRecordEntity heartBeatRecordEntity = new HeartBeatRecordEntity();
        heartBeatRecordEntity.setCheckNo("0");
        heartBeatRecordEntity.setCmd(3);
        heartBeatRecordEntity.setCountyCode(collectorRegisterEntity.getCountyCode());
        heartBeatRecordEntity.setDeviceCode(device_code);
        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);


        String formatNow = DateUtil.getNowWithDigital();



       // hm+2+20170203094133+4+end
        return "hm+2+"+formatNow+"+4+end";
    }




  /*   * @Method      : _handleGetHeartbeatBag
     * @Description : 获取心跳包
     * @Params      : [clientMessageArray]
     * @Return      : java.lang.String
     *
     * @Author      : ZhuoNing
     * @Date        : 2021/1/15
     * @Time        : 17:38
     */
    public String getHeartbeatBag(String[] clientMessageArray) {







        return "hm+3+0+6+end";
    }

}
