package cn.pluss.platform.channel;

import cn.pluss.platform.api.ResultCode;
import cn.pluss.platform.entity.MerchantRateNew;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.exception.RateNoChangeException;
import org.springframework.transaction.annotation.Transactional;

/**
 * 多种支付方式费率分开
 */
public interface MchChannelRateNewService {

    /**
     * 修改商户的费率
     */
    void before(String userId, MerchantRateNew merchantRate);

    /**
     * 渠道操作
     *
     * @param userId
     */
    void process(String userId, MerchantRateNew merchantRate);

    void after(String userId, MerchantRateNew merchantRate);

    @Transactional(rollbackFor = Exception.class)
    default void changeRate(String userId, MerchantRateNew rate, boolean checkFlag) {
        if (checkFlag) {
            try {
                before(userId, rate);
            } catch (RateNoChangeException e) {
                return;
            } catch (Exception e) {
                e.printStackTrace();
                throw new MsgException(ResultCode.SERVICE_UNAVAILABLE, e.getMessage(), null);
            }
        }

        process(userId, rate);

        after(userId, rate);
    }

    String getChannelId();

    MerchantRateNew getDefaultRate();
}
