package org.jeecg.modules.wps.controller;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.orderplatform.service.IPlatformScriptService;
import org.jeecg.modules.wps.base.Response;
import org.jeecg.modules.wps.dto.FileListDTO;
import org.jeecg.modules.wps.service.IWpsFileService;
import org.jeecg.modules.wps.util.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author zm
 * 用户实现获取wps可预览URL
 */
@Slf4j
@RestController
@RequestMapping("v1/api/file")
public class UserImplController{

    @Autowired
    private IWpsFileService wpsFileService;

    /**
     * 获取网络文件预览URL
     * @param fileUrl fileUrl
     * @return t
     */
    @GetMapping("getViewUrlWebPath")
    public ResponseEntity<Object> getViewUrlWebPath(String fileUrl){
        log.info("getViewUrlWebPath：fileUrl={}",fileUrl);
        Token t = wpsFileService.getViewUrl(fileUrl,true);
        return Response.success(t);
    }

    /**
     * 获取所有文件
     * @return db下的所有文件
     */
    @GetMapping("getFileList")
    public ResponseEntity<Object> getFileList(){
        log.info("获取所有文件");
        List<FileListDTO> result = wpsFileService.getFileList();
        return Response.success(result);
    }

    /**
     * 获取所有文件
     * @return db下的所有文件
     */
    @PostMapping("getFileListByPage")
    public ResponseEntity<Object> getFileListByPage(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
        Map<String,Object> result = wpsFileService.getFileListByPage(pageNo,pageSize);
        return Response.success(result);
    }

    /**
     * 删除用户文件
     * @return true Or false
     */
    @GetMapping("delFile")
    public ResponseEntity<Object> delFile(String id){
        log.info("删除文件：{}",id);
        int res = wpsFileService.delFile(id);
        if (res == 1){
            return Response.success(true,"删除成功");
        }else if (res == 0){
            return Response.success(false,"删除失败，该文件不允许被删除");
        }else {
            return Response.success(false,"数据异常");
        }
    }

    /**
     * 通过fileId获取wpsUrl以及token
     * @param fileId 文件id
     * @return token（包含url）
     */
    @GetMapping("getViewUrlDbPath")
    public ResponseEntity<Object> getViewUrlDbPath(String fileId, String userId){
        log.info("getViewUrlDbPath：fileId={},userId={}",fileId,userId);
        Token t = wpsFileService.getViewUrl(fileId,userId,true);
        if (t != null){
            return Response.success(t);
        }else {
            return Response.bad("文件不存在或其它异常！");
        }
    }

    /**
     * 通过fileId获取wpsUrl以及token
     * @param fileId 文件id
     * @return token（包含url）
     */
    @GetMapping("getPlatformScriptViewUrlDbPath")
    public ResponseEntity<Object> getPlatformScriptViewUrlDbPath(String fileId, String userId){
        log.info("getViewUrlDbPath：fileId={},userId={}",fileId,userId);
        Token t = wpsFileService.getPlatformScriptViewUrl(fileId,userId,true);
        if (t != null){
            return Response.success(t);
        }else {
            return Response.bad("文件不存在或其它异常！");
        }
    }

}
