package com.persagy.dmp.rwd.qrcode.handler;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
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.model.DigitalManageMessage;
import com.persagy.dmp.common.constant.CommonConstant;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.digital.client.DigitalObjectFacade;
import com.persagy.dmp.file.model.UploadMes;
import com.persagy.dmp.basic.constant.DigitalMessageConstant;
import com.persagy.dmp.rwd.qrcode.service.impl.QrCodeServiceImpl;
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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 二维码消息处理
 * @author:linhuili
 * @date:2021/11/8
 */
@Slf4j
@Component
@RabbitListener(queues = "queue-dmp-rwd")
public class QrCodeMessageHandler extends AbstractRabbitHandlerTemplate {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private QrCodeServiceImpl qrCodeService;

    @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())){
                log.info("新增消息格式有误：{}"+ messageVO);
            }
            //获取基本参数
            String groupCode = objectDigital.get("groupCode").textValue();
            String projectId = objectDigital.get("projectId").textValue();
            //创建二维码图片
            if(DigitalMessageConstant.OPERATE_AFTER_INSERT.equals(messageVO.getOperatorType())){
                createObjectQrCode(groupCode,projectId,objectDigital);
            }
            //删除二维码信息
            if(DigitalMessageConstant.OPERATE_AFTER_DELETE.equals(messageVO.getOperatorType())){
                removeObjectQrCode(groupCode,projectId,objectDigital);
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new BusinessException(e.getMessage(), e);
        }
    }

    /**
     * 生成对象二维码信息
     * @param digital
     */
    private void createObjectQrCode(String groupCode,String projectId,ObjectNode digital){
        //生成二维码图片并上传到文件服务器
        UploadMes objectQrCode = qrCodeService.createObjectQrCode(groupCode,projectId,digital);
        if(objectQrCode == null){
            log.info("生成二维码失败");
            return;
        }
        //设置对象二维码信息
        qrCodeService.setObjectQrCode(digital,objectQrCode);
        //更新对象二维码信息
        List<ObjectNode> list = new ArrayList<>();
        list.add(digital);
        DigitalObjectFacade.update(groupCode,projectId,null,null,list);
    }

    /**
     * 移除对象二维码信息点
     * @param digital
     */
    public void removeObjectQrCode(String groupCode,String projectId,ObjectNode digital){
        ObjectNode infos = JsonNodeFactory.instance.objectNode();
        //删除二维码信息点
        ArrayNode removeInfos = JsonNodeFactory.instance.arrayNode();
        removeInfos.add(CommonConstant.QR_CODE_PIC_INFO);
        infos.put(CommonConstant.REMOVE_FIELD, removeInfos);

        List<ObjectNode> list = new ArrayList<>();
        list.add(digital);
        DigitalObjectFacade.update(groupCode,projectId,null,null,list);
    }
}
