
package com.tmerclub.cloud.order.service.impl;

import cn.hutool.core.date.DateUtil;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.order.constant.ReturnProcessStatusEnum;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.order.constant.PlatformInterventionStatus;
import com.tmerclub.cloud.order.dto.OrderRefundInterventionDTO;
import com.tmerclub.cloud.order.mapper.OrderRefundInterventionMapper;
import com.tmerclub.cloud.order.model.OrderRefundIntervention;
import com.tmerclub.cloud.order.service.OrderRefundInterventionService;
import com.tmerclub.cloud.order.service.OrderRefundService;
import com.tmerclub.cloud.order.vo.OrderRefundVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 订单退款介入记录信息
 *
 * @author tmerclub
 * @date 2023-08-25 10:45:35
 */
@Service
public class OrderRefundInterventionServiceImpl implements OrderRefundInterventionService {

    @Autowired
    private OrderRefundInterventionMapper orderRefundInterventionMapper;

    @Autowired
    private OrderRefundService orderRefundService;

    @Override
    public void savePlatormIntervertion(OrderRefundInterventionDTO orderRefundParam) {
        OrderRefundVO orderRefundVO = orderRefundService.getByRefundId(orderRefundParam.getRefundId());
        if (Objects.isNull(orderRefundVO)) {
            // 撤销失败 退款订单不存在
            throw new LuckException("退款订单不存在");
        }
        if (Objects.equals(orderRefundVO.getReturnMoneySts(), ReturnProcessStatusEnum.SUCCESS.value())) {
            // 买家已发货，不能撤销退款申请
            throw new LuckException("退款已经成功，不能提交凭证");
        }
        if (!Objects.equals(orderRefundVO.getReturnMoneySts(), ReturnProcessStatusEnum.FAIL.value())) {
            // 卖家正在处理退款，不能撤销退款申请
            throw new LuckException("卖家正在处理退款，不能提交凭证");
        }
        if (!Objects.equals(orderRefundVO.getPlatformInterventionStatus(), PlatformInterventionStatus.APPLY.value())) {
            throw new LuckException("平台介入状态异常，不能提交凭证");
        }
        Boolean timeCheck = DateUtil.compare(DateUtil.offsetHour(orderRefundVO.getApplyInterventionTime(),72) , DateUtil.date()) < 0;
        if (timeCheck) {
            throw new LuckException("已过添加凭证的时间，不能继续提交凭证");
        }
        OrderRefundIntervention orderRefundIntervention = BeanUtil.map(orderRefundParam, OrderRefundIntervention.class);
        orderRefundInterventionMapper.save(orderRefundIntervention);
    }

}
