package com.jeeplus.finance.projectReport.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.finance.invoice.domain.CwFinanceInvoiceBase;
import com.jeeplus.finance.invoice.mapper.CwFinanceInvoiceBaseMapper;
import com.jeeplus.finance.invoice.mapper.CwFinanceInvoiceMapper;
import com.jeeplus.finance.invoice.service.dto.CwFinanceInvoiceBaseDTO;
import com.jeeplus.finance.invoice.service.dto.CwFinanceInvoiceDTO;
import com.jeeplus.finance.projectRecords.domain.CwProjectRecords;
import com.jeeplus.finance.projectReport.domain.*;
import com.jeeplus.finance.projectReport.mapper.CwProjectInfoMapper;
import com.jeeplus.finance.projectReport.mapper.CwProjectReportNumberApplyMapper;
import com.jeeplus.finance.projectReport.mapper.CwProjectReportReviewMapper;
import com.jeeplus.finance.projectReport.service.dto.NumberReplaceDTO;
import com.jeeplus.finance.reimbursementApproval.approvalInfo.service.dto.ReportNoDto;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.feign.IWorkAttachmentApi;
import com.jeeplus.sys.service.dto.UserDTO;
import org.apache.commons.lang3.StringUtils;
import org.flowable.editor.language.json.converter.util.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * @author 王强
 * @version 1.0
 * @date 2023-12-12 17:02
 */
@Service
@Transactional
public class CwProjectReportNumberApplyService extends ServiceImpl<CwProjectReportNumberApplyMapper, CwProjectReportNumberApply> {

    @Resource
    private CwProjectReportNumberApplyMapper mapper;

    @Resource
    private CwProjectReportReviewMapper reviewMapper;

    @Resource
    private CwProjectInfoMapper infoMapper;

    @Resource
    private CwProjectReportService cwProjectReportService;

    @Resource
    private CwFinanceInvoiceMapper cwFinanceInvoiceMapper;

    @Resource
    private CwFinanceInvoiceBaseMapper cwFinanceInvoiceBaseMapper;

    public CwProjectReportData queryByReportId(String id) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        CwProjectReportData data = mapper.selectByPortId(id);
        //查询复核相关信息

