package com.hai.order.service.impl;

import com.hai.entity.GoodsSku;
import com.hai.entity.HighChildOrder;
import com.hai.entity.HighCouponCode;
import com.hai.entity.HighGasOrder;
import com.hai.goods.service.GoodsSkuService;
import com.hai.order.service.OrderCancelHandleService;
import com.hai.order.type.OrderChildStatus;
import com.hai.order.type.OrderOilStatus;
import com.hai.service.HighCouponCodeService;
import com.hai.service.HighGasOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @className: CancelOrderBusServiceImpl
 * @author: HuRui
 * @date: 2022/8/29
 **/
@Service("cancelOrderHandleService")
public class OrderCancelHandleServiceImpl implements OrderCancelHandleService {

    @Resource
    private HighCouponCodeService couponCodeService;

    @Resource
    private HighGasOrderService gasOrderService;

    @Resource
    private GoodsSkuService goodsSkuService;

    @Override
    public void couponHandle(HighChildOrder childOrder)  {
        // 解除兑换码与订单的关系
        HighCouponCode couponCode = couponCodeService.getCodeByChildOrderNo(childOrder.getChildOrderNo());
        if (couponCode != null ) {
            couponCode.setChildOrderNo(null);
            couponCode.setStatus(1);
            couponCodeService.updateCouponCode(couponCode);
        }
    }

    @Override
    public void oilHandle(HighChildOrder childOrder) {
        // 查询加油订单
        HighGasOrder gasOrder = gasOrderService.getDetailByChildOrderNo(childOrder.getChildOrderNo());
        if (gasOrder != null) {
            gasOrder.setCancelTime(new Date());
            gasOrder.setStatus(OrderOilStatus.STATUS3.getNumber());
            gasOrderService.updateGasOrder(gasOrder);
        }
    }

    @Override
    public void goodsHandle(HighChildOrder childOrder) {

        GoodsSku goodsSku = goodsSkuService.findGoodsSkuById(childOrder.getSkuId());
        goodsSku.setStock(goodsSku.getStock() + childOrder.getSaleCount());
        goodsSkuService.updateGoodsSku(goodsSku);
    }
}
