package com.hzs.third.mqcall.listener;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.constant.RabbitMqConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.system.sys.IBusinessLogApi;
import com.hzs.system.sys.dto.BusinessLogDTO;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务操作日志监听
 */
@Slf4j
@Component
public class SysBusinessLogListener {

    @DubboReference
    IBusinessLogApi iBusinessLogApi;

    /**
     * 业务操作日志监听处理
     *
     * @param message
     * @param channel
     * @throws Exception
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = RabbitMqConstants.BUSINESS_LOG_EXCHANGE, type = "topic"),
            value = @Queue(value = RabbitMqConstants.BUSINESS_LOG_QUEUE, durable = "true", autoDelete = "false"),
            key = RabbitMqConstants.BUSINESS_LOG_KEY))
    @RabbitHandler
    public void onMessage(Message<List<BusinessLogDTO>> message, Channel channel) throws Exception {
        // 手动应答
        Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        channel.basicAck(deliveryTag, false);

        // 获取传入的日志列表
        List<BusinessLogDTO> dtoList = message.getPayload();
        log.info("业务操作日志入参信息：{}", dtoList);

        if (CollectionUtil.isNotEmpty(dtoList)) {
            // 实际入库数据
            List<BusinessLogDTO> saveList = new ArrayList<>();

            for (BusinessLogDTO businessLogDTO : dtoList) {
                if (null == businessLogDTO.getBusinessType()) {
                    log.warn("业务类型为空");
                    continue;
                }
                if (StringUtils.isEmpty(businessLogDTO.getBusinessCode()) && null == businessLogDTO.getBusinessId()) {
                    log.warn("业务单号和业务ID都为空");
                    continue;
                }
                if (null == businessLogDTO.getPkCreator()) {
                    log.warn("操作人为空");
                    continue;
                }
                if (null == businessLogDTO.getPkCountry()) {
                    log.warn("所属国家为空");
                    continue;
                }
                if (null == businessLogDTO.getFreeSignFlag()) {
                    businessLogDTO.setFreeSignFlag(false);
                }
                saveList.add(businessLogDTO);
            }

            if (CollectionUtil.isNotEmpty(saveList)) {
                R<String> saveR = iBusinessLogApi.saveBatch(saveList);
                if (!saveR.isSuccess()) {
                    log.error("插入业务操作日志失败. {}", StringUtils.isNotEmpty(saveR.getMsg()) ? saveR.getMsg() : "");
                }
            }
        }
    }

}
