package com.jpsoft.shinestar.mq.listener;

import com.jpsoft.shinestar.modules.base.entity.PersonDeviceRelation;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.service.PersonDeviceRelationService;
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.WechatMessageUtil;
import com.jpsoft.shinestar.modules.pacs.dto.PacsMsgResult;
import com.jpsoft.shinestar.modules.pacs.service.IGuardService;
import com.jpsoft.shinestar.modules.sys.entity.SysLog;
import com.jpsoft.shinestar.modules.sys.service.SysLogService;
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.util.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
@Component
@RabbitListener(queues = "removePersonGuestQueue",concurrency = "10")
public class RemovePersonGuestListener {

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private GuestInfoRecordService guestInfoRecordService;

    @Autowired
    private IGuardService guardService;

    @Autowired
    private PersonDeviceRelationService personDeviceRelationService;

    @Autowired
    private SysLogService sysLogService;

    @RabbitHandler
    public void process(String id) {
        log.warn("收到访客访问记录：" + id);

        try {

            GuestInfoRecord guestInfoRecord = guestInfoRecordService.get(id);

            int affectCount = 0;

            Date now = new Date();

            if(guestInfoRecord.getPersonId()!=null) {
                PersonInfo personInfo = personInfoService.get(guestInfoRecord.getPersonId());
                personInfo.setDelFlag(true);
                personInfo.setUpdateBy("-1");
                personInfo.setUpdateTime(now);

                affectCount = personInfoService.update(personInfo);

                if(affectCount>0){
                    //删除访客和设备关系
                    List<PersonDeviceRelation> personDeviceRelationList = personDeviceRelationService.findByPersonId(personInfo.getId());

                    for (PersonDeviceRelation personDeviceRelation:personDeviceRelationList) {
                        personDeviceRelation.setDelFlag(true);
                        personDeviceRelation.setUpdateBy("-1");
                        personDeviceRelation.setUpdateTime(now);
                        personDeviceRelationService.update(personDeviceRelation);
                    }

                    //删除设备人脸信息
                    List<PacsMsgResult> pacsMsgResultList = guardService.deletePerson(personInfo.getId());

                    for (PacsMsgResult pacsMsgResult:pacsMsgResultList) {
                        if(pacsMsgResult.isSuccess()){
                            log.warn("删除设备上访客人脸信息成功！" + "访客ID："+pacsMsgResult.getPersonName()+";设备:"+ pacsMsgResult.getPersonName());
                        }
                        else{
                            log.warn("删除设备上访客人脸信息失败！" + "访客ID："+pacsMsgResult.getPersonName()+";设备:"+ pacsMsgResult.getPersonName());
                        }
                    }

                }
            }
        }
        catch (Exception ex){
            log.error(ex.getMessage(),ex);
        }
    }

}
