package com.seer.teach.pay.admin.withdrawal.convert;

import com.seer.teach.common.utils.MoneyUtil;
import com.seer.teach.pay.admin.withdrawal.controller.request.PayWithdrawalApplyReq;
import com.seer.teach.pay.admin.withdrawal.controller.response.PayWithdrawalApplyResponse;
import com.seer.teach.pay.entity.PayMerchantWithdrawalApplyEntity;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.factory.Mappers;

import java.util.List;

@Mapper
public interface PayWithdrawalApplyConvert {

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

    @Mapping(source = "id", target = "withdrawalId")
    PayWithdrawalApplyResponse entityToResponse(PayMerchantWithdrawalApplyEntity payMerchantWithdrawalApplyEntity);

    @Mapping(source = "id", target = "withdrawalId")
    List<PayWithdrawalApplyResponse> entityListToResponseList(List<PayMerchantWithdrawalApplyEntity> payMerchantWithdrawalApplyEntityList);

    PayMerchantWithdrawalApplyEntity reqToEntity(PayWithdrawalApplyReq payWithdrawalApply);


    /**
     * 在映射完成后，处理需要转换的金额字段
     *
     * @param entity   源对象
     * @param response 目标对象
     */
    @AfterMapping
    default void convertCentToYuan(PayMerchantWithdrawalApplyEntity entity, @MappingTarget PayWithdrawalApplyResponse response) {
        if (entity.getActualAmount() != null) {
            response.setActualAmount(MoneyUtil.centToYuan(entity.getActualAmount()));
        }
        if (entity.getApplyAmount() != null) {
            response.setApplyAmount(MoneyUtil.centToYuan(entity.getApplyAmount()));
        }
        if( entity.getPlatformFee() != null ){
            response.setPlatformFee(MoneyUtil.centToYuan(entity.getPlatformFee()));
        }
        if( entity.getTaxAmount() != null ){
            response.setTaxAmount(MoneyUtil.centToYuan(entity.getTaxAmount()));
        }
    }
}