package com.cool.store.service.wallet;

import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.wallet.WalletTradeModuleEnum;
import com.cool.store.exception.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 钱包交易业务处理工厂
 * </p>
 *
 * @author wangff
 * @since 2026/2/4
 */
@Service
public class WalletTradeResolveFactory {
    @Resource
    private IWalletTradeResolve[] resolves;

    private final Map<WalletTradeModuleEnum, IWalletTradeResolve> resolveMap = new HashMap<>();

    @PostConstruct
    public void init() {
        for (IWalletTradeResolve resolve : resolves) {
            resolveMap.put(resolve.getModule(), resolve);
        }
    }

    public IWalletTradeResolve getResolve(WalletTradeModuleEnum module) {
        IWalletTradeResolve resolve = resolveMap.get(module);
        if (Objects.isNull(resolve)) {
            throw new ServiceException(ErrorCodeEnum.NOT_EXIST_CALLBACK_RESOLVE);
        }
        return resolve;
    }
}
