package com.hzs.scm.listener;

import cn.hutool.json.JSONUtil;
import com.hzs.common.core.constant.RabbitMqConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.domain.sale.ext.SaDeliverExt;
import com.hzs.common.domain.scm.sale.IcDelSaleLog;
import com.hzs.sale.deliver.ISaDeliverServiceApi;
import com.hzs.scm.pub.dto.DeliverDTO;
import com.hzs.scm.sale.service.IIcDelSaleLogService;
import com.hzs.scm.sale.service.IIcSaleoutHService;
import com.hzs.system.sys.dto.LoginUser;
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.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description: 进销存发货处理监听
 * @Author: jiang chao
 * @Time: 2023/12/19 14:24
 * @Classname: ScmDeliverListener
 * @PackageName: com.hzs.third.mqcall.listener
 */
@Slf4j
@Component
public class ScmDeliverListener {

    @DubboReference
    private ISaDeliverServiceApi iSaDeliverServiceApi;

    @Autowired
    private IIcSaleoutHService saleOutService;

    @Autowired
    private IIcDelSaleLogService delSaleLogService;

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = RabbitMqConstants.SCM_DELIVER_EXCHANGE, type = "topic"),
            value = @Queue(value = RabbitMqConstants.SCM_DELIVER_QUEUE, durable = "true", autoDelete = "false"),
            key = RabbitMqConstants.SCM_DELIVER_KEY))
    @RabbitHandler
    public void onMessage(Message<DeliverDTO> message, Channel channel) throws Exception {
        // 手动应答
        Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        channel.basicAck(deliveryTag, false);
        // 根据传入的ID获取翻译内容
        DeliverDTO deliverDTO = message.getPayload();
        log.info("进销存发货处理数据：{}", deliverDTO);
        LoginUser loginUser = deliverDTO.getLoginUser();
        // 查询发货单及明细数据
        R<List<SaDeliverExt>> r = iSaDeliverServiceApi.queryDeliverList(deliverDTO.getDeliverIdList(), loginUser.getDataCountry());
        if (r.isSuccess()) {
            List<SaDeliverExt> saDeliverExtList = r.getData();
            for (SaDeliverExt deliverExt : saDeliverExtList) {
                IcDelSaleLog icDelSaleLog = IcDelSaleLog.builder()
                        .acceptParam(JSONUtil.toJsonStr(deliverExt))
                        .sourceCode(deliverExt.getDeliverCode())
                        .successStatus(EYesNo.YES.getIntValue())
                        .build();
                icDelSaleLog.setPkCreator(deliverExt.getPkCreator());
                icDelSaleLog.setPkCountry(deliverExt.getPkCountry());
                try {
                    saleOutService.executeDeliverTransSaleOutByParam(deliverExt);
                } catch (Exception e) {
                    icDelSaleLog.setErrorMsg(e.getMessage());
                    icDelSaleLog.setSuccessStatus(EYesNo.NO.getIntValue());
                } finally {
                    // 记录日志
                    delSaleLogService.save(icDelSaleLog);
                }
            }
            return;
        }
        log.error("查询发货单及明细数据失败：{}", r.getMsg());
    }

}
