package cn.reghao.autodop.dmaster.app.service.impl;

import cn.reghao.autodop.dmaster.app.db.query.config.AppConfigQuery;
import cn.reghao.autodop.dmaster.app.model.po.AppBuilding;
import cn.reghao.autodop.dmaster.app.model.po.AppDeploying;
import cn.reghao.autodop.dmaster.app.model.po.AppRunning;
import cn.reghao.autodop.dmaster.app.model.po.config.AppConfig;
import cn.reghao.autodop.dmaster.app.db.repository.AppBuildingRepository;
import cn.reghao.autodop.dmaster.app.db.repository.AppDeployingRepository;
import cn.reghao.autodop.dmaster.app.db.repository.AppRunningRepository;
import cn.reghao.autodop.dmaster.app.service.BuildDeployConfigService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2021-06-04 10:01:41
 */
@Service
public class BuildDeployConfigServiceImpl implements BuildDeployConfigService {
    private AppConfigQuery appConfigQuery;
    private AppBuildingRepository buildingRepository;
    private AppDeployingRepository deployingRepository;
    private AppRunningRepository runningRepository;

    public BuildDeployConfigServiceImpl(AppConfigQuery appConfigQuery,
                                        AppBuildingRepository buildingRepository,
                                        AppDeployingRepository deployingRepository,
                                        AppRunningRepository runningRepository) {
        this.appConfigQuery = appConfigQuery;
        this.buildingRepository = buildingRepository;
        this.deployingRepository = deployingRepository;
        this.runningRepository = runningRepository;
    }

    /**
     * 刷新应用构建信息
     *
     * @param
     * @return
     * @date 2021-06-18 下午5:24
     */
    @Override
    public void refreshAppBuilding(AppConfig app) {
        AppBuilding appBuilding = buildingRepository.findByAppId(app.getAppId());
        if (appBuilding == null) {
            appBuilding = new AppBuilding(app);
        } else {
            appBuilding.refresh(app);
        }
        buildingRepository.save(appBuilding);
    }

    /**
     * 刷新应用部署信息
     *
     * @param
     * @return
     * @date 2021-06-18 下午5:25
     */
    @Override
    public void refreshAppDeploying(AppConfig app) {
        String appId = app.getAppId();
        List<AppDeploying> list = app.getDeployConfigs().stream()
                .map(deployConfig -> {
                    String machineId = deployConfig.getMachineId();
                    AppDeploying appDeploying = deployingRepository.findByAppIdAndMachineId(appId, machineId);
                    if (appDeploying == null) {
                        appDeploying = new AppDeploying(app, deployConfig);
                    } else {
                        appDeploying.refresh(app.getAppName(), deployConfig.getMachineIpv4());
                    }
                    return appDeploying;
                })
                .collect(Collectors.toList());
        deployingRepository.saveAll(list);
    }

    /**
     * 刷新应用运行信息
     *
     * @param
     * @return
     * @date 2021-06-18 下午5:29
     */
    @Override
    public void refreshAppRunning(AppConfig app) {
        String appId = app.getAppId();
        List<AppRunning> list = app.getDeployConfigs().stream()
                .map(deployConfig -> {
                    String machineId = deployConfig.getMachineId();
                    AppRunning appRunning = runningRepository.findByAppIdAndMachineId(appId, machineId);
                    if (appRunning == null) {
                        appRunning = new AppRunning(app, deployConfig);
                    } else {
                        appRunning.update(app, deployConfig.getMachineIpv4());
                    }
                    return appRunning;
                })
                .collect(Collectors.toList());
        runningRepository.saveAll(list);
    }

    @Override
    public void delete(String appId) {
        AppBuilding appBuilding = buildingRepository.findByAppId(appId);
        if (appBuilding != null) {
            buildingRepository.delete(appBuilding);
        }

        List<AppDeploying> appDeployings = deployingRepository.findByAppId(appId);
        if (!appDeployings.isEmpty()) {
            deployingRepository.deleteAll(appDeployings);
        }

        List<AppRunning> appRunnings = runningRepository.findByAppId(appId);
        if (!appRunnings.isEmpty()) {
            runningRepository.deleteAll(appRunnings);
        }
    }
}
