package com.css.simulation.resource.common.domain.service.algorithm;

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.constants.DictConstants;
import api.common.util.CollectionUtil;
import api.common.util.JsonUtil;
import com.css.simulation.resource.common.infra.acl.entity.AlgorithmExpandAclEntity;
import com.css.simulation.resource.common.infra.mysql.entity.AlgorithmExpandEntity;
import com.css.simulation.resource.common.infra.mysql.mapper.AlgorithmExpandMapper;
import com.css.simulation.resource.common.infra.mysql.param.AlgorithmExpandInfrastructureParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class AlgorithmExpandDomainService {

    @Resource
    private AlgorithmExpandMapper algorithmExpandMapper;

    public void checkIsExisted(ResponseBodyVO<String> algorithmBody) {
        final String algorithmListResponseJson = algorithmBody.getInfo();
        log.info("算法平台请求结果为：{}", algorithmListResponseJson);
        final String algorithmListJson = JsonUtil.getString(algorithmListResponseJson, "data.content");
        log.info("算法平台的算法列表JSON为：{}", algorithmListJson);
        final List<AlgorithmExpandAclEntity> algorithmExpandAclEntities = JsonUtil.jsonToList(algorithmListJson, AlgorithmExpandAclEntity.class);
        log.info("算法平台的算法列表为：{}", algorithmExpandAclEntities);
        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(AlgorithmExpandInfrastructureParam.builder().algorithmIds(aclAlgorithmIds).build());
            //2 不存在的数据插入数据库并设置状态为“未测试”
            algorithmExpandAclEntities.forEach(algorithmExpandAclEntity -> {
                final String algorithmId = algorithmExpandAclEntity.getAlgorithmId();
                if (infrastructureAlgorithmIds.contains(algorithmId)) {
                    log.info("更新算法平台算法。");
                    final List<AlgorithmExpandEntity> AlgorithmExpandInfrastructureEntities = algorithmExpandMapper.select(AlgorithmExpandInfrastructureParam.builder().algorithmId(algorithmId).build());
                    final AlgorithmExpandEntity algorithmExpandEntity = AlgorithmExpandInfrastructureEntities.get(0);
                    // status 不能被覆盖
//                    BeanUtils.copyProperties(algorithmExpandAclEntity, algorithmExpandEntity);
                    {
                        algorithmExpandEntity.setId(algorithmExpandAclEntity.getId());
                        algorithmExpandEntity.setAlgorithmId(algorithmExpandAclEntity.getAlgorithmId());
                        algorithmExpandEntity.setAlgorithmName(algorithmExpandAclEntity.getAlgorithmName());
                        algorithmExpandEntity.setDescription(algorithmExpandAclEntity.getDescription());
                        algorithmExpandEntity.setAlgorithmType(algorithmExpandAclEntity.getAlgorithmType());
                        algorithmExpandEntity.setAlgorithmVersion(algorithmExpandAclEntity.getAlgorithmVersion());
                        algorithmExpandEntity.setTeam(algorithmExpandAclEntity.getTeam());
                        algorithmExpandEntity.setTopic(algorithmExpandAclEntity.getTopic());
                    }
                    algorithmExpandMapper.update(algorithmExpandEntity);
                } else {
                    log.info("插入算法平台算法。");
                    final AlgorithmExpandEntity algorithmExpandEntity = new AlgorithmExpandEntity();
                    BeanUtils.copyProperties(algorithmExpandAclEntity, algorithmExpandEntity);
                    algorithmExpandEntity.setStatus(DictConstants.ALGORITHM_EXPAND_STATUS_NOT_TESTED);
                    algorithmExpandMapper.insert(algorithmExpandEntity);
                }
            });
        }
    }


}
