package top.tqx.demo_1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.tqx.demo_1.constant.CommonConstant;
import top.tqx.demo_1.entity.File;
import top.tqx.demo_1.entity.FilePermission;
import top.tqx.demo_1.entity.User;
import top.tqx.demo_1.mapper.FileMapper;
import top.tqx.demo_1.mapper.FilePermissionMapper;
import top.tqx.demo_1.mapper.UserMapper;
import top.tqx.demo_1.service.DataPermissionService;
import top.tqx.demo_1.service.DepartmentService;

import java.util.ArrayList;
import java.util.List;

@Service
public class DataPermissionServiceImpl implements DataPermissionService {

    @Autowired
    private FileMapper fileMapper;

    @Autowired
    private FilePermissionMapper filePermissionMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DepartmentService departmentService;

    @Override
    public List<Long> getUserAuthorizedFileIds(Long userId) {
        List<Long> authorizedFileIds = new ArrayList<>();

        User user = userMapper.selectById(userId);
        if (user == null) {
            return authorizedFileIds;
        }

        if (CommonConstant.RoleType.SUPER_ADMIN.equals(user.getUserType())) {
            LambdaQueryWrapper<File> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(File::getStatus, CommonConstant.STATUS_ENABLE);
            List<File> files = fileMapper.selectList(wrapper);
            for (File file : files) {
                authorizedFileIds.add(file.getId());
            }
            return authorizedFileIds;
        }

        if (CommonConstant.RoleType.DEPT_ADMIN.equals(user.getUserType())) {
            List<Long> deptIds = getAllSubDepartmentIds(user.getDeptId());
            deptIds.add(user.getDeptId());
            
            LambdaQueryWrapper<File> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(File::getStatus, CommonConstant.STATUS_ENABLE);
            wrapper.in(File::getDeptId, deptIds);
            List<File> files = fileMapper.selectList(wrapper);
            for (File file : files) {
                authorizedFileIds.add(file.getId());
            }
        } else {
            LambdaQueryWrapper<File> ownFilesWrapper = new LambdaQueryWrapper<>();
            ownFilesWrapper.eq(File::getStatus, CommonConstant.STATUS_ENABLE);
            ownFilesWrapper.eq(File::getUploadUserId, userId);
            List<File> ownFiles = fileMapper.selectList(ownFilesWrapper);
            for (File file : ownFiles) {
                authorizedFileIds.add(file.getId());
            }

            LambdaQueryWrapper<FilePermission> permissionWrapper = new LambdaQueryWrapper<>();
            permissionWrapper.eq(FilePermission::getUserId, userId);
            List<FilePermission> permissions = filePermissionMapper.selectList(permissionWrapper);
            for (FilePermission permission : permissions) {
                authorizedFileIds.add(permission.getFileId());
            }
        }

        return authorizedFileIds;
    }

    @Override
    public List<Long> getAllSubDepartmentIds(Long deptId) {
        List<Long> subDeptIds = new ArrayList<>();
        findSubDepartments(deptId, subDeptIds);
        return subDeptIds;
    }

    private void findSubDepartments(Long deptId, List<Long> subDeptIds) {
        LambdaQueryWrapper<top.tqx.demo_1.entity.Department> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(top.tqx.demo_1.entity.Department::getParentId, deptId);
        wrapper.eq(top.tqx.demo_1.entity.Department::getStatus, CommonConstant.STATUS_ENABLE);
        
        List<top.tqx.demo_1.entity.Department> subDepartments = departmentService.list(wrapper);
        for (top.tqx.demo_1.entity.Department dept : subDepartments) {
            subDeptIds.add(dept.getId());
            findSubDepartments(dept.getId(), subDeptIds);
        }
    }
}
