package com.heyu.api.listener;

import com.alibaba.fastjson.JSONObject;
import com.heyu.api.alibaba.request.mm.enums.DelayTypeEnums;
import com.heyu.api.alibaba.request.mm.enums.OrderStatusEnums;
import com.heyu.api.data.dao.vv.VvPackageDao;
import com.heyu.api.data.dao.vv.VvTradeOrderLineDao;
import com.heyu.api.data.dto.PackageDelivedDelayDTO;
import com.heyu.api.data.entity.vv.VvPackageEntity;
import com.heyu.api.data.entity.vv.VvTradeOrderLineEntity;
import com.heyu.api.data.enums.RoleEnums;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class DelaySimpleRabbitListener {

    @Value("${eb.config.rabbitQueue.delayQueue}")
    private String delayQueue;


    @Autowired
    private VvPackageDao vvPackageDao;


    @Autowired
    private VvTradeOrderLineDao vvTradeOrderLineDao;


    @RabbitHandler
    @RabbitListener(queues = "#{delayQueue.name}", containerFactory = "accountAmountQueueSimpleRabbitListenerContainerFactory")
    public void consumeMessage(@Payload String message, @Header(AmqpHeaders.DELIVERY_TAG) long delivertTag, Channel channel) {
        try {
            log.info("DeliveredDelaySimpleRabbitListener delayQueue :{} message:{}", delayQueue, message);
            Map<String, Object> map = JSONObject.parseObject(message, Map.class);
            String type = map.get("type") + "";
            if (DelayTypeEnums.delivered.getType().equals(type)) {
                PackageDelivedDelayDTO packageDelivedDelayDTO = JSONObject.parseObject(message, PackageDelivedDelayDTO.class);

                // 更新包信息
                VvPackageEntity vvPackage = vvPackageDao.selectVvPackageById(packageDelivedDelayDTO.getPackageId());
                vvPackage.setIscheck("3");
                vvPackage.setGmtDelivered(new Date());
                vvPackage.setDeliveredBy(RoleEnums.job.getRole());

                vvPackageDao.insertOrUpdateVvPackage(vvPackage);


                // 更新子单信息
                List<VvTradeOrderLineEntity> vvTradeOrderLineEntityList = vvTradeOrderLineDao.selectVvTradeOrderLineByTrackNumber(vvPackage.getTrackNumber());
                for (VvTradeOrderLineEntity vvTradeOrderLineEntity : vvTradeOrderLineEntityList) {
                    vvTradeOrderLineEntity.setDeliveredBy(RoleEnums.job.getRole());
                    vvTradeOrderLineEntity.setGmtDelivered(new Date());
                    vvTradeOrderLineEntity.setStatus(OrderStatusEnums.delivered.getStatus());
                    vvTradeOrderLineDao.updateVvTradeOrderLineById(vvTradeOrderLineEntity);
                }
            }
        } catch (Exception e) {
            log.error("DeliveredDelaySimpleRabbitListener handle", e);
        } finally {
            try {
                log.info("DeliveredDelaySimpleRabbitListener 消息{},消息确认完成", message);
                channel.basicAck(delivertTag, true);
            } catch (IOException e) {
                log.error("DeliveredDelaySimpleRabbitListener 消息确认异常", e);
            }
        }
    }
}