package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.GenerateCode;
import com.ruoyi.system.domain.Order;
import com.ruoyi.system.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.OrderRefundMapper;
import com.ruoyi.system.domain.OrderRefund;
import com.ruoyi.system.service.IOrderRefundService;

/**
 * 订单退款Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-04-28
 */
@Service
public class OrderRefundServiceImpl implements IOrderRefundService 
{
    @Autowired
    private OrderRefundMapper orderRefundMapper;
    @Autowired
    private OrderServiceImpl orderService;
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 查询订单退款
     * 
     * @param id 订单退款主键
     * @return 订单退款
     */
    @Override
    public OrderRefund selectOrderRefundById(String id)
    {
        return orderRefundMapper.selectOrderRefundById(id);
    }

    /**
     * 查询订单退款列表
     * 
     * @param orderRefund 订单退款
     * @return 订单退款
     */
    @Override
    public List<OrderRefund> selectOrderRefundList(OrderRefund orderRefund)
    {
        List<OrderRefund> orderRefunds = orderRefundMapper.selectOrderRefundList(orderRefund);
        for (OrderRefund refund : orderRefunds) {
            Order order = orderService.selectOrderById(refund.getOrderId());
            refund.setOrder(order);
        }
        return orderRefunds;

    }

    /**
     * 新增订单退款
     * 
     * @param orderRefund 订单退款
     * @return 结果
     */
    @Override
    public AjaxResult insertOrderRefund(OrderRefund orderRefund)
    {
        Order order = orderMapper.selectOrderById(orderRefund.getOrderId());
        if(order ==null ){
          return AjaxResult.error("找不到该订单");
        }
       // 制作中、制作完成、聚餐中、已完成
        String state = order.getState();

        if("2".equals(state)||"3".equals(state)){

           // 4 支付制作中 5 制作完成未取 6 取餐中 7 取餐完成
        }else if("4".equals(state)||"5".equals(state)||"6".equals(state)||"7".equals(state)){

        }else{
            return AjaxResult.error("该订单状态不允许退款");
        }

        orderRefund.setCreatedAt(new Date());
        orderRefund.setUpdatedAt(new Date());
        orderRefund.setState("0");
        orderRefund.setOrderNo(order.getOrderNo());
        orderRefund.setRefundNo(GenerateCode.getCode("R","%09d"));
        orderRefund.setTotalFee(order.getAmount());
        int i = orderRefundMapper.insertOrderRefund(orderRefund);
        return AjaxResult.success(orderRefund);
    }

    /**
     * 修改订单退款
     * 
     * @param orderRefund 订单退款
     * @return 结果
     */
    @Override
    public int updateOrderRefund(OrderRefund orderRefund)
    {
        return orderRefundMapper.updateOrderRefund(orderRefund);
    }

    /**
     * 批量删除订单退款
     * 
     * @param ids 需要删除的订单退款主键
     * @return 结果
     */
    @Override
    public int deleteOrderRefundByIds(String[] ids)
    {
        return orderRefundMapper.deleteOrderRefundByIds(ids);
    }

    /**
     * 删除订单退款信息
     * 
     * @param id 订单退款主键
     * @return 结果
     */
    @Override
    public int deleteOrderRefundById(String id)
    {
        return orderRefundMapper.deleteOrderRefundById(id);
    }

    @Override
    public OrderRefund getOrderRefund(String id) {
        OrderRefund orderRefund = orderRefundMapper.selectOrderRefundById(id);
        if("0".equals(orderRefund.getState()) ||"3".equals(orderRefund.getState())){

        }else{
            throw new ServiceException("该状态不允许退款");
        }
        Order order = orderMapper.selectOrderById(orderRefund.getOrderId());
        orderRefund.setTotalFee(order.getAmount());
        return orderRefund;
    }
}
