global class OCMTOTMSShareUserBatch  implements Database.Batchable<sObject> {
    String query;
	String targetId;
	private BatchIF_Log__c iflog;
	private String errStr;
    private Map<String, OCM_Management_Province__c> mpMap;
    
	global OCMTOTMSShareUserBatch() {
		iflog = new BatchIF_Log__c();
        iflog.Type__c = 'OCMTOTMSShareUserBatch';
        iflog.Is_Error__c = 0;
        iflog.Log__c = 'OCMTOTMSShareUserBatch start1\n';
        iflog.ErrorLog__c = '';
        insert iflog;
	}
    global OCMTOTMSShareUserBatch(Map<String, OCM_Management_Province__c> mpMap) {
        mpMap = mpMap;
		iflog = new BatchIF_Log__c();
        iflog.Type__c = 'OCMTOTMSShareUserBatch';
        iflog.Is_Error__c = 0;
        iflog.Log__c = 'OCMTOTMSShareUserBatch start2\n';
        iflog.ErrorLog__c = '';
        insert iflog;
	}
    global Database.QueryLocator start(Database.BatchableContext BC) {
        //共享给TMS对象 ET/ENG窗口 根据 省和 ET_ENG  
		query = 'select id,TMS_Province__c,Ownerid,ET_ENG__c from Dealer_ConsumableManage__c ';
        query += ' where ET_ENG__c !=null and TMS_Province__c!=null';
        if (null!=mpMap) {
			query += ' and TMS_Province__c in :mpMap.keySet()';
		}
        return Database.getQueryLocator(query);

    }
    global void execute(Database.BatchableContext BC, List<Dealer_ConsumableManage__c> tmsList) {
        String rowCause = Schema.Dealer_ConsumableManage__Share.RowCause.OCSMToTMSUser__c;
        List<Dealer_ConsumableManage__Share> temsharlist =new List<Dealer_ConsumableManage__Share>();
        if(null == mpMap){
            mpMap = new Map<String, OCM_Management_Province__c>();
            List<OCM_Management_Province__c> mpList = [select id, Name, GI_assistant__c, SP_assistant__c, Energy_assistant__c,Province__c,ET_DataWindow__c,ET_BusinessWindow__c,ET_Assistant1__c,ET_Assistant2__c,ENG_DataWindow__c,ENG_BusinessWindow__c,ENG_Assistant1__c,ENG_Assistant2__c,ENGShareOpp__c,ETShareOpp__c,ENG_Flag__c,ET_Flag__c from OCM_Management_Province__c];
            for (OCM_Management_Province__c mp : mpList) {
                mpMap.put(mp.Name, mp);
            }    
        }
        List<Id> deletetemsharlist = new List<Id>();    
        for (Dealer_ConsumableManage__c nObj : tmsList) {
            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(mpMap.get(nObj.TMS_Province__c).ET_Flag__c==true){
                        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);     
                        }
                        deletetemsharlist.add(nObj.Id);	
                    }
                        
                    }else{//ENG 
                    if(mpMap.get(nObj.TMS_Province__c).ENG_Flag__c==true){
                        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);	
                    }
                    }
                    
                }
                
            }
        }  
        try{
            // 先 Delete 后 Insert
            if (null!=deletetemsharlist && deletetemsharlist.size() > 0) {
                List<Dealer_ConsumableManage__Share> deleteList = [SELECT Id
                    FROM Dealer_ConsumableManage__Share
                    WHERE RowCause = :rowCause
                    AND ParentId IN :deletetemsharlist
                ];
                if(null!=deleteList && deleteList.size()>0){
                    delete deleteList;
                }
                system.debug('删除得TMS共享数据'+deleteList.size());
            }
            system.debug('插入得TMS共享数据'+temsharlist.size());
            if(null!=temsharlist && temsharlist.size()>0){
                insert temsharlist;
            }
        }catch(Exception e){
            errStr += e.getMessage()+'\n';
            errStr += 'TMS插入的共享==='+temsharlist.size()+temsharlist+'\n';
            errStr += 'TMS需要删除的共享==='+deletetemsharlist.size()+'\n'+deletetemsharlist+'\n';
            errStr += e.getLineNumber()+'\n';
        }  
    }
    global void finish(Database.BatchableContext BC) {
        String tmp = '';
        if (String.isNotBlank(errStr)) {
            tmp = errStr;
        }
        iflog.Log__c += 'OCMTOTMSShareUserBatch finish()\n';
        iflog.Log__c += '\n OCMTOTMSShareUserBatch end';

        tmp += iflog.ErrorLog__c;
        if (tmp.length() > 60000) {
          tmp = tmp.substring(0, 60000);
          tmp += ' ...have more lines...';
          iflog.ErrorLog__c = tmp;
        }
        String tmp2 = iflog.Log__c;
        if (tmp2.length() > 60000) {
          tmp2 = tmp2.substring(0, 60000);
          tmp2 += ' ...have more lines...';
          iflog.Log__c = tmp2;
        }
        
        update iflog;
        if(null!=mpMap){  
            Database.executeBatch(new OCMTOIMSShareUserBatch(mpMap),200);
        }  
    } 
     
}