global class RollupToUnprocessToMailBatch implements Database.Batchable<sObject> {
    
    String query;
    
    global RollupToUnprocessToMailBatch() {
        
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        query  ='select id, status, createdby.name, createddate, targetObject.name ';
        query  = query + 'from processinstance ';
        query  = query + 'where status = \'Pending\' '; 
        // 20210914 ljh  SWAG-C6NAEV start
        query  += ' and createddate > 2021-04-01T00:01:01.000+0000 '; 
        // query  += ' and Id in (\'04g1m000000G0KEAA0\',\'04g10000007rrRJAAY\') ';// 202100915 ljh 测试功能数据
        // 20210914 ljh  SWAG-C6NAEV end  
        query  = query + 'order by createdby.name ';
        
        system.debug(query);
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<sObject> scope) {
        List<id> PiIdlist = new List<id>();
        List<id> UserId   = new List<id>();
        List<Id> TaskId   = new List<id>();
        List<User> toUpdateUser = new List<User>();
        List<processinstance> ScopeList = new List<processinstance>();
        List<ProcessInstanceWorkitem> getTaskDetails = new List<ProcessInstanceWorkitem>();
        Map<id,String> userAndImf = new Map<id,String>();
        ScopeList = scope;
        for(processinstance pi : ScopeList){
            PiIdlist.add(pi.Id);
        }
        getTaskDetails = [SELECT Id, ProcessInstanceId,ProcessInstance.status,ProcessInstance.targetObjectid, 
                                 OriginalActorid, Actorid, IsDeleted, CreatedBy.name 
                          FROM  ProcessInstanceWorkitem
                          where ProcessInstanceId in:PiIdlist ];
        for(ProcessInstanceWorkitem piw: getTaskDetails){
            UserId.add(piw.OriginalActorid);
            if(userAndImf.containsKey(piw.OriginalActorid)){
                String elseCache = userAndImf.get(piw.OriginalActorid);
                // 20210914 ljh 邮件换行 SWAG-C6NAEV start
                // elseCache = elseCache+ System.Label.Environment_Url+piw.ProcessInstance.targetObjectid +'<br/>';
                elseCache = elseCache+'\n'+ System.Label.Environment_Url+piw.ProcessInstance.targetObjectid;
                userAndImf.put(piw.OriginalActorid ,elseCache);
            }else{
                // String InCache = System.Label.Environment_Url+piw.ProcessInstance.targetObjectid+'<br/>';
                String InCache = System.Label.Environment_Url+piw.ProcessInstance.targetObjectid;
                userAndImf.put(piw.OriginalActorid ,InCache);
            }
                // 20210914 ljh 邮件换行 SWAG-C6NAEV end
        }

        toUpdateUser = [select id,name,Unprocessed_Tack_Flag__c,Process_Task_List__c
                        from user 
                        where id in:UserId];
        //生成任务清单字段
            //获取任务清单
        for(User u:toUpdateUser){
            if(userAndImf.containsKey(u.Id)){
                if(u.Unprocessed_Tack_Flag__c==0||u.Unprocessed_Tack_Flag__c==233){
                    u.Process_Task_List__c = userAndImf.get(u.Id);
                    u.Unprocessed_Tack_Flag__c =100;
                }else{
                    // 20210914 ljh 邮件换行 SWAG-C6NAEV  start
                    // u.Process_Task_List__c = u.Process_Task_List__c + userAndImf.get(u.Id);
                    u.Process_Task_List__c = u.Process_Task_List__c +'\n'+ userAndImf.get(u.Id);
                    // 20210914 ljh 邮件换行 SWAG-C6NAEV end
                }
                u.Unprocessed_Tack_Flag__c = u.Unprocessed_Tack_Flag__c+100;
                
            }
        }
        update toUpdateUser;
    }
    
    global void finish(Database.BatchableContext BC) {
        List<User> toUpdateUser = [ select 
                                        id,
                                        Send_Or_Not__c
                                    from 
                                        user
                                    where 
                                        Unprocessed_Tack_Flag__c!=0
                                    ];
        for(user u:toUpdateUser){
            u.Send_Or_Not__c = true;
        }
        update toUpdateUser;
    }

}