高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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;
    }
 
}