package com.fzm.mall.server.admin.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.exception.MyException;
import com.fzm.mall.server.admin.order.model.DeliveryRefund;
import com.fzm.mall.server.admin.order.mapper.DeliveryRefundMapper;
import com.fzm.mall.server.admin.order.service.IDeliveryRefundService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 提货退货单表 服务实现类
 * </p>
 *
 * @author wulixian
 * @since 2021-04-29
 */
@Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DeliveryRefundServiceImpl extends ServiceImpl<DeliveryRefundMapper, DeliveryRefund> implements IDeliveryRefundService {

    private final DeliveryRefundMapper deliveryRefundMapper;

    @Override
    public void refundExamine(String orderDeliveryId, Boolean isApproved, String remark, String addressId) {
        DeliveryRefund deliveryRefund = new DeliveryRefund();
        deliveryRefund.setOrderDeliveryId(orderDeliveryId);
        deliveryRefund.setErrMsg(remark);
        if(isApproved){
            deliveryRefund.setState(DeliveryRefund.DELIVERY_REFUND_STATE_MERCHANT);
            deliveryRefund.setAddressId(addressId);
        }else{
            deliveryRefund.setState(DeliveryRefund.DELIVERY_REFUND_STATE_RFUND_RERUSED);
        }

        UpdateWrapper<DeliveryRefund> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("order_delivery_id", orderDeliveryId);

        deliveryRefundMapper.update(deliveryRefund, updateWrapper);
    }

    @Override
    public void returnReceiptExamine(String orderDeliveryId, Boolean isApproved, String remark) {
        DeliveryRefund deliveryRefund = new DeliveryRefund();
        deliveryRefund.setOrderDeliveryId(orderDeliveryId);
        deliveryRefund.setErrMsg(remark);
        if(isApproved){
            deliveryRefund.setState(DeliveryRefund.DELIVERY_REFUND_STATE_TO_RFUND_ASSET);
        }else{
            deliveryRefund.setState(DeliveryRefund.DELIVERY_REFUND_STATE_RFUND_RERUSED);
        }

        UpdateWrapper<DeliveryRefund> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("order_delivery_id", orderDeliveryId);

        deliveryRefundMapper.update(deliveryRefund, updateWrapper);
    }
}
