package com.css.simulation.resource.monitor.app.service.impl;

import api.common.util.CollectionUtil;
import com.css.simulation.resource.monitor.acl.entity.AlgorithmExpandAclEntity;
import com.css.simulation.resource.monitor.acl.service.AlgorithmExpandAclService;
import com.css.simulation.resource.monitor.app.service.AlgorithmExpandApplicationService;
import com.css.simulation.resource.monitor.infra.mysql.entity.AlgorithmExpandEntity;
import com.css.simulation.resource.monitor.infra.mysql.mapper.AlgorithmExpandMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class AlgorithmExpandApplicationServiceImpl implements AlgorithmExpandApplicationService {

    @Resource
    private AlgorithmExpandAclService algorithmExpandAclService;
    @Resource
    private AlgorithmExpandMapper algorithmExpandMapper;

    @Override
    public void syncAlgorithmExpand() {
        //1 查询全部算法列表
        final List<AlgorithmExpandAclEntity> algorithmExpandAclEntities = algorithmExpandAclService.getAlgorithmList();
        if (CollectionUtil.isNotEmpty(algorithmExpandAclEntities)) {
            //1 获取算法平台查询到的所有算法ID
            final List<String> aclAlgorithmIds = algorithmExpandAclEntities.stream().map(AlgorithmExpandAclEntity::getAlgorithmId).collect(Collectors.toList());
            log.info("算法平台的算法ID列表为：{}", aclAlgorithmIds);
            List<String> infrastructureAlgorithmIds = algorithmExpandMapper.selectAlgorithmIds(AlgorithmExpandEntity.builder().build());
            //2 仿真云存在而算法平台不存在，则将仿真云的数据删除
            for (String infrastructureAlgorithmId : infrastructureAlgorithmIds) {
                if (!aclAlgorithmIds.contains(infrastructureAlgorithmId)) {
                    algorithmExpandMapper.delete(AlgorithmExpandEntity.builder().algorithmId(infrastructureAlgorithmId).build());
                }
            }
        }
    }
}
