package com.jpsoft.shinestar.mq.listener;

import com.alibaba.fastjson.JSONObject;
import com.jpsoft.shinestar.modules.base.dto.PersonDeviceRelationDTO;
import com.jpsoft.shinestar.modules.base.entity.DeviceInfo;
import com.jpsoft.shinestar.modules.base.entity.ExecuteFailLog;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.service.DeviceInfoService;
import com.jpsoft.shinestar.modules.base.service.ExecuteFailLogService;
import com.jpsoft.shinestar.modules.base.service.PersonInfoService;
import com.jpsoft.shinestar.modules.pacs.dto.PacsResult;
import com.jpsoft.shinestar.modules.pacs.service.IGuardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * concurrency 表示并发数
 */
@Slf4j
@Component
@RabbitListener(queues = "importImageQueue",concurrency = "2")
public class ImportImageListener {
    @Autowired
    private IGuardService guardService;

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private ExecuteFailLogService executeFailLogService;

    @Autowired
    private DeviceInfoService deviceInfoService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RabbitHandler
    public void process(Long personId) {
        log.warn("收到用户编号:" + personId);

        try {
            List<PacsResult> msgResultList = guardService.addPerson(personId);

            StringBuilder sb = new StringBuilder();

            boolean success = true;

            for (int i = 0; i < msgResultList.size(); i++) {
                PacsResult pacsResult = msgResultList.get(i);

                sb.append(pacsResult.getMsg());

                if (i != msgResultList.size() - 1) {
                    sb.append(",");
                } else {
                    sb.append("。");
                }

                success &= pacsResult.isSuccess();
            }

            PersonInfo personInfo = personInfoService.get(personId);

            personInfo.setFaceBound(success);
            personInfo.setUpdateTime(new Date());

            personInfoService.update(personInfo);

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


    @RabbitHandler
    public void process(PersonDeviceRelationDTO dto) {
        log.warn("收到用户编号:{},设备编号:{}",dto.getPersonId(), dto.getDeviceId());

        try {
//            PacsResult pacsResult = guardService.addPersonToDevice(dto.getPersonId(), dto.getDeviceId());
//            log.warn("同步结果:{},{}",pacsResult.isSuccess(),pacsResult.getMsg());
            DeviceInfo deviceInfo = deviceInfoService.get(dto.getDeviceId());

            if (deviceInfo==null){
                log.error(dto.getDeviceId() + "设备不存在!");
            }
            else {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("personId", dto.getPersonId());
                jsonObject.put("deviceId", dto.getDeviceId());

                //如果不想再走队列，可以直接调用DeviceMessageListener.handleMessage
                rabbitTemplate.convertAndSend("deviceExchange", "device_" + deviceInfo.getDeviceNo(),jsonObject);
            }
        }
        catch (Exception ex){
            log.error(ex.getMessage(),ex);
        }
    }

    @RabbitHandler
    public void process(ExecuteFailLog efl) {
        log.warn("收到用户编号:{},设备编号:{}",efl.getPersonId(), efl.getDeviceId());

        try {
            DeviceInfo deviceInfo = deviceInfoService.get(efl.getDeviceId());

            if (deviceInfo.getIsOnline()==null || !deviceInfo.getIsOnline()) {
                log.error(deviceInfo.getDeviceNo() + "已离线!");
                return;
            }

            PacsResult pacsResult = guardService.addPersonToDevice(efl.getPersonId(), efl.getDeviceId());

            log.warn("同步结果:{},{}", pacsResult.isSuccess(), pacsResult.getMsg());

//            if (pacsResult.isSuccess()) {
                //如何失败会再添加一条记录，所以当前记录设置为已执行
                efl.setExecuteSuccess(true);
                efl.setExecuteTime(new Date());
                executeFailLogService.update(efl);
//            } else {
//                efl.setExecuteSuccess(false);
//                efl.setExecuteTime(new Date());
//                executeFailLogService.update(efl);
//            }
        }
        catch (Exception ex){
            log.error(ex.getMessage(),ex);
        }
    }
}
