package cn.iocoder.yudao.module.system.controller.admin.file;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.controller.admin.file.vo.FileRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.file.FileService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 文件管理")
@RestController
@RequestMapping("/system/file")
@Validated
public class FileController {

    @Resource
    private FileService fileService;

    @Resource
    private AdminUserService userService;

    @GetMapping("/getFileByUserId")
    @Operation(summary = "获得指定用户的文件")
    @Parameter(name = "userId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:labor-contract:query')")
    public CommonResult<List<FileRespVO>> getFileList(@RequestParam("userId") Long userId) {

        AdminUserDO userDO = userService.getUser(userId);
        List<FileRespVO> fileRespVOS = fileService.getFileList(userDO.getMobile());
        fileRespVOS = fileRespVOS.stream().filter(item -> item.getFileType().contains("pdf") || item.getFileType().contains("doc")).collect(Collectors.toList());

        return success(fileRespVOS);
    }
}
