package com.ruoyi.project.office;

import com.ruoyi.common.core.controller.BaseController;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@Component
@RestController
@RequestMapping(value = "/onlyoffice", method = RequestMethod.POST)
public class OnlyofficeController extends BaseController {
//
//    protected final Logger logger = LoggerFactory.getLogger(BaseController.class);
//    @Autowired
//    private ISysUserService sysUserService;
//
//    @Autowired
//    private FileConfigurer<DefaultFileWrapper> fileConfigurer;
//
//    @Value("${deployment.host}")
//    private String deploymentHost;
//    @Value("${server.port}")
//    private String port;
//
//    @Value("${files.docservice.url.site}")
//    private String docserviceSite;
//
//    @Value("${files.docservice.url.api}")
//    private String docserviceApiUrl;
//
//    @Autowired
//    private ITFileService tFileService;
//
//    @PostMapping("/previewFile")
//    public AjaxResult preview(HttpServletRequest request, @RequestBody PreviewOfficeFileDTO previewOfficeFileDTO) {
//        //获取当前用户
//        SysUser sysUser = sysUserService.selectUserById(getUserId());
//        Action action = Action.view;
//        Type type = Type.desktop;
//        Locale locale = new Locale("zh");
//
//        User user = new User(sysUser);
//        //文件
//        UserFile userFile = new UserFile();
//        TFile rcfile = tFileService.selectTFileById(Long.parseLong(previewOfficeFileDTO.getUserFileId()));
//        userFile.setFileId(rcfile.getId().toString());
//        userFile.setFilePath(rcfile.getFileUrl());
//        userFile.setFileName(rcfile.getFileName());
//        userFile.setExtendName("");
//        userFile.setUserFileId(rcfile.getId().toString());
//        userFile.setUploadTime(String.valueOf(rcfile.getUploadDate().getTime()));
//
//        //下载地址
//        String previewUrl = deploymentHost + rcfile.getFileUrl();
////        String previewUrl = "/onlyoffice/download?filePath=" + rcfile.getFileUrl();
//
//
//        FileModel fileModel = fileConfigurer.getFileModel(
//                DefaultFileWrapper
//                        .builder()
//                        .userFile(userFile)
//                        .type(type)
//                        .lang(locale.toLanguageTag())
//                        .action(action)
//                        .user(user)
//                        .actionData(previewUrl)
//                        .build()
//        );
//        fileModel.setToken(null);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("file", fileModel);
//        jsonObject.put("docserviceApiUrl", docserviceSite + docserviceApiUrl);
//        jsonObject.put("reportName", userFile.getFileName());
//        return AjaxResult.success(jsonObject);
//    }
//
///*    @PostMapping("/editFile")
//
//    public AjaxResult edit(HttpServletRequest request,  @RequestBody PreviewOfficeFileDTO previewOfficeFileDTO) {
//
//        //
//        SysUser sysUser = sysUserService.selectUserById(getUserId());
//        Action action = Action.edit;
//        Type type = Type.desktop;
//        Locale locale = new Locale("zh");
//
//        User user = new User(sysUser);
//        //文件
//        UserFile userFile = new UserFile();
//        TRcfile rcfile = tRcfileService.selectTRcfileById(Long.parseLong(previewOfficeFileDTO.getUserFileId()));
//        userFile.setFileId(rcfile.getId().toString());
//        userFile.setFilePath(rcfile.getFileUrl());
//        userFile.setFileName(rcfile.getFileName());
//        userFile.setExtendName("");
//        userFile.setUserFileId(rcfile.getId().toString());
//        userFile.setUploadTime(String.valueOf(rcfile.getUpdatedate().getTime()));
//
//        String previewUrl = "/onlyoffice/download?filePath=" + rcfile.getFileUrl();
//
//
//        FileModel fileModel = fileConfigurer.getFileModel(
//                DefaultFileWrapper
//                        .builder()
//                        .userFile(userFile)
//                        .type(type)
//                        .lang(locale.toLanguageTag())
//                        .action(action)
//                        .user(user)
//                        .actionData(previewUrl)
//                        .build()
//        );
//        fileModel.setToken(null);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("file",fileModel);
//        jsonObject.put("docserviceApiUrl", docserviceSite + docserviceApiUrl);
//        jsonObject.put("reportName",userFile.getFileName());
//        return  AjaxResult.success(jsonObject);
//    }
//*/
//
///*    @RequestMapping(value = "/IndexServlet", method = RequestMethod.POST)
//    @ResponseBody
//    public void IndexServlet(HttpServletResponse response, HttpServletRequest request) throws IOException {
//        logger.info(JSON.toJSONString(request.getParameterMap()));
//        String token = request.getParameter("token");
//
//        PrintWriter writer = response.getWriter();
//        Scanner scanner = new Scanner(request.getInputStream()).useDelimiter("\\A");
//        String body = scanner.hasNext() ? scanner.next() : "";
//        JSONObject jsonObj = JSON.parseObject(body);
//        logger.info(jsonObj.toJSONString());
//        logger.info("===saveeditedfile:" + jsonObj.get("status"));
//        String status = jsonObj != null ? jsonObj.get("status").toString() : "";
//        if ("2".equals(status) || "6".equals(status)) {
//            String type = request.getParameter("type");
//            String downloadUri = (String) jsonObj.get("url");
//            if("edit".equals(type)){ //修改报告
//                String userFileId = request.getParameter("userFileId");
//                TRcfile rcfile = tRcfileService.selectTRcfileById(Long.parseLong( userFileId ));
//
//                String pathForSave = RuoYiConfig.getProfile() + rcfile.getFileUrl().replace("/profile","");
//                URL url = new URL(downloadUri);
//                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//
//                InputStream stream = connection.getInputStream();
//
//                File savedFile = new File(pathForSave);
//                try (FileOutputStream out = new FileOutputStream(savedFile)) {
//                    int read;
//                    final byte[] bytes = new byte[1024];
//                    while ((read = stream.read(bytes)) != -1) {
//                        out.write(bytes, 0, read);
//                    }
//                    out.flush();
//                }
//                connection.disconnect();
//                //更新文件时间
//                rcfile.setUpdatedate(new Date());
//                tRcfileService.updateTRcfile(rcfile);
//            }
//        }
//        writer.write("{\"error\":" + "0" + "}");
//    }*/
//
//    /**
//     *
//     * @param response
//     */
///*    @GetMapping("/download")
//    public void download(UserFile userFile, HttpServletResponse response) {
//        try {
//            String newFilePath = userFile.getFilePath().replace("/profile","");
//            // 需要转换的文件路径
//           String pathName = RuoYiConfig.getProfile()+newFilePath;
//            logger.info("pathName::::::::::" + pathName);
//            String fileName = pathName.substring(pathName.lastIndexOf("/") + 1);
//            logger.info("fileName::::::::::" + fileName);
//            FileUtils.downloadInline(pathName, fileName,response);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//
//    }*/
//

}
