19626
2023-10-23 1727a4f4d02e429475608e60f142a63bc24127bc
force-app/main/default/classes/TenderOpportunityLinkHandler.cls
@@ -10,12 +10,13 @@
    this.newList = (List<Tender_Opportunity_Link__c>) Trigger.new;
    this.oldList = (List<Tender_Opportunity_Link__c>) Trigger.old;
}
protected override void afterInsert() {
    // 判断是否跳过
    updateTerminateTender(this.newList);  //lt 20230419 项目终止流程开发 add
    if (!StaticParameter.EscapeTOLinkTrigger) {
        updateTender(this.newList);
        updateOppotunityByInsert(this.newList);
        updateOppotunityByInsert(this.newList);
    }
}
@@ -39,6 +40,189 @@
            if (link.Tender_Opportunity_Uniq__c == null || link.Tender_Opportunity_Uniq__c == '') {
                link.Tender_Opportunity_Uniq__c = link.Tender_information__c + '' + link.Opportunity__c;
            }
        }
    }
}
//DB202306372336  you start
public void UpdateStrategic_department_Opp(List<Tender_Opportunity_Link__c> tenderList,List<Tender_information__c> UpdateTenders){
    Map<String, Set<String>> updateTenMap = new Map<String, Set<String>>();//招标项目id,战略科室分类
    //String partment_Opp='';
        if(tenderList.size() > 0){
            for (Tender_Opportunity_Link__c tender : tenderList){
                 if(String.isNotBlank(tender.Opportunity__r.Department_Class__r.Department_Class_Label__c)){
                    //String ten_Opp =tender.Opportunity__r.Department_Class__r.name.split(' ')[1];
                    //if(tender.Opportunity__r.Department_Class__r.name.split(' ').size() ==3 ){
                    //   ten_Opp =tender.Opportunity__r.Department_Class__r.name.split(' ')[2];
                    //}
                    String ten_Opp =tender.Opportunity__r.Department_Class__r.Department_Class_Label__c;
                    Set<String> temp = new Set<String>();
                    if(ten_Opp.indexOf('其他') == -1){
                        if(updateTenMap.containsKey(tender.Tender_information__c)){
                            temp= updateTenMap.get(tender.Tender_information__c);
                            temp.add(ten_Opp);
                        } else{
                            temp.add(ten_Opp);
                        }
                    }else{//其他归普外
                        if(updateTenMap.containsKey(tender.Tender_information__c)){
                            temp= updateTenMap.get(tender.Tender_information__c);
                            temp.add('普外科');
                        } else{
                            temp.add('普外科');
                        }
                    }
                    updateTenMap.put(tender.Tender_information__c,temp);
                    system.debug(tender.Opportunity__r.Department_Class__r.Department_Class_Label__c+'==temp=='+temp);
                }
            }
            if(null!=updateTenMap && updateTenMap.size()>0){
                 for(String ksy :updateTenMap.keySet()){
                    String partment_Opp=String.join(new List<String>(updateTenMap.get(ksy)), ';');
                    system.debug('=====partment_Opp='+partment_Opp);
                    partment_Opp=partment_Opp.replaceAll(',', ';');
                    if(null ==UpdateTenders || UpdateTenders.size()==0){
                      Tender_information__c tin1 =new Tender_information__c();
                      tin1.id=ksy;
                      tin1.Strategic_department_Opp__c = partment_Opp;
                      UpdateTenders.add(tin1);
                    }else{
                      for (Tender_information__c up : UpdateTenders){
                        if(up.id== ksy){
                           up.Strategic_department_Opp__c = partment_Opp;
                        }else{
                          Tender_information__c tin2 =new Tender_information__c();
                          tin2.id=ksy;
                          tin2.Strategic_department_Opp__c = partment_Opp;
                          UpdateTenders.add(tin2);
                        }
                      }
                    }
                 }
                }
        }
        //if(tenderList.size() > 0){
        //    for (Tender_Opportunity_Link__c tender : tenderList){
        //       if(String.isNotBlank(partment_Opp)){
        //           partment_Opp.substring(0, partment_Opp.length()-1);
        //           if(null ==UpdateTenders || UpdateTenders.size()==0){
        //              Tender_information__c tin1 =new Tender_information__c();
        //              tin1.id=tender.Tender_information__c;
        //              tin1.Strategic_department_Opp__c = partment_Opp;
        //              UpdateTenders.add(tin1);
        //           }else{
        //              for (Tender_information__c up : UpdateTenders){
        //                if(up.id== tender.Tender_information__c){
        //                   up.Strategic_department_Opp__c = partment_Opp;
        //                }else{
        //                  Tender_information__c tin2 =new Tender_information__c();
        //                  tin2.id=tender.Tender_information__c;
        //                  tin2.Strategic_department_Opp__c = partment_Opp;
        //                  UpdateTenders.add(tin2);
        //                }
        //              }
        //           }
        //        }
        //    }
        //}
}
 //DB202306372336  you end
