package cn.cslg.pas.service;

import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.pas.domain.PatentField;
import cn.cslg.pas.domain.ProjectField;
import cn.cslg.pas.common.core.base.Constants;
import cn.cslg.pas.common.model.vo.SystemFieldVO;
import cn.cslg.pas.common.utils.CacheUtils;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统字段 服务类
 * </p>
 *
 * @author 王岩
 * @since 2022-03-04
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class PatentFieldService {

    private final CacheUtils cacheUtils;
    private final ProjectFieldService projectFieldService;
    private final List<String> finalDefaultList = Arrays.asList("name", "ipcList", "applicationNo", "applicationDate", "applicant2", "applicant4");
    private final List<String> finalDefaultQuery = Arrays.asList("13", "18-6", "19-6", "25-12");
    private final List<String> finalProjectField = Arrays.asList("clientName", "scenarioid", "typeid", "technicalTheme");
    private final LoginUtils loginUtils;
    public List<PatentField> getUserFieldDataList(Integer projectId, String from) {
        List<PatentField> patentFieldList = this.getList(from);
        List<ProjectField> projectField = projectFieldService.getProjectFieldByProjectId(projectId);
        if (from.equals(Constants.SYSTEM_FIELD_QUERY)) {
            projectField = projectField.stream().filter(item -> !item.getType().equals(6)).collect(Collectors.toList());
        }
        patentFieldList.addAll(projectField.stream().map(item -> {
            PatentField field = new PatentField();
            field.setName(item.getName());
            field.setType(String.valueOf(item.getType()));
            field.setKey(String.valueOf(item.getId()));
            return field;
        }).collect(Collectors.toList()));
        return patentFieldList;
    }


    public List<PatentField> getUserSetting(SystemFieldVO params) {
        Integer createId =loginUtils.getId();
        if (params.getRefresh() != null && params.getRefresh()) {
            cacheUtils.deleteUserSystemFieldSetting(params.getProjectId(), params.getType(), params.getView(), 3,createId);
        }
        List<PatentField> dataList = this.getUserFieldDataList(params.getProjectId(), params.getType());
        List<PatentField> userSetting = cacheUtils.getUserSystemFieldSetting(params.getProjectId(), params.getType(), params.getView(), 3,createId);
        if (userSetting == null || userSetting.size() == 0) {
            List<PatentField> defaultList = new ArrayList<>();
            List<PatentField> patentFieldList = this.getList(params.getType());
            switch (params.getType()) {
                case Constants.SYSTEM_FIELD_QUERY:
                    defaultList = patentFieldList.stream().filter(item -> finalDefaultQuery.contains(item.getKey())).collect(Collectors.toList());
                    break;
                case Constants.SYSTEM_FIELD_PATENT_LIST:
                    defaultList = patentFieldList.stream().filter(item -> finalDefaultList.contains(item.getKey())).collect(Collectors.toList());
                    break;
                case Constants.SYSTEM_FIELD_PROJECT:
                    defaultList = patentFieldList.stream().filter(item -> finalProjectField.contains(item.getKey())).collect(Collectors.toList());
                    break;
            }
            cacheUtils.setUserSystemFieldSetting(params.getProjectId(), params.getType(), params.getView(), 3, defaultList,createId);
            userSetting = defaultList;
        }
        List<PatentField> finalUserSetting = userSetting;
        dataList.forEach(item -> {
            PatentField setting = finalUserSetting.stream().filter(patentField -> patentField.getKey().equals(item.getKey())).findFirst().orElse(null);
            item.setHidden(setting == null);
            item.setOrder(setting == null ? 0 : setting.getOrder());
        });
        return dataList.stream().sorted(Comparator.comparing(PatentField::getOrder)).collect(Collectors.toList());
    }

    public List<PatentField> setUserSetting(SystemFieldVO params) {
        List<PatentField> patentFieldList = params.getData().stream().filter(item -> !item.getHidden()).collect(Collectors.toList());
        for (int i = 0; i < patentFieldList.size(); i++) {
            patentFieldList.get(i).setHidden(null);
            patentFieldList.get(i).setOrder(i);
        }
        Integer createId =loginUtils.getId();
        cacheUtils.setUserSystemFieldSetting(params.getProjectId(), params.getType(), params.getView(), 3, patentFieldList,createId);
        params.setRefresh(false);
        return this.getUserSetting(params);
    }

    public List<PatentField> getList() {
        List<PatentField> dataList = new ArrayList<PatentField>() {{
//            add(new SystemField("name", "标题", Constants.SYSTEM_FIELD_QUERY, 1));
//            add(new SystemField("abstract", "摘要", Constants.SYSTEM_FIELD_QUERY, 1));
//            add(new SystemField("41-1", "公开（公告）号", Constants.SYSTEM_FIELD_QUERY, 1));
//            add(new SystemField("41-2", "申请号", Constants.SYSTEM_FIELD_QUERY, 2));
            add(new PatentField("18-6", "申请人", Constants.SYSTEM_FIELD_QUERY, 3));
            add(new PatentField("19-6", "权利人", Constants.SYSTEM_FIELD_QUERY, 4));
            add(new PatentField("20-0", "发明人", Constants.SYSTEM_FIELD_QUERY, 5));
            add(new PatentField("18-7", "[标]申请人", Constants.SYSTEM_FIELD_QUERY, 6));
            add(new PatentField("19-7", "[标]权利人", Constants.SYSTEM_FIELD_QUERY, 7));
            add(new PatentField("18-8", "[合并]申请人", Constants.SYSTEM_FIELD_QUERY, 8));
            add(new PatentField("19-8", "[合并]权利人", Constants.SYSTEM_FIELD_QUERY, 9));
            add(new PatentField("20-1", "[合并]发明人", Constants.SYSTEM_FIELD_QUERY, 9));
            add(new PatentField("13", "IPC分类号", Constants.SYSTEM_FIELD_QUERY, 10));
            add(new PatentField("15", "CPC分类号", Constants.SYSTEM_FIELD_QUERY, 11));
            add(new PatentField("17", "UPC分类号", Constants.SYSTEM_FIELD_QUERY, 12));
            add(new PatentField("16", "LOC分类号", Constants.SYSTEM_FIELD_QUERY, 13));
            add(new PatentField("25-12", "申请年", Constants.SYSTEM_FIELD_QUERY, 14));
            add(new PatentField("28-0", "受理局", Constants.SYSTEM_FIELD_QUERY, 15));
            add(new PatentField("37-0", "专利类型", Constants.SYSTEM_FIELD_QUERY, 16));
            add(new PatentField("29-17", "申请人国家", Constants.SYSTEM_FIELD_QUERY, 17));
            add(new PatentField("29-18", "申请人地区", Constants.SYSTEM_FIELD_QUERY, 18));
            add(new PatentField("36-0", "专利状态", Constants.SYSTEM_FIELD_QUERY, 19));
            add(new PatentField("40-0", "法律事件", Constants.SYSTEM_FIELD_QUERY, 20));
            add(new PatentField("26-12", "公开年", Constants.SYSTEM_FIELD_QUERY, 21));
            add(new PatentField("54-0", "标签", Constants.SYSTEM_FIELD_QUERY, 22));
            add(new PatentField("pname", "标题", Constants.SYSTEM_FIELD_EXPORT, 1));
            add(new PatentField("pname_out", "标题(译)", Constants.SYSTEM_FIELD_EXPORT, 1));
            add(new PatentField("patentno", "专利号", Constants.SYSTEM_FIELD_EXPORT, 2));
            add(new PatentField("abstract", "摘要", Constants.SYSTEM_FIELD_EXPORT, 3));
            add(new PatentField("abstract_out", "摘要(译)", Constants.SYSTEM_FIELD_EXPORT, 3));
            add(new PatentField("abstract_path", "摘要附图", Constants.SYSTEM_FIELD_EXPORT, 4));
            add(new PatentField("applicationno", "申请号", Constants.SYSTEM_FIELD_EXPORT, 5));
            add(new PatentField("appdate", "申请日", Constants.SYSTEM_FIELD_EXPORT, 6));
            add(new PatentField("publicno", "公开号", Constants.SYSTEM_FIELD_EXPORT, 7));
            add(new PatentField("publicdate", "公开日", Constants.SYSTEM_FIELD_EXPORT, 8));
            add(new PatentField("fpublicdate", "首次公开日", Constants.SYSTEM_FIELD_EXPORT, 9));
            add(new PatentField("publictono", "授权号", Constants.SYSTEM_FIELD_EXPORT, 10));
            add(new PatentField("publictodate", "授权日", Constants.SYSTEM_FIELD_EXPORT, 11));
            add(new PatentField("tags", "标签", Constants.SYSTEM_FIELD_EXPORT, 12));
            add(new PatentField("bureau", "受理局", Constants.SYSTEM_FIELD_EXPORT, 13));
            add(new PatentField("simplefamily", "简单同族", Constants.SYSTEM_FIELD_EXPORT, 14));
            add(new PatentField("inpadocfamily", "Inpadoc同族", Constants.SYSTEM_FIELD_EXPORT, 15));
            add(new PatentField("patsnapfamily", "扩展同族", Constants.SYSTEM_FIELD_EXPORT, 15));
            add(new PatentField("intclassno", "IPC主分类号", Constants.SYSTEM_FIELD_EXPORT, 16));
            add(new PatentField("ipc", "IPC分类号", Constants.SYSTEM_FIELD_EXPORT, 16));
            add(new PatentField("intclasscpcno", "CPC分类号", Constants.SYSTEM_FIELD_EXPORT, 17));
            add(new PatentField("intclassupcno", "UPC主分类号", Constants.SYSTEM_FIELD_EXPORT, 18));
            add(new PatentField("upc", "UPC分类号", Constants.SYSTEM_FIELD_EXPORT, 18));
            add(new PatentField("intclasslocno", "LOC分类号", Constants.SYSTEM_FIELD_EXPORT, 19));
            add(new PatentField("pstatus", "专利状态", Constants.SYSTEM_FIELD_EXPORT, 20));
            add(new PatentField("ptype", "专利类型", Constants.SYSTEM_FIELD_EXPORT, 21));
            add(new PatentField("law_datetime", "发生日期", Constants.SYSTEM_FIELD_EXPORT, 22));
            add(new PatentField("law_status", "法律状态", Constants.SYSTEM_FIELD_EXPORT, 23));
            add(new PatentField("law_simplestatus", "简单法律状态", Constants.SYSTEM_FIELD_EXPORT, 24));
            add(new PatentField("law_content", "事务内容", Constants.SYSTEM_FIELD_EXPORT, 25));
            add(new PatentField("applicant", "申请人", Constants.SYSTEM_FIELD_EXPORT, 26));
            add(new PatentField("applicant1", "[标]申请人", Constants.SYSTEM_FIELD_EXPORT, 26));
            add(new PatentField("rightholder", "权利人", Constants.SYSTEM_FIELD_EXPORT, 27));
            add(new PatentField("rightholder1", "[标]权利人", Constants.SYSTEM_FIELD_EXPORT, 27));
            add(new PatentField("applicantcountry", "申请人国家", Constants.SYSTEM_FIELD_EXPORT, 28));
            add(new PatentField("rightholdercountry", "权利人国家", Constants.SYSTEM_FIELD_EXPORT, 29));
            add(new PatentField("applicantaddress", "申请人地址", Constants.SYSTEM_FIELD_EXPORT, 30));
            add(new PatentField("rightholderaddress", "权利人地址", Constants.SYSTEM_FIELD_EXPORT, 31));
//            add(new SystemField("applicantaddr", "申请人省市区", Constants.SYSTEM_FIELD_EXPORT, 32));
//            add(new SystemField("rightholderaddr", "权利人省市区", Constants.SYSTEM_FIELD_EXPORT, 33));
            add(new PatentField("firstapplicant", "第一申请人", Constants.SYSTEM_FIELD_EXPORT, 34));
            add(new PatentField("firstrightholder", "第一权利人", Constants.SYSTEM_FIELD_EXPORT, 34));
            add(new PatentField("firstapplicantaddr", "第一申请人地址", Constants.SYSTEM_FIELD_EXPORT, 35));
            add(new PatentField("firstrightholderaddr", "第一权利人地址", Constants.SYSTEM_FIELD_EXPORT, 35));
            add(new PatentField("firstapplicantnum", "申请人数量", Constants.SYSTEM_FIELD_EXPORT, 36));
            add(new PatentField("inventor", "发明人", Constants.SYSTEM_FIELD_EXPORT, 37));
            add(new PatentField("firstnventoraddr", "第一发明人地址", Constants.SYSTEM_FIELD_EXPORT, 38));
            add(new PatentField("firstnventor", "第一发明人", Constants.SYSTEM_FIELD_EXPORT, 39));
            add(new PatentField("inventornum", "发明人数量", Constants.SYSTEM_FIELD_EXPORT, 40));
            add(new PatentField("num2", "权利要求数量", Constants.SYSTEM_FIELD_EXPORT, 41));
            add(new PatentField("content", "权利要求", Constants.SYSTEM_FIELD_EXPORT, 42));
            add(new PatentField("content_out", "权利要求(译)", Constants.SYSTEM_FIELD_EXPORT, 42));
            add(new PatentField("selfcontent", "独立权利要求", Constants.SYSTEM_FIELD_EXPORT, 43));
            add(new PatentField("priorityno", "优先权号", Constants.SYSTEM_FIELD_EXPORT, 44));
            add(new PatentField("prioritycountry", "优先权国家", Constants.SYSTEM_FIELD_EXPORT, 45));
            add(new PatentField("prioritydate", "优先权日", Constants.SYSTEM_FIELD_EXPORT, 46));
            add(new PatentField("page", "文献页数", Constants.SYSTEM_FIELD_EXPORT, 47));
            add(new PatentField("code", "文献代码", Constants.SYSTEM_FIELD_EXPORT, 48));
            add(new PatentField("manual", "说明书", Constants.SYSTEM_FIELD_EXPORT, 49));
            add(new PatentField("simplefamilynum", "简单同族数量", Constants.SYSTEM_FIELD_EXPORT, 50));
            add(new PatentField("inpadocfamilynum", "Inpadoc同族数量", Constants.SYSTEM_FIELD_EXPORT, 51));
            add(new PatentField("patsnapfamilynum", "扩展同族数量", Constants.SYSTEM_FIELD_EXPORT, 51));
            add(new PatentField("quoteno", "引用专利数量", Constants.SYSTEM_FIELD_EXPORT, 52));
            add(new PatentField("quotedno", "被引用数量", Constants.SYSTEM_FIELD_EXPORT, 53));
            add(new PatentField("quotedno3", "三年内被引用数量", Constants.SYSTEM_FIELD_EXPORT, 54));
            add(new PatentField("quotedno5", "五年内被引用数量", Constants.SYSTEM_FIELD_EXPORT, 55));
            add(new PatentField("agency", "代理机构", Constants.SYSTEM_FIELD_EXPORT, 56));
            add(new PatentField("agent", "代理人", Constants.SYSTEM_FIELD_EXPORT, 57));
            add(new PatentField("wonational", "WO国家阶段", Constants.SYSTEM_FIELD_EXPORT, 58));
            add(new PatentField("examiner", "审查员", Constants.SYSTEM_FIELD_EXPORT, 59));
            add(new PatentField("assexaminer", "助理审查员", Constants.SYSTEM_FIELD_EXPORT, 60));
            add(new PatentField("quote", "引用专利", Constants.SYSTEM_FIELD_EXPORT, 61));
            add(new PatentField("quoted", "被引用专利", Constants.SYSTEM_FIELD_EXPORT, 62));
            add(new PatentField("nonpatentquote", "非专利引用文献", Constants.SYSTEM_FIELD_EXPORT, 63));
            add(new PatentField("epcountry", "指定国状态", Constants.SYSTEM_FIELD_EXPORT, 64));
            add(new PatentField("name", "标题", Constants.SYSTEM_FIELD_PATENT_LIST, 1));
//            add(new PatentField("nameOut", "标题(译)", Constants.SYSTEM_FIELD_PATENT_LIST, 1));
            add(new PatentField("abstractStr", "摘要", Constants.SYSTEM_FIELD_PATENT_LIST, 3));
//            add(new PatentField("abstractOut", "摘要(译)", Constants.SYSTEM_FIELD_PATENT_LIST, 3));
            add(new PatentField("applicationNo", "申请号", Constants.SYSTEM_FIELD_PATENT_LIST, 4));
            add(new PatentField("applicationDate", "申请日", Constants.SYSTEM_FIELD_PATENT_LIST, 5));
            add(new PatentField("publicDate", "公开日", Constants.SYSTEM_FIELD_PATENT_LIST, 7));
            add(new PatentField("publicAccreditDate", "授权日", Constants.SYSTEM_FIELD_PATENT_LIST, 8));
            add(new PatentField("label", "标签", Constants.SYSTEM_FIELD_PATENT_LIST, 9));
            add(new PatentField("simpleFamily", "简单同族", Constants.SYSTEM_FIELD_PATENT_LIST, 10));
            add(new PatentField("inpadocFamily", "Inpadoc同族", Constants.SYSTEM_FIELD_PATENT_LIST, 11));
            add(new PatentField("patSnapFamily", "扩展同族", Constants.SYSTEM_FIELD_PATENT_LIST, 11));
            add(new PatentField("ipcList", "IPC分类号", Constants.SYSTEM_FIELD_PATENT_LIST, 12));
            add(new PatentField("cpcList", "CPC分类号", Constants.SYSTEM_FIELD_PATENT_LIST, 13));
            add(new PatentField("upcList", "UPC分类号", Constants.SYSTEM_FIELD_PATENT_LIST, 14));
            add(new PatentField("locList", "LOC分类号", Constants.SYSTEM_FIELD_PATENT_LIST, 15));
            add(new PatentField("rightNum", "权利要求数", Constants.SYSTEM_FIELD_PATENT_LIST, 16));
            add(new PatentField("simpleStatus", "专利状态", Constants.SYSTEM_FIELD_PATENT_LIST, 17));
            add(new PatentField("type", "专利类型", Constants.SYSTEM_FIELD_PATENT_LIST, 18));
            add(new PatentField("applicant1", "[标]权利人", Constants.SYSTEM_FIELD_PATENT_LIST, 19));
            add(new PatentField("applicant2", "权利人", Constants.SYSTEM_FIELD_PATENT_LIST, 20));
            add(new PatentField("applicant3", "[标]申请人", Constants.SYSTEM_FIELD_PATENT_LIST, 21));
            add(new PatentField("applicant4", "申请人", Constants.SYSTEM_FIELD_PATENT_LIST, 22));
            add(new PatentField("inventor", "发明人", Constants.SYSTEM_FIELD_PATENT_LIST, 23));
            add(new PatentField("quoteNum", "引用专利数量", Constants.SYSTEM_FIELD_PATENT_LIST, 24));
            add(new PatentField("quotedNum", "被引用数量", Constants.SYSTEM_FIELD_PATENT_LIST, 25));
            add(new PatentField("agency", "代理机构", Constants.SYSTEM_FIELD_PATENT_LIST, 26));
            add(new PatentField("agent", "代理人", Constants.SYSTEM_FIELD_PATENT_LIST, 27));
            add(new PatentField("clientName", "委托方", Constants.SYSTEM_FIELD_PROJECT, 1));
            add(new PatentField("scenarioid", "应用场景", Constants.SYSTEM_FIELD_PROJECT, 1));
            add(new PatentField("typeid", "调查类型", Constants.SYSTEM_FIELD_PROJECT, 1));
            add(new PatentField("technicalTheme", "技术主题", Constants.SYSTEM_FIELD_PROJECT, 1));
            add(new PatentField("contractNo", "合同号", Constants.SYSTEM_FIELD_PROJECT, 1));
            add(new PatentField("caseDate", "委案日", Constants.SYSTEM_FIELD_PROJECT, 1));
            add(new PatentField("createName", "创建人", Constants.SYSTEM_FIELD_PROJECT, 1));
            add(new PatentField("createTime", "创建时间", Constants.SYSTEM_FIELD_PROJECT, 1));
            add(new PatentField("departmentName", "所属部门", Constants.SYSTEM_FIELD_PROJECT, 1));
            add(new PatentField("personnelName", "专题库负责人", Constants.SYSTEM_FIELD_PROJECT, 1));
        }};
        return dataList;
    }

    public List<PatentField> getList(String type) {
        List<PatentField> patentFieldList = this.getList();
        return patentFieldList.stream().filter(item -> item.getType().equals(type)).collect(Collectors.toList());
    }

    //获得专题库的所有自定义字段
    //获得自定义字段列表
    public List<ProjectField> getPageList(Integer projectId) {
        LambdaQueryWrapper<ProjectField> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProjectField::getProjectId, projectId);
        queryWrapper.orderByDesc(ProjectField::getId);
        List<ProjectField> projectFields = projectFieldService.list(queryWrapper);
        return projectFields;

    }
}
