package com.web.rocketmq.consumer;

import com.hai.entity.HighDiscountAgentCode;
import com.hai.entity.HighDiscountUserRel;
import com.hai.entity.HighOrder;
import com.hai.entity.HighOrderRefund;
import com.hai.order.service.OrderRefundHandleService;
import com.hai.order.service.OrderRefundService;
import com.hai.order.service.OrderService;
import com.hai.order.type.OrderChildGoodsType;
import com.hai.order.type.OrderProductType;
import com.hai.order.type.OrderStatus;
import com.hai.service.BsMsgService;
import com.hai.service.HighDiscountAgentCodeService;
import com.hai.service.HighDiscountUserRelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Component
@Slf4j
@RocketMQMessageListener(consumerGroup = "order-refund-success-group", topic = "order-topic",selectorExpression = "refund-success")
public class OrderRefundSuccessConsumer implements RocketMQListener<HighOrderRefund> {

    @Resource
    private OrderService orderService;

    @Resource
    private OrderRefundHandleService refundHandleService;

    @Resource
    private HighDiscountUserRelService discountUserRelService;
    @Resource
    private HighDiscountAgentCodeService discountAgentCodeService;


    @Override
    public void onMessage(HighOrderRefund orderRefund) {

        // 交易订单
        HighOrder order = orderService.getOrderDetailByNo(orderRefund.getOrderNo());
        if (order != null && order.getMemDiscountId() != null) {
            // 初始化金额 避免null值
            order.setSurplusRefundPrice(order.getSurplusRefundPrice()!=null?order.getSurplusRefundPrice():new BigDecimal("0"));
            order.setSurplusRefundIntegral(order.getSurplusRefundIntegral()!=null?order.getSurplusRefundIntegral():0);

            // 剩余退款的积分
            int surplusRefundIntegral = order.getSurplusRefundIntegral() - orderRefund.getRefundIntegral();
            // 剩余退款的金额
            BigDecimal surplusRefundPrice = order.getSurplusRefundPrice().subtract(orderRefund.getRefundPrice());

            // 交易订单可退金额为0，才会退还优惠券
            if (surplusRefundIntegral == 0 && surplusRefundPrice.compareTo(new BigDecimal("0")) == 0) {
                HighDiscountUserRel rel = discountUserRelService.getRelById(order.getMemDiscountId());
                if (rel != null) {
                    rel.setStatus(1);  // 状态  0:已过期  1：未使用  2：已使用
                    rel.setUseTime(null);
                    discountUserRelService.updateDiscountUserRel(rel);

                    HighDiscountAgentCode code = discountAgentCodeService.getCodeById(rel.getDiscountAgentCodeId());
                    code.setStatus(2);
                    discountAgentCodeService.updateCode(code);
                }
            }
        }

        if (orderRefund.getGoodsType().equals(OrderChildGoodsType.TYPE1.getNumber())) {
            refundHandleService.couponHandle(orderRefund);

        } else if (orderRefund.getGoodsType().equals(OrderChildGoodsType.TYPE2.getNumber())) {

        } else if (orderRefund.getGoodsType().equals(OrderChildGoodsType.TYPE3.getNumber())) {
            refundHandleService.oilHandle(orderRefund);

        } else if (orderRefund.getGoodsType().equals(OrderChildGoodsType.TYPE4.getNumber())) {

        } else if (orderRefund.getGoodsType().equals(OrderChildGoodsType.TYPE5.getNumber())) {

        } else if (orderRefund.getGoodsType().equals(OrderChildGoodsType.TYPE6.getNumber())) {

            // todo 话费订单单独处理 后续需要迁移进入到统一退款处理业务

        } else if (orderRefund.getGoodsType().equals(OrderChildGoodsType.TYPE7.getNumber())) {

        } else if (orderRefund.getGoodsType().equals(OrderChildGoodsType.TYPE8.getNumber())) {

        } else if (orderRefund.getGoodsType().equals(OrderChildGoodsType.TYPE9.getNumber())) {

        } else if (orderRefund.getGoodsType().equals(OrderChildGoodsType.TYPE10.getNumber())) {

        } else if (orderRefund.getGoodsType().equals(OrderChildGoodsType.TYPE11.getNumber())) {

        }
    }
}