//lt 20230419 项目终止流程开发 终止申请状态清除 add
//项目终止流程开发 -- 清除招标项目终止申请的信息,项目重启标识打勾
public void updateTerminateTender(List<Tender_Opportunity_Link__c> records){
    if (records != null && records.size() > 0){
        List<String> tenders = new List<String>();//DB202305552102 you 关联取消的询价 项目不重启  取一条link
        List<String> tenlinks = new List<String>();//DB202306372336  you start  取当前招标下所有link
        // 获得要更新的招标项目
        for (Tender_Opportunity_Link__c record: records) {
            String lhid=record.Tender_information__c+''+record.Opportunity__c;
            if (!tenders.contains(lhid)) {
                tenders.add(lhid);
            }
            //DB202306372336  you start
            if(!tenlinks.contains(record.Tender_information__c)){
                tenlinks.add(record.Tender_information__c);
            }
            //DB202306372336  you end
            /**
             if (!tenders.contains(record.Tender_information__c)) {
                tenders.add(record.Tender_information__c);
            }
            **/
        }
        /**
        List<Tender_information__c> UpdateTenders = new List<Tender_information__c>();
        if (tenders.size() > 0){
            List<Tender_information__c> tenderList = [SELECT id, status__c, TerminateReason__c, IsTerminate__c,
                                                             TerminateApprovalStatus__c, TerminateApprovalTime__c,
                                                             ProjectRestartFLG__c,subInfoType__c
                                                      FROM Tender_information__c
                                                      WHERE id in :tenders];
            if(tenderList.size() > 0){
                for (Tender_information__c tender : tenderList){
                    //if(tender.status__c == '09.终止' && tender.TerminateReason__c != '经销商原因' && tender.subInfoType__c != '3-1:废标公告' && tender.subInfoType__c != '3-2:流标公告')
                    if(tender.status__c == '09.终止'){
                        tender.ProjectRestartFLG__c = true;
                        tender.IsTerminate__c = null;
                        tender.TerminateApprovalTime__c = null;
                        tender.TerminateApprovalStatus__c = null;
                        UpdateTenders.add(tender);
                    }
                }
            }
        }
        **/
        //DB202305552102 you start
        List<Tender_information__c> UpdateTenders = new List<Tender_information__c>();
        List<Tender_Opportunity_Link__c> tolList = new List<Tender_Opportunity_Link__c>();
        if (tenders.size() > 0) {
           tolList =  [select id, Opportunity__r.stagename,Opportunity__r.Department_Class__r.Department_Class_Label__c, Tender_information__c,Tender_information__r.status__c,Tender_information__r.ProjectRestartFLG__c,Tender_information__r.IsTerminate__c,Tender_information__r.TerminateApprovalTime__c,Tender_information__r.TerminateApprovalStatus__c from Tender_Opportunity_Link__c where Tender_Opportunity_Uniq__c in :tenders];
        }
        if(tolList.size() > 0){
                for (Tender_Opportunity_Link__c tender : tolList){
                    system.debug(tenders+'======='+tender.Tender_information__r.status__c+'=====终止===='+tender.Opportunity__r.stagename);
                    if(tender.Tender_information__r.status__c == '09.终止' && tender.Opportunity__r.stagename !='削除'){
                        Tender_information__c tin =new Tender_information__c();
                        tin.id=tender.Tender_information__c;
                        tin.ProjectRestartFLG__c = true;
                        tin.IsTerminate__c = null;
                        tin.TerminateApprovalTime__c = null;
                        tin.TerminateApprovalStatus__c = null;
                        UpdateTenders.add(tin);
                    }
                }
        }
        //DB202305552102 you end
        //DB202306372336  you start
        List<Tender_Opportunity_Link__c> tenderList = new List<Tender_Opportunity_Link__c>();
        if (tenlinks.size() > 0){
            tenderList = [SELECT id, Opportunity__r.Department_Class__r.Department_Class_Label__c,Tender_information__c
                                                      FROM Tender_Opportunity_Link__c
                                                      WHERE Tender_information__c in :tenlinks];
        }
        if(tenderList.size() > 0){
          UpdateStrategic_department_Opp(tenderList,UpdateTenders);
        }
        system.debug('==需要更新的==='+UpdateTenders);
        //DB202306372336  you end
        if(UpdateTenders.size() > 0){
            StaticParameter.EscapeTenderInformationUpdate =false;
            update UpdateTenders;
            StaticParameter.EscapeTenderInformationUpdate =true;
        }
    }
}
@@ -188,13 +372,15 @@
                        tender.Hospital4__c = fiveHospitalMap.get('Hospital4__c');
                    }
                }
                if (Trigger.isAfter && Trigger.isDelete) {
                StaticParameter.EscapeTenderInformationUpdate = false;
                if (Trigger.isAfter && Trigger.isDelete) {
                    StaticParameter.EscapeOtherUpdateTenOwner = false;
                    update tender_list;
                    StaticParameter.EscapeOtherUpdateTenOwner = true;
                    StaticParameter.EscapeOtherUpdateTenOwner = true;
                } else {
                    update tender_list;
                }
                StaticParameter.EscapeTenderInformationUpdate = true;
            }
        }
    }
