package com.seer.teach.mall.convert;

import com.seer.teach.common.utils.MoneyUtil;
import com.seer.teach.mall.entity.OrderGoodsEntity;
import com.seer.teach.mall.entity.TradeAfterSaleEntity;
import com.seer.teach.mall.response.AfterSaleResp;
import com.seer.teach.mall.response.OrderItemResp;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.factory.Mappers;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface OrderGoodsConverter {

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

    OrderItemResp one(OrderGoodsEntity orderGood);

    List<OrderItemResp> list(List<OrderGoodsEntity> orderGoods);

    @Mapping(source = "orderItem", target = "orderItem")
    @Mapping(source = "afterSaleEntity.id", target = "id")
    @Mapping(source = "afterSaleEntity.orderId", target = "orderId")
    @Mapping(source = "afterSaleEntity.childOrderId", target = "childOrderId")
    @Mapping(source = "afterSaleEntity.skuId", target = "skuId")
    @Mapping(source = "afterSaleEntity.picUrl", target = "picUrl")
    AfterSaleResp one(TradeAfterSaleEntity afterSaleEntity, OrderGoodsEntity orderItem);
    
    /**
     * 在映射完成后，将分转换为元
     * @param entity 源对象
     * @param response 目标对象
     */
    @AfterMapping
    default void convertCentToYuan(OrderGoodsEntity entity, @MappingTarget OrderItemResp response) {
        // 将价格从分转换为元
        if (entity.getPrice() != null) {
            response.setPrice(MoneyUtil.centToYuan(entity.getPrice()));
        }
    }

    @AfterMapping
    default void convertCentToYuan(TradeAfterSaleEntity afterSaleEntity, @MappingTarget AfterSaleResp response) {
        // 将价格从分转换为元
        if (afterSaleEntity.getRefundPrice() != null) {
            response.setRefundPrice(MoneyUtil.centToYuan(afterSaleEntity.getRefundPrice()));
        }
    }
}