package com.yc.ship.module.trade.convert.order;




import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.module.trade.api.dto.TradeRefundAuditReqDTO;
import com.yc.ship.module.trade.api.dto.TradeRefundCreateReqDTO;
import com.yc.ship.module.trade.controller.admin.order.vo.refund.TradeRefundAuditReqVO;
import com.yc.ship.module.trade.controller.admin.order.vo.refund.TradeRefundCreateReqVO;
import com.yc.ship.module.trade.controller.admin.order.vo.rule.RefundAuditNotifyReqVO;
import com.yc.ship.module.trade.dal.dataobject.order.TradeDetailBaseDO;
import com.yc.ship.module.trade.dal.dataobject.refund.RefundAuditDO;
import com.yc.ship.module.trade.dal.dataobject.refund.RefundDO;
import com.yc.ship.module.trade.dal.dataobject.refund.RefundDetailBaseDO;
import com.yc.ship.module.trade.dal.dataobject.refund.RefundDetailDO;
import com.yc.ship.module.trade.enums.RefundChannelEnum;
import com.yc.ship.module.trade.enums.RefundStatusEnum;
import com.yc.ship.module.trade.service.order.bo.TradeDetailBO;
import com.yc.ship.module.trade.service.refund.bo.RefundAuditNotifyBO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.math.BigDecimal;

import static com.yc.ship.module.trade.enums.ApiConstants.REFUND_METHOD_NORMAL;


@Mapper
public interface TradeRefundConvert {

    TradeRefundConvert INSTANCE = Mappers.getMapper(TradeRefundConvert.class);

    RefundDO convert(TradeRefundCreateReqDTO reqDTO);

    default RefundDetailDO convert(RefundDO refundDO, TradeDetailBO tradeDetailBO, BigDecimal refundAmount){
        RefundDetailDO refundDetailDO = new RefundDetailDO();
        refundDetailDO.setId(IdWorker.getId());
        refundDetailDO.setRefundId(refundDO.getId());
        refundDetailDO.setRefundAmount(refundAmount);
        refundDetailDO.setTradeDetailId(tradeDetailBO.getId());
        refundDetailDO.setVoucherStatus(tradeDetailBO.getVoucherStatus());
        refundDetailDO.setRefundStatus(RefundStatusEnum.INIT.getStatus());
        return refundDetailDO;
    }

    default RefundDetailBaseDO convert(RefundDetailDO refundDetailDO, TradeDetailBaseDO tradeDetailBaseDO, BigDecimal refundAmount){
        RefundDetailBaseDO refundDetailBaseDO = new RefundDetailBaseDO();
        refundDetailBaseDO.setId(IdWorker.getId());
        refundDetailBaseDO.setRefundAmount(refundAmount);
        refundDetailBaseDO.setTradeDetailId(refundDetailDO.getTradeDetailId());
        refundDetailBaseDO.setRefundDetailId(refundDetailDO.getId());
        refundDetailBaseDO.setRefundStatus(RefundStatusEnum.INIT.getStatus());
        refundDetailBaseDO.setUseStatus(tradeDetailBaseDO.getUseStatus());
        refundDetailBaseDO.setTradeDetailBaseId(tradeDetailBaseDO.getId());
        refundDetailBaseDO.setPlatSupplierOrderId(tradeDetailBaseDO.getPlatSupplierOrderId());
        refundDetailBaseDO.setRefundId(refundDetailDO.getRefundId());
        refundDetailBaseDO.setSupplierVoucherCode(tradeDetailBaseDO.getSupplierVoucherCode());
        return refundDetailBaseDO;
    }

    TradeRefundCreateReqDTO convert(TradeRefundCreateReqVO reqVO);

    RefundAuditDO convert(TradeRefundAuditReqDTO reqDTO);

    TradeRefundAuditReqDTO convert(TradeRefundAuditReqVO reqVO);


    default TradeRefundCreateReqDTO convertOtaRefund(Long orderId, Long distributorId,Integer isForceRefund,String otaRefundNo){
        TradeRefundCreateReqDTO refundCreateReqDTO = new TradeRefundCreateReqDTO();
        refundCreateReqDTO.setRefundChannel(RefundChannelEnum.OTA.getType()); // OTA
        refundCreateReqDTO.setRefundUser(distributorId);
        refundCreateReqDTO.setOrderId(orderId);
        refundCreateReqDTO.setRefundMethod(isForceRefund !=null ? isForceRefund:REFUND_METHOD_NORMAL);
        refundCreateReqDTO.setOtaRefundNo(otaRefundNo);
        return refundCreateReqDTO;
    }


    default TradeRefundCreateReqDTO.RefundItem convertRefundItem(BigDecimal refundAmt,Long orderId,Long detailId,String voucherCode){
        TradeRefundCreateReqDTO.RefundItem refundItem = new TradeRefundCreateReqDTO.RefundItem();
        refundItem.setRefundAmount(refundAmt);
        refundItem.setOrderId(orderId);
        refundItem.setOrderDetailId(detailId);
        refundItem.setVoucherCode(voucherCode);
        return refundItem;
    }

    RefundAuditNotifyBO convert(RefundAuditNotifyReqVO reqVO);
}
