package com.ningdatech.pmapi.projectlib.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.pmapi.common.constant.BizConst;
import com.ningdatech.pmapi.performance.model.entity.ProjectCoreBusinessIndicators;
import com.ningdatech.pmapi.projectlib.model.entity.Project;
import com.ningdatech.pmapi.projectlib.mapper.ProjectMapper;
import com.ningdatech.pmapi.projectlib.model.entity.ProjectApplication;
import com.ningdatech.pmapi.projectlib.service.IProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WendyYang
 * @since 2023-02-03
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {

    @Override
    public List<Long> getAllVersionProjectId(Project project) {
        //查出 项目code
        String projectCode = project.getProjectCode();

        List<Project> projects = list(Wrappers.lambdaQuery(Project.class)
                .eq(Project::getProjectCode, projectCode));

        if(CollUtil.isEmpty(projects)){
            return Collections.emptyList();
        }

        return projects.stream().map(Project::getId).collect(Collectors.toList());
    }

    @Override
    public List<Long> getAllVersionProjectId(Long projectId) {
        Project project = getById(projectId);

        if(Objects.isNull(project)){
            return null;
        }

        return getAllVersionProjectId(project);
    }

    @Override
    public Project getNewProject(Long projectId) {
        Project project = getById(projectId);

        if(Objects.isNull(project)){
            return null;
        }

        String projectCode = project.getProjectCode();

        LambdaQueryWrapper<Project> last = Wrappers.lambdaQuery(Project.class)
                .eq(Project::getNewest, Boolean.TRUE)
                .eq(Project::getProjectCode, projectCode)
                .last(BizConst.LIMIT_1);
        Project newest = getOne(last);

        return newest;
    }

    @Override
    public Project getNewConstructProject(Long projectId) {
        Project project = getById(projectId);

        if(Objects.isNull(project)){
            return null;
        }

        String projectCode = project.getProjectCode();

        LambdaQueryWrapper<Project> last = Wrappers.lambdaQuery(Project.class)
                .eq(Project::getNewest, Boolean.TRUE)
                .eq(Project::getProjectCode, projectCode)
                .eq(Project::getIsConstruct, Boolean.TRUE)
                .last(BizConst.LIMIT_1);
        Project newest = getOne(last);

        return newest;
    }

    @Override
    public Project getProjectByCode(String projectCode) {
        Project project = this.getOne(Wrappers.lambdaQuery(Project.class)
                .eq(Project::getProjectCode, projectCode)
                .eq(Project::getNewest,Boolean.TRUE)
                .last(BizConst.LIMIT_1));
        return project;
    }
}
