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

import com.seer.teach.common.utils.MoneyUtil;
import com.seer.teach.pay.admin.withdrawal.controller.response.AdminPayMerchantRevenueDetailResp;
import com.seer.teach.pay.admin.withdrawal.controller.response.AdminPayMerchantRevenueUsageResp;
import com.seer.teach.pay.entity.PayMerchantRevenueDetailEntity;
import com.seer.teach.pay.entity.PayMerchantRevenueUsageEntity;
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 AdminPayMerchantRevenueUsageConvert {

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

    /**
     * 实体转响应
     *
     * @param entity 实体
     * @return 响应
     */
    @Mapping(target = "item", ignore = true)
    AdminPayMerchantRevenueUsageResp entityToResponse(PayMerchantRevenueUsageEntity entity);

    /**
     * 实体列表转响应列表
     *
     * @param entityList 实体列表
     * @return 响应列表
     */
    @Mapping(target = "item", ignore = true)
    List<AdminPayMerchantRevenueUsageResp> entityListToResponseList(List<PayMerchantRevenueUsageEntity> entityList);


    /**
     * 在映射完成后，处理需要转换的金额字段
     * @param entity 源对象
     * @param response 目标对象
     */
    @AfterMapping
    default void convertCentToYuan(PayMerchantRevenueUsageEntity entity, @MappingTarget AdminPayMerchantRevenueUsageResp response) {

        if (entity.getAvailableAmountAfter() != null) {
            response.setAvailableAmountAfter(MoneyUtil.centToYuan(entity.getAvailableAmountAfter()));
        }
        if (entity.getAvailableAmountBefore() != null) {
            response.setAvailableAmountBefore(MoneyUtil.centToYuan(entity.getAvailableAmountBefore()));
        }
        if (entity.getUsedAmount() != null) {
            response.setUsedAmount(MoneyUtil.centToYuan(entity.getUsedAmount()));
        }
    }

}