package cn.cslg.pas.service.upLoadPatent;

import cn.cslg.pas.common.core.base.Constants;
import cn.cslg.pas.common.model.vo.ProjectImportPatentVO;
import cn.cslg.pas.common.model.vo.UploadParamsVO;
import cn.cslg.pas.domain.Patent;
import cn.cslg.pas.domain.SystemDict;
import cn.cslg.pas.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
 * 保存专利及所有与之关联数据入库
 *
 * @Author chenyu
 * @Date 2023/5/30
 */
@Service
@RequiredArgsConstructor
public class UploadPatentToDBService {
    private final SystemDictService systemDictService;
    private final PatentAgencyService patentAgencyService;
    private final UploadPatentBatchService uploadPatentBatchService;
    private final PatentImageService patentImageService;
    private final PatentService patentService;

    /**
     * 摘要附图入库（Excel方式）
     *
     * @param uploadParamsVO 专利内容
     */
    public void uploadPatentImage(UploadParamsVO uploadParamsVO) throws IOException {
        //摘要附图数据装配及入库（摘要附图表"os_patent_img"）
        if (uploadParamsVO.getPatent() != null) {
            if (uploadParamsVO.getPatent().getId() != null && uploadParamsVO.getPictureData() != null) {
                uploadParamsVO.getPatent().setAbstractPath(patentImageService.updatePatentImage(uploadParamsVO.getPatent().getId(), uploadParamsVO.getPictureData()));
                //专利表"os_patent"更新数据（更新摘要附图路径）
                patentService.updateById(uploadParamsVO.getPatent());
            }
        }
    }

    /**
     * 摘要附图入库（网站导入方式）
     *
     * @param uploadParamsVO 专利内容
     */
    public void uploadPatentImage2(UploadParamsVO uploadParamsVO) throws IOException {
        //摘要附图数据装配及入库（摘要附图表"os_patent_img"）
        if (uploadParamsVO.getPatent() != null) {
            if (uploadParamsVO.getPatent().getId() != null && uploadParamsVO.getFileDTO() != null) {
                uploadParamsVO.getPatent().setAbstractPath(patentImageService.updatePatentImage2(uploadParamsVO));
                //专利表"os_patent"更新数据（更新摘要附图路径）
                patentService.updateById(uploadParamsVO.getPatent());
            }
        }
    }

    /**
     * 著录项目
     *
     * @param uploadParamsVO 专利内容
     */
    public void uploadPatentZhulu(UploadParamsVO uploadParamsVO) {
        //保存法律状态（专利表"os_patent"）
        if (uploadParamsVO.getPatent() != null) {
            List<SystemDict> systemDictList = systemDictService.getSystemDictListByType(Collections.singletonList(Constants.PATENT_SIMPLE_STATUS));
            Integer simpleStatus = Integer.parseInt(systemDictList.stream()
                    .filter(systemDict -> systemDict.getType().equals(Constants.PATENT_SIMPLE_STATUS) && systemDict.getLabel().equals(uploadParamsVO.getPatentSimpleStatus()))
                    .findFirst()
                    .orElse(new SystemDict()).getValue());
            uploadParamsVO.getPatent().setSimpleStatus(simpleStatus);
            uploadParamsVO.getPatent().updateById();
        }

        //保存代理机构（代理机构表"os_patent_agency"）
        if (uploadParamsVO.getPatent() != null) {
            if (uploadParamsVO.getPatent().getAgencyId() != null) {
                uploadParamsVO.getPatent().setAgencyId(patentAgencyService.getAgencyStringIdByName(uploadParamsVO.getPatent().getAgencyId()));
                //修改专利表（"os_patent"）agencyid数据
                Patent patent = new Patent();
                patent.setId(uploadParamsVO.getPatent().getId());
                patent.setAgencyId(uploadParamsVO.getPatent().getAgencyId());
                patentService.updateById(patent);
            }
        }

        //保存代理人（表"os_patent_agent"、表"os_patent_agent_link"）
        if (uploadParamsVO.getPatentAgentList() != null) {
            uploadPatentBatchService.patentAgentBusiness(uploadParamsVO);
        }

        //保存简单同族（表""）
        uploadPatentBatchService.patentSimplyFamilyBusiness(uploadParamsVO);

        //保存质押质权人（表"os_patent_pledge"）
        if (uploadParamsVO.getPatentPledgeList() != null) {
            uploadPatentBatchService.patentPledgeBusiness(uploadParamsVO);
        }

        //保存许可人（表"os_patent_licensor"）
        if (uploadParamsVO.getPatentLicensorList() != null) {
            uploadPatentBatchService.patentLicensorBusiness(uploadParamsVO);
        }

        //保存发明人（表"os_patent_inventor"、表"os_patent_inventor_relation"）
        uploadPatentBatchService.patentInventorBusiness(uploadParamsVO);

        //保存申请人(原始)/权利人(当前)（表"os_patent_applicant"、表"os_applicant_attr"）
        uploadPatentBatchService.patentApplicationBusiness(uploadParamsVO);

        //保存申请人地址（表""）
        uploadPatentBatchService.patentApplicationAddressBusiness(uploadParamsVO);

        //保存分类号关联（表"os_patent_typeno"）
        uploadPatentBatchService.patentTypeNoBusiness(uploadParamsVO);

        //保存事务信息（表"os_patent_affair"）
        if (uploadParamsVO.getPatentAffair() != null) {
            uploadPatentBatchService.patentAffairBusiness(uploadParamsVO);
        }
    }

