public without sharing class FrameNumController {

    public List<FrameNumManage__c> FnmList{get; set;}
    // public List<FrameNumManage__c> FnmTrueList{get; set;} // 2022-12-08   zyh   add   存储国内采购产品  // 2023-02-07 zyh注释不需要
    // public List<FileAddress__c> FileList{get; set;}  // 2023-02-07 zyh注释不需要
    public List<String> FileStrList{get; set;} //用于存文件名
    public List<testInit> initList{get; set;}
    public List<testInit> newinitList{get; set;}
    public List<testInit> trueInitList{get; set;} // 2022-12-08   zyh   add   用于存储国内采购产品
    public Map<String,FrameNumManage__c> FnmMap{get; set;}
    public Map<String,String> FnmStrMap{get; set;} //用于存管理表的单名
    // public Map<String,FileAddress__c> FileMap{get; set;}  // 2023-02-07 zyh注释不需要
    public Map<String,String> FileStrMap{get; set;} //用于存文件下载链接
    // public List<Map<String,FrameNumManage__c>> mapList{get; set;}  // 2023-02-07 zyh注释不需要
    // public List<String> strList{get; set;}  // 2023-02-07 zyh注释不需要
    public String recordId{get; private set;}
    public FrameNumController(ApexPages.StandardController stdController) { 
        //zhj 2022-12-08 新增了文件地址的AWSkey 和 fileName
        FnmList = [SELECT Id, Name ,ManagementCode__c, ApplyList__c,InspectionCard__c ,InspectionCard_Select__c ,InspectionCard_Select__r.ViewLink__c,InspectionCard_Select__r.DownloadLink__c, ApplyList_Select__c,ApplyList_Select__r.DownloadLink__c,ApplyList_Select__r.ViewLink__c, SerialNumber__c,Material__c , MaterialDepict__c,ApplyList_Select__r.AWS_File_Key__c,ApplyList_Select__r.FileName__c,InspectionCard_Select__r.AWS_File_Key__c,InspectionCard_Select__r.FileName__c from FrameNumManage__c];
        FnmList = new List<FrameNumManage__c>();
        initList = new List<testInit>();
        newinitList = new List<testInit>();
        trueInitList = new List<testInit>(); // 2022-12-08   zyh   add
        recordId = stdController.getId();
    }
    public void init(){
        FnmMap = new Map<String,FrameNumManage__c>();
        FileStrMap = new Map<String,String>();
        FnmStrMap = new Map<String,String>();
        FileStrList = new List<String>();
        if (recordId == null) {
            recordId = ApexPages.currentPage().getParameters().get('id');
        }
        //zhj 2022-12-08 新增了文件地址的AWSkey 和 fileName
        FnmList = [SELECT Id, Name ,
                            ManagementCode__c, 
                            ApplyList__c , 
                            InspectionCard__c ,
                            ApplyList_Link__c,
                            InspectionCard_Link__c,
                            InspectionCard_Select__c ,
                            InspectionCard_Select__r.ViewLink__c,
                            InspectionCard_Select__r.DownloadLink__c, 
                            ApplyList_Select__c,
                            ApplyList_Select__r.DownloadLink__c,
                            ApplyList_Select__r.ViewLink__c,  
                            Num__c , 
                            SerialNumber__c,
                            Material__c , 
                            MaterialDepict__c,
                            ApplyList_Select__r.AWS_File_Key__c,
                            ApplyList_Select__r.FileName__c,
                            InspectionCard_Select__r.AWS_File_Key__c,
                            InspectionCard_Select__r.FileName__c,
                            InspectionCard_NoHave__c,
                            ApplyList_NoHave__c  
                            from FrameNumManage__c 
                            WHERE signInForm__c = :recordId 
                            /*AND HomeBuy__c = false */order by ApplyList_NoHave__c desc,InspectionCard_NoHave__c desc ,SerialNumber__c desc ];
        /*if (FnmList.size()>0) {
            for (FrameNumManage__c nObj : FnmList ) {
                FnmStrMap.put(nObj.ApplyList__c, nObj.ApplyList__c);
            }
            for (String str : FnmStrMap.keySet()) {
                FileStrList.add(str);
            }
            FileList = [SELECT Id, Name, FileName__c,ViewLink__c  FROM FileAddress__c WHERE FileName__c IN: FileStrList];
            for (FileAddress__c file : FileList) {
                FileStrMap.put(file.FileName__c, file.ViewLink__c);
            }
        }*/ // 2022-11-21 zyh 注释：ljh的Batch直接赋值，暂时无需再次查找
        // for (ImportDocT__c nObj : testList) {
        //  if (testMap==null) {
        //      testMap.put(nObj.code__c, nObj);
        //  }else{
        //      testMap.put(nObj.code__c, nObj);
        //  }
        //  if (testMap.size() > 0) {
        //      mapList.add(testMap);
        //  }
        // }
        // for (ImportDocT__c mObj : testMap) {
        //  strList.add(testMap.get(mObj.code__c).size());
        // }
        Integer n = 0; // 初始化合并行数
        Integer n_sj = 0; // 初始化合并行数（商检证） 2023-01-09 zyh add
        for (Integer i=0;i<FnmList.size() ;i++ ) {
            testInit init = new testInit();
            if (i==0) {
                init.Name = FnmList[i].ManagementCode__c;
                init.ProductName = FnmList[i].Material__c + '-' + FnmList[i].MaterialDepict__c;
                // init.code = FnmList[i].ApplyList__c;
                // 2023-02-07   zyh   add   报关单商检证新需求，重新判断   start
                if (String.isNotBlank(FnmList[i].ApplyList_Select__c)) {
                    // 报关单预览链接判断
                    if (String.isNotBlank(FnmList[i].ApplyList_Select__r.ViewLink__c)) {
                        init.code_yl = FnmList[i].ApplyList_Select__r.ViewLink__c;
                    }else{
                        init.code_yl = '';
                    }
                    // 报关单下载链接判断
                    if (String.isNotBlank(FnmList[i].ApplyList_Select__r.DownloadLink__c)) {
                        init.code = FnmList[i].ApplyList_Select__r.DownloadLink__c;
                    }else{
                        init.code = '';
                    }
                    //报关单awkey,filename判断 zhj 2022-12-08 start
                    if (String.isNotBlank(FnmList[i].ApplyList_Select__r.AWS_File_Key__c)) {
                        init.awsKey = FnmList[i].ApplyList_Select__r.AWS_File_Key__c;
                    }else{
                        init.awsKey = '';
                    }
                    if (String.isNotBlank(FnmList[i].ApplyList_Select__r.FileName__c)) {
                        init.fileName = FnmList[i].ApplyList_Select__r.FileName__c;
                    }else{
                        init.fileName = '';
                    }
                    //报关单awkey,filename判断 zhj 2022-12-08 end
                }else {
                    init.code = FnmList[i].ApplyList_NoHave__c;
                    init.code_yl = '';
                    init.awsKey = '';       // zhj 2022-12-08 
                    init.fileName = '';     // zhj 2022-12-08
                } 
                // 2023-02-07   zyh   add   报关单商检证新需求，重新判断   end
                // 2022-12-2   zyh   add   判断报关单并赋值，以文本为主   start
                /*if (String.isNotBlank(FnmList[i].ApplyList__c)) {
                	if (FnmList[i].ApplyList__c.equals('不出证')) {
                		init.code = '不出证';
                	}else{
                		// 报关单预览链接判断
                		if (String.isNotBlank(FnmList[i].ApplyList_Select__r.ViewLink__c)) {
		                	init.code_yl = FnmList[i].ApplyList_Select__r.ViewLink__c;
		                }else{
		                	init.code_yl = '';
		                }
                		// 报关单下载链接判断
                		if (String.isNotBlank(FnmList[i].ApplyList_Select__r.DownloadLink__c)) {
		                	init.code = FnmList[i].ApplyList_Select__r.DownloadLink__c;
		                }else{
		                	init.code = '';
		                }
                        //报关单awkey,filename判断 zhj 2022-12-08 start
                        if (String.isNotBlank(FnmList[i].ApplyList_Select__r.AWS_File_Key__c)) {
		                	init.awsKey = FnmList[i].ApplyList_Select__r.AWS_File_Key__c;
		                }else{
		                	init.awsKey = '';
		                }
                        if (String.isNotBlank(FnmList[i].ApplyList_Select__r.FileName__c)) {
		                	init.fileName = FnmList[i].ApplyList_Select__r.FileName__c;
		                }else{
		                	init.fileName = '';
		                }
                        //报关单awkey,filename判断 zhj 2022-12-08 end
                	}
                }else{
                	init.code = '';
                	init.code_yl = '';
                    init.awsKey = '';       // zhj 2022-12-08 
                    init.fileName = '';     // zhj 2022-12-08
                }*/ // 2023-02-07   zyh   注释，用新逻辑
                // 2022-12-2   zyh   add   判断报关单并赋值，以文本为主   end
                /*if (String.isNotBlank(FnmList[i].ApplyList_Select__r.DownloadLink__c)) {
                	init.code = FnmList[i].ApplyList_Select__r.DownloadLink__c;
                }else{
                	init.code = '不出证';
                }*/ //2022-11-21 zyh add 报关单判断;   2022-12-2   zyh   注释，有新的逻辑判断
                // 2023-02-07   zyh   add   报关单商检证新需求，重新判断   start
                if (String.isNotBlank(FnmList[i].InspectionCard_Select__c)) {
                    // 报关单预览链接判断
                    if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.ViewLink__c)) {
                        init.sj_code_yl = FnmList[i].InspectionCard_Select__r.ViewLink__c;
                    }else{
                        init.sj_code_yl = '';
                    }
                    // 报关单下载链接判断
                    if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.DownloadLink__c)) {
                        init.sj_code = FnmList[i].InspectionCard_Select__r.DownloadLink__c;
                    }else{
                        init.sj_code = '';
                    }
                    //报关单awkey,filename判断 zhj 2022-12-08 start
                    if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.AWS_File_Key__c)) {
                        init.sj_awsKey = FnmList[i].InspectionCard_Select__r.AWS_File_Key__c;
                    }else{
                        init.sj_awsKey = '';
                    }
                    if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.FileName__c)) {
                        init.sj_fileName = FnmList[i].InspectionCard_Select__r.FileName__c;
                    }else{
                        init.sj_fileName = '';
                    }
                    //报关单awkey,filename判断 zhj 2022-12-08 end
                }else {
                    init.sj_code = FnmList[i].InspectionCard_NoHave__c;
                    init.sj_code_yl = '';
                    init.sj_awsKey = '';       // zhj 2022-12-08 
                    init.sj_fileName = '';     // zhj 2022-12-08
                } 
                // 2023-02-07   zyh   add   报关单商检证新需求，重新判断   end
                // 2022-12-2   zyh   add   判断商检证并赋值，以文本为主   start
                /*if (String.isNotBlank(FnmList[i].InspectionCard__c)) {
                	if (FnmList[i].InspectionCard__c.equals('不出证')) {
                		init.sj_code = '不出证';
                	}else{
                		// 商检证预览链接判断
                		if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.ViewLink__c)) {
		                	init.sj_code_yl = FnmList[i].InspectionCard_Select__r.ViewLink__c;
		                }else{
		                	init.sj_code_yl = '';
		                }
                		// 商检证下载链接判断
                		if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.DownloadLink__c)) {
		                	init.sj_code = FnmList[i].InspectionCard_Select__r.DownloadLink__c;
		                }else{
		                	init.sj_code = '';
		                }
                        //商检证awkey,filename判断 zhj 2022-12-19 start
                        if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.AWS_File_Key__c)) {
		                	init.sj_awsKey = FnmList[i].InspectionCard_Select__r.AWS_File_Key__c;
		                }else{
		                	init.sj_awsKey = '';
		                }
                        if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.FileName__c)) {
		                	init.sj_fileName = FnmList[i].InspectionCard_Select__r.FileName__c;
		                }else{
		                	init.sj_fileName = '';
		                }
                        //商检证awkey,filename判断 zhj 2022-12-19 end
                	}
                }else{
                	init.sj_code = '';
                	init.sj_code_yl = '';
                    init.sj_awsKey = '';       // zhj 2022-12-19 
                    init.sj_fileName = '';     // zhj 2022-12-19
                }*/ // 2023-02-07   zyh   注释，用新逻辑
                // 2022-12-2   zyh   add   判断商检证并赋值，以文本为主   end
                /*if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.DownloadLink__c)) {
                	init.sj_code = FnmList[i].InspectionCard_Select__r.DownloadLink__c;
                }else{
                	init.sj_code = '不出证';
                }*/ //2022-11-21 zyh add 商检证判断;   2022-12-2   zyh   注释，有新的逻辑判断
                
                // init.code = FileStrMap.get(FnmList[i].ApplyList__c);
                init.Num = FnmList[i].Num__c;
                init.Id = FnmList[i].Id;
                init.FrameNo = FnmList[i].SerialNumber__c;
                n = n+1;
                n_sj = n_sj+1; // 2023-01-09   zyh   add
                if (i != FnmList.size()-1) {
                    // 判断第一个与第二个是否一样，不一样赋值1
                    // if (FnmList[i].ApplyList__c != FnmList[i+1].ApplyList__c) { // 2023-02-07   zyh   注释
                    if (FnmList[i].ApplyList_NoHave__c != FnmList[i+1].ApplyList_NoHave__c) { // 2023-02-07   zyh   add
                        init.count = n;
                    }
                    // 2023-01-09 zyh  add start
                    // 判断第一个与下一个是否一样，不一样赋值1——商检证
                    // if (FnmList[i].InspectionCard__c != FnmList[i+1].InspectionCard__c) { // 2023-02-07   zyh   注释
                    if (FnmList[i].InspectionCard_NoHave__c != FnmList[i+1].InspectionCard_NoHave__c) { // 2023-02-07   zyh   add
                        init.count_sj = n_sj;
                    }
                    // 2023-01-09 zyh  add end
                }else{
                    init.count = 1; // 2022-12-2   zyh   add   如果只有一条  赋值合并行数
                	init.count_sj = 1; // 2023-01-09   zyh   add   如果只有一条  赋值合并行数
                }
                
            }else {
                // 判断与前一个编码是一致
                // if (FnmList[i].ApplyList__c == FnmList[i-1].ApplyList__c) { // 2023-02-07   zyh   注释
                if (FnmList[i].ApplyList_NoHave__c == FnmList[i-1].ApplyList_NoHave__c) { // 2023-02-07   zyh   add
                    init.Name = FnmList[i].ManagementCode__c;
                    init.ProductName = FnmList[i].Material__c + '-' + FnmList[i].MaterialDepict__c;
                    // init.code = FnmList[i].ApplyList__c;
                    // 2023-02-07   zyh   add   报关单商检证新需求，重新判断   start
                    if (String.isNotBlank(FnmList[i].ApplyList_Select__c)) {
                        // 报关单预览链接判断
                        if (String.isNotBlank(FnmList[i].ApplyList_Select__r.ViewLink__c)) {
                            init.code_yl = FnmList[i].ApplyList_Select__r.ViewLink__c;
                        }else{
                            init.code_yl = '';
                        }
                        // 报关单下载链接判断
                        if (String.isNotBlank(FnmList[i].ApplyList_Select__r.DownloadLink__c)) {
                            init.code = FnmList[i].ApplyList_Select__r.DownloadLink__c;
                        }else{
                            init.code = '';
                        }
                        //报关单awkey,filename判断 zhj 2022-12-08 start
                        if (String.isNotBlank(FnmList[i].ApplyList_Select__r.AWS_File_Key__c)) {
                            init.awsKey = FnmList[i].ApplyList_Select__r.AWS_File_Key__c;
                        }else{
                            init.awsKey = '';
                        }
                        if (String.isNotBlank(FnmList[i].ApplyList_Select__r.FileName__c)) {
                            init.fileName = FnmList[i].ApplyList_Select__r.FileName__c;
                        }else{
                            init.fileName = '';
                        }
                        //报关单awkey,filename判断 zhj 2022-12-08 end
                    }else {
                        init.code = FnmList[i].ApplyList_NoHave__c;
                        init.code_yl = '';
                        init.awsKey = '';       // zhj 2022-12-08 
                        init.fileName = '';     // zhj 2022-12-08
                    } 
                    // 2023-02-07   zyh   add   报关单商检证新需求，重新判断   end
	                // 2022-12-2   zyh   add   判断报关单并赋值，以文本为主   start
	                /*if (String.isNotBlank(FnmList[i].ApplyList__c)) {
	                	if (FnmList[i].ApplyList__c.equals('不出证')) {
	                		init.code = '不出证';
	                	}else{
	                		// 报关单预览链接判断
	                		if (String.isNotBlank(FnmList[i].ApplyList_Select__r.ViewLink__c)) {
			                	init.code_yl = FnmList[i].ApplyList_Select__r.ViewLink__c;
			                }else{
			                	init.code_yl = '';
			                }
	                		// 报关单下载链接判断
	                		if (String.isNotBlank(FnmList[i].ApplyList_Select__r.DownloadLink__c)) {
			                	init.code = FnmList[i].ApplyList_Select__r.DownloadLink__c;
			                }else{
			                	init.code = '';
			                }
                            //报关单awkey,filename判断 zhj 2022-12-08 start
                            if (String.isNotBlank(FnmList[i].ApplyList_Select__r.AWS_File_Key__c)) {
                                init.awsKey = FnmList[i].ApplyList_Select__r.AWS_File_Key__c;
                            }else{
                                init.awsKey = '';
                            }
                            if (String.isNotBlank(FnmList[i].ApplyList_Select__r.FileName__c)) {
                                init.fileName = FnmList[i].ApplyList_Select__r.FileName__c;
                            }else{
                                init.fileName = '';
                            }
                            //报关单awkey,filename判断 zhj 2022-12-08 end
                        }
	                }else{
	                	init.code = '';
	                	init.code_yl = '';
                        init.awsKey = '';       // zhj 2022-12-08 
                        init.fileName = '';     // zhj 2022-12-08
	                }*/  // 2023-02-07   zyh   注释，用新逻辑
	                // 2022-12-2   zyh   add   判断报关单并赋值，以文本为主   end
	                /*if (String.isNotBlank(FnmList[i].ApplyList_Select__r.DownloadLink__c)) {
	                	init.code = FnmList[i].ApplyList_Select__r.DownloadLink__c;
	                }else{
	                	init.code = '不出证';
	                }*/ //2022-11-21 zyh add 报关单判断;   2022-12-2   zyh   注释，有新的逻辑判断
                    // 2023-02-07   zyh   add   报关单商检证新需求，重新判断   start
                    if (String.isNotBlank(FnmList[i].InspectionCard_Select__c)) {
                        // 报关单预览链接判断
                        if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.ViewLink__c)) {
                            init.sj_code_yl = FnmList[i].InspectionCard_Select__r.ViewLink__c;
                        }else{
                            init.sj_code_yl = '';
                        }
                        // 报关单下载链接判断
                        if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.DownloadLink__c)) {
                            init.sj_code = FnmList[i].InspectionCard_Select__r.DownloadLink__c;
                        }else{
                            init.sj_code = '';
                        }
                        //报关单awkey,filename判断 zhj 2022-12-08 start
                        if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.AWS_File_Key__c)) {
                            init.sj_awsKey = FnmList[i].InspectionCard_Select__r.AWS_File_Key__c;
                        }else{
                            init.sj_awsKey = '';
                        }
                        if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.FileName__c)) {
                            init.sj_fileName = FnmList[i].InspectionCard_Select__r.FileName__c;
                        }else{
                            init.sj_fileName = '';
                        }
                        //报关单awkey,filename判断 zhj 2022-12-08 end
                    }else {
                        init.sj_code = FnmList[i].InspectionCard_NoHave__c;
                        init.sj_code_yl = '';
                        init.sj_awsKey = '';       // zhj 2022-12-08 
                        init.sj_fileName = '';     // zhj 2022-12-08
                    } 
                    // 2023-02-07   zyh   add   报关单商检证新需求，重新判断   end
	                // 2022-12-2   zyh   add   判断商检证并赋值，以文本为主   start
	                /*if (String.isNotBlank(FnmList[i].InspectionCard__c)) {
	                	if (FnmList[i].InspectionCard__c.equals('不出证')) {
	                		init.sj_code = '不出证';
	                	}else{
	                		// 商检证预览链接判断
	                		if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.ViewLink__c)) {
			                	init.sj_code_yl = FnmList[i].InspectionCard_Select__r.ViewLink__c;
			                }else{
			                	init.sj_code_yl = '';
			                }
	                		// 商检证下载链接判断
	                		if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.DownloadLink__c)) {
			                	init.sj_code = FnmList[i].InspectionCard_Select__r.DownloadLink__c;
			                }else{
			                	init.sj_code = '';
			                }
                            //商检证awkey,filename判断 zhj 2022-12-19 start
                            if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.AWS_File_Key__c)) {
                                init.sj_awsKey = FnmList[i].InspectionCard_Select__r.AWS_File_Key__c;
                            }else{
                                init.sj_awsKey = '';
                            }
                            if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.FileName__c)) {
                                init.sj_fileName = FnmList[i].InspectionCard_Select__r.FileName__c;
                            }else{
                                init.sj_fileName = '';
                            }
                            //商检证awkey,filename判断 zhj 2022-12-19 end
	                	}
	                }else{
	                	init.sj_code = '';
	                	init.sj_code_yl = '';
                        init.sj_awsKey = '';       // zhj 2022-12-19 
                        init.sj_fileName = '';     // zhj 2022-12-19
	                }*/  // 2023-02-07   zyh   注释，用新逻辑
	                // 2022-12-2   zyh   add   判断商检证并赋值，以文本为主   end
	                /*if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.DownloadLink__c)) {
	                	init.sj_code = FnmList[i].InspectionCard_Select__r.DownloadLink__c;
	                }else{
	                	init.sj_code = '不出证';
	                }*/ //2022-11-21 zyh add 商检证判断;   2022-12-2   zyh   注释，有新的逻辑判断
                    // init.code = FileStrMap.get(FnmList[i].ApplyList__c);
                    init.Num = FnmList[i].Num__c;
                    init.Id = FnmList[i].Id;
                    init.FrameNo = FnmList[i].SerialNumber__c;
                    n = n+1;
                    // 判断是不是最后一个
                    if (i != FnmList.size()-1) {
                        // 不是最后一个，判断与下一个编码一致不一致，不一致直接赋值
                        // if (FnmList[i].ApplyList__c != FnmList[i+1].ApplyList__c) { // 2023-02-07   zyh   注释
                        if (FnmList[i].ApplyList_NoHave__c != FnmList[i+1].ApplyList_NoHave__c) { // 2023-02-07   zyh   add
                            init.count = n;
                        }
                    }
                    // 最后一个直接赋值
                    if (i == FnmList.size()-1) {
                        init.count = n;
                    }
                }else {
                    // 下一组开始
                    n = 0;
                    init.Name = FnmList[i].ManagementCode__c;
                    init.ProductName = FnmList[i].Material__c + '-' + FnmList[i].MaterialDepict__c;
                    // init.code = FnmList[i].ApplyList__c;
                    // 2023-02-07   zyh   add   报关单商检证新需求，重新判断   start
                    if (String.isNotBlank(FnmList[i].ApplyList_Select__c)) {
                        // 报关单预览链接判断
                        if (String.isNotBlank(FnmList[i].ApplyList_Select__r.ViewLink__c)) {
                            init.code_yl = FnmList[i].ApplyList_Select__r.ViewLink__c;
                        }else{
                            init.code_yl = '';
                        }
                        // 报关单下载链接判断
                        if (String.isNotBlank(FnmList[i].ApplyList_Select__r.DownloadLink__c)) {
                            init.code = FnmList[i].ApplyList_Select__r.DownloadLink__c;
                        }else{
                            init.code = '';
                        }
                        //报关单awkey,filename判断 zhj 2022-12-08 start
                        if (String.isNotBlank(FnmList[i].ApplyList_Select__r.AWS_File_Key__c)) {
                            init.awsKey = FnmList[i].ApplyList_Select__r.AWS_File_Key__c;
                        }else{
                            init.awsKey = '';
                        }
                        if (String.isNotBlank(FnmList[i].ApplyList_Select__r.FileName__c)) {
                            init.fileName = FnmList[i].ApplyList_Select__r.FileName__c;
                        }else{
                            init.fileName = '';
                        }
                        //报关单awkey,filename判断 zhj 2022-12-08 end
                    }else {
                        init.code = FnmList[i].ApplyList_NoHave__c;
                        init.code_yl = '';
                        init.awsKey = '';       // zhj 2022-12-08 
                        init.fileName = '';     // zhj 2022-12-08
                    } 
                    // 2023-02-07   zyh   add   报关单商检证新需求，重新判断   end
	                // 2022-12-2   zyh   add   判断报关单并赋值，以文本为主   start
	                /*if (String.isNotBlank(FnmList[i].ApplyList__c)) {
	                	if (FnmList[i].ApplyList__c.equals('不出证')) {
	                		init.code = '不出证';
	                	}else{
	                		// 报关单预览链接判断
	                		if (String.isNotBlank(FnmList[i].ApplyList_Select__r.ViewLink__c)) {
			                	init.code_yl = FnmList[i].ApplyList_Select__r.ViewLink__c;
			                }else{
			                	init.code_yl = '';
			                }
	                		// 报关单下载链接判断
	                		if (String.isNotBlank(FnmList[i].ApplyList_Select__r.DownloadLink__c)) {
			                	init.code = FnmList[i].ApplyList_Select__r.DownloadLink__c;
			                }else{
			                	init.code = '';
			                }
                            //报关单awkey,filename判断 zhj 2022-12-08 start
                            if (String.isNotBlank(FnmList[i].ApplyList_Select__r.AWS_File_Key__c)) {
                                init.awsKey = FnmList[i].ApplyList_Select__r.AWS_File_Key__c;
                            }else{
                                init.awsKey = '';
                            }
                            if (String.isNotBlank(FnmList[i].ApplyList_Select__r.FileName__c)) {
                                init.fileName = FnmList[i].ApplyList_Select__r.FileName__c;
                            }else{
                                init.fileName = '';
                            }
                            //报关单awkey,filename判断 zhj 2022-12-08 end
	                	}
	                }else{
	                	init.code = '';
	                	init.code_yl = '';
                        init.awsKey = '';       // zhj 2022-12-08 
                        init.fileName = '';     // zhj 2022-12-08
	                }*/ // 2023-02-07   zyh   注释，用新逻辑
	                // 2022-12-2   zyh   add   判断报关单并赋值，以文本为主   end
	                /*if (String.isNotBlank(FnmList[i].ApplyList_Select__r.DownloadLink__c)) {
	                	init.code = FnmList[i].ApplyList_Select__r.DownloadLink__c;
	                }else{
	                	init.code = '不出证';
	                }*/ //2022-11-21 zyh add 报关单判断;   2022-12-2   zyh   注释，有新的逻辑判断
                    // 2023-02-07   zyh   add   报关单商检证新需求，重新判断   start
                    if (String.isNotBlank(FnmList[i].InspectionCard_Select__c)) {
                        // 报关单预览链接判断
                        if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.ViewLink__c)) {
                            init.sj_code_yl = FnmList[i].InspectionCard_Select__r.ViewLink__c;
                        }else{
                            init.sj_code_yl = '';
                        }
                        // 报关单下载链接判断
                        if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.DownloadLink__c)) {
                            init.sj_code = FnmList[i].InspectionCard_Select__r.DownloadLink__c;
                        }else{
                            init.sj_code = '';
                        }
                        //报关单awkey,filename判断 zhj 2022-12-08 start
                        if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.AWS_File_Key__c)) {
                            init.sj_awsKey = FnmList[i].InspectionCard_Select__r.AWS_File_Key__c;
                        }else{
                            init.sj_awsKey = '';
                        }
                        if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.FileName__c)) {
                            init.sj_fileName = FnmList[i].InspectionCard_Select__r.FileName__c;
                        }else{
                            init.sj_fileName = '';
                        }
                        //报关单awkey,filename判断 zhj 2022-12-08 end
                    }else {
                        init.sj_code = FnmList[i].InspectionCard_NoHave__c;
                        init.sj_code_yl = '';
                        init.sj_awsKey = '';       // zhj 2022-12-08 
                        init.sj_fileName = '';     // zhj 2022-12-08
                    } 
                    // 2023-02-07   zyh   add   报关单商检证新需求，重新判断   end
	                // 2022-12-2   zyh   add   判断商检证并赋值，以文本为主   start
	                /*if (String.isNotBlank(FnmList[i].InspectionCard__c)) {
	                	if (FnmList[i].InspectionCard__c.equals('不出证')) {
	                		init.sj_code = '不出证';
	                	}else{
	                		// 商检证预览链接判断
	                		if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.ViewLink__c)) {
			                	init.sj_code_yl = FnmList[i].InspectionCard_Select__r.ViewLink__c;
			                }else{
			                	init.sj_code_yl = '';
			                }
	                		// 商检证下载链接判断
	                		if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.DownloadLink__c)) {
			                	init.sj_code = FnmList[i].InspectionCard_Select__r.DownloadLink__c;
			                }else{
			                	init.sj_code = '';
			                }
                            //商检证awkey,filename判断 zhj 2022-12-19 start
                            if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.AWS_File_Key__c)) {
                                init.sj_awsKey = FnmList[i].InspectionCard_Select__r.AWS_File_Key__c;
                            }else{
                                init.sj_awsKey = '';
                            }
                            if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.FileName__c)) {
                                init.sj_fileName = FnmList[i].InspectionCard_Select__r.FileName__c;
                            }else{
                                init.sj_fileName = '';
                            }
                            //商检证awkey,filename判断 zhj 2022-12-19 end
	                	}
	                }else{
	                	init.sj_code = '';
	                	init.sj_code_yl = '';
                        init.sj_awsKey = '';       // zhj 2022-12-19 
                        init.sj_fileName = '';     // zhj 2022-12-19
	                }*/ // 2023-02-07   zyh   注释，用新逻辑
	                // 2022-12-2   zyh   add   判断商检证并赋值，以文本为主   end
	                /*if (String.isNotBlank(FnmList[i].InspectionCard_Select__r.DownloadLink__c)) {
	                	init.sj_code = FnmList[i].InspectionCard_Select__r.DownloadLink__c;
	                }else{
	                	init.sj_code = '不出证';
	                }*/ //2022-11-21 zyh add 商检证判断;   2022-12-2   zyh   注释，有新的逻辑判断
                    // init.code = FileStrMap.get(FnmList[i].ApplyList__c);
                    init.Num = FnmList[i].Num__c;
                    init.Id = FnmList[i].Id;
                    init.FrameNo = FnmList[i].SerialNumber__c;
                    n = n+1;
                    // init.count = 1;
                    // 2022-12-2   zyh   start   如果中途只有一条，需要给count赋值
                    if (i == FnmList.size()-1) {
                        init.count = n;
                    }else{
                        // if (FnmList[i].ApplyList__c != FnmList[i+1].ApplyList__c) { // 2023-02-07   zyh   注释
                    	if (FnmList[i].ApplyList_NoHave__c != FnmList[i+1].ApplyList_NoHave__c) { // 2023-02-07   zyh   add
                            init.count = n;
                        }
                    }
                    // 2022-12-2   zyh   end   如果中途只有一条，需要给count赋值
                }
                // 2023-01-09   zyh   add   start   商检证排序合并
                // if (FnmList[i].InspectionCard__c == FnmList[i-1].InspectionCard__c) { // 2023-02-08   zyh   注释
                if (FnmList[i].InspectionCard_NoHave__c == FnmList[i-1].InspectionCard_NoHave__c) { // 2023-02-08   zyh   add
                    n_sj = n_sj+1;
                    if (i != FnmList.size()-1) {
                        // if (FnmList[i].InspectionCard__c != FnmList[i+1].InspectionCard__c) { // 2023-02-07   zyh   注释
                        if (FnmList[i].InspectionCard_NoHave__c != FnmList[i+1].InspectionCard_NoHave__c) { // 2023-02-07   zyh   add
                            init.count_sj = n_sj;
                        }
                    }else{
                        init.count_sj = n_sj;
                    }
                    System.debug('==========第'+ i +'个，'+n_sj);

                }else{
                    n_sj = 0;
                    n_sj = n_sj+1;
                    if (i == FnmList.size()-1) {
                        init.count_sj = n_sj;
                    }else{
                        // if (FnmList[i].InspectionCard__c != FnmList[i+1].InspectionCard__c) { // 2023-02-07   zyh   注释
                        if (FnmList[i].InspectionCard_NoHave__c != FnmList[i+1].InspectionCard_NoHave__c) { // 2023-02-07   zyh   add
                            init.count_sj = n_sj;
                        }
                    }
                }
                // 2023-01-09   zyh   add   end   商检证排序合并
            }
            initList.add(init);
        }
        // 倒序显示
        for (Integer i = initList.size()-1; i>=0 ; i--) {
            testInit init = new testInit();
            init = initList[i];
            newinitList.add(init);
        }
        /*FnmTrueList = [SELECT Id, Name ,ManagementCode__c, ApplyList__c , InspectionCard__c ,InspectionCard_Select__c ,InspectionCard_Select__r.ViewLink__c,InspectionCard_Select__r.DownloadLink__c, ApplyList_Select__c,ApplyList_Select__r.DownloadLink__c,ApplyList_Select__r.ViewLink__c,  Num__c , SerialNumber__c,Material__c , MaterialDepict__c  from FrameNumManage__c WHERE signInForm__c = :recordId AND HomeBuy__c = true order by ApplyList__c desc ];
        for (FrameNumManage__c nObj : FnmTrueList) {
        	testInit init = new testInit();
        	init.Name = nObj.ManagementCode__c;
            init.ProductName = nObj.Material__c + '-' + nObj.MaterialDepict__c;
            init.FrameNo = nObj.SerialNumber__c;
            init.code = '国内采购品';
            trueInitList.add(init);
        }*/ // 2023-02-07   zyh   注释
    }
    // 定义内部类，自定义参数
    class testInit{
        public Boolean check{get; set;}
        public String ProductName{get; set;}
        public String Name{get; set;}
        public String FrameNo{get; set;}
        public String code{get; set;}
        public String code_yl{get; set;}
        public String sj_code{get; set;}
        public String sj_code_yl{get; set;}
        public Decimal Num{get; set;}
        public String Id{get; set;}
        public Integer count{get; set;}
        public Integer count_sj{get; set;}          //2023-01-09 zyh add 商检证单独合并
        public String awsKey{get; set;}             //zhj 2022-12-08 报关单预览与下载增强
        public String fileName{get; set;}           //zhj 2022-12-08 报关单预览与下载增强
        public String sj_awsKey{get; set;}          //zhj 2022-12-19 商验证预览与下载增强
        public String sj_fileName{get; set;}        //zhj 2022-12-19 商验证预览与下载增强
    }
}