package com.persagy.dmp.rwd.version.handler;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
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.basic.model.QueryCriteria;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.digital.client.DigitalObjectFacade;
import com.persagy.dmp.digital.entity.ObjectDigital;
import com.persagy.dmp.rwd.version.service.IObjectDigitalHistoryService;
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;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.DataInput;

/**
 * 历史对象记录消息处理
 * @author:linhuili
 * @date:2021/11/15
 */
@Slf4j
@Component
@RabbitListener
public class ObjectDigitalHistoryMessageHandler extends AbstractRabbitHandlerTemplate {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private IObjectDigitalHistoryService objectDigitalHisService;

    @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())){
                return;
            }
            ObjectDigital objectDigitalVO = objectMapper.readValue(objectDigital.toString(), ObjectDigital.class);
            //新增对象，历史记录
            if(DigitalMessageConstant.OPERATE_AFTER_INSERT.equals(messageVO.getOperatorType())){
                objectDigitalHisService.insert(objectDigitalVO);
            }
            //修改对象，历史记录
            if(DigitalMessageConstant.OPERATE_AFTER_UPDATE.equals(messageVO.getOperatorType())){
                objectDigitalHisService.update(objectDigitalVO);
            }
            //删除对象，历史记录
            if(DigitalMessageConstant.OPERATE_AFTER_DELETE.equals(messageVO.getOperatorType())){
                objectDigitalHisService.delete(objectDigitalVO);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new BusinessException(e.getMessage(), e);
        }

    }
}