        return data;
    }

    /**
     * 修改状态
     * @param data
     * @return
     */
    public String updateReviewStatysByReportId(CwProjectReportData data) {
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken ( ));
        mapper.updateReviewStatysByReportId(data.getApplyStatus(),data.getId());

        return "操作成功";
    }

    /**
     * 保存项目以及其他相关信息
     * @param reportData
     * @return
     * @throws Exception
     */
    public String saveForm(CwProjectReportData reportData) throws Exception{
        if (StringUtils.isNotEmpty(reportData.getId())){

            CwProjectReportNumberApply detail = mapper.selectById(reportData.getId());
            if (detail != null){
                reportData.setCreateBy(reportData.getCreateBy());
                return update(reportData,detail);
            } else {
                //传过来的是报告表id
                detail = mapper.selectByReportId(reportData.getId());
                if (detail != null){
                    reportData.setCreateBy(reportData.getCreateBy());
                    return update(reportData,detail);
                }
            }
        }
        return add(reportData);

    }

    /**
     * 报告号修改
     */
    @Transactional(rollbackFor = Exception.class)
    public String update(CwProjectReportData reportData,CwProjectReportNumberApply detail) throws Exception{
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken ( ));
        CwProjectReportNumberApply review = new CwProjectReportNumberApply();
        BeanUtils.copyProperties(reportData, review);
        review.setCreateById(detail.getCreateById());
        review.setCreateTime(detail.getCreateTime());
        review.setUpdateById(userDTO.getId());
        review.setUpdateTime(new Date());
        review.setId(detail.getId());
        // 设置跨年标识默认值，确保数据完整性
        if (StringUtils.isBlank(review.getCrossYearFlag())) {
            review.setCrossYearFlag("0");
        }
        String currentToken = TokenProvider.getCurrentToken();


        if (reportData.getReviewStatus().equals("5")){
            // 如果是跨年申请（crossYearFlag=1），在此处预留报告号赋值点位
            // TODO: 流程节点名称=所长审核 时，根据跨年标识生成相应年度的报告号
            // if ("1".equals(review.getCrossYearFlag())) {
            //     // 跨年报告号生成逻辑：获取上一年度的报告号前缀
            //     // 例如：当前是2026年，则使用2025年的报告号序列
            // }
            
            //设置报告文号、报告流水号
            CwProjectInfoData infoData = new CwProjectInfoData();
            //报告文号生成
            String documentNo = "";
            if(StringUtils.isNotBlank(reportData.getReportNo())){
                infoData.setReportNo(reportData.getReportNo());
            }else{
                if (!reportData.getTakeNumberType().equals("2")){
                    if (StringUtils.isBlank(review.getCrossYearFlag()) || "0".equals(review.getCrossYearFlag())) {
                        if(StringUtils.isNotBlank(reportData.getReportType())) {
                            ReportNoDto projectReportByNumber = new ReportNoDto();
                            switch (reportData.getReportType()) {
                                case "1":   //基字
                                    do{
                                        documentNo = SpringUtil.getBean ( IWorkAttachmentApi.class ).genSerialNumNoSort(userDTO.getCompanyDTO().getId(), CwProjectInfoData.BIZ_CODE,currentToken);
                                        //根据报告号查询诗剧苦中是否存在
                                        projectReportByNumber = reviewMapper.getProjectReportByNumber(documentNo);
                                    }while (null != projectReportByNumber && StringUtils.isNotBlank(projectReportByNumber.getReportNo()));
                                    break;
                                case "2":   //咨字
                                    do{
                                        documentNo = SpringUtil.getBean ( IWorkAttachmentApi.class ).genSerialNumNoSort(userDTO.getCompanyDTO().getId(), CwProjectInfoData.CONSULT_BIZ_CODE,currentToken);
                                        //根据报告号查询诗剧苦中是否存在
                                        projectReportByNumber = reviewMapper.getProjectReportByNumber(documentNo);
                                    }while (null != projectReportByNumber && StringUtils.isNotBlank(projectReportByNumber.getReportNo()));
                                    break;
                                case "3":   //审字
                                    do{
                                        documentNo = SpringUtil.getBean ( IWorkAttachmentApi.class ).genSerialNumNoSort(userDTO.getCompanyDTO().getId(), CwProjectInfoData.EXAMINE_BIZ_CODE,currentToken);
                                        //根据报告号查询诗剧苦中是否存在
                                        projectReportByNumber = reviewMapper.getProjectReportByNumber(documentNo);
                                    }while (null != projectReportByNumber && StringUtils.isNotBlank(projectReportByNumber.getReportNo()));
                                    break;
                                case "4":   //验字
                                    do{
                                        documentNo = SpringUtil.getBean ( IWorkAttachmentApi.class ).genSerialNumNoSort(userDTO.getCompanyDTO().getId(), CwProjectInfoData.CHECK_BIZ_CODE,currentToken);
                                        //根据报告号查询诗剧苦中是否存在
                                        projectReportByNumber = reviewMapper.getProjectReportByNumber(documentNo);
                                    }while (null != projectReportByNumber && StringUtils.isNotBlank(projectReportByNumber.getReportNo()));
                                    break;
                                case "5":   //特字
                                    do{
                                        documentNo = SpringUtil.getBean ( IWorkAttachmentApi.class ).genSerialNumNoSort(userDTO.getCompanyDTO().getId(), CwProjectInfoData.SPECIAL_BIZ_CODE,currentToken);
                                        //根据报告号查询诗剧苦中是否存在
                                        projectReportByNumber = reviewMapper.getProjectReportByNumber(documentNo);
                                    }while (null != projectReportByNumber && StringUtils.isNotBlank(projectReportByNumber.getReportNo()));
                                    break;
                            }
                        }
                    } else if ("1".equals(review.getCrossYearFlag())){
                        // 跨年领号：生成上一年度的报告号
                        int lastYear = java.time.Year.now().getValue() - 1;
                        if(StringUtils.isNotBlank(reportData.getReportType())) {
                            ReportNoDto projectReportByNumber = new ReportNoDto();
                            switch (reportData.getReportType()) {
                                case "1":   //基字 - 苏兴会基字
                                    String bizPrefix = "苏兴会基字[" + lastYear + "]";
                                    do{
                                        documentNo = generateCrossYearReportNo(bizPrefix);
                                        //根据报告号查询诗剧苦中是否存在
                                        projectReportByNumber = reviewMapper.getProjectReportByNumber(documentNo);
                                    }while (null != projectReportByNumber && StringUtils.isNotBlank(projectReportByNumber.getReportNo()));
                                    break;
                                case "2":   //咨字 - 苏兴会咨字
                                    String consultPrefix = "苏兴会咨字[" + lastYear + "]";
                                    do{
                                        documentNo = generateCrossYearReportNo(consultPrefix);
                                        //根据报告号查询诗剧苦中是否存在
                                        projectReportByNumber = reviewMapper.getProjectReportByNumber(documentNo);
                                    }while (null != projectReportByNumber && StringUtils.isNotBlank(projectReportByNumber.getReportNo()));
                                    break;
                                case "3":   //审字 - 苏兴会审字
                                    String examinePrefix = "苏兴会审字[" + lastYear + "]";
                                    do{
                                        documentNo = generateCrossYearReportNo(examinePrefix);
                                        //根据报告号查询诗剧苦中是否存在
                                        projectReportByNumber = reviewMapper.getProjectReportByNumber(documentNo);
                                    }while (null != projectReportByNumber && StringUtils.isNotBlank(projectReportByNumber.getReportNo()));
                                    break;
                                case "4":   //验字 - 苏兴会验字
                                    String checkPrefix = "苏兴会验字[" + lastYear + "]";
                                    do{
                                        documentNo = generateCrossYearReportNo(checkPrefix);
                                        //根据报告号查询诗剧苦中是否存在
                                        projectReportByNumber = reviewMapper.getProjectReportByNumber(documentNo);
                                    }while (null != projectReportByNumber && StringUtils.isNotBlank(projectReportByNumber.getReportNo()));
                                    break;
                                case "5":   //特字 - 苏兴会特字
                                    String specialPrefix = "苏兴会特字[" + lastYear + "]";
                                    do{
                                        documentNo = generateCrossYearReportNo(specialPrefix);
                                        //根据报告号查询诗剧苦中是否存在
                                        projectReportByNumber = reviewMapper.getProjectReportByNumber(documentNo);
                                    }while (null != projectReportByNumber && StringUtils.isNotBlank(projectReportByNumber.getReportNo()));
                                    break;
                            }
                        }
                    }

                }
                if(StringUtils.isNotBlank(reportData.getReportNumber())){
                    infoData.setReportNumber(reportData.getReportNumber());
                }else {
                    //报告流水号生成  根据项目编号去new_line表查询
                    String reportNumber = infoMapper.selectReportNumberByProjectNo(reportData.getProjectNumber());
                    if (null != reportNumber) {
                        int stress = 0;
                        stress = reportNumber.indexOf("-");//分隔符位置
                        String substring = reportNumber.substring(stress + 1, reportNumber.length());
                        int integer = Integer.parseInt(substring);

                        String newReportNumber = "";
                        if (integer < 10) {
                            if (integer == 9) {
                                newReportNumber = reportData.getProjectNumber() + "-010";
                            } else {
                                newReportNumber = reportData.getProjectNumber() + "-00" + (integer + 1);
                            }
                        } else {
                            if (integer < 100) {
                                if (integer == 99) {
                                    newReportNumber = reportData.getProjectNumber() + "-100";
                                } else {
                                    newReportNumber = reportData.getProjectNumber() + "-0" + (integer + 1);
                                }
                            } else {
                                newReportNumber = reportData.getProjectNumber() + "-" + (integer + 1);
                            }
                        }

                        infoData.setReportNumber(newReportNumber);
                    } else {
                        infoData.setReportNumber(reportData.getProjectNumber() + "-001");
                    }
                }
                infoData.setReportId(detail.getReportId());
                infoData.setReportNo(documentNo);
                // 如果数据已经生成报告文号，则不重复生成
                CwProjectInfoData byReportId = infoMapper.getByReportId(detail.getReportId());
                if(ObjectUtil.isNotEmpty(byReportId)) {
                    if (StringUtils.isNotBlank(byReportId.getReportNo())) {
                        infoData.setReportNo(byReportId.getReportNo());
                    }
                }
                infoMapper.updateByReportId(detail.getReportId(),infoData.getReportNumber(),infoData.getReportNo());
            }

            if (ObjectUtil.isNotEmpty(reportData)) {
                if (ObjectUtil.isNotEmpty(reportData.getReportDate())) {
                    CwProjectInfoData line = new CwProjectInfoData();
                    line.setReportDate(reportData.getReportDate());
                    line.setReportId(reportData.getId());
                    infoMapper.update(line, new QueryWrapper<CwProjectInfoData>().lambda().eq(CwProjectInfoData::getReportId,reportData.getId()));
                }
                CwProjectReport report = new CwProjectReport();
                if (StringUtils.isNotBlank(reportData.getRegisterAddress())){
                    report.setRegisterAddress(reportData.getRegisterAddress());
                }
                if (StringUtils.isNotBlank(reportData.getBusinessLocation())){
                    report.setBusinessLocation(reportData.getBusinessLocation());
                }
                if (StringUtils.isNotBlank(reportData.getSecurityBusiness())){
                    report.setSecurityBusiness(reportData.getSecurityBusiness());
                }
                if (StringUtils.isNotBlank(reportData.getIndustry())){
                    report.setIndustry(reportData.getIndustry());
                }
                if (StringUtils.isNotBlank(reportData.getBusinessObjects())){
                    report.setBusinessObjects(reportData.getBusinessObjects());
                }
                if (ObjectUtil.isNotEmpty(report)) {
                    cwProjectReportService.updateCwProjectReportDataById(report);
                }
            }
            //当开票编号不为空的时候则将发票表中的programId改为报告id  type改为报告值->2
            if (StringUtils.isNotBlank(reportData.getInvoiceNumber())){
                //根据开票编号查询开票信息
                CwFinanceInvoiceBase cwFinanceInvoiceBase=cwFinanceInvoiceMapper.getByNo(reportData.getInvoiceNumber());
                if (ObjectUtil.isNotEmpty(cwFinanceInvoiceBase)){
                    CwFinanceInvoiceBase invoiceBase = new CwFinanceInvoiceBase();
                    invoiceBase.setId(cwFinanceInvoiceBase.getId());
                    invoiceBase.setProgramId(reportData.getId());
                    invoiceBase.setProgramName(reportData.getReportName());
                    invoiceBase.setType("2");
                    cwFinanceInvoiceBaseMapper.updateById(invoiceBase);
                }
            }


        }
        mapper.updateReviewById(review);
        //对复核数据进行持久化操作
        //保存复核信息  只保存一个下一节点审核人信息
        CwProjectReview cwProjectReview = reviewMapper.selectByReportId(reportData.getId());
        cwProjectReview.setReviewBy(reportData.getReviewBy());
        reviewMapper.updateReviewById(cwProjectReview);
        return review.getId();
    }

    public String add(CwProjectReportData dto) throws Exception{

        //生成主键id值
        String id = UUID.randomUUID().toString().replace("-", "");
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken ( ));
        //保存基本信息
        CwProjectReportNumberApply info = new CwProjectReportNumberApply();
        BeanUtils.copyProperties(dto, info);

        info.setId(id);
        info.setCreateById(userDTO.getId());
        info.setCreateTime(new Date());
        info.setUpdateById(userDTO.getId());
        info.setUpdateTime(new Date());
        info.setReportId(dto.getId());
        // 设置跨年标识，默认为0（当年）
        if (StringUtils.isBlank(info.getCrossYearFlag())) {
            info.setCrossYearFlag("0");
        }
        mapper.insert(info);
        //保存复核信息  只保存一个下一节点审核人信息
        CwProjectReview cwProjectReview = reviewMapper.selectByReportId(dto.getId());
        cwProjectReview.setReviewBy(dto.getReviewBy());
        reviewMapper.updateReviewById(cwProjectReview);

        return info.getId();
    }

    /**
     * 对报告号进行替换
     * @param listA (表中的数据)
     * @param replaceDTOArrayList
     * @param year
     */
    public void replaceNumber(List<NumberReplaceDTO> listA, ArrayList<NumberReplaceDTO> replaceDTOArrayList,String year) {
        if (CollectionUtil.isNotEmpty(listA)){
            for (NumberReplaceDTO dto : listA) {
                NumberReplaceDTO numberReplaceDTO = new NumberReplaceDTO();
                BeanUtils.copyProperties(dto,numberReplaceDTO);

                if (StringUtils.isNotBlank(dto.getReportNo()) && dto.getReportNo().contains("苏兴会")){
                    //获取 苏兴会x字
                    String letterPart = dto.getReportNo().substring(0, dto.getReportNo().lastIndexOf("["));

                    String newNumber = letterPart + "["+ year + "]";
                    int anInt = 0;
                    //判断该报告号的类型是基字,特字,审字,验字
                    String num = reviewMapper.getNewProjectReportByNumber(newNumber);
                    switch (letterPart) {
                        case "苏兴会基字":   //基字
                            //查询对应年份的最新编号
                            if (StringUtils.isNotBlank(num)){
                                //根据编号获取流水号
                                String numberPart = num.substring(num.lastIndexOf("]") + 1, num.lastIndexOf("号"));
                                anInt = Integer.parseInt(numberPart);
                                anInt++;
                                do {
                                    String replaceNumber = letterPart + "["+ year + "]"+anInt+"号";
                                    //根据最新的replaceNumber查询该报告号是否存在
                                    ReportNoDto projectReportByNumber = reviewMapper.getProjectReportByNumber(replaceNumber);
                                    if (ObjectUtil.isEmpty(projectReportByNumber)){
                                        //修改报告号
                                        ReportNoDto reportNoDto = reviewMapper.getProjectReportByNumber(dto.getReportNo());
                                        if (ObjectUtil.isNotEmpty(reportNoDto)){
                                            Integer result = reviewMapper.updateNumberById(replaceNumber, reportNoDto.getId());
                                            if(result == 1){
                                                numberReplaceDTO.setReplaceNo(replaceNumber);
                                            }
                                            break;
                                        }else{
                                            break;
                                        }

                                    }
                                    anInt++;
                                } while (true);

                                //重新赋值
                                replaceDTOArrayList.add(numberReplaceDTO);
                            }
                            break;
                        case "苏兴会咨字":   //咨字
                            //查询对应年份的最新编号
                            if (StringUtils.isNotBlank(num)){
                                //根据编号获取流水号
                                String numberPart = num.substring(num.lastIndexOf("]") + 1, num.lastIndexOf("号"));
                                anInt = Integer.parseInt(numberPart);
                                anInt++;
                                do {
                                    String replaceNumber = letterPart + "["+ year + "]"+anInt+"号";
                                    //根据最新的replaceNumber查询该报告号是否存在
                                    ReportNoDto projectReportByNumber = reviewMapper.getProjectReportByNumber(replaceNumber);
                                    if (ObjectUtil.isEmpty(projectReportByNumber)){
                                        //修改报告号
                                        ReportNoDto reportNoDto = reviewMapper.getProjectReportByNumber(dto.getReportNo());
                                        if (ObjectUtil.isNotEmpty(reportNoDto)){
                                            Integer result = reviewMapper.updateNumberById(replaceNumber, reportNoDto.getId());
                                            if(result == 1){
                                                numberReplaceDTO.setReplaceNo(replaceNumber);
                                            }
                                            break;
                                        }else{
                                            break;
                                        }

                                    }
                                    anInt++;
                                } while (true);

                                //重新赋值
                                replaceDTOArrayList.add(numberReplaceDTO);
                            }
                            break;
                        case "苏兴会审字":   //审字
                            //查询对应年份的最新编号
                            if (StringUtils.isNotBlank(num)){
                                //根据编号获取流水号
                                String numberPart = num.substring(num.lastIndexOf("]") + 1, num.lastIndexOf("号"));
                                anInt = Integer.parseInt(numberPart);
                                anInt++;
                                do {
                                    String replaceNumber = letterPart + "["+ year + "]"+anInt+"号";
                                    //根据最新的replaceNumber查询该报告号是否存在
                                    ReportNoDto projectReportByNumber = reviewMapper.getProjectReportByNumber(replaceNumber);
                                    if (ObjectUtil.isEmpty(projectReportByNumber)){
                                        //修改报告号
                                        ReportNoDto reportNoDto = reviewMapper.getProjectReportByNumber(dto.getReportNo());
                                        if (ObjectUtil.isNotEmpty(reportNoDto)){
                                            Integer result = reviewMapper.updateNumberById(replaceNumber, reportNoDto.getId());
                                            if(result == 1){
                                                numberReplaceDTO.setReplaceNo(replaceNumber);
                                            }
                                            break;
                                        }else{
                                            break;
                                        }

                                    }
                                    anInt++;
                                } while (true);

                                //重新赋值
                                replaceDTOArrayList.add(numberReplaceDTO);
                            }
                            break;
                        case "苏兴会验字":   //验字
                            //查询对应年份的最新编号
                            if (StringUtils.isNotBlank(num)){
                                //根据编号获取流水号
                                String numberPart = num.substring(num.lastIndexOf("]") + 1, num.lastIndexOf("号"));
                                anInt = Integer.parseInt(numberPart);
                                anInt++;
                                do {
                                    String replaceNumber = letterPart + "["+ year + "]"+anInt+"号";
                                    //根据最新的replaceNumber查询该报告号是否存在
                                    ReportNoDto projectReportByNumber = reviewMapper.getProjectReportByNumber(replaceNumber);
                                    if (ObjectUtil.isEmpty(projectReportByNumber)){
                                        //修改报告号
                                        ReportNoDto reportNoDto = reviewMapper.getProjectReportByNumber(dto.getReportNo());
                                        if (ObjectUtil.isNotEmpty(reportNoDto)){
                                            Integer result = reviewMapper.updateNumberById(replaceNumber, reportNoDto.getId());
                                            if(result == 1){
                                                numberReplaceDTO.setReplaceNo(replaceNumber);
                                            }
                                            break;
                                        }else{
                                            break;
                                        }

                                    }
                                    anInt++;
                                } while (true);

                                //重新赋值
                                replaceDTOArrayList.add(numberReplaceDTO);
                            }
                            break;
                        case "苏兴会特字":   //特字
                            //查询对应年份的最新编号
                            if (StringUtils.isNotBlank(num)){
                                //根据编号获取流水号
                                String numberPart = num.substring(num.lastIndexOf("]") + 1, num.lastIndexOf("号"));
                                anInt = Integer.parseInt(numberPart);
                                anInt++;
                                do {
                                    String replaceNumber = letterPart + "["+ year + "]"+anInt+"号";
                                    //根据最新的replaceNumber查询该报告号是否存在
                                    ReportNoDto projectReportByNumber = reviewMapper.getProjectReportByNumber(replaceNumber);
                                    if (ObjectUtil.isEmpty(projectReportByNumber)){
                                        //修改报告号
                                        ReportNoDto reportNoDto = reviewMapper.getProjectReportByNumber(dto.getReportNo());
                                        if (ObjectUtil.isNotEmpty(reportNoDto)){
                                            Integer result = reviewMapper.updateNumberById(replaceNumber, reportNoDto.getId());
                                            if(result == 1){
                                                numberReplaceDTO.setReplaceNo(replaceNumber);
                                            }
                                            break;
                                        }else{
                                            break;
                                        }

                                    }
                                    anInt++;
                                } while (true);

                                //重新赋值
                                replaceDTOArrayList.add(numberReplaceDTO);
                            }
                            break;
                    }
                }
            }
        }

    }

    /**
     * 生成跨年报告号
     * @param reportNoPrefix 报告号前缀，例如：苏兴会基字[2023]
     * @return 生成的报告号，例如：苏兴会基字[2023]723号
     */
    private String generateCrossYearReportNo(String reportNoPrefix) {
        // 查询数据库中该前缀下最大的报告号
        ReportNoDto maxReportNo = reviewMapper.getMaxReportNoByPrefix(reportNoPrefix);
        
        int nextSerialNumber = 1; // 默认从1开始
        
        if (maxReportNo != null && StringUtils.isNotBlank(maxReportNo.getReportNo())) {
            // 提取流水号部分：从最后一个']'后面到'号'前面
            String reportNo = maxReportNo.getReportNo();
            int bracketEndIndex = reportNo.lastIndexOf("]");
            int haoIndex = reportNo.lastIndexOf("号");
            
            if (bracketEndIndex != -1 && haoIndex != -1 && haoIndex > bracketEndIndex) {
                String serialNumberStr = reportNo.substring(bracketEndIndex + 1, haoIndex);
                try {
                    int currentSerialNumber = Integer.parseInt(serialNumberStr);
                    nextSerialNumber = currentSerialNumber + 1;
                } catch (NumberFormatException e) {
                    // 如果解析失败，使用默认值1
                    nextSerialNumber = 1;
                }
            }
        }
        
        // 生成新的报告号：前缀 + 流水号 + "号"
        return reportNoPrefix + nextSerialNumber + "号";
    }
}
