package com.jpsoft.shinestar.mq.listener;

import com.jpsoft.shinestar.modules.base.entity.CompanyInfo;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.service.CompanyInfoService;
import com.jpsoft.shinestar.modules.base.service.PersonInfoService;
import com.jpsoft.shinestar.modules.business.entity.GuestInfoRecord;
import com.jpsoft.shinestar.modules.business.service.GuestInfoRecordService;
import com.jpsoft.shinestar.modules.common.utils.StringUtils;
import com.jpsoft.shinestar.modules.uniview.dto.PersonNotification;
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;

@Slf4j
@Component
@RabbitListener(queues = "personGuestRecordQueue",concurrency = "10")
public class PersonGuestRecordListener {
    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private GuestInfoRecordService guestInfoRecordService;

    @Autowired
    private CompanyInfoService companyInfoService;

    @RabbitHandler
    public void process(PersonNotification notification) {
        log.warn("收到身份识别通知：" + notification.getPersonId());

        try {
            PersonInfo personInfo = personInfoService.get(notification.getPersonId());

            if(personInfo!=null) {

                CompanyInfo companyInfo = companyInfoService.get(personInfo.getCompanyId());

                //如果为访客就更新访客记录表的进厂时间
                if(companyInfo.getName().equals("访客公司")) {

                    GuestInfoRecord guestInfoRecord = guestInfoRecordService.findByPersonId(notification.getPersonId());

                    if (guestInfoRecord != null) {
                        guestInfoRecord.setComeInTime(notification.getRecordTime());
                        guestInfoRecord.setUpdateBy(notification.getPersonId().toString());
                        guestInfoRecord.setUpdateTime(notification.getRecordTime());
                        guestInfoRecord.setTemperature(notification.getTemperature());
                        guestInfoRecordService.update(guestInfoRecord);
                    }
                }
            }
        }
        catch (Exception ex){
            log.error(ex.getMessage(),ex);
        }
    }
}
