package com.persagy.dmp.rwd.handler;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.dmp.amqp.handler.AbstractRabbitHandlerTemplate;
import com.persagy.dmp.basic.constant.DigitalMessageConstant;
import com.persagy.dmp.basic.model.DigitalManageMessage;
import com.persagy.dmp.digital.entity.ObjectDigital;
import com.persagy.dmp.digital.entity.ObjectRelation;
import com.persagy.dmp.rwd.qrcode.service.impl.QrCodeServiceImpl;
import com.persagy.dmp.rwd.version.service.IObjectDigitalHistoryService;
import com.persagy.dmp.rwd.version.service.IObjectRelationHistoryService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 物理世界对象消息处理
 * @author:linhuili
 * @date:2021/11/23
 */
@Slf4j
@Component
@RabbitListener(queues = DigitalMessageConstant.MESSAGE_QUEUE)
public class DigitalMessageHandler extends AbstractRabbitHandlerTemplate {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private QrCodeServiceImpl qrCodeService;

    @Autowired
    private IObjectDigitalHistoryService objectDigitalHisService;

    @Autowired
    private IObjectRelationHistoryService objectRelationHisService;

    /**
     * 物理世界对象：消息监听处理
     * @param message 消息体字符串
     * @param channel 通道对象
     * @param vo     消息对象
     */
    @Override
    protected void handler(String message, Channel channel, Message vo) {
        try {
            log.info("接收对象消息：{}", message);
            //接收对象消息
            DigitalManageMessage messageVO = objectMapper.readValue(message, DigitalManageMessage.class);
            ObjectNode objectDigital = messageVO.getNewObj();
            if(objectDigital == null || StrUtil.isEmpty(messageVO.getOperatorType()) || StrUtil.isEmpty(messageVO.getOperatorObj())){
                log.info("对象消息格式有误：{}"+ messageVO);
                return;
            }
            if(ObjectDigital.class.getSimpleName().equals(messageVO.getOperatorObj())){
               //处理物理世界对象数据
                handleDigitalObjectMessage(messageVO,objectDigital);
            }
            if(ObjectRelation.class.getSimpleName().equals(messageVO.getOperatorObj())){
                //处理物理世界关系数据
                handlerDigitalRelationMessage(messageVO,objectDigital);
            }
        }catch (Exception e){
          log.info("物理世界对象消息处理失败：{} "+e.getMessage());
        }
    }

    /**
     * 处理物理世界对象消息
     * @param messageVO
     * @param objectDigital
     * @throws Exception
     */
    private void handleDigitalObjectMessage(DigitalManageMessage messageVO, ObjectNode objectDigital) throws Exception{
        ObjectDigital objectDigitalVO = objectMapper.readValue(objectDigital.toString(), ObjectDigital.class);
        //获取基本参数
        String groupCode = objectDigital.get("groupCode").textValue();
        String projectId = objectDigital.get("projectId").textValue();
        if(DigitalMessageConstant.OPERATE_AFTER_INSERT.equals(messageVO.getOperatorType())){
            //创建二维码信息
            log.info("消息处理：创建对象二维码{}"+objectDigital);
            qrCodeService.createObjectQrCode(groupCode,projectId,objectDigital);
            //新增对象历史记录
            log.info("消息处理：新增对象历史记录{}"+objectDigital);
            objectDigitalHisService.insert(objectDigitalVO);
        }
        if(DigitalMessageConstant.OPERATE_AFTER_UPDATE.equals(messageVO.getOperatorType())){
            //修改对象历史记录
            log.info("消息处理：修改对象历史记录{}"+objectDigital);
            objectDigitalHisService.update(objectDigitalVO);
        }
        if(DigitalMessageConstant.OPERATE_AFTER_DELETE.equals(messageVO.getOperatorType())){
            //删除对象二维码
            log.info("消息处理：删除对象二维码{}"+objectDigital);
            qrCodeService.removeObjectQrCode(groupCode,projectId,objectDigital);
            //删除对象历史记录
            log.info("消息处理：删除对象历史记录{}"+objectDigital);
            objectDigitalHisService.delete(objectDigitalVO);
        }
    }

    /**
     * 处理物理世界关系数据
     * @param messageVO
     * @param objectRelation
     * @throws Exception
     */
    private void handlerDigitalRelationMessage(DigitalManageMessage messageVO, ObjectNode objectRelation) throws Exception{
        ObjectRelation objectRelationVO = objectMapper.readValue(objectRelation.toString(), ObjectRelation.class);
        if(DigitalMessageConstant.OPERATE_AFTER_INSERT.equals(messageVO.getOperatorType())){
            //新增对象关系-历史记录
            log.info("消息处理：新增对象关系历史记录{}"+objectRelation);
            objectRelationHisService.insert(objectRelationVO);
        }
        if(DigitalMessageConstant.OPERATE_AFTER_INSERT.equals(messageVO.getOperatorType())){
            //修改对象关系-历史记录
            log.info("消息处理：修改对象关系历史记录{}"+objectRelation);
            objectRelationHisService.update(objectRelationVO);
        }
        if(DigitalMessageConstant.OPERATE_AFTER_INSERT.equals(messageVO.getOperatorType())){
            //删除对象关系-历史记录
            log.info("消息处理：删除对象关系历史记录{}"+objectRelation);
            objectRelationHisService.delete(objectRelationVO);
        }
    }


}
