package com.github.wxiaoqi.security.admin.handler;


import cn.hutool.json.JSONUtil;
import com.github.wxiaoqi.security.admin.biz.AppUserSellingWaterBiz;
import com.github.wxiaoqi.security.admin.biz.BaseOrderAcceptDetailedBiz;
import com.github.wxiaoqi.security.admin.biz.BranchCompanyBiz;
import com.github.wxiaoqi.security.admin.dto.OrderGoodsDTO;
import com.github.wxiaoqi.security.admin.dto.OrderWaterDTO;
import com.github.wxiaoqi.security.admin.entity.BranchCompany;
import com.rabbitmq.client.Channel;
import com.xxfc.platform.order.contant.enumerate.ItemTypeEnum;
import com.xxfc.platform.order.contant.enumerate.OrderTypeEnum;
import com.xxfc.platform.order.entity.OrderItem;
import com.xxfc.platform.order.pojo.mq.OrderMQDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.github.wxiaoqi.security.admin.config.RabbitAdminConfig.ORDER_WATER_QUEUE;
import static com.github.wxiaoqi.security.admin.config.RabbitAdminConfig.VEHICLE_COMPANY_QUEUE;
import static com.xxfc.platform.order.pojo.mq.OrderMQDTO.*;

@Component
@Slf4j
public class VehicleCompanyMQHandler {

    @Autowired
    BranchCompanyBiz branchCompanyBiz;



    @RabbitListener(queues = VEHICLE_COMPANY_QUEUE)
    public void integralHandler(Message message, @Headers Map<String, Object> headers, Channel channel) {
        log.info("公司范围队列 messageJson："+ JSONUtil.parse(message));
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    String messageId = message.getMessageProperties().getMessageId();
                    String msg = new String(message.getBody(), "UTF-8");
                    BranchCompany branchCompany = JSONUtil.toBean(msg, BranchCompany.class);
                    branchCompanyBiz.addOrUpd(branchCompany);
                    executorService.shutdown();
                    Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
                    // 手动签收
                    channel.basicAck(deliveryTag, false);
                } catch (Exception e) {
                    log.info("接收到的消息失败");
                    try {
                        channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
                    } catch (IOException i) {
                        log.error(e.getMessage(), i);
                    }
                    log.error(e.getMessage(), e);
                }
            }
        });
    }
}
