package com.hfkj.service.order;

import com.hfkj.entity.*;
import com.hfkj.model.order.OrderChildModel;
import com.hfkj.qianzhu.CinemaService;
import com.hfkj.service.coupon.BsOrderCouponService;
import com.hfkj.service.goods.BsOrderCinemaService;
import com.hfkj.service.goods.BsOrderStarbucksService;
import com.hfkj.service.goods.impl.BsOrderGoodsServiceImpl;
import com.hfkj.sysenum.order.OrderChildProductTypeEnum;
import com.hfkj.sysenum.order.OrderChildStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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


/**
  * @ClassName OrderRefundSuccessService
  * @Author Sum1Dream
  * @Description 退款成功业务处理
  * @Date 2024/7/4 下午5:31
 **/
@Component
public class OrderRefundSuccessService {

    @Resource
    private BsOrderCinemaService bsOrderCinemaService;
    @Autowired
    private BsOrderGoodsServiceImpl bsOrderGoodsService;
    @Resource
    private BsOrderCouponService orderCouponService;
    @Resource
    private BsOrderStarbucksService bsOrderStarbucksService;

    /**
     * 订单业务处理
     * @param orderChild
     * @throws Exception
     */
    public void orderRefundHandle(OrderChildModel orderChild) {
        try {
            if (orderChild.getProductType().equals(OrderChildProductTypeEnum.type1.getCode())) {
                // 实物产品退款业务
                goods(orderChild);
            } else if (orderChild.getProductType().equals(OrderChildProductTypeEnum.type2.getCode())) {
                // 虚拟产品退款业务
                orderCoupon(orderChild);
            } else if (orderChild.getProductType().equals(OrderChildProductTypeEnum.type3.getCode())) {
                // 电影票退款业务
                cinema(orderChild);
            }
        } catch (Exception e) {
            System.out.println("=========================================");
            System.out.println(e.getMessage());
        }
    }

    public void goods(BsOrderChild childOrder) {
        // 查询实物订单
        BsOrderGoods orderGoods = bsOrderGoodsService.findGoodsOrderByChild(childOrder.getOrderNo());

        try {

            if (orderGoods != null) {
                Map<String , Object> map = new HashMap<>();
                map.put("orderNo", childOrder.getOrderNo());
                CinemaService.refundMovieOrder(map);
                orderGoods.setStatus(OrderChildStatusEnum.status4.getCode());
                orderGoods.setLogisticsStatusDesc(null);
                orderGoods.setLogisticsNo(null);
                orderGoods.setLogisticsStatus(null);
                orderGoods.setExceptionStatus(false);
                bsOrderGoodsService.update(orderGoods);
            }
        } catch (Exception e) {
            orderGoods.setExceptionStatus(true);
            orderGoods.setExceptionMsg(e.getMessage());
            bsOrderGoodsService.update(orderGoods);
        }
    }


    public void orderCoupon(BsOrderChild childOrder) {
        // 查询虚拟订单
        BsOrderCoupon orderCoupon = orderCouponService.getDetailByChildOrderNo(childOrder.getChildOrderNo());
        if (orderCoupon != null) {
            orderCoupon.setRefundTime(new Date());
            orderCoupon.setStatus(OrderChildStatusEnum.status4.getCode());
            orderCouponService.editData(orderCoupon);
        }
    }


    public void cinema(BsOrderChild childOrder) {
        // 查询电影票订单
        BsOrderCinema orderCinema = bsOrderCinemaService.findGoodsOrder(childOrder.getOrderNo());

        try {

            if (orderCinema != null) {
                Map<String , Object> map = new HashMap<>();
                map.put("orderNo", childOrder.getOrderNo());
                CinemaService.refundMovieOrder(map);
                orderCinema.setStatus(-5);
                orderCinema.setExceptionStatus(false);
                bsOrderCinemaService.update(orderCinema);
            }
        } catch (Exception e) {
            orderCinema.setExceptionStatus(true);
            orderCinema.setExceptionMsg(e.getMessage());
            bsOrderCinemaService.update(orderCinema);
        }

    }

    public void starbucks(BsOrderChild childOrder) {
        // 查询星巴克订单
        BsOrderStarbucks orderStarbucks = bsOrderStarbucksService.findGoodsOrder(childOrder.getOrderNo());
        if (orderStarbucks != null) {
            orderStarbucks.setStatus(OrderChildStatusEnum.status4.getCode());
            orderStarbucks.setUpdateTime(new Date());
            bsOrderStarbucksService.update(orderStarbucks);
        }
    }
}
