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.PatentDTO;
import cn.cslg.pas.common.model.dto.PatentMarketDataDTO;
import cn.cslg.pas.common.utils.*;
import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.pas.common.utils.auth.checkAuth;
import cn.cslg.pas.domain.Patent;
import cn.cslg.pas.common.model.vo.*;
import cn.cslg.pas.domain.PatentRight;
import cn.cslg.pas.service.*;

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.util.Collections;
import java.util.List;

/**
 * <p>
 * 专利 前端控制器
 * </p>
 *
 * @author 王岩
 * @since 2021-12-24
 */
@Tag(name = "专利")
@RestController
@RequestMapping(Constants.API_VERSION_V2 + "/patent")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class PatentController {
    private final PatentService patentService;
    private final ProjectPatentLinkService projectPatentLinkService;
    private final TaskService taskService;
    private final CacheUtils cacheUtils;
    private final ProjectFieldPatentLinkService projectFieldPatentLinkService;
    private final LoginUtils loginUtils;
    private final PatentRightService patentRightService;
    private final IPatentMarketDataService patentMarketDataService;

    @GetMapping("/read/total")
    @Operation(summary = "专利阅读状态统计")
    public String getPatentReadTotal(PatentReadVO params) {
        return Response.success(projectPatentLinkService.getPatentReadTotal(params));
    }

    @checkAuth(FunId = "/workspace/folder/isRead")
    @Permission(roles = {2})
    @PostMapping("/read/update")
    @Operation(summary = "修改专利阅读状态")
    public String updatePatentReadStatus(@RequestBody PatentReadVO params) {
        return Response.success(projectPatentLinkService.updatePatentReadStatus(params));
    }

    @PostMapping("list")
    @Operation(summary = "专利列表")
    public String getList(@RequestBody PatentVO params) {
        return Response.success(patentService.getPageList(params));
    }

    @GetMapping("{id}")
    @Operation(summary = "根据ID获取专利")
    public String getPatentById(@PathVariable Integer id, Integer projectId) {
        return Response.success(patentService.getPatentDTOById(id, projectId));
    }

    @GetMapping("patentNo")
    @Operation(summary = "根据专利号获取专利所有信息")
    public String getPatentIdByPatentNo(String patentNo,Integer projectId) {
        PatentDTO patentDTO =patentService.getPatentDTOById(patentNo,projectId);
        return Response.success(patentDTO);
    }

    @GetMapping("id")
    @Operation(summary = "根据专利号获取专利")
    public String getPatentByPatentNo(String patentNo) {
        Patent patent = patentService.getByPatentNo(patentNo);
        return Response.success(patent);
    }

    @PostMapping("ids")
    @Operation(summary = "根据范围获取专利ID")
    public String getPatentListIds(@RequestBody PatentVO params) {
        return Response.success(patentService.getPatentListIds(params));
    }

    @checkAuth(FunId = "/workspace/folder/exportPatent")
    @PostMapping("export")
    @Operation(summary = "导出专利")
    public String export(@RequestBody PatentExportParams params) {
        String id = cacheUtils.getSelectPatentIds(params.getKey());
        List<Integer> ids;
        if (StringUtils.isEmpty(id) && params.getPatentId() == null) {
            return Response.error(ResponseEnum.QUERY_CACHE_ERROR);
        }
        if (StringUtils.isNotEmpty(params.getKey())) {
            ids = StringUtils.changeStringToInteger(id, ",");
        } else {
            ids = Collections.singletonList(params.getPatentId());
        }
        Integer taskId = taskService.add2(params.getProjectId(), params.getSelected().stream().filter(PatentExportVO::getSelected).distinct().count(), ids.size());
        TaskParams taskParams = new TaskParams();
        taskParams.setTaskId(taskId);
        taskParams.setTaskType(2);
        taskParams.setIds(StringUtils.join(ids, ","));
        taskParams.setProjectId(params.getProjectId());
        taskParams.setSelected(JsonUtils.objectToJson(params.getSelected()));
        taskParams.setUserId(String.valueOf(loginUtils.getId()));
        patentService.exportPatent(taskParams);
        return Response.success(true);
    }

    @PostMapping("/query/source")
    @Operation(summary = "查看字段的统计数据")
    public String getPatentQuerySourcePageList(@RequestBody PatentQueryFieldSourceVO params) {
        return Response.success(patentService.getPatentQuerySourcePageList(params));
    }


    @PostMapping("/save/select")
    @Operation(summary = "保存选择的专利")
    public String saveSelectPatentIds(@RequestBody PatentVO params) {
        List<Integer> ids = patentService.getQueryPatentIds(params);
        return Response.success(cacheUtils.setSelectPatentIds(ids));
    }

    @checkAuth(FunId = "/workspace/folder/batchOperation/batchIndexing")
    @Permission(roles = {1,2})
    @PostMapping("/batch/index")
    @Operation(summary = "批量批量专利标引")
    public String patentBatchIndex(@RequestBody PatentBatchIndexVO params) {
        projectFieldPatentLinkService.patentBatchIndex(params);
        return Response.success(true);
    }

    @GetMapping("/index/setting")
    @Operation(summary = "获取专利标引情况")
    public String getPatentIndexSetting(PatentIndexSettingVO params) {
        return Response.success(projectFieldPatentLinkService.getPatentIndexSetting(params));
    }

    @GetMapping("/index/setting/all")
    @Operation(summary = "获取当前专利所有标引情况")
    public String getPatentIndexSetting2(Integer patentId, Integer projectId) {
        return Response.success(projectFieldPatentLinkService.getPatentIndexSetting2(patentId, projectId));
    }

    @checkAuth(FunId = "/workspace/details/indexing")
    @Permission(roles = {1,2})
    @PostMapping("/index/setting")
    @Operation(summary = "修改专利标引")
    public String updatePatentIndexSetting(@RequestBody PatentIndexSettingVO params) {
        projectFieldPatentLinkService.updatePatentIndexSetting(params);
        return Response.success(true);
    }

    @PostMapping("between")
    @Operation(summary = "获取当前专利相邻的两篇专利和统计信息")
    public String getBetweenPatentList(@RequestBody PatentVO params) {
        return Response.success(patentService.getBetweenPatentList(params));
    }

    @GetMapping("right")
    @Operation(summary = "根据专利号获得专利的权要")
    public String getRightList(String patentNo) {
        List<PatentRight> patentRights =patentRightService.getPatentRightByPatentNo(patentNo);
        return Response.success(patentRights);
    }

    @GetMapping("getPatentNos")
    @Operation(summary = "根据专题库号获得专利号")
    public String getPatentNos(Integer projectId) {
      String res =patentService.getPatentNos(projectId);
        return res;
    }

    @PostMapping("getPagination")
    @Operation(summary = "分页获取专利")
    public String getPagination(@RequestBody PatentVO patentVO){
       return patentService.getPagination(patentVO.getPatentNos(),patentVO.getStartNumber());
    }

    @Operation(summary = "查询专利价值曲线")
    @PostMapping("/showTrend")
    public String showTrend(@RequestBody PatentMarketDataDTO dto) {
        List<PatentTrendVO> queryResult = patentMarketDataService.showTrend(dto);
        if (queryResult != null){
            return Response.success(queryResult);
        } else {
            return Response.error("查询失败");
        }
    }


}
