package com.yc.ship.module.ota.job;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yc.ship.framework.quartz.core.handler.JobHandler;
import com.yc.ship.framework.tenant.core.aop.TenantIgnore;
import com.yc.ship.framework.tenant.core.context.TenantContextHolder;
import com.yc.ship.framework.tenant.core.job.TenantJob;
import com.yc.ship.module.infra.api.config.ConfigApi;
import com.yc.ship.module.ota.dal.dataobject.distributor.DistributorDO;
import com.yc.ship.module.ota.service.distributor.DistributorService;
import com.yc.ship.module.ota.service.distributorselfrecharge.DistributorSelfRechargeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class AbcQueryJob implements JobHandler {

    @Resource
    private DistributorSelfRechargeService distributorSelfRechargeService;

    @Resource
    private DistributorService distributorService;

    @Resource
    private ConfigApi configApi;

    @Override
    @TenantIgnore
    public String execute(String param) {
        log.error(">>>>>>>>>>>>>>>>>>>>>> AbcQueryJob <<<<<<<<<<<<<<<<<<<<<<");
        List<DistributorDO> list = distributorService.selectChildNo();

        String host = configApi.getPlatformConfigValueByKey("dgConfig_ip");
        String portStr = configApi.getPlatformConfigValueByKey("dgConfig_port");
        String yqtCard = configApi.getPlatformConfigValueByKey("dgConfig_card");
        String day = configApi.getPlatformConfigValueByKey("dgConfig_day");
        String url = configApi.getPlatformConfigValueByKey("dgConfig_url");
        int ofday = 1;
        if (StringUtils.isNotBlank(day)) {
            try {
                ofday = Integer.parseInt(day);
            } catch (Exception ignored) {

            }
        }

        int port = 15999;
        if (StringUtils.isNotBlank(portStr)) {
            try {
                port = Integer.parseInt(portStr);
            } catch (Exception ignored) {

            }
        }

        int finalPort = port;
        int finalOfday = ofday;
        list.forEach(distributorDO -> {
            try {
                distributorSelfRechargeService.abcRemittance(distributorDO,host, finalPort, yqtCard, finalOfday,url);
            } catch (Exception e) {
                log.error(distributorDO.getId()+"查询失败",e);
            }
        });
        return "成功";
    }
}

