// 20210917 ljh 处理历史数据 后续用不到
|
global class rentalApplyHistoryBatch implements Database.Batchable<sObject> {
|
public String query;
|
public String type;
|
global rentalApplyHistoryBatch() {
|
this.query = query;
|
}
|
global rentalApplyHistoryBatch(String type) {
|
this.query = query;
|
this.type = type;
|
}
|
|
global Database.QueryLocator start(Database.BatchableContext bc) {
|
query = '';
|
query += 'select Id,Root_Rental_Apply__c,CreatedDate from Rental_Apply__c where ';
|
if(type == '1'){
|
query += ' Root_Rental_Apply__c != null and Split_Apply_Reason__c != \'补充借用\' ';
|
}else{
|
// query += 'RA_Status__c not in ('取消','完了','草案中','已回收','已回寄','欠品中') and Root_Rental_Apply__c = null';
|
query += ' Root_Rental_Apply__c = null ';
|
// query += 'and RA_Status__c not in (\'取消\',\'完了\',\'草案中\',\'已回收\',\'已回寄\',\'欠品中\')';
|
query += 'and RA_Status__c not in (\'取消\',\'完了\',\'草案中\')';
|
|
}
|
// CreatedDate
|
query += ' and First_level_actual_approver__c = null and Second_level_actual_approver__c = null and Request_demo_time__c > 2019-05-01T00:00:00.000+0000 order by Request_demo_time__c';
|
system.debug('query==='+query);
|
return Database.getQueryLocator(query);
|
}
|
global void execute(Database.BatchableContext BC, list<Sobject> scope) {
|
List<Rental_Apply__c> raList = new List<Rental_Apply__c>();
|
List<Rental_Apply_Equipment_Set__c> raesList = new List<Rental_Apply_Equipment_Set__c>();
|
Set<Id> rootIdSet = new Set<Id>();
|
List<Id> IdList = new List<Id>();
|
Map<Id,List<Id>> rootMap = new Map<Id,List<Id>>();
|
Map<Id,List<String>> rootSPMap = new Map<Id,List<String>>();
|
Set<Id> IdSet = new Set<Id>();
|
List<Rental_Apply__c> uRaList = new List<Rental_Apply__c>();
|
if(type == '1'){
|
ralist = scope;
|
for(Rental_Apply__c ra:ralist){
|
rootIdSet.add(ra.Root_Rental_Apply__c);
|
if(rootMap.containsKey(ra.Root_Rental_Apply__c)){
|
IdList = rootMap.get(ra.Root_Rental_Apply__c);
|
IdList.add(ra.Id);
|
rootMap.put(ra.Root_Rental_Apply__c,IdList);
|
}else{
|
IdList = new List<Id>();
|
IdList.add(ra.Id);
|
rootMap.put(ra.Root_Rental_Apply__c,IdList);
|
}
|
}
|
}else{
|
raList = scope;
|
for(Rental_Apply__c ra:raList){
|
rootIdSet.add(ra.Id);
|
if(rootMap.containsKey(ra.Id)){
|
continue;
|
}else{
|
IdList = new List<Id>();
|
IdList.add(ra.Id);
|
rootMap.put(ra.Id,IdList);
|
}
|
}
|
}
|
system.debug('====00:'+rootMap);
|
List<ProcessInstance> piList= [SELECT Id
|
,CompletedDate
|
,LastActorId
|
,ProcessDefinitionId
|
,Status
|
,TargetObjectId,
|
(
|
SELECT Id,
|
ProcessNode.DeveloperName,
|
CreatedDate,
|
StepStatus,
|
OriginalActorId,
|
ActorId,
|
Actor.Name,
|
Comments
|
FROM StepsAndWorkitems
|
order by CreatedDate desc
|
)
|
FROM
|
ProcessInstance
|
WHERE
|
TargetObjectId IN :rootIdSet
|
AND ProcessDefinitionId IN ('04a10000000bpagAAA','04a10000000bpafAAA','04a10000000bpaiAAA','04a10000000bpZCAAY','04a10000000bmuZAAQ','04a10000000LGRBAA4'
|
,'04a10000000bmubAAA'
|
,'04a10000000g2ebAAA'
|
,'04a10000000bpZDAAY'
|
,'04a10000000bpahAAA'
|
,'04a10000000g2TCAAY'
|
,'04a10000000bmudAAA'
|
,'04a10000000bpZEAAY'
|
)
|
ORDER BY CreatedDate DESC
|
];
|
for(ProcessInstance pi:piList){
|
List<String> sList = new List<String>();
|
system.debug('zheli:'+pi.StepsAndWorkitems.size());
|
|
if(!IdSet.contains(pi.TargetObjectId)){
|
IdSet.add(pi.TargetObjectId);
|
for(Integer i = 0;i< pi.StepsAndWorkitems.size();i++){
|
if(String.isNotBlank(pi.StepsAndWorkitems[i].ProcessNode.DeveloperName) &&
|
(
|
(pi.StepsAndWorkitems[i].ProcessNode.DeveloperName == 'Buchang_approval' && pi.StepsAndWorkitems[i].StepStatus == 'Approved')
|
|| (pi.StepsAndWorkitems[i].ProcessNode.DeveloperName == 'Manager_loaner_approval' && pi.StepsAndWorkitems[i].StepStatus == 'Approved')
|
)
|
){
|
if(rootSPMap.containsKey(pi.TargetObjectId) && rootSPMap.get(pi.TargetObjectId).size() == 2){
|
if(pi.StepsAndWorkitems[i].ProcessNode.DeveloperName == 'Manager_loaner_approval'){
|
sList = rootSPMap.get(pi.TargetObjectId);
|
sList.add(1,pi.StepsAndWorkitems[i].Actor.Name);
|
rootSPMap.put(pi.TargetObjectId,sList);
|
}
|
}else if(!rootSPMap.containsKey(pi.TargetObjectId)){
|
System.debug('zheli130');
|
sList = new List<String>();
|
//顺序不可以调换
|
if(pi.StepsAndWorkitems[i].ProcessNode.DeveloperName == 'Manager_loaner_approval'){
|
sList.add('');
|
}
|
sList.add(pi.StepsAndWorkitems[i].Actor.Name);
|
if(pi.StepsAndWorkitems[i].ProcessNode.DeveloperName == 'Buchang_approval'){
|
sList.add('');
|
}
|
rootSPMap.put(pi.TargetObjectId,sList);
|
}
|
}
|
}
|
}
|
}
|
system.debug('====11:'+rootSPMap);
|
for(Id key:rootMap.keySet()){
|
if(rootSPMap != null && rootSPMap.get(key) != null && rootSPMap.get(key).size() > 0){
|
|
if(type == '2'){
|
Rental_Apply__c tempRaRoot = new Rental_Apply__c();
|
tempRaRoot.Id = key;
|
if(rootSPMap.get(key).size() > 1){
|
tempRaRoot.First_level_actual_approver__c = rootSPMap.get(key)[1];
|
tempRaRoot.Second_level_actual_approver__c = rootSPMap.get(key)[0];
|
}
|
uRaList.add(tempRaRoot);
|
}
|
if(type == '1'){
|
system.debug('zheli11:'+rootMap.get(key));
|
for(Id rId:rootMap.get(key)){
|
Rental_Apply__c tempRa = new Rental_Apply__c();
|
tempRa.Id = rId;
|
if(rootSPMap.get(key).size() > 1){
|
tempRa.First_level_actual_approver__c = rootSPMap.get(key)[1];
|
tempRa.Second_level_actual_approver__c = rootSPMap.get(key)[0];
|
}
|
uRaList.add(tempRa);
|
}
|
}
|
}
|
}
|
system.debug('zheli00:'+uRaList);
|
update uRaList;
|
}
|
|
global void finish(Database.BatchableContext BC) {
|
if(System.Test.isRunningTest()){
|
test();
|
}
|
}
|
@TestVisible private static void test() {
|
Integer i = 0;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
}
|
}
|