    /**
     * 权利要求
     *
     * @param uploadParamsVO 专利内容
     */
    public void uploadPatentRight(UploadParamsVO uploadParamsVO) {
        //保存权利要求（权要表"os_patent_right"）
        uploadPatentBatchService.patentRightBusiness(uploadParamsVO);
    }

    /**
     * 说明书文本
     *
     * @param uploadParamsVO 专利内容
     */
    public void uploadPatentInstructionText(UploadParamsVO uploadParamsVO) {
        //保存说明书文本（说明书文本表"os_patent_instruction_text"）
        if (uploadParamsVO.getPatentInstructionText() != null) {
            uploadPatentBatchService.patentInstructionTextBusiness(uploadParamsVO);
        }
    }

    /**
     * 专题库与专利关联
     *
     * @param uploadParamsVO 专利内容
     * @param projectId      专题库id
     */
    public void uploadAssoThemaPat(UploadParamsVO uploadParamsVO, Integer projectId) {
        //保存专题库与专利关联（表"os_thematic_pid"）
        if (uploadParamsVO.getPatent() != null && projectId != null) {
            uploadPatentBatchService.patentThematicPidBusiness(uploadParamsVO, projectId);
        }
    }

    /**
     * 标引内容与专利关联
     *
     * @param uploadParamsVO        专利内容
     * @param projectImportPatentVO 专题库关联专利内容（专题库id、专利id、自定义字段）
     */
    public void uploadAssoFieldPat(UploadParamsVO uploadParamsVO, ProjectImportPatentVO projectImportPatentVO) {
        //保存标引内容与专利关联（表"os_patent_field_patent_link"、"os_patent_field_text"）
        if (uploadParamsVO.getPatent() != null && projectImportPatentVO != null) {
            uploadPatentBatchService.patentProjectFieldPatentLinkBusiness(uploadParamsVO, projectImportPatentVO);
        }
    }

    /**
     * 文件夹与专利关联
     *
     * @param uploadParamsVO        专利内容
     * @param projectImportPatentVO 文件夹关联专利（专题库id、专利id、文件夹id）
     */
    public void uploadAssoPorPat(UploadParamsVO uploadParamsVO, ProjectImportPatentVO projectImportPatentVO) {
        //保存文件夹与专利关联（表"os_portfolio_link"）
        if (uploadParamsVO.getPatent() != null && projectImportPatentVO != null) {
            uploadPatentBatchService.patentProjectFolderPatentLinkBusiness(uploadParamsVO, projectImportPatentVO);
        }
    }


}
