package com.tzy.common.dto.pay;

import lombok.Data;

import java.util.concurrent.atomic.AtomicInteger;

@Data
public class PaymentChannel {
    private String paymentSubType;
    private int weight;
    private AtomicInteger currentWeight=new AtomicInteger(0);

    public PaymentChannel(String paymentSubType, int weight) {
        this.paymentSubType = paymentSubType;
        this.weight = weight;
    }

    public int getCurrentWeight() {
        return currentWeight.get();
    }

    public void addCurrentWeight(int delta) {
        this.currentWeight.addAndGet(delta);
    }

    public void subtractCurrentWeight(int delta) {
        this.currentWeight.addAndGet(-delta);
    }

    public static PaymentChannel toPaymentChannel(PaymentParam payment) {
        Integer firstWeight = payment.getRandomRateList().get(0);
        if (firstWeight >= 10) {
            return new PaymentChannel(payment.getCode(), firstWeight);
        }
        return new PaymentChannel(payment.getCode(), payment.getRandomRateList().size() * 10);
    }
}