package com.yingpaipay.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yingpaipay.business.domain.Project;
import com.yingpaipay.business.mapper.ProjectMapper;
import com.yingpaipay.system.domain.SysUserFolders;
import com.yingpaipay.system.domain.SysUserProjects;
import com.yingpaipay.system.mapper.SysUserProjectsMapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CommonProjectService {

    private final ProjectMapper baseMapper;
    private final SysUserProjectsMapper userProjectsMapper;

    public List<Long> queryIds(String projectCode, String name) {
        List<Long> ids = new ArrayList<>();
        SysUserProjects userProjects = userProjectsMapper.selectOne(Wrappers.lambdaQuery(SysUserProjects.class).eq(SysUserProjects::getUserId, LoginHelper.getUserId()));
        if (userProjects == null) {
            return List.of();
        }
        LambdaQueryWrapper<Project> wrapper = Wrappers.lambdaQuery(Project.class)
            .like(StringUtils.isNotBlank(projectCode), Project::getCode, projectCode)
            .like(StringUtils.isNotBlank(name), Project::getName, name);

        // 因为Arrays代码会前置执行，所以使用condition作为判断会抛出异常
        if (!userProjects.getProjects().equals("*")) {
            wrapper.in(Project::getId, Arrays.stream(userProjects.getProjects().split(",")).map(Long::valueOf).toList());
        }

        baseMapper.selectList(wrapper).forEach(e -> ids.add(e.getId()));
        return ids;
    }

    public List<Project> queryOnSearch(String name, String code) {
        return baseMapper.selectList(
            Wrappers.lambdaQuery(Project.class)
                .like(StringUtils.isNotBlank(name), Project::getName, name)
                .like(StringUtils.isNotBlank(code), Project::getCode, code)
        );
    }

    public Project queryById(Long projectId) {
        return baseMapper.selectById(projectId);
    }

    public List<Long> queryIdsOnFiling(String projectCode, String name) {
        List<Long> ids = new ArrayList<>();
        SysUserProjects userProjects = userProjectsMapper.selectOne(Wrappers.lambdaQuery(SysUserProjects.class).eq(SysUserProjects::getUserId, LoginHelper.getUserId()));
        if (userProjects == null) {
            return List.of();
        }
        LambdaQueryWrapper<Project> wrapper = Wrappers.lambdaQuery(Project.class)
            .like(StringUtils.isNotBlank(projectCode), Project::getCode, projectCode)
            .like(StringUtils.isNotBlank(name), Project::getName, name);

        // 因为Arrays代码会前置执行，所以使用condition作为判断会抛出异常
        if (!userProjects.getProjects().equals("*")) {
            wrapper.in(Project::getId, Arrays.stream(userProjects.getProjects().split(",")).map(Long::valueOf).toList());
        }

        baseMapper.selectList(wrapper).forEach(e -> ids.add(e.getId()));
        return ids;
    }

    public List<Project> queryByIds(List<Long> ids) {
        if (ids.isEmpty()) {
            return Collections.emptyList();
        }
        return baseMapper.selectList(
            Wrappers.lambdaQuery(Project.class)
                .in(Project::getId, ids)
        );
    }

    public List<Project> queryListByName(String name) {

        SysUserProjects projects = userProjectsMapper.selectOne(Wrappers.lambdaQuery(SysUserProjects.class).eq(SysUserProjects::getUserId, LoginHelper.getUserId()));
        if (projects == null) {
            return Collections.emptyList();
        }
        if (projects.getProjects().isEmpty()) {
            return Collections.emptyList();
        }

        // 因为Arrays代码会前置执行，所以使用condition作为判断会抛出异常
        List<Long> projectIds = new ArrayList<>();
        if (!projects.getProjects().equals("*")) {
            projectIds = Arrays.stream(projects.getProjects().split(",")).map(Long::valueOf).toList();
        }

        return baseMapper.selectList(
            Wrappers.lambdaQuery(Project.class)
                .in(!projects.getProjects().equals("*"), Project::getId, projectIds)
                .like(StringUtils.isNotBlank(name), Project::getName, name)
        );
    }
}
