package com.hai.order.service.impl;

import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.dao.HighOrderRefundMapper;
import com.hai.entity.HighChildOrder;
import com.hai.entity.HighOrder;
import com.hai.entity.HighOrderRefund;
import com.hai.order.service.OrderRefundService;
import com.hai.order.service.OrderService;
import com.hai.order.type.OrderChildStatus;
import com.hai.order.type.OrderPayType;
import com.hai.order.type.OrderRefundStatus;
import com.hai.order.type.OrderStatus;
import com.hai.service.HighUserService;
import org.springframework.stereotype.Service;

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

/**
 * @className: OrderRefundServiceImpl
 * @author: HuRui
 * @date: 2022/9/1
 **/
@Service("orderRefundService")
public class OrderRefundServiceImpl implements OrderRefundService {

    @Resource
    private HighOrderRefundMapper orderRefundMapper;

    @Resource
    private HighUserService userService;

    @Resource
    private OrderService orderService;

    @Override
    public HighOrder createOrderRefund(HighOrder order, String remarks) {
        // 使用了积分抵扣
        if (order.getPayGold() != null && order.getPayGold().intValue() > 0) {
            // 积分返回
            String remark = "订单" + order.getOrderNo() + "退款，退还积分：" +  order.getPayGold();
            userService.goldHandle(order.getMemId(), order.getPayGold(), 1, 3, order.getId() , remark);
        }
        order.setOrderStatus(OrderStatus.STATUS4.getNumber());
        order.setRefundTime(new Date());


        for (HighChildOrder childOrder : order.getHighChildOrderList()) {
            childOrder.setRefundTime(new Date());


        }

        if (order.getPayType() != null) {
            // 原路退款
            if (order.getPayType().equals(OrderPayType.PAY_TYPE1.getNumber())) {

            } else if (order.getPayType().equals(OrderPayType.PAY_TYPE2.getNumber())) {

            } else if (order.getPayType().equals(OrderPayType.PAY_TYPE4.getNumber())) {

            } else if (order.getPayType().equals(OrderPayType.PAY_TYPE5.getNumber())) {

            } else if (order.getPayType().equals(OrderPayType.PAY_TYPE6.getNumber())) {

            } else if (order.getPayType().equals(OrderPayType.PAY_TYPE7.getNumber())) {

            }
        }

        return orderService.updateOrderData(order);
    }

    @Override
    public HighChildOrder createOrderChildRefund(HighChildOrder orderChild,Integer refundCount, String remarks) {
        if (orderChild.getChildOrderStatus().equals(OrderChildStatus.STATUS2.getNumber())
            || orderChild.getChildOrderStatus().equals(OrderChildStatus.STATUS3.getNumber())){
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "子订单状态错误");
        }
        if (orderChild.getSurplusRefundIntegral() <= 0
                && orderChild.getSurplusRefundPrice().compareTo(new BigDecimal("0")) <= 0) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "子订单余额不足");
        }

        // 判断剩余可退商品数量
        if (orderChild.getSurplusRefundNum() < refundCount) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "退款商品数量过多");
        }



        HighOrderRefund orderRefund = new HighOrderRefund();
        orderRefund.setOrderId(orderChild.getOrderId());
        orderRefund.setOrderNo(orderChild.getOrderNo());
        orderRefund.setOrderChildId(orderChild.getId());
        orderRefund.setOrderChildNo(orderChild.getChildOrderNo());
        orderRefund.setMemId(orderChild.getMemId());
        orderRefund.setMemName(orderChild.getMemPhone());
        orderRefund.setMemPhone(orderChild.getMemPhone());
        orderRefund.setGoodsType(orderChild.getGoodsType());
        orderRefund.setGoodsId(orderChild.getGoodsId());
        orderRefund.setGoodsName(orderChild.getGoodsName());
        orderRefund.setGoodsDesc(orderChild.getGoodsDesc());
        orderRefund.setGoodsImg(orderChild.getGoodsImg());
        orderRefund.setGoodsSpecName(orderChild.getGoodsSpecName());
        orderRefund.setGoodsCount(orderChild.getSaleCount());

        orderRefund.setRefundOrder(orderChild.getOrderNo());
        orderRefund.setReturnTotalPrice(orderChild.getTotalActualPrice());
        orderRefund.setRefundPrice(orderChild.getSurplusRefundPrice());
        orderRefund.setRefundGold(orderChild.getSurplusRefundIntegral());
        orderRefund.setRefundRemark(remarks);
        orderRefund.setRefundStatus(OrderRefundStatus.STATUS1.getNumber());

        orderRefund.setCreateTime(new Date());
        orderRefund.setUpdateTime(new Date());
        orderRefundMapper.insert(orderRefund);

        return orderChild;
    }
}
