package cn.cslg.report.controller;

import cn.cslg.report.common.core.base.Constants;
import cn.cslg.report.common.model.dto.PatentQueryFieldSourceDTO;
import cn.cslg.report.common.model.vo.PatentIndexSettingVO;
import cn.cslg.report.common.model.vo.PatentQueryFieldSourceVO;
import cn.cslg.report.common.model.vo.ReportFieldVO;
import cn.cslg.report.common.utils.Response;
import cn.cslg.report.common.utils.StringUtils;
import cn.cslg.report.entity.ReportField;
import cn.cslg.report.entity.ReportFieldOption;
import cn.cslg.report.entity.SysDictItem;
import cn.cslg.report.service.business.CompareFilesService;
import cn.cslg.report.service.business.ReportFieldOptionService;
import cn.cslg.report.service.business.ReportFieldService;
import cn.cslg.report.service.business.ReportFieldTreeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "自定义字段")
@RestController
@RequestMapping(Constants.REPORT_API + "/patentField")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class PatentFiledController {
    private final ReportFieldService reportFieldService;
    private final ReportFieldTreeService reportFieldTreeService;
    private final ReportFieldOptionService reportFieldOptionService;

    @PostMapping("add")
    @Operation(summary = "新增自定义字段")
    public String add(@RequestBody ReportField reportFieldField) {
        return reportFieldService.add(reportFieldField);
    }

    @PostMapping("edit")
    @Operation(summary = "编辑自定义字段")
    public String edit(@RequestBody ReportField reportField) {
        Integer reportId = reportField.getReportId();
        if (reportId != null) {
            return reportFieldService.updateReportId(reportField);
        }

        return reportFieldService.edit(reportField);
    }

    @GetMapping("delete")
    @Operation(summary = "删除自定义字段")
    public String delete(Integer id, Integer reportId) {

        return reportFieldService.delete(id);
    }

    @PostMapping("list")
    @Operation(summary = "自定义字段列表")
    public String getPageList(@RequestBody ReportFieldVO reportFieldVO) {
        return reportFieldService.getPageList(reportFieldVO);
    }

    @GetMapping("option/list")
    @Operation(summary = "选项列表")
    public String getList(Integer fieldId, Integer type, Integer reportId) throws IOException {
        if (type == 6) {
            return Response.success(reportFieldTreeService.getReportFieldTreeNodeByFieldId(fieldId));
        } else {
            return Response.success(reportFieldOptionService.getListByFieldId(fieldId));
        }

    }

    @PostMapping("update")
    @Operation(summary = "更新选项")
    public String update(@RequestBody ReportFieldOption reportFieldOption) {
        return reportFieldOptionService.updateReportFieldOption(reportFieldOption);
    }

    @GetMapping("option/delete")
    @Operation(summary = "删除选项")
    public String delete(Integer type, Integer id, Integer reportId) {
//        if(reportId!=null){
//          return reportFieldService.deleteReportId(reportId,id);
//        }
        if (type == 6) {
            reportFieldTreeService.delete(id);
        } else {
            reportFieldOptionService.delete(id);
        }
        return Response.success(true);
    }
    @GetMapping("option/setect")
    @Operation(summary = "报告id查找")
    public String setect(Integer reportId)throws IOException{
        return reportFieldService.selectReportField(reportId);
    }
    @PostMapping("/query/source")
    @Operation(summary = "查看字段的统计数据")
    public String getPatentQuerySourcePageList(@RequestBody PatentQueryFieldSourceVO params) {
        return Response.success(reportFieldService.getPatentQuerySourcePageList(params));
    }
}
