package com.propertyManagement.pay.support.pay;

import cn.hutool.core.collection.CollUtil;
import com.propertyManagement.pay.domain.CmsPayApp;
import com.propertyManagement.pay.domain.CmsPayBiz;
import com.propertyManagement.pay.domain.CmsPayMerchant;
import com.propertyManagement.pay.domain.CmsPayProvider;
import com.propertyManagement.pay.service.IPayAppService;
import com.propertyManagement.pay.service.IPayBizService;
import com.propertyManagement.pay.service.IPayMerchantService;
import com.propertyManagement.pay.service.IPayProviderService;
import com.siashan.unipay4j.core.PayChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 支付渠道初始化加载
 *
 * @author siashan
 * @date 2023/5/5
 */
@Component
@Slf4j
public class PayLoader implements ApplicationRunner {


    @Autowired
    private IPayProviderService cmsPayProviderService;
    @Autowired
    private IPayMerchantService cmsPayMerchantService;
    @Autowired
    private IPayBizService cmsPayBizService;
    @Autowired
    private IPayAppService cmsPayAppService;


    /**
     * 程序启动时，获取最新的启用的短信通道
     * <p>
     * 这里即便缓存中为空也没关系，定时任务会定时重新加载短信通道
     *
     * @param args 启动参数
     */
    @Override
    public void run(ApplicationArguments args) {
        List<CmsPayBiz> payBizList = cmsPayBizService.listAllUsable();
        List<PayChannelDto> channelDtoList = new ArrayList<>();
        List<CmsPayProvider> payProviderList = cmsPayProviderService.listAllUsable();
        if (CollUtil.isNotEmpty(payProviderList)) {
            for (CmsPayProvider payProvider : payProviderList) {
                List<CmsPayMerchant> merchantList = cmsPayMerchantService.listAllUsable(payProvider.getProviderCode());
                PayChannelDto channelDto = new PayChannelDto();
                if (CollUtil.isNotEmpty(merchantList)) {
                    List<CmsPayMerchantDto> merchantDtoList = new ArrayList<>();
                    for (CmsPayMerchant merchant : merchantList) {
                        CmsPayMerchantDto merchantDto = new CmsPayMerchantDto();
                        BeanUtils.copyProperties(merchant, merchantDto);
                        List<CmsPayApp> payAppList = cmsPayAppService.listAllUsable(merchant.getMerchantCode());
                        if (CollUtil.isNotEmpty(payAppList)) {
                            merchantDto.setAppList(payAppList);
                        }
                        merchantDtoList.add(merchantDto);
                    }
                    channelDto.setMerchantList(merchantDtoList);
                }
                channelDto.setChannel(PayChannel.valueOf(payProvider.getProviderCode()));
                channelDtoList.add(channelDto);
            }
        }

        CmsUnipayProvider.init(payBizList, channelDtoList);
    }


}
