public without sharing class InquiryFormHandler extends Oly_TriggerHandler {
    private Map<Id, Inquiry_form__c> newMap;
      private Map<Id, Inquiry_form__c> oldMap;
      private List<Inquiry_form__c> newList;
      private List<Inquiry_form__c> oldList;

    public InquiryFormHandler(){

     this.newMap = (Map<Id, Inquiry_form__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Inquiry_form__c>) Trigger.oldMap;
        this.newList = (List<Inquiry_form__c>) Trigger.new;
        this.oldList = (List<Inquiry_form__c>) Trigger.old;
        
    }

    protected override void beforeInsert() {
        beforeExecute();
        //客户人员录入后,【电话】【邮箱】自动录入 thh 20220321 start
        // getContactInformation();
        //客户人员录入后,【电话】【邮箱】自动录入 thh 20220321 end
    }
    protected override void beforeUpdate() {
        beforeExecute();
        //客户人员录入后,【电话】【邮箱】自动录入 thh 20220321 start
        // getContactInformation();
        //客户人员录入后,【电话】【邮箱】自动录入 thh 20220321 end
    }

    protected override void afterInsert() {
        //FSE主担当共享
        shareToFSEMain();
    }

     protected override void afterUpdate() {
        shareToFSEMain();
    }
     //Before処理
    private void beforeExecute() {
        Map<Id,Account> accMap = new Map<Id,Account>();
        Inquiry_form__c oObj = null;
        List<String> depIDList = new List<String>();
        List<Account> accList = new List<Account>();
        //20220511 you SWAG-CBUB2W start
        Map<String, OCM_Management_Province__c> mpMap = new Map<String, OCM_Management_Province__c>();
        Set<String> accOCSMset = new Set<String>();
        //20220511 you SWAG-CBUB2W end

        for(Inquiry_form__c nnObj : newList) {
            if (oldMap != null && oldMap.containsKey(nnObj.Id)) {
                oObj = oldMap.get(nnObj.Id);
            }
            if (String.isNotBlank(nnObj.Hospital_Name__c) && (Trigger.isInsert || oObj.Hospital_Name__c != nnObj.Hospital_Name__c)) {// || System.Label.Inquiry_form_flag == 'True')
                depIDList.add(nnObj.Hospital_Name__c);
            }
            if (Trigger.isUpdate && String.isBlank(nnObj.Hospital_Name__c)){
                nnObj.Depart_Owner__c = null;     //战略科室所有人           
                nnObj.OwnerId = nnObj.CreatedById; //所有人
                nnObj.Department_Class__c = null; //战略科室
                nnObj.HospitalName__c = null;     //医院名
                nnObj.Hospital__c = null;         //医院
                nnObj.CreateDate__c = null;      //创建日
            }
            //20220507  SWAG-CBUB2W you start
            if(Trigger.isInsert || oObj.Request1__c != nnObj.Request1__c){
                if(String.isNotBlank(nnObj.Request1__c) && '服务对应' == nnObj.Request1__c && String.isBlank(nnObj.Follow_Content__c)){
                   nnObj.Status__c ='';
                }else if( String.isBlank(nnObj.Status__c) && (nnObj.Request1__c.indexOf('需要报价')!=-1 || nnObj.Request1__c.indexOf('OPD')!=-1 || nnObj.Request1__c.indexOf('产品说明')!=-1 || nnObj.Request1__c.indexOf('寄送产品资料')!=-1)){
                   nnObj.Status__c ='01.未跟进';
                }
                
            }
            //20220507  SWAG-CBUB2W you end
            //20220511 you SWAG-CBUB2W start
            if(Trigger.isUpdate){
                /** 挪到营业跟进页面
                if(false != nnObj.Redistribution__c && String.isBlank(nnObj.GotoDepartment__c)){
                    nnObj.GotoDepartment__c.addError('勾选时必须填写转至科室');
                }
                if(false == nnObj.Redistribution__c && String.isNotBlank(nnObj.GotoDepartment__c)){
                    nnObj.GotoDepartment__c.addError('未勾选时不能填写转至科室');
                }
                **/
                if(String.isNotBlank(nnObj.GotoDepartment__c) && nnObj.GotoDepartment__c != oObj.GotoDepartment__c){
                   nnObj.RedistributionDate__c = System.now();
                   accOCSMset.add(nnObj.OCM_man_province_cus__c);
                }
                if(String.isNotBlank(nnObj.Hospital_Name__c) && String.isNotBlank(oObj.Hospital_Name__c) && nnObj.Hospital_Name__c !=  oObj.Hospital_Name__c ){
                   nnObj.AssistantRedistributionDate__c = System.now();
                }
                
                //20221130 you DB202211530484 start
                if(null!= nnObj.Product_Owner__c && nnObj.Product_Owner__c != oObj.Product_Owner__c){
                   nnObj.ProductAssumeDate__c = System.now();
                   accOCSMset.add(nnObj.OCM_man_province_cus__c);
                }
                if(null!= nnObj.OwnerId && nnObj.OwnerId != oObj.OwnerId){
                   nnObj.AssistantChangeOwnerTime__c = System.now();
                }
                //20221130 you DB202211530484 end
           }
           //20220511 you SWAG-CBUB2W end
        }

        if (depIDList.size()>0) {
            accList  = [Select Id,OwnerId,
                            Hospital__c,Hospital__r.Name,
                            Department_Class__c 
                            from Account where Id =: depIDList]; 
            if (accList.size()>0) {
                for(Account acc : accList){
                    accMap.put(acc.Id, acc);
                }
            }   
        }
        //20220511 you SWAG-CBUB2W start
        if(null !=accOCSMset){
            List<OCM_Management_Province__c> mpList = 
                    [select id, Name, GI_assistant__c,SP_assistant__c from OCM_Management_Province__c where Name =: accOCSMset];

            // 取得OCSM管理省的GI,SP 助理
            for (OCM_Management_Province__c mp : mpList) {
                mpMap.put(mp.Name, mp);
            }
        }
        //20220511 you SWAG-CBUB2W end

        for(Inquiry_form__c nObj : newList) {
            if (accMap.containsKey(nObj.Hospital_Name__c)){
                nObj.Depart_Owner__c = accMap.get(nObj.Hospital_Name__c).OwnerId;                  //战略科室所有人
                //20221128 you DB202211530484  当勾选 转产品担当时，所有人可以手动更改，不需要同步跟科室所有人一致
                if(nObj.Product_Owner__c == false){
                    nObj.OwnerId = accMap.get(nObj.Hospital_Name__c).OwnerId;                          //所有人
                }
                nObj.Department_Class__c = accMap.get(nObj.Hospital_Name__c).Department_Class__c; //战略科室
                nObj.HospitalName__c = accMap.get(nObj.Hospital_Name__c).Hospital__r.Name;        //医院名
                nObj.Hospital__c = accMap.get(nObj.Hospital_Name__c).Hospital__c;                 //医院
            }


            
          //产品信息的拼接
            if (String.isNotBlank(nObj.Product1__c) && String.isNotBlank(nObj.Product1_Manual__c)){
                nObj.Product1__c = nObj.Product1__c +';'+nObj.Product1_Manual__c;
            }else if (String.isBlank(nObj.Product1__c) && String.isNotBlank(nObj.Product1_Manual__c)){
                nObj.Product1__c = nObj.Product1_Manual__c;
            }else if (String.isNotBlank(nObj.Product1__c) && String.isBlank(nObj.Product1_Manual__c)) {
                nObj.Product1__c = nObj.Product1__c;
            }else {
                nObj.Product1__c = '';
            }

            //产品咨询单名称
            if (String.isNotBlank(nObj.Product1__c)&& String.isNotBlank(nObj.HospitalName__c)){
                nObj.Name = nObj.HospitalName__c+'-'+nObj.Product1__c;         
            }else if (String.isBlank(nObj.HospitalName__c)&& String.isNotBlank(nObj.Product1__c)) {
                nObj.Name = nObj.Product1__c;
            }else if (String.isNotBlank(nObj.HospitalName__c)&& String.isBlank(nObj.Product1__c)){
                nObj.Name = nObj.HospitalName__c;                              
            }else{
                nObj.Name = '*';
            }
            
            nObj.Product1_Manual__c = '';
             //20220419 you SWAG-CBUB2W start
            //公式考文本  给FSE担当赋值 这段必须放在这，因为新创建的数据，存在科室为空的情况，
            System.debug(nObj.FSE_Owner_id__c+'==123=='+nObj.FSE_Owner__c);
            if(nObj.FSE_Owner_id__c != nObj.FSE_Owner__c){
                nObj.FSE_Owner__c = nObj.FSE_Owner_id__c;
            }
            if(Trigger.isInsert){
                if( nObj.Request1__c.indexOf('服务对应') !=-1 || nObj.ServiceCorrespond__c ==true){
                    nObj.Service_Status__c ='01.未跟进';
                }
                if(String.isBlank(nObj.Hospital_Name__c)){
                    nObj.CreateDate__c = null;      //创建日
                }else{
                    nObj.CreateDate__c = Date.today();
                }
            }
            if(Trigger.isUpdate){

                if((nObj.Request1__c != oldMap.get(nObj.Id).Request1__c && nObj.Request1__c.indexOf('服务对应') !=-1) || (nObj.ServiceCorrespond__c != oldMap.get(nObj.Id).ServiceCorrespond__c &&  nObj.ServiceCorrespond__c ==true)){
                    if(String.isBlank(nObj.Service_Status__c)){
                       nObj.Service_Status__c ='01.未跟进';
                    }else{

                    }
                }else if( nObj.Request1__c.indexOf('服务对应') ==-1 && nObj.ServiceCorrespond__c ==false){
                    nObj.Service_Status__c ='';
                }
                if(nObj.Hospital_Name__c !=  oldMap.get(nObj.Id).Hospital_Name__c && String.isBlank(oldMap.get(nObj.Id).Hospital_Name__c)){
                    nObj.CreateDate__c = Date.today();
                }
                //20220419 you SWAG-CBUB2W end
                //20220511 you SWAG-CBUB2W start
                system.debug(mpMap+'===='+nObj.OCM_man_province_cus__c);
                if(null!=mpMap && mpMap.containsKey(nObj.OCM_man_province_cus__c)){
                   OCM_Management_Province__c omp = mpMap.get(nObj.OCM_man_province_cus__c);
                    if (omp != null) {
                        nObj.GI_assistant__c = omp.GI_assistant__c;
                        nObj.SP_assistant__c = omp.SP_assistant__c;
                        //nObj.OCSM_ID__c = omp.id;
                    } 
                }
                //20220511 you SWAG-CBUB2W end 
                //不需要日期，跟进日期，服务跟进日期，创建意向日期，比较最早日期
                //20230710 you DB202306544691  增加 转服务跟进日期
                String date1;
                String date2;
                String date3;
                String date4;
                String date6;
                if(null != nObj.No_Need_Date__c){
                  date1=String.valueOf(nObj.No_Need_Date__c);
                }
                if(null != nObj.Follow_Date__c){
                  date2=String.valueOf(nObj.Follow_Date__c);
                }
                if(null != nObj.Confirmation_Date__c){
                  date3=String.valueOf(nObj.Confirmation_Date__c);
                }
                if(null != nObj.Service_Follow_Date__c){
                  date4=String.valueOf(nObj.Service_Follow_Date__c);
                }
                if(null != nObj.Transfer_Service_Follow_Date__c){
                  date6=String.valueOf(nObj.Transfer_Service_Follow_Date__c);
                }

                //方法调用 
                 Date date_MD;
                 if((String.isNotBlank(date1) && nObj.No_Need_Date__c !=oldMap.get(nObj.Id).No_Need_Date__c) || (String.isNotBlank(date2) && nObj.Follow_Date__c !=oldMap.get(nObj.Id).Follow_Date__c) || (String.isNotBlank(date3) && nObj.Confirmation_Date__c !=oldMap.get(nObj.Id).Confirmation_Date__c) || (String.isNotBlank(date4) && nObj.Service_Follow_Date__c !=oldMap.get(nObj.Id).Service_Follow_Date__c) || (String.isNotBlank(date6) && nObj.Transfer_Service_Follow_Date__c !=oldMap.get(nObj.Id).Transfer_Service_Follow_Date__c)){
                    date_MD =getBigTime(date1,date2,date3,date4,date6); 
                    nObj.MinimumDate__c = date_MD;//最小日期
                    String date5 = nObj.CreateDate__c==null?'':String.valueOf(nObj.CreateDate__c);
                    if(null != nObj.CreateDate__c){
                         String confdate=calendarUtil.getWorkDayNum(date5,String.valueOf(date_MD));
                         //nObj.addError(s+'=='+date5+'==='+String.valueOf(date_MD)+'=='+e+'=='+confdate);
                         nObj.Confirmation_days__c =Integer.valueOf(confdate);
                         system.debug('跑这了3');
                    }else{
                         nObj.Confirmation_days__c = null;
                         system.debug('跑这了1');
                    }
                   
                 } 
                 /**
                 //处理老数据
                 if( System.Label.Inquiry_form_flag == 'True' && null != nObj.CreateDate__c){
                    if(String.isNotBlank(date1) || String.isNotBlank(date2) || String.isNotBlank(date3) || String.isNotBlank(date4)){
                       date_MD =getBigTime(date1,date2,date3,date4); 
                       nObj.MinimumDate__c = date_MD;//最小日期
                       String date5 = nObj.CreateDate__c==null?'':String.valueOf(nObj.CreateDate__c);
                       String confdate=calendarUtil.getWorkDayNum(date5,String.valueOf(date_MD));
                       system.debug('跑这了2'+date5+'==='+String.valueOf(date_MD)+'=='+confdate);
                       nObj.Confirmation_days__c =Integer.valueOf(confdate);
                    }
                 }
                 system.debug('==='+date_MD);
                 **/
                }
            
            

        }
    }
  
    //20220419 you SWAG-CBUB2W start
    private void shareToFSEMain() {
        List<Id> deleteTargetAOIdList = new List<Id>();
        //存放(产品咨询单id,共享对象)
         Map<Id,Inquiry_form__Share> insertMap1 = new Map<Id,Inquiry_form__Share>();
         Map<Id,Inquiry_form__Share> insertMap2 = new Map<Id,Inquiry_form__Share>();
         Map<Id,Inquiry_form__Share> insertMap3 = new Map<Id,Inquiry_form__Share>();
         //存放用于的id
         List<Id> userIdList = new List<Id>();
         //存放用于的id 用来删除
         List<Id> userIdListold = new List<Id>();
         //新增一个共享原因
         String rowCause = Schema.Inquiry_form__Share.RowCause.FSE_Owner_c_User__c;
         for(Inquiry_form__c nObj : newList) {
             Inquiry_form__c oObj = null; 
             if (oldMap != null && oldMap.containsKey(nObj.Id)) {
                     oObj = oldMap.get(nObj.Id);
             }

             if ( nObj.FSE_Owner__c != null && (oObj == null || oObj.FSE_Owner__c != nObj.FSE_Owner__c)) {
                 Inquiry_form__Share aos = new Inquiry_form__Share(
                     RowCause = rowCause,
                     ParentId = nObj.Id,
                     UserOrGroupId = nObj.FSE_Owner__c,
                     AccessLevel = 'Edit');
                 //存放（产品咨询单id,共享对象）;
                 insertMap1.put(nObj.Id,aos);
                 //存放用户id 用作检索条件
                 userIdList.add(nObj.FSE_Owner__c);
                 if(oObj != null && oObj.FSE_Owner__c != nObj.FSE_Owner__c){
                   userIdListold.add(oObj.FSE_Owner__c);
                   deleteTargetAOIdList.add(nObj.Id);     
                 }
                 System.debug('insertMap1:'+insertMap1);
                 System.debug('IDlIST:'+deleteTargetAOIdList);
                 //shareToFSEMainPublicMethod(insertMap,userIdList,deleteTargetAOIdList,rowCause,userIdListold);
             }
             if( nObj.GI_assistant__c != null && oObj.GI_assistant__c != nObj.GI_assistant__c){
                  Inquiry_form__Share aos = new Inquiry_form__Share(
                     RowCause = rowCause,
                     ParentId = nObj.Id,
                     UserOrGroupId = nObj.GI_assistant__c,
                     AccessLevel = 'Edit');
                 //存放（产品咨询单id,共享对象）;
                 insertMap2.put(nObj.Id,aos);
                 //存放用户id 用作检索条件
                 userIdList.add(nObj.GI_assistant__c);
                 if(oObj != null && oObj.GI_assistant__c != nObj.GI_assistant__c){
                   userIdListold.add(oObj.GI_assistant__c);
                   deleteTargetAOIdList.add(nObj.Id);     
                 }
                 System.debug('insertMap2:'+insertMap2);
                 System.debug('IDlIST:'+deleteTargetAOIdList);
                 //shareToFSEMainPublicMethod(insertMap,userIdList,deleteTargetAOIdList,rowCause,userIdListold);
             }
             if( nObj.SP_assistant__c != null && oObj.SP_assistant__c != nObj.SP_assistant__c){
                  Inquiry_form__Share aos = new Inquiry_form__Share(
                     RowCause = rowCause,
                     ParentId = nObj.Id,
                     UserOrGroupId = nObj.SP_assistant__c,
                     AccessLevel = 'Edit');
                 //存放（产品咨询单id,共享对象）;
                 insertMap3.put(nObj.Id,aos);
                 //存放用户id 用作检索条件
                 userIdList.add(nObj.SP_assistant__c);
                 if(oObj != null && oObj.SP_assistant__c != nObj.SP_assistant__c){
                   userIdListold.add(oObj.SP_assistant__c);
                   deleteTargetAOIdList.add(nObj.Id);     
                 }
                 System.debug('insertMap3:'+insertMap3);
                 System.debug('IDlIST:'+deleteTargetAOIdList);
                 
             }
             
             
        }
        if(null!= insertMap1){
           shareToFSEMainPublicMethod(insertMap1,userIdList,deleteTargetAOIdList,rowCause,userIdListold);
        }
        if(null!= insertMap2){
           shareToFSEMainPublicMethod(insertMap2,userIdList,deleteTargetAOIdList,rowCause,userIdListold);
        }
        if(null!= insertMap3){
           shareToFSEMainPublicMethod(insertMap3,userIdList,deleteTargetAOIdList,rowCause,userIdListold);
        }
        
        
        
    }    
    public void shareToFSEMainPublicMethod(Map<Id,Inquiry_form__Share> insertMap,List<Id> userIdList,List<Id> deleteTargetAOIdList,String rowCause, List<Id> userIdListold) {
         
         //存放最后需要新增的共享数据
         List<Inquiry_form__Share> lastInsertList = new List<Inquiry_form__Share>();
         //存放已有的相同的共享原因的数据         
         // String rowCause = 'Manual';
         System.debug('rowCause:'+rowCause);
         String ownerCause = 'Owner';
        
         // 先 Delete 后 Insert
         if (deleteTargetAOIdList.size() > 0) {
             List<Inquiry_form__Share> deleteList = [SELECT Id
                 FROM Inquiry_form__Share
                 WHERE RowCause = :rowCause
                   AND UserOrGroupId in : userIdListold
                   AND ParentId IN :deleteTargetAOIdList
             ];
             delete deleteList;
         }
         //判断需要共享的人  是不是创建人 如果是则说明有一条这个人的owner数据 则不新增
         if (insertMap!= null) {
             List<Inquiry_form__Share> ownerList = [SELECT Id,ParentId,UserOrGroupId
                 FROM Inquiry_form__Share
                 WHERE RowCause = :ownerCause
                   AND ParentId IN :insertMap.keySet()
                   AND UserOrGroupId IN :userIdList
             ];
             System.debug('ownerList:'+ownerList);
             if(ownerList.size() > 0){
                 for( Inquiry_form__Share  inq:ownerList){
                     String id = String.valueOf(inq.ParentId);
                     System.debug('id:'+id);
                     if(insertMap.containsKey(id)){
                         insertMap.remove(inq.ParentId);
                     }
                 }
             }
            
         }
         System.debug('insertMap2:'+insertMap);
        
         if(insertMap != null){
                 for(Inquiry_form__Share inquiry : insertMap.values()){
                     lastInsertList.add(inquiry);
                 }
         }
        

         System.debug('共享内容:' +lastInsertList);
         if(lastInsertList.size() > 0){
             insert lastInsertList;
         }
        
     }
     //20220419 you SWAG-CBUB2W end
 //多个日期比较最小日期
    public static Date getBigTime(String date1, String date2, String date3, String date4, String date6) {
        system.debug(date1+'======'+date2+'====='+date3);
         long millis1 =0;
         Long millis2 =0;
         Long millis3 =0;
         Long millis4 =0;
         Long millis6 =0;
         List<Long> list_dateDecimal = new List<Long>();
         if(String.isNotBlank(date1)){
           millis1 = Datetime.valueOf(date1+' 08:00:05').getTime();
           list_dateDecimal.add(millis1);
         }
         if(String.isNotBlank(date2)){
           millis2 = Datetime.valueOf(date2+' 08:00:05').getTime();
           list_dateDecimal.add(millis2);
         }
         if(String.isNotBlank(date3)){
           millis3 = Datetime.valueOf(date3+' 08:00:05').getTime();
           list_dateDecimal.add(millis3);
         }
         if(String.isNotBlank(date4)){
           millis4 = Datetime.valueOf(date4+' 08:00:05').getTime();
           list_dateDecimal.add(millis4);
         }
         if(String.isNotBlank(date6)){
           millis6 = Datetime.valueOf(date6+' 08:00:05').getTime();
           list_dateDecimal.add(millis6);
         }
         
         list_dateDecimal.sort();
         Date date_Mid;
         if(null !=list_dateDecimal && list_dateDecimal.size()>0){
             Long aaa=list_dateDecimal[0];
             DateTime dt =DateTime.newInstance(aaa);
             date_Mid = dt.dateGmt();
             // DateTime.newInstance
             system.debug('最小的==='+date_Mid+list_dateDecimal);
         }
        
         return date_Mid;
      
    }
//俩个日期之间的工作日
    public integer diff(Datetime sdate, Datetime edate){
        integer count=0;
        Date d1 = Date.valueOf(sdate);
        Date d2 = Date.valueOf(edate);
        integer wDay = d1.daysBetween(d2) ;

        do{
        if(sdate.format('EEEE') == 'Saturday') {
        count++;
        }
        if(sdate.format('EEEE') == 'Sunday') {
        count++;
        }
        sdate= sdate.adddays(1);
        }while(sdate<=edate);

        wDay = wDay-count+1;
        system.debug('*************'+wDay);
        return wDay;
    }
    //客户人员录入后,【电话】【邮箱】自动录入 thh 20220321 start
    // private void getContactInformation(){
    //     List<String> ContactIdList = new List<String>();
    //     List<Contact> ContactList = new List<Contact>();
    //     Map<String, Contact> ContactMap = new Map<String, Contact>();
    //     for(Inquiry_form__c nnObj : newList) {
    //         if(String.isNotBlank(nnObj.Contact_Name__c)){
    //             ContactIdList.add(nnObj.Contact_Name__c);
    //         }
    //     }
    //     if(ContactIdList.size() > 0){
    //         ContactList = [select id, Phone, Email from Contact where Id IN :ContactIdList];
    //         for(Contact contact : ContactList){
    //             ContactMap.put(contact.Id, contact);
    //         }
    //     }
    //     for(Inquiry_form__c nnObj : newList) {
    //         if(String.isNotBlank(nnObj.Contact_Name__c)){
    //             if(String.isNotBlank(ContactMap.get(nnObj.Contact_Name__c).Phone)){
    //                 nnObj.Phone__c = ContactMap.get(nnObj.Contact_Name__c).Phone;
    //             }
    //             if(String.isNotBlank(ContactMap.get(nnObj.Contact_Name__c).Email)){
    //                 nnObj.Email__c = ContactMap.get(nnObj.Contact_Name__c).Email;
    //             }
    //         }
    //     }
    // }
    //客户人员录入后,【电话】【邮箱】自动录入 thh 20220321 end

     // 战略科室的主担当 を取得、Inquiry_form__Shareに設定(Read)
    // private void shareToOSCM() {
    //     //存放用于新增的共享数据
    //     List<Inquiry_form__Share> insertList = new List<Inquiry_form__Share>();
    //     //存放(会议询问单id,共享对象)
    //     Map<Id,Inquiry_form__Share> insertMap = new Map<Id,Inquiry_form__Share>();
    //     //存放最后需要新增的共享数据
    //     List<Inquiry_form__Share> lastInsertList = new List<Inquiry_form__Share>();
    //     //存放已有的相同的共享原因的数据
    //     List<Id> deleteTargetAOIdList = new List<Id>();
    //     //存放用于的id
    //     List<Id> userIdList = new List<Id>();
    //     // String rowCause = 'Manual';
    //     //新增一个共享原因
    //     String rowCause = Schema.Inquiry_form__Share.RowCause.OCSM_Owner_c_User__c;
    //     System.debug('rowCause:'+rowCause);
    //     String ownerCause = 'Owner';
    //     //Apex共有の理由名OCSM_Owner_c_User
    //     for(Inquiry_form__c nObj : newList) {
    //         Inquiry_form__c oObj = null;
    //         System.debug('战略科室担当：'+nObj.Depart_Owner__c);
    //         if (oldMap != null && oldMap.containsKey(nObj.Id)) {
    //             oObj = oldMap.get(nObj.Id);
    //         }
            

    //         if ( nObj.Depart_Owner__c != null && (oObj == null || oObj.Depart_Owner__c != nObj.Depart_Owner__c)) {
    //             Inquiry_form__Share aos = new Inquiry_form__Share(
    //                     RowCause = rowCause,
    //                     ParentId = nObj.Id,
    //                     UserOrGroupId = nObj.Depart_Owner__c,
    //                     AccessLevel = 'Edit');
    //             //存放要新增的共享数据
    //             insertList.add(aos);
    //             System.debug('key:'+nObj.Id);
    //             //存放（会议询问单id,共享对象）;
    //             insertMap.put(nObj.Id,aos);
    //             //存放用户id 用作检索条件
    //             userIdList.add(nObj.Depart_Owner__c);
    //             if (oObj != null && oObj.Depart_Owner__c != nObj.Depart_Owner__c) {
    //                 deleteTargetAOIdList.add(nObj.Id);
    //             }
    //         }
    //     }
    //     System.debug('insertList:'+insertList);
    //     System.debug('insertMap1:'+insertMap);
    //     System.debug('IDlIST:'+deleteTargetAOIdList);
    //     // 先 Delete 后 Insert
    //     if (deleteTargetAOIdList.size() > 0) {
    //         List<Inquiry_form__Share> deleteList = [SELECT Id
    //             FROM Inquiry_form__Share
    //             WHERE RowCause = :rowCause
    //               AND ParentId IN :deleteTargetAOIdList
    //         ];
    //         delete deleteList;
    //     }
    //     //判断需要共享的人  是不是创建人 如果是则说明有一条这个人的owner数据 则不新增
    //     if (insertMap!= null) {
    //         List<Inquiry_form__Share> ownerList = [SELECT Id,ParentId,UserOrGroupId
    //             FROM Inquiry_form__Share
    //             WHERE RowCause = :ownerCause
    //               AND ParentId IN :insertMap.keySet()
    //               AND UserOrGroupId IN :userIdList
    //         ];
    //         System.debug('ownerList:'+ownerList);
    //         if(ownerList.size() > 0){
    //             for( Inquiry_form__Share  inq:ownerList){
    //                 String id = String.valueOf(inq.ParentId);
    //                 System.debug('id:'+id);
    //                 if(insertMap.containsKey(id)){
    //                     insertMap.remove(inq.ParentId);
    //                 }
    //             }
    //         }
            
    //     }
    //     System.debug('insertMap2:'+insertMap);
        
    //     if(insertMap != null){
    //             for(Inquiry_form__Share inquiry : insertMap.values()){
    //                 lastInsertList.add(inquiry);
    //             }
    //     }
        

    //     System.debug('共享内容:' +lastInsertList);
    //     if(lastInsertList.size() > 0){
    //         insert lastInsertList;
    //     }
        
    // }
   

     @TestVisible private static void test() {
        //可以多写点
    }
}