package com.jeequan.jeepay.converter;

import cn.hutool.core.util.DesensitizedUtil;
import com.jeequan.jeepay.core.entity.TransferOrder;
import com.jeequan.jeepay.core.entity.TransferWallet;
import com.jeequan.jeepay.core.model.openapi.biz.transfer.TransferResp;
import com.jeequan.jeepay.db.entity.TransferOrderEntity;
import com.jeequan.jeepay.db.entity.TransferWalletEntity;
import org.mapstruct.*;

/**
 * 代付相关实体类转换器
 *
 * @author deng
 * @since 2024/5/9
 */
@Mapper(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
public interface TransferConverter {

    @BeanMapping(builder = @Builder(disableBuilder = true))
    TransferOrder toModel(TransferOrderEntity entity);

    @Mapping(target = "ext", ignore = true)
    @Mapping(target = "dateRange", ignore = true)
    @BeanMapping(builder = @Builder(disableBuilder = true))
    TransferOrderEntity toDbEntity(TransferOrder model);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    TransferWallet toModel(TransferWalletEntity entity);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    TransferWalletEntity toDbEntity(TransferWallet entity);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    void cp(TransferOrder transferOrder, @MappingTarget TransferResp target);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    void cp(TransferOrderEntity transferOrder, @MappingTarget TransferResp target);

    default void desensitized(TransferResp transferResp) {
        if (transferResp == null) {
            return;
        }

        if (transferResp.getOriginAccountNo() != null) {
            transferResp.setOriginAccountNo(DesensitizedUtil.bankCard(transferResp.getOriginAccountNo()));
        }

        if (transferResp.getOriginAccountName() != null) {
            transferResp.setOriginAccountName(DesensitizedUtil.chineseName(transferResp.getOriginAccountName()));
        }

        if (transferResp.getAccountNo() != null) {
            transferResp.setAccountNo(DesensitizedUtil.bankCard(transferResp.getAccountNo()));
        }

        if (transferResp.getAccountName() != null) {
            transferResp.setAccountName(DesensitizedUtil.chineseName(transferResp.getAccountName()));
        }
    }
}
