package io.wdd.server.coreService.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.wdd.server.beans.po.ProjectInfoPO;
import io.wdd.server.beans.po.ProjectServerRelationPO;
import io.wdd.server.beans.po.ServerInfoPO;
import io.wdd.server.beans.request.ProjectQueryEntity;
import io.wdd.server.beans.vo.ProjectServerVO;
import io.wdd.server.coreService.CoreProjectServerService;
import io.wdd.server.coreService.CoreProjectService;
import io.wdd.server.service.ProjectServerRelationService;
import io.wdd.server.service.ServerInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class CoreProjectServerServiceImpl implements CoreProjectServerService {

    @Resource
    CoreProjectService coreProjectService;

    @Resource
    ProjectServerRelationService projectServerRelationService;

    @Resource
    ServerInfoService serverInfoService;


    @Override
    public List<ProjectServerVO> projectServerAll() {

        return coreProjectService
                .projectGetAll()
                .stream()
                .map(
                        projectInfoPO -> this.projectServerOne(projectInfoPO.getProjectId())
                )
                .collect(Collectors.toList());

    }

    @Override
    public ProjectServerVO projectServerQuery(ProjectQueryEntity projectQueryEntity) {

        Page<ProjectInfoPO> projectInfoPOPage = coreProjectService.projectQueryByEntity(projectQueryEntity);
        if (projectInfoPOPage
                .getRecords()
                .size() == 0) {
            return null;
        }

        // 假定是唯一性查询
        ProjectInfoPO projectInfoPO = projectInfoPOPage
                .getRecords()
                .get(0);

        // 为了统一 再查一次

        return this.projectServerOne(projectInfoPO.getProjectId());
    }

    @Override
    public ProjectServerVO projectServerOne(Long projectId) {

        // 首先查询projectId是否存在 pass


        // 查询与之绑定的所有server
        // serverId projectId
        List<ProjectServerRelationPO> projectServerRelationPOList = new LambdaQueryChainWrapper<ProjectServerRelationPO>(projectServerRelationService.getBaseMapper())
                .eq(
                        ProjectServerRelationPO::getProjectId,
                        projectId
                )
                .list();

        if (CollectionUtils.isEmpty(projectServerRelationPOList)) {
            return null;
        }

        // 获取 projectId对应的所有serverId
        List<Long> serverIdList = projectServerRelationPOList
                .stream()
                .map(ps -> ps.getServerId())
                .collect(Collectors.toList());

        // 查询server详情
        List<ServerInfoPO> serverInfoPOS = serverInfoService.listByIds(serverIdList);

        // 查询
        ProjectInfoPO projectInfoPO = coreProjectService.projectGetOne(projectId);
        if (projectInfoPO == null) {
            return null;
        }

        // 构造返回结果
        ProjectServerVO projectServerVO = new ProjectServerVO();
        BeanUtils.copyProperties(
                projectInfoPO,
                projectServerVO
        );
        projectServerVO.setBindingServerList(serverInfoPOS);

        return projectServerVO;
    }

    @Override
    public boolean projectServerCreate(Long projectId, Long serverId) {

        ProjectServerRelationPO byId = projectServerRelationService.getById(serverId);

        if (byId != null && byId
                .getProjectId()
                .equals(projectId)) {
            log.debug("ProjectServer绑定关系已经存在！");
            return true;
        }

        // no exist then create
        // 判定 serverId 和 projectId是否合规
        if (coreProjectService.projectGetOne(projectId) == null || serverInfoService.getById(serverId) == null) {
            log.warn("传入的 projectId 或者 serverId 有误！");
            return false;
        }

        ProjectServerRelationPO projectServerRelationPO = new ProjectServerRelationPO();
        projectServerRelationPO.setProjectId(projectId);
        projectServerRelationPO.setServerId(serverId);

        return projectServerRelationService.save(projectServerRelationPO);
    }

    @Override
    public boolean projectServerCreateMulti(Long projectId, List<Long> serverIdList) {

        for (Long serverId : serverIdList) {
            if (!this.projectServerCreate(
                    projectId,
                    serverId
            )) {

                log.error("绑定失败！");
                return false;
            }
        }

        return true;
    }

    @Override
    public boolean projectServerUpdate(Long projectId, Long serverId) {
        return this.projectServerCreate(
                projectId,
                serverId
        );
    }

    @Override
    public boolean projectServerDelete(Long projectId, Long serverId) {

        ProjectServerRelationPO byId = projectServerRelationService.getById(serverId);

        if (byId != null && byId
                .getProjectId()
                .equals(projectId)) {

            return projectServerRelationService.removeById(serverId);
        }

        return true;
    }
}
