package com.jpsoft.shinestar.mq.listener;


import cn.hutool.core.date.DateUtil;
import com.jpsoft.shinestar.modules.base.entity.PersonHealthLedger;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.service.CompanySuperviseService;
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.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * @author 墨鱼_mo
 * @date 2021-1-22 10:38
 */
@Slf4j
@Component
@RabbitListener(queues = "companySuperviseQueue", concurrency = "1")
public class CompanySuperviseListener {

    @Autowired
    private CompanySuperviseService companySuperviseService;

    @RabbitHandler
    public void process(Map map) {

       try{

           try{

               Date t1 = DateUtil.parse("00:00:00");
               Date t2 = DateUtil.parse("00:01:00");
               Long a1 = t1.getTime();
               Long a2 = t2.getTime();
               Long a3 = new Date().getTime();
               if (a3>a1 && a3<a2){
                   Thread.sleep(a2-a3);
               }




           }catch(InterruptedException ie){
               ie.printStackTrace();
           }

           String deviceNo = null;
           PersonInfo personInfo = null;
           Date recordDate = null;
           BigDecimal temperature = BigDecimal.ZERO;
           String address = null;
           PersonHealthLedger personHealthLedger = null;
           Object a = map.get("deviceNo");
           Object b = map.get("personInfo");
           Object c = map.get("recordDate");
           Object d = map.get("temperature");
           Object e = map.get("address");
           Object f = map.get("personHealthLedger");
           if (a != null) {
               deviceNo = (String)a;
           }
           if (b != null){
               personInfo = (PersonInfo)b;
           }
           if (c != null){
               recordDate = (Date) c;
           }
           if (d != null){
               temperature = (BigDecimal) d;
           }
           if (e != null){
               address =(String)e;
           }
           if (f != null){
               personHealthLedger = (PersonHealthLedger)f;
           }

           companySuperviseService.insertCompanySupervise(deviceNo,personInfo,recordDate,temperature,address,personHealthLedger);
       }catch (Exception ex){
           log.error(ex.getMessage(),ex);
       }

    }
}
