package com.hz.pm.api.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.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hz.pm.api.common.model.constant.BizConst;
import com.hz.pm.api.common.model.constant.BooleanBit;
import com.hz.pm.api.projectlib.mapper.ProjectMapper;
import com.hz.pm.api.projectlib.model.entity.Project;
import com.hz.pm.api.projectlib.model.po.ProjectPO;
import com.hz.pm.api.projectlib.model.req.ProjectListReq;
import com.hz.pm.api.projectlib.service.IProjectService;
import lombok.RequiredArgsConstructor;
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
@RequiredArgsConstructor
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {

    private final ProjectMapper projectMapper;

    @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 Collections.emptyList();
        }
        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);

        return getOne(last);
    }

    @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, BooleanBit.TRUE)
                .eq(Project::getProjectCode, projectCode)
                .eq(Project::getIsConstruct, BooleanBit.TRUE)
                .last(BizConst.LIMIT_1);

        return getOne(last);
    }

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

    @Override
    public Page<ProjectPO> pageAllWithPermission(Page<ProjectPO> page, ProjectListReq req) {
        return projectMapper.pageAllWithPermission(page, req);
    }

    @Override
    public Page<ProjectPO> pageLibWithPermission(Page<ProjectPO> page, ProjectListReq req) {
        return projectMapper.pageLibWithPermission(page, req);
    }
}
