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.model.dto.ProjectCopyDTO;
import cn.cslg.pas.common.model.vo.ProjectVO;
import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.common.utils.StringUtils;
import cn.cslg.pas.common.utils.auth.checkAuth;
import cn.cslg.pas.domain.Project;
import cn.cslg.pas.domain.ProjectUser;
import cn.cslg.pas.exception.XiaoShiException;
import cn.cslg.pas.service.ProjectPatentLinkService;
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 org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 专题库 前端控制器
 * </p>
 *
 * @author 王岩
 * @since 2021-12-24
 */
@Tag(name = "专题库")
@RestController
@RequestMapping(Constants.API_VERSION_V2 + "/project")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ProjectController {
    private final ProjectService projectService;
    private final ProjectPatentLinkService projectPatentLinkService;

    @PostMapping("list")
    @checkAuth(FunId = "/workspace/project/check")
    @Operation(summary = "专题库列表")
    public String getPageList(@RequestBody ProjectVO params) throws IOException {
        return Response.success(projectService.getPageList(params));
    }

    @GetMapping("getProjectById")
    @Operation(summary = "根据Id获得专题库")
    public String getProject(Integer id) throws IOException {
        return Response.success(projectService.getProjectByIds(Arrays.asList(id)));
    }

    @checkAuth(FunId = "/workspace/project/add")
    @PostMapping("add")
    @Operation(summary = "新增专题库")
    public String add(@RequestBody Project project) throws IOException {
        Integer projectId;
        try {
            projectId = projectService.add(project);
        } catch (XiaoShiException e) {
            return Response.error("新增专题库失败，" + e.getMessage());
        }
        return Response.success(projectId);
    }

    @Permission(roles = {2})
    @checkAuth(FunId = "/workspace/project/modify")
    @PostMapping("edit")
    @Operation(summary = "编辑专题库")
    public String edit(@RequestBody Project project) throws IOException {
        try {
            projectService.edit(project);
        } catch (XiaoShiException e) {
            return Response.error(e.getMessage());
        }
        return Response.success();
    }

    @Permission(roles = {2})
    @PostMapping("delete")
    @checkAuth(FunId = "/workspace/project/delete")
    @Operation(summary = "删除专题库")
    public String delete(Integer id) {
        return projectService.delete(id);
    }


    @checkAuth(FunId = "/workspace/project/delete")
    @PostMapping("/patent/delete")
    @Operation(summary = "删除专题库专利")
    public String deletePatent(Integer projectId, String ids) {
        List<Integer> patentIds = StringUtils.changeStringToInteger(ids, ",");
        projectPatentLinkService.deleteByProjectIdAndPatentIds(projectId, patentIds);
        return Response.success(true);
    }

    @GetMapping("total")
    @Operation(summary = "专题库企业应用场景统计数据")
    public String getScenarioAndTypeTotal() throws IOException {
        return Response.success(projectService.getScenarioAndTypeTotal());
    }

    @GetMapping("/status/total")
    @Operation(summary = "专题库状态统计数据")
    public String getProjectStatusTotal() throws IOException {
        return Response.success(projectService.getProjectStatusTotal());
    }

    @GetMapping("/type/total")
    @Operation(summary = "专题库调查类型统计数据")
    public String getProjectTypeTotal(String scenario) throws IOException {
        return Response.success(projectService.getProjectTypeTotal(scenario));
    }

    @GetMapping("chartData")
    @Operation(summary = "专题库图表统计数据")
    public String getChartData() {
        return Response.success();
    }

    @Permission(roles = {2})
    @PostMapping("share")
    @checkAuth(FunId = "/workspace/project/project_share")
    @Operation(summary = "分享专题库")
    public String share(Integer id, @RequestBody List<ProjectUser.User> users) {
        List<Integer> userIds = new ArrayList<>();
        users.forEach(item -> userIds.add(item.getUserId()));
        return projectService.share(id, userIds, users);
    }

    @checkAuth(FunId = "/workspace/project/import/Excel_template")
    @PostMapping("importExcel")
    @Operation(summary = "根据模板导入专题库信息")
    public String importExcel(MultipartFile file) {
        return projectService.importExcel(file);
    }

    @checkAuth(FunId = "/workspace/project/export_list")
    @PostMapping("/export/list")
    @Operation(summary = "导出专题库信息列表")
    public void exportProjectList(HttpServletResponse response) {
        projectService.exportProject(response);
    }


    @PostMapping("copy")
    @Operation(summary = "复制专题库")
    public String copy(@RequestBody ProjectCopyDTO projectCopyDTO) throws IOException {
        Integer projectId = projectService.copy(projectCopyDTO);
        return Response.success(projectId);
    }
}
