trigger QISTrigger on QIS_Report__c ( before insert,after insert, after update, before update, after delete) { 
 | 
System.debug( '-----:QIS起動'); 
 | 
  
 | 
    // QISの画像を設定する 
 | 
    if (Trigger.isBefore) { 
 | 
        if(Trigger.new.size() == 1 && Trigger.isUpdate){ 
 | 
            List<OlympusCalendar__c> workday = [ 
 | 
                    select Id, Date__c, IsWorkDay__c  
 | 
                      from OlympusCalendar__c  
 | 
                     where Date__c >= :Date.Today() 
 | 
                       and IsWorkDay__c = 1 
 | 
                     order by Date__c 
 | 
                     limit 3]; 
 | 
            if(workday.size()==3){ 
 | 
                Date selectDate = workday[2].Date__c; 
 | 
                for (QIS_Report__c newTarget : Trigger.new) { 
 | 
                    if(newTarget.PAE_AE_DeadLine_Date__c == null && newTarget.QIS_Submit_day__c == Date.today()){ 
 | 
                        newTarget.PAE_AE_DeadLine_Date__c = selectDate; 
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
             
 | 
             
 | 
        } 
 | 
         
 | 
        for (QIS_Report__c newTarget : Trigger.new) { 
 | 
            newTarget.Owner_System__c = newTarget.OwnerId; 
 | 
        } 
 | 
        QISTrigger.setPhotoImgText(Trigger.new, Trigger.old, Trigger.newMap, Trigger.oldMap); 
 | 
         
 | 
    } 
 | 
     
 | 
    // 2020/11/18 songxiaoqi 取的询价的最终用户的管理编码  start  
 | 
    list<String> CnecID = new list<String>(); 
 | 
    if (Trigger.isBefore) { 
 | 
        if (Trigger.isUpdate || Trigger.isInsert) { 
 | 
            for(QIS_Report__c local : Trigger.new) { 
 | 
                QIS_Report__c old = null; 
 | 
                if(Trigger.isUpdate) { 
 | 
                    old = Trigger.oldMap.get(local.Id); 
 | 
                } //&& local.contract_number_ET__c != old.contract_number_ET__c 
 | 
                // LHJ WLIG-C2V9UY Start 
 | 
                // if (Trigger.isInsert || (Trigger.isUpdate && local.contract_number_ET__c != null && local.contract_number_ET__c != old.contract_number_ET__c)){ 
 | 
                //     CnecID.add(local.contract_number_ET__c); 
 | 
                // } 
 | 
                // if (Trigger.isUpdate && old.contract_number_ET__c !=  local.contract_number_ET__c && local.Managementtext__c != null && local.contract_number_ET__c == null ) { 
 | 
                //     local.Managementtext__c = null;                                  
 | 
                // } 
 | 
                if (Trigger.isInsert || (Trigger.isUpdate && local.capital_or_consumable__c == '耗材' && local.contract_number__c != old.contract_number__c)){ 
 | 
                    CnecID.add(local.contract_number__c); 
 | 
                } 
 | 
                // LHJ WLIG-C2V9UY End 
 | 
            }     
 | 
        } 
 | 
    } 
 | 
     
 | 
    if(CnecID.size() > 0){ 
 | 
        Map<String,Statu_Achievements__c>  opstatumap = new Map<String,Statu_Achievements__c>();  
 | 
        List<Statu_Achievements__c>  opstatuList = [select id, 
 | 
                                                        //合同编号 
 | 
                                                        ContractNO__c, 
 | 
                                                        //询价最终用户是的管理号码 
 | 
                                                        Opportunity__r.Account.Management_Code__c, 
 | 
                                                        //询价最终用户爷爷的管理号码 
 | 
                                                        Opportunity__r.Account.Parent.Parent.Management_Code__c, 
 | 
                                                        //记录类型名 
 | 
                                                        Opportunity__r.Account.RecordType.DeveloperName 
 | 
                                                    from Statu_Achievements__c where ContractNO__c in : CnecID 
 | 
                                                    ];                
 | 
        for (Statu_Achievements__c  optsta : opstatuList){ 
 | 
            opstatumap.put(optsta.ContractNO__c,optsta); 
 | 
        } 
 | 
        if (Trigger.isBefore) { 
 | 
            if (Trigger.isUpdate || Trigger.isInsert){ 
 | 
                for (QIS_Report__c orc : Trigger.new){ 
 | 
                    // LHJ WLIG-C2V9UY Start 
 | 
                    // if(orc.contract_number_ET__c != null && opstatumap.containsKey(orc.contract_number_ET__c)) { 
 | 
                    //     //查看客户的记录类型 如果客户名是合同的话就取他parent的管理编码 如果是科室的话就取parent的parent的管理编码 
 | 
                    //     if(opstatumap.get(orc.contract_number_ET__c).Opportunity__r.Account.RecordType.DeveloperName == 'AgencyContract') { 
 | 
                    //         if (opstatumap.get(orc.contract_number_ET__c).Opportunity__r.Account.Management_Code__c != orc.Managementtext__c) { 
 | 
                    //             orc.Managementtext__c = opstatumap.get(orc.contract_number_ET__c).Opportunity__r.Account.Management_Code__c;     
 | 
                    //         } 
 | 
                    //     }else{ 
 | 
                    //         if(opstatumap.get(orc.contract_number_ET__c).Opportunity__r.Account.Parent.Parent.Management_Code__c != orc.Managementtext__c) { 
 | 
                    //             orc.Managementtext__c = opstatumap.get(orc.contract_number_ET__c).Opportunity__r.Account.Parent.Parent.Management_Code__c;     
 | 
                    //         } 
 | 
                    //     }  
 | 
                    // } 
 | 
                    if(orc.capital_or_consumable__c == '耗材' && opstatumap.containsKey(orc.contract_number__c)) { 
 | 
                        //查看客户的记录类型 如果客户名是合同的话就取他parent的管理编码 如果是科室的话就取parent的parent的管理编码 
 | 
                        if(opstatumap.get(orc.contract_number__c).Opportunity__r.Account.RecordType.DeveloperName == 'AgencyContract') { 
 | 
                            if (opstatumap.get(orc.contract_number__c).Opportunity__r.Account.Management_Code__c != orc.Managementtext__c) { 
 | 
                                orc.Managementtext__c = opstatumap.get(orc.contract_number__c).Opportunity__r.Account.Management_Code__c;     
 | 
                            } 
 | 
                        }else{ 
 | 
                            if(opstatumap.get(orc.contract_number__c).Opportunity__r.Account.Parent.Parent.Management_Code__c != orc.Managementtext__c) { 
 | 
                                orc.Managementtext__c = opstatumap.get(orc.contract_number__c).Opportunity__r.Account.Parent.Parent.Management_Code__c;     
 | 
                            } 
 | 
                        }  
 | 
                    } 
 | 
                    // LHJ WLIG-C2V9UY End 
 | 
                }    
 | 
            }     
 | 
        }        
 | 
    } 
 | 
        // 2020/11/18 songxiaoqi 取的询价的最终用户的管理编码  end 
 | 
     
 | 
    // ---------------------------------------------------------------------- 
 | 
    // これより、On-Callの「On-Call跟进结果」を更新するロジック 
 | 
    // ---------------------------------------------------------------------- 
 | 
     
 | 
    if ( ( Trigger.isBefore && ( Trigger.isInsert || Trigger.isUpdate)) || ( Trigger.isAfter && Trigger.isDelete)) {   
 | 
     
 | 
        // 処理対象となるQISを全部確保 
 | 
        List<QIS_Report__c> qiss = ( Trigger.isBefore ? Trigger.New : new List<QIS_Report__c>()); 
 | 
        Set<Id> cncallIds = new Set<Id>(); 
 | 
        for ( QIS_Report__c qis : ( Trigger.isBefore ? Trigger.New : Trigger.Old)) { 
 | 
            cncallIds.add( qis.Source_OnCall__c); 
 | 
        } 
 | 
         
 | 
        // 処理対象となるオンコールを全部確保 
 | 
        List<On_Call__c> oncalls = OncallFinalStatusManager.getOncallList(cncallIds); 
 | 
     
 | 
        // 処理対象となる修理を全部確保 
 | 
        List<Repair__c> repairs = OncallFinalStatusManager.getRepairListFromOncalls(oncalls); 
 | 
         
 | 
        // FinalStatus更新処理 
 | 
        OncallFinalStatusManager.setFinalStatus( oncalls, repairs, qiss); 
 | 
         
 | 
        OncallFinalStatusManager.updOncalls(oncalls); 
 | 
    } 
 | 
     
 | 
    // ---------------------------------------------------------------------- 
 | 
    // ここまで、On-Callの「On-Call跟进结果」を更新するロジック 
 | 
    // ---------------------------------------------------------------------- 
 | 
     
 | 
/* 
 | 
    // ---------------------------------------------------------------------- 
 | 
    // これより、ユーザの経理項目をQISにセット 
 | 
    // ---------------------------------------------------------------------- 
 | 
     
 | 
    if (!Trigger.isDelete) { 
 | 
        // ログインユーザーからプレセス用の上司を設定 
 | 
        User loginUser = null; 
 | 
        for (QIS_Report__c qis : Trigger.new) { 
 | 
            if (loginUser == null) { 
 | 
                loginUser = [SELECT Id, Name, ManagerId, SalesManager__c, BuchangApprovalManagerSales__c, JingliApprovalManager__c, BuchangApprovalManager__c, ZongjianApprovalManager__c FROM User WHERE Id = :UserInfo.getUserId()]; 
 | 
            } 
 | 
            qis.Owner_Manager__c = loginUser.ManagerId == null ? loginUser.Id : loginUser.ManagerId; 
 | 
            qis.SalesManager__c = loginUser.SalesManager__c == null ? loginUser.Id : loginUser.SalesManager__c; 
 | 
            qis.BuchangApprovalManagerSales__c = loginUser.BuchangApprovalManagerSales__c == null ? loginUser.Id : loginUser.BuchangApprovalManagerSales__c; 
 | 
            qis.JingliApprovalManager__c = loginUser.JingliApprovalManager__c == null ? loginUser.Id : loginUser.JingliApprovalManager__c; 
 | 
            qis.BuchangApprovalManager__c = loginUser.BuchangApprovalManager__c == null ? loginUser.Id : loginUser.BuchangApprovalManager__c; 
 | 
            qis.ZongjianApprovalManager__c = loginUser.ZongjianApprovalManager__c == null ? loginUser.Id : loginUser.ZongjianApprovalManager__c; 
 | 
        } 
 | 
    } 
 | 
*/ 
 | 
// Add by HANCHAO 8.25 for approve 
 | 
    List<String> IDlist = new List<String>(); 
 | 
    if(Trigger.isBefore && Trigger.isUpdate ){ 
 | 
        for(QIS_Report__c qis : Trigger.new){ 
 | 
            QIS_Report__c old = new QIS_Report__c(); 
 | 
            old = Trigger.oldMap.get(qis.Id); 
 | 
            if(qis.RC__c !=old.RC__c){ 
 | 
                IDlist.add(qis.RC__c); 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
    List<User> Decider = [SELECT Id, Name, JingliApprovalManager__c, BuchangApprovalManager__c, ZongjianApprovalManager__c, SalesManager__c, BuchangApprovalManagerSales__c FROM User WHERE Id = :IDlist]; 
 | 
    Map<ID,User> QIStoUser = new Map<ID,User>(); 
 | 
    for(user us : Decider){ 
 | 
        if(QIStoUser.containsKey(us.Id)){ 
 | 
            continue; 
 | 
        }else{ 
 | 
            QIStoUser.put(us.Id, us); 
 | 
        } 
 | 
    } 
 | 
    if (Trigger.isBefore && Trigger.isUpdate) { 
 | 
        for (QIS_Report__c qis : Trigger.new) { 
 | 
            QIS_Report__c  old = Trigger.oldMap.get(qis.Id); 
 | 
            // DOJ2.1 gzw AwareDate公式考文本 start 
 | 
            if (qis.AwareDate__c != qis.Aware_date__c && 
 | 
                (qis.Aware_date__c >= Date.newInstance(2021, 9, 1) ||  
 | 
                qis.AwareDate__c >= Date.newInstance(2021, 9, 1) )) { 
 | 
                qis.Aware_date__c = qis.AwareDate__c; 
 | 
            } 
 | 
            // DOJ2.1 gzw AwareDate公式考文本 end 
 | 
  
 | 
            User usl = new User(); 
 | 
  
 | 
  
 | 
            if(old.RC__c != qis.RC__c){ 
 | 
                if(QIStoUser.get(qis.RC__c)!=null){ 
 | 
                usl = QIStoUser.get(qis.RC__c); 
 | 
                }else{ 
 | 
                   continue; 
 | 
                } 
 | 
              
 | 
             // LZHU-BNM4SP 20200420 Start 
 | 
             //qis.ApproveManager__c  = usl.JingliApprovalManager__c == null ? qis.RC__c : usl.JingliApprovalManager__c; 
 | 
             //qis.ApproveBuZhang__c  = usl.BuchangApprovalManager__c == null ? qis.RC__c : usl.BuchangApprovalManager__c; 
 | 
             qis.ApproveManager__c  = usl.SalesManager__c == null ? qis.RC__c : usl.SalesManager__c; 
 | 
             qis.ApproveBuZhang__c  = usl.BuchangApprovalManagerSales__c == null ? qis.RC__c : usl.BuchangApprovalManagerSales__c; 
 | 
             // LZHU-BNM4SP 20200420 End 
 | 
             qis.AppeoveZongJian__c = usl.ZongjianApprovalManager__c == null ? qis.RC__c : usl.ZongjianApprovalManager__c;   
 | 
             }  
 | 
             
 | 
            // HWAG-AWW5GU 字段:未收到实物提醒用邮件  设值 
 | 
            String email = null; 
 | 
            if (qis.Salesdepartment__c.contains('华东')) { 
 | 
                email = 'OCM-HD-member@olympus.com.cn'; 
 | 
            } else if (qis.Salesdepartment__c.contains('东北')) { 
 | 
                email = 'OCM-DB-member@olympus.com.cn'; 
 | 
            } else if (qis.Salesdepartment__c.contains('西北') || qis.Salesdepartment__c.contains('华北')) { 
 | 
                email = 'OCM-BF-member@olympus.com.cn'; 
 | 
            } else if (qis.Salesdepartment__c.contains('华南') || qis.Salesdepartment__c.contains('西南')) { 
 | 
                email = 'OCM-NF-member@olympus.com.cn'; 
 | 
            } 
 | 
            if (qis.Not_Get_Asset_Remind__c != email) { 
 | 
                qis.Not_Get_Asset_Remind__c = email; 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
    // 注释 by Gzw 20200922 Complaint_Number从aws获取,OSH_Affirmant点击按钮时记录相关信息 start 
 | 
   //  //20200221 ljh add start 
 | 
   //  if ( Trigger.isBefore && Trigger.isUpdate || ( Trigger.isAfter && Trigger.isDelete)) {  
 | 
   //      List<QIS_Report__c> newList = Trigger.new; 
 | 
   //      Map<Id,QIS_Report__c> newMap = Trigger.newMap; 
 | 
   //      //newMap.values(); 
 | 
   //      List<QIS_Report__c> oldList = Trigger.old; 
 | 
   //      Map<Id,QIS_Report__c> oldMap = Trigger.oldMap; 
 | 
         
 | 
   //      for(QIS_Report__c qisr : newList){ 
 | 
   //          QIS_Report__c old = oldMap.get(qisr.Id); 
 | 
   //          if(qisr.Complaint_Number__c != old.Complaint_Number__c ){ 
 | 
   //              qisr.OSH_Affirmant__c = UserInfo.getUserId(); 
 | 
   //              qisr.OSH_ConfirmationDate__c = Date.today(); 
 | 
   //          } 
 | 
   //      } 
 | 
   // } 
 | 
    // 注释 by Gzw 20200922 Complaint_Number从aws获取,OSH_Affirmant点击按钮时记录相关信息 end 
 | 
   if (Trigger.isBefore) { 
 | 
       List<QIS_Report__c> newList = Trigger.new; 
 | 
       Date d = Date.valueOf(System.Label.dateJudge); 
 | 
       for(QIS_Report__c qisr : newList){ 
 | 
           if(qisr.QIS_Submit_day__c > d ){ 
 | 
               qisr.isAE_Profile__c = qisr.IS_AE__c; 
 | 
               if(!String.isBlank(qisr.IS_PAE__c)){ 
 | 
                   qisr.isPAE_Profile__c = qisr.IS_PAE__c; 
 | 
               } 
 | 
           } 
 | 
       } 
 | 
    
 | 
   } 
 | 
    //20200221 ljh add end   
 | 
    // 20211011 XinHongLu 小程序-报修子单创建QIS 后 回更 报修子单 状态 Start 
 | 
    if (Trigger.isAfter && Trigger.isInsert) { 
 | 
        List<RepairSubOrder__c> updateRepairSubOrderList = new List<RepairSubOrder__c>(); 
 | 
        List<QIS_Report__c> newList = Trigger.new; 
 | 
  
 | 
        for(QIS_Report__c qisr : newList){ 
 | 
            if (String.isNotBlank(qisr.QisSubOrder__c)) { 
 | 
                RepairSubOrder__c repairSubOrder = new RepairSubOrder__c(); 
 | 
                repairSubOrder.Id = qisr.QisSubOrder__c; 
 | 
                repairSubOrder.Status__c = '已转QIS'; 
 | 
                updateRepairSubOrderList.add(repairSubOrder); 
 | 
            } 
 | 
        } 
 | 
  
 | 
        if (updateRepairSubOrderList.size() >0) { 
 | 
            update updateRepairSubOrderList; 
 | 
        } 
 | 
    } 
 | 
    // 20211011 XinHongLu 小程序-报修子单创建QIS 后 回更 报修子单 状态 End 
 | 
      
 | 
    //wangweipeng        LJPH-C7ZBSE          2021/10/27              start 
 | 
    //当“产品分类1=ET,ENG;产品分类2=耗材”时,系统通知发送给如下4位同事:陈水榕、陈维汉、高治明、李俊彪 
 | 
    //系统通知邮件包含:【QIS申请】、【QIS判定结果通知】、【QIS判定结果(OSH)】、【QIS取消申请】 
 | 
    if (Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)) { 
 | 
        List<QIS_Report__c> newList = Trigger.new; 
 | 
        for(QIS_Report__c qrc : newList){ 
 | 
            if(String.isNotBlank(qrc.nonyushohin__c)){ 
 | 
                if(qrc.ET_QIS_SEND_EMAIL__c){ 
 | 
                    if(String.isBlank(qrc.ET_QIS_SEND_EMAIL1__c)){ 
 | 
                         
 | 
                        qrc.ET_QIS_SEND_EMAIL1__c = 'Shuirong_Chen@olympus.com.cn.stagefull'; 
 | 
                        qrc.ET_QIS_SEND_EMAIL2__c = 'Junbiao_Li@olympus.com.cn.stagefull'; 
 | 
                        qrc.ET_QIS_SEND_EMAIL3__c = 'Weihan_Chen@olympus.com.cn.stagefull'; 
 | 
                        qrc.ET_QIS_SEND_EMAIL4__c = 'Zhiming_Gao@olympus.com.cn.stagefull'; 
 | 
  
 | 
  
 | 
                        /*qrc.ET_QIS_SEND_EMAIL1__c = 'Shuirong_Chen@olympus.com.cn'; 
 | 
                        qrc.ET_QIS_SEND_EMAIL2__c = 'Junbiao_Li@olympus.com.cn'; 
 | 
                        qrc.ET_QIS_SEND_EMAIL3__c = 'Weihan_Chen@olympus.com.cn'; 
 | 
                        qrc.ET_QIS_SEND_EMAIL4__c = 'Zhiming_Gao@olympus.com.cn';*/ 
 | 
                    } 
 | 
                }else{ 
 | 
                    if(String.isNotBlank(qrc.ET_QIS_SEND_EMAIL1__c)){ 
 | 
                        qrc.ET_QIS_SEND_EMAIL1__c = ''; 
 | 
                        qrc.ET_QIS_SEND_EMAIL2__c = ''; 
 | 
                        qrc.ET_QIS_SEND_EMAIL3__c = ''; 
 | 
                        qrc.ET_QIS_SEND_EMAIL4__c = ''; 
 | 
                    } 
 | 
                } 
 | 
            }else{ 
 | 
                if(String.isNotBlank(qrc.ET_QIS_SEND_EMAIL1__c)){ 
 | 
                    qrc.ET_QIS_SEND_EMAIL1__c = ''; 
 | 
                    qrc.ET_QIS_SEND_EMAIL2__c = ''; 
 | 
                    qrc.ET_QIS_SEND_EMAIL3__c = ''; 
 | 
                    qrc.ET_QIS_SEND_EMAIL4__c = ''; 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
    }    
 | 
    //wangweipeng        LJPH-C7ZBSE          2021/10/27              start 
 | 
} 
 |