package com.ghy.web.core.adapay;

import com.ghy.common.adapay.AdapayConfig;
import com.ghy.common.adapay.AdapayProperties;
import com.ghy.common.adapay.model.Merchant;
import com.ghy.system.domain.SysDeptConfig;
import com.ghy.system.service.ISysDeptConfigService;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 初始化 Adapay
 *
 * @author HH 2022/5/21
 */
@Component
public class InitAdapay {

    @Resource
    AdapayProperties adapayProperties;
    @Resource
    ISysDeptConfigService deptConfigService;

    @PostConstruct
    public void initAdapay() throws Exception {

        List<SysDeptConfig> deptConfigs = deptConfigService.selectAllMerchant();

        Set<Merchant> merchants = deptConfigs.stream().map(x -> {
            Merchant merchant = new Merchant();
            merchant.setDeptId(x.getDeptId());
            merchant.setAppId(x.getAdapayAppId());
            merchant.setApiKey(x.getAdapayApiKey());
            merchant.setMockApiKey(x.getAdapayMockApiKey());
            merchant.setRsaPrivateKey(x.getAdapayRsaPrivateKey());
            return merchant;
        }).collect(Collectors.toSet());

        AdapayConfig.setMerchants(merchants);

        AdapayConfig.initAdapay(adapayProperties);
    }
}
