package com.huimv.eartag.listener;

import com.alibaba.fastjson.JSONObject;
import com.huimv.eartag.service.IDeviceService;
import com.huimv.eartag.utils.Const;
import com.huimv.eartag.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Map;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@Component
@Slf4j
public class ManageListener {
    @Autowired
    private IDeviceService deviceService;
    /**
     * @Method      : processSyncDeviceRegister
     * @Description : 监听处理同步设备注册信息队列
     * @Params      : [map]
     * @Return      : void
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/22
     * @Time        : 16:03
     */
    @RabbitListener(queues = Const.QUEUE_SYNC_DEVICE_REGISTER)
    @RabbitHandler
    public void processSyncDeviceRegister(Map map) throws ParseException {
        System.out.println("device queue>>");
        JSONObject deviceRegisterJo = JSONObject.parseObject(map.get("deviceRegister").toString()) ;
        System.out.println("deviceRegisterJo>>"+deviceRegisterJo.toJSONString());
        DateUtil dateUtil = new DateUtil();
        String todayDateText = dateUtil.getTodayDateText();

//        System.out.println("同步设备注册信息>>"+deviceRegisterJo);
        //{计算有效设备总数并存储到缓存}
        deviceService.countEffectiveDeviceTotal(deviceRegisterJo);
        System.out.println("4.");

        //{计算设备和耳标总状态}
        deviceService.countFarmAllStatus(deviceRegisterJo);
        System.out.println("5.");

        //{}
        deviceService.unCancelDeviceOnlineLiveStatus(deviceRegisterJo,todayDateText);
        System.out.println("6.");
    }

    /**
     * @Method      : processSyncEartagRegister
     * @Description : 监听处理-同步耳标数据
     * @Params      : [map]
     * @Return      : void
     * 
     * @Author      : ZhuoNing
     * @Date        : 2022/3/25       
     * @Time        : 10:14
     */
//    @RabbitListener(queues = Const.QUEUE_SYNC_EARTAG_REGISTER)
//    @RabbitHandler
//    public void processSyncEartagRegister(Map map) throws ParseException {
//        System.out.println("eartag queue>>");
//        JSONObject deviceRegisterJo = JSONObject.parseObject(map.get("eartagRegister").toString()) ;
//        System.out.println("deviceRegisterJo>>"+deviceRegisterJo.toJSONString());
//
////        System.out.println("同步设备注册信息>>"+deviceRegisterJo);
//        //{计算有效耳标总数并存储到缓存}
////        deviceService.countEffectiveDeviceTotal(deviceRegisterJo);
//        System.out.println("4.");
//
//        //{计算设备和耳标总状态}
////        deviceService.countFarmAllStatus(deviceRegisterJo);
//        System.out.println("5.");
//    }


    /**
     * @Method      : processSyncDeviceTotal
     * @Description : 同步设备总数
     * @Params      : [map]
     * @Return      : void
     * 
     * @Author      : ZhuoNing
     * @Date        : 2022/4/1       
     * @Time        : 11:34
     */
    @RabbitListener(queues = Const.QUEUE_SYNC_DEVICE_TOTAL)
    @RabbitHandler
    public void processSyncDeviceTotal(Map map) throws ParseException {
//        System.out.println("device_total queue>>"+map.toString());
        DateUtil dateUtil = new DateUtil();
        String todayDateText = dateUtil.getTodayDateText();
        //{重新计算有效设备总数并存储到缓存}
        deviceService.countEffectiveDeviceTotal(map);

        //{更新总状态}
        deviceService.cancelFarmAllStatus(map);

        //{}
        deviceService.cancelDeviceOnlineLiveStatus(map,todayDateText);

        System.out.println("重新计算有效设备总数并存储到缓存完成.");
    }
}
