MerchantConverter.java 1.49 KB
package com.diligrp.cashier.boss.util;

import com.diligrp.cashier.boss.CashierDeskProperties;
import com.diligrp.cashier.boss.model.MerchantDO;
import com.diligrp.cashier.shared.codec.IConverter;
import com.diligrp.cashier.shared.util.JsonUtils;
import com.diligrp.cashier.trade.domain.Merchant;
import com.diligrp.cashier.trade.domain.MerchantParams;

import java.util.Objects;

public class MerchantConverter implements IConverter<MerchantDO, Merchant> {

    private final CashierDeskProperties cashierProperties;

    public MerchantConverter(CashierDeskProperties cashierProperties) {
        this.cashierProperties = cashierProperties;
    }

    @Override
    public Merchant convert(MerchantDO merchantDO) {
        Merchant merchant = new Merchant();
        merchant.setMchId(merchantDO.getMchId());
        merchant.setName(merchantDO.getName());
        if (Objects.nonNull(merchantDO.getParam())) {
            merchant.setParams(JsonUtils.fromJsonString(merchantDO.getParam(), MerchantParams.class));
        } else {
            merchant.setParams(new MerchantParams());
        }
        MerchantParams params = merchant.getParams();

        if (Objects.isNull(params.getTokenTimeout())) {
            params.setTokenTimeout(cashierProperties.getTokenTimeout());
        }
        if (Objects.isNull(params.getCashier())) {
            params.setCashier(new MerchantParams.CashierParams());
        }
        params.getCashier().override(cashierProperties.getCashier());

        return merchant;
    }
}