@@ -338,6 +524,58 @@
//20220718 you 询价任务 start
//删除link时,任务取消,清空询价中标信息
public void updateOppotunityBeforeDelete(List<Tender_Opportunity_Link__c> records) {
    //DB202306372336  you start
    List<String> tenlinks = new List<String>();// 取当前招标下所有link
    List<String> tenlink = new List<String>();// 取当前删除的link
    if (records != null && records.size() > 0) {
       for (Tender_Opportunity_Link__c record: records) {
           if(!tenlinks.contains(record.Tender_information__c)){
                tenlinks.add(record.Tender_information__c);
            }
            if(!tenlink.contains(record.id)){
                tenlink.add(record.id);
            }
       }
    }
    List<Tender_information__c> UpdateTenders = new List<Tender_information__c>();
    List<Tender_Opportunity_Link__c> tenderList = new List<Tender_Opportunity_Link__c>();
    if (tenlinks.size() > 0){
        tenderList = [SELECT id, Opportunity__r.Department_Class__r.Department_Class_Label__c,Tender_information__c
                                                  FROM Tender_Opportunity_Link__c
                                                  WHERE Tender_information__c in :tenlinks and id not in :tenlink];
     }
    if(tenderList.size() > 0){
       UpdateStrategic_department_Opp(tenderList,UpdateTenders);
     } else{
        //删除招标项目最后一条link
        List<Tender_Opportunity_Link__c> tenderList1 = [SELECT id, Opportunity__r.Department_Class__r.Department_Class_Label__c,Tender_information__c
                                                  FROM Tender_Opportunity_Link__c
                                                  WHERE id in :tenlink];
        system.debug(tenderList1.size()+'==当前删除得link=='+tenlink);
        if(null!=tenderList1 && tenderList1.size()>0){
            for(Tender_Opportunity_Link__c ti :tenderList1){
                  Tender_information__c tin1 =new Tender_information__c();
                  tin1.id=ti.Tender_information__c;
                  tin1.Strategic_department_Opp__c = '';
                  system.debug('删除进来了');
                  UpdateTenders.add(tin1);
            }
        }
     }
     system.debug('=====UpdateTenders====='+UpdateTenders);
    if(UpdateTenders.size() > 0){
       StaticParameter.EscapeTenderInformationUpdate =false;
       update UpdateTenders;
       StaticParameter.EscapeTenderInformationUpdate =true;
    }
//DB202306372336  you end
    if (records != null && records.size() > 0) {
        Set<String> oppTens = new Set<String>();//询价,招标项目 拼接
        List<String> oppIds = new List<String>();
@@ -395,6 +633,7 @@
// 更新询价信息
public void updateOppotunityByDelete(List<Tender_Opportunity_Link__c> records) {
    if (records != null && records.size() > 0) {
        List<String> oppIds = new List<String>();
        //SWAG-CHL67J 【委托】【FY23询价改善】-询价页面/招标项目增加统计字段 fy start 
@@ -444,7 +683,7 @@
                        //20220718 you 询价任务 end
                        **/
                        // 20221028 ljh SWAG-CKL5UC start
                        opp.LeakageNumber__c = null;
                        //opp.LeakageNumber__c = null;  //lt 20230625 DB202306421819 【BUG】漏单数有误 update
                        // 20221028 ljh SWAG-CKL5UC end
                    } else {
                        for (Tender_Opportunity_Link__c link : links) {
@@ -505,102 +744,6 @@
}
 @TestVisible
    public static void testI() {
        integer i = 0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
    }
}