package cn.cslg.pas.controller;

import cn.cslg.pas.common.core.base.Constants;
import cn.cslg.pas.common.model.vo.ProjectExportVO;
import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.pas.common.utils.auth.checkAuth;
import cn.cslg.pas.domain.ProjectExport;
import cn.cslg.pas.service.ProjectExportService;
import cn.cslg.pas.service.ProjectService;
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 java.io.IOException;

/**
 * <p>
 * 专题库 前端控制器
 * </p>
 *
 * @author 王岩
 * @since 2021-12-24
 */
@Tag(name = "专题库数据导出")
@RestController
@RequestMapping(Constants.API_VERSION_V2 + "/project/export")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ProjectExportController {

    private final ProjectService projectService;
    private final ProjectExportService projectExportService;
    private final LoginUtils loginUtils;

    @PostMapping
    @Operation(summary = "数据导出")
    public String export(@RequestBody ProjectExportVO params) {
        ProjectExport projectExport = new ProjectExport();
        projectExport.setProjectId(params.getProjectId());
        projectExport.setStatus(0);
        Integer exportId = projectExportService.add(projectExport);
        projectService.export(params, exportId, loginUtils.getId());
        return Response.success(true);
    }

    @GetMapping("list")
    @Operation(summary = "导出记录列表")
    public String getExportPageList(ProjectExportVO params) throws IOException {
        return Response.success(projectExportService.getPageList(params));
    }

    @checkAuth(FunId = "/workspace/folder/topicLibraryExport/delete")
    @PostMapping("delete")
    @Operation(summary = "删除导出记录")
    public String deleteExport(Integer id) {
        projectExportService.delete(id);
        return Response.success(true);
    }

}
