public with sharing class OCSMTOTMSManageShareHandler extends Oly_TriggerHandler  {
    /**
     * 20240223 you
     * 根据省，ET/ENG，共享办事处对应的ET/ENG窗口 
     */
    public static void OCSMToTMS(List<Dealer_ConsumableManage__c> newList, Map<Id, Dealer_ConsumableManage__c> newMap, List<Dealer_ConsumableManage__c> oldList, Map<Id, Dealer_ConsumableManage__c> oldMap){
        Set<String> TmsPros = new Set<String>();
        String rowCause = Schema.Dealer_ConsumableManage__Share.RowCause.OCSMToTMSUser__c;
        List<OCM_Management_Province__c> mpList = new List<OCM_Management_Province__c>();
        Map<String, OCM_Management_Province__c> mpMap = new Map<String, OCM_Management_Province__c>();

        List<Dealer_ConsumableManage__Share> temsharlist =new List<Dealer_ConsumableManage__Share>();
        List<Id> deletetemsharlist = new List<Id>();

        for (Dealer_ConsumableManage__c newtms : newList) {
            if (Trigger.isInsert || (Trigger.isUpdate && newtms.ET_ENG__c != oldMap.get(newtms.Id).ET_ENG__c)) {
                TmsPros.add(newtms.TMS_Province__c); 
            }            
        }
        
        if(null !=TmsPros && TmsPros.size()>0){
            mpList = [select id,Name,ET_DataWindow__c,ET_BusinessWindow__c,ET_Assistant1__c,ET_Assistant2__c,ENG_DataWindow__c,ENG_BusinessWindow__c,ENG_Assistant1__c,ENG_Assistant2__c from OCM_Management_Province__c where Name in: TmsPros];
            for (OCM_Management_Province__c mp: mpList){
                mpMap.put(mp.Name, mp);
            }          
        }
        if(null!=mpMap){      
            for (Dealer_ConsumableManage__c nObj : newList) {
                if (mpMap.containsKey(nObj.TMS_Province__c)) {
                   if(null!=nObj.ET_ENG__c && ''!= nObj.ET_ENG__c && null!=mpMap.get(nObj.TMS_Province__c)){
                      if(nObj.ET_ENG__c=='ET'){
                        if(null!=mpMap.get(nObj.TMS_Province__c).ET_DataWindow__c && !''.equals(mpMap.get(nObj.TMS_Province__c).ET_DataWindow__c) && nObj.Ownerid !=mpMap.get(nObj.TMS_Province__c).ET_DataWindow__c){
                            Dealer_ConsumableManage__Share tmsshare = new Dealer_ConsumableManage__Share(
                            ParentId = nObj.Id,
                            RowCause = rowCause,
                            UserOrGroupId = mpMap.get(nObj.TMS_Province__c).ET_DataWindow__c,
                            AccessLevel = 'read');
                            temsharlist.add(tmsshare);
                        }
                        if(null!=mpMap.get(nObj.TMS_Province__c).ET_BusinessWindow__c && !''.equals(mpMap.get(nObj.TMS_Province__c).ET_BusinessWindow__c) && nObj.Ownerid !=mpMap.get(nObj.TMS_Province__c).ET_BusinessWindow__c){
                            Dealer_ConsumableManage__Share tmsshare = new Dealer_ConsumableManage__Share(
                            ParentId = nObj.Id,
                            RowCause = rowCause,
                            UserOrGroupId = mpMap.get(nObj.TMS_Province__c).ET_BusinessWindow__c,
                            AccessLevel = 'read');
                            temsharlist.add(tmsshare);
                        } 
						if(null!=mpMap.get(nObj.TMS_Province__c).ET_Assistant1__c && !''.equals(mpMap.get(nObj.TMS_Province__c).ET_Assistant1__c) && nObj.Ownerid !=mpMap.get(nObj.TMS_Province__c).ET_Assistant1__c){
                            Dealer_ConsumableManage__Share tmsshare = new Dealer_ConsumableManage__Share(
                            ParentId = nObj.Id,
                            RowCause = rowCause,
                            UserOrGroupId = mpMap.get(nObj.TMS_Province__c).ET_Assistant1__c,
                            AccessLevel = 'read');
                            temsharlist.add(tmsshare);
                        }
						if(null!=mpMap.get(nObj.TMS_Province__c).ET_Assistant2__c && !''.equals(mpMap.get(nObj.TMS_Province__c).ET_Assistant2__c) && nObj.Ownerid !=mpMap.get(nObj.TMS_Province__c).ET_Assistant2__c){
                            Dealer_ConsumableManage__Share tmsshare = new Dealer_ConsumableManage__Share(
                            ParentId = nObj.Id,
                            RowCause = rowCause,
                            UserOrGroupId = mpMap.get(nObj.TMS_Province__c).ET_Assistant2__c,
                            AccessLevel = 'read');
                            temsharlist.add(tmsshare);
                        }
                      }else{//ENG 
                        if(null!=mpMap.get(nObj.TMS_Province__c).ENG_DataWindow__c && !''.equals(mpMap.get(nObj.TMS_Province__c).ENG_DataWindow__c) && nObj.Ownerid !=mpMap.get(nObj.TMS_Province__c).ENG_DataWindow__c){
                            Dealer_ConsumableManage__Share tmsshare = new Dealer_ConsumableManage__Share(
                            ParentId = nObj.Id,
                            RowCause = rowCause,
                            UserOrGroupId = mpMap.get(nObj.TMS_Province__c).ENG_DataWindow__c,
                            AccessLevel = 'read');
                            temsharlist.add(tmsshare);
                        }
                        if(null!=mpMap.get(nObj.TMS_Province__c).ENG_BusinessWindow__c && !''.equals(mpMap.get(nObj.TMS_Province__c).ENG_BusinessWindow__c) && nObj.Ownerid !=mpMap.get(nObj.TMS_Province__c).ENG_BusinessWindow__c){
                            Dealer_ConsumableManage__Share tmsshare = new Dealer_ConsumableManage__Share(
                            ParentId = nObj.Id,
                            RowCause = rowCause,
                            UserOrGroupId = mpMap.get(nObj.TMS_Province__c).ENG_BusinessWindow__c,
                            AccessLevel = 'read');
                            temsharlist.add(tmsshare);
                        }
						if(null!=mpMap.get(nObj.TMS_Province__c).ENG_Assistant1__c && !''.equals(mpMap.get(nObj.TMS_Province__c).ENG_Assistant1__c) && nObj.Ownerid !=mpMap.get(nObj.TMS_Province__c).ENG_Assistant1__c){
                            Dealer_ConsumableManage__Share tmsshare = new Dealer_ConsumableManage__Share(
                            ParentId = nObj.Id,
                            RowCause = rowCause,
                            UserOrGroupId = mpMap.get(nObj.TMS_Province__c).ENG_Assistant1__c,
                            AccessLevel = 'read');
                            temsharlist.add(tmsshare);
                        }
						if(null!=mpMap.get(nObj.TMS_Province__c).ENG_Assistant2__c && !''.equals(mpMap.get(nObj.TMS_Province__c).ENG_Assistant2__c) && nObj.Ownerid !=mpMap.get(nObj.TMS_Province__c).ENG_Assistant2__c){
                            Dealer_ConsumableManage__Share tmsshare = new Dealer_ConsumableManage__Share(
                            ParentId = nObj.Id,
                            RowCause = rowCause,
                            UserOrGroupId = mpMap.get(nObj.TMS_Province__c).ENG_Assistant2__c,
                            AccessLevel = 'read');
                            temsharlist.add(tmsshare);
                        }
                        
                      }
                      deletetemsharlist.add(nObj.Id);
                   }
                 
               }
            }    
            
        }
          // 先 Delete 后 Insert
        if (deletetemsharlist.size() > 0) {
            List<Dealer_ConsumableManage__Share> deleteList = [SELECT Id
                FROM Dealer_ConsumableManage__Share
                WHERE RowCause = :rowCause
                  AND ParentId IN :deletetemsharlist
            ];
            delete deleteList;
        }
        if(null!=temsharlist){
            insert temsharlist;
        }
    }
}