package com.hfkj.service.order;

import com.hfkj.common.utils.RedisUtil;
import com.hfkj.entity.BsOrderChild;
import com.hfkj.entity.BsOrderCoupon;
import com.hfkj.entity.GoodsMsg;
import com.hfkj.model.order.OrderChildModel;
import com.hfkj.model.order.OrderModel;
import com.hfkj.service.coupon.BsOrderCouponService;
import com.hfkj.service.goods.GoodsMsgService;
import com.hfkj.service.goods.GoodsSpecsService;
import com.hfkj.sysenum.order.OrderChildProductTypeEnum;
import com.hfkj.sysenum.order.OrderChildStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 取消订单业务
 * @className: OrderCancelService
 * @author: HuRui
 * @date: 2024/5/7
 **/
@Component
public class OrderCancelService {
    Logger log = LoggerFactory.getLogger(OrderCancelService.class);
    @Autowired
    private RedisUtil redisUtil;
    @Resource
    private BsOrderService orderService;
    @Resource
    private BsOrderChildService orderChildService;
    @Resource
    private BsOrderCouponService orderCouponService;
    @Resource
    private GoodsMsgService goodsMsgService;
    @Resource
    private GoodsSpecsService goodsSpecsService;
    /**
     * 订单业务处理
     * @param order
     * @throws Exception
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED, isolation = Isolation.READ_UNCOMMITTED)
    public void orderBusHandle(OrderModel order) {
        for (OrderChildModel childOrder : order.getOrderChildList()) {
            if (childOrder.getProductType().equals(OrderChildProductTypeEnum.type2.getCode())) {
                product(childOrder);
            }
        }
    }

    /**
     * 订单取消处理商品业务
     * @param orderChild
     */
    public void product(OrderChildModel orderChild) {
        // 查询产品
        GoodsMsg goodsMsg = goodsMsgService.queryDetail(orderChild.getProductId());
        if (goodsMsg.getType() == 1) {
            // 实物
        } else if (goodsMsg.getType() == 2) {
            // 归库
            goodsSpecsService.editStockNum(orderChild.getProductSpecId(), orderChild.getProductCount(), 1);

            // 虚拟
            BsOrderCoupon orderCoupon = orderCouponService.getDetailByChildOrderNo(orderChild.getChildOrderNo());
            orderCoupon.setCancelTime(new Date());
            orderCoupon.setStatus(OrderChildStatusEnum.status5.getCode());
            orderCouponService.editData(orderCoupon);
        }
    }


}
