package cn.cslg.pas.controller;


import cn.cslg.pas.common.core.annotation.Permission;
import cn.cslg.pas.common.core.base.Constants;
import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.pas.common.utils.auth.checkAuth;
import cn.cslg.pas.domain.PatentInstruction;
import cn.cslg.pas.service.PatentInstructionService;
import cn.cslg.pas.service.TaskService;
import cn.cslg.pas.common.utils.FileUtils;
import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.common.model.vo.PatentInstructionVO;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * <p>
 * 专利说明书 前端控制器
 * </p>
 *
 * @author 王岩
 * @since 2022-03-02
 */
@Tag(name = "专利说明书")
@RestController
@RequestMapping(Constants.API_VERSION_V2 + "/patent/instruction")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class PatentInstructionController {

    private final PatentInstructionService patentInstructionService;
    private final TaskService taskService;
    private final FileUtils fileUtils;
    private final LoginUtils loginUtils;

    @GetMapping("list")
    @Operation(summary = "说明书列表")
    public String getPageList(PatentInstructionVO params) {
        return Response.success(patentInstructionService.getPageList(params));
    }

    @checkAuth(FunId = "/workspace/folder/batchUploadSpecification")
    @PostMapping("/batch/upload")
    @Operation(summary = "批量上传说明书")
    public String batchUpload(String url, Integer type, String remark) throws IOException {
        patentInstructionService.batchUpload(url, type, remark, loginUtils.getId());
        return Response.success(true);
    }

    @Permission(roles = {1, 2})
    @PostMapping("add")
    @Operation(summary = "新增说明书")
    public String add(MultipartFile file, PatentInstruction patentInstruction) {
        return patentInstructionService.add(file, patentInstruction);
    }

    @checkAuth(FunId = "/workspace/details/updataInstruction")
    @PostMapping("edit")
    @Operation(summary = "编辑说明书")
    public String edit(MultipartFile file, PatentInstruction patentInstruction) {
        return patentInstructionService.edit(file, patentInstruction);
    }

    @Permission(roles = {1, 2})
    @PostMapping("delete")
    @Operation(summary = "删除说明书")
    public String delete(Integer id) {
        return patentInstructionService.delete(id);
    }

    @PostMapping("file")
    @Operation(summary = "获取说明书文件流")
    public void pdfFile(String patentNo, Integer type, HttpServletResponse response) {
        try {
            PatentInstruction patentInstruction = patentInstructionService.getPatentInstructionByPatentNoAndType(patentNo, type);
            if (patentInstruction != null) {
                response.setHeader("Content-Disposition", "attachment;filename=" + patentInstruction.getFileName());
                ServletOutputStream out = response.getOutputStream();
//                out.write(FileUtil.readBytes(fileUtils.getSystemPath(patentInstruction.getUrl())));
                String fil =fileUtils.getSystemPath()+patentInstruction.getUrl();
                out.write(FileUtil.readBytes(fil));
                IoUtil.close(out);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

