liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
global class AgencyQuolifiedExpireBatch implements Database.Batchable<sObject> {
    String query;
    global AgencyQuolifiedExpireBatch() {
        
    }
 
    global Database.QueryLocator start(Database.BatchableContext BC) {
        query = 'select id, UnshippedAmount_F__c,Opportunity__c from Statu_Achievements__c where UnshippedAmount_F__c >=1 and Status_2_Formula__c NOT IN (\'无效\', \'冻结\', \'16 已结束\',\'14 已发货\')'; 
        return Database.getQueryLocator(query);
        
    }
 
    global void execute(Database.BatchableContext BC, List<SObject> scope) {
        List<id> OppIdlist = new List<id>();
        List<Statu_Achievements__c> StatuAchievementList = new List<Statu_Achievements__c>();
        StatuAchievementList = scope;
        List<Opportunity> OpportunityList = new List<Opportunity>();
 
        Map<id, Statu_Achievements__c> saMap = new Map<id, Statu_Achievements__c>();
        for (Statu_Achievements__c sa : StatuAchievementList) {
            if(!saMap.containskey(sa.Opportunity__c)){
                OppIdlist.add(sa.Opportunity__c);
            }
            saMap.put(sa.Opportunity__c, sa);
        }
 
        // CHAN-BEZB4N 20190819 Start
        System.debug('OppIdlist:::::' + OppIdlist);
        // String[] stageNames = new String[] {'询价', '取消', '失单'};
        String[] stageNames = new String[] {'询价', '取消', '失单', '完毕'};
        // CHAN-BEZB4N 20190819 End
 
 
        OpportunityList = [select id, Agency1__r.id, StageName__c,Agency1__r.Name, Sales_assistantWIN_ID__c
                          FROM  Opportunity
                          where Id in:OppIdlist and StageName__c NOT IN :stageNames];
 
        System.debug('OpportunityList:::::' + OpportunityList);
 
        
        Map<id, Opportunity> accoppMap = new Map<id, Opportunity>();
        Map<id, Opportunity> useroppMap = new Map<id, Opportunity>();
        List<id> accIdList = new List<id>();
        List<id> userIdList = new List<id>();
        for(Opportunity Opp : OpportunityList){
            if(!accoppMap.containsKey(Opp.Agency1__r.id)){
                accIdList.add(Opp.Agency1__r.id);
            }
            accoppMap.put(Opp.Agency1__r.id, Opp);
            
            if(!useroppMap.containsKey(Opp.Sales_assistantWIN_ID__c)){
                userIdList.add(Opp.Sales_assistantWIN_ID__c);
            }
            useroppMap.put(Opp.Sales_assistantWIN_ID__c, Opp);
            
        }
 
        Map<id, Account> accMap = new Map<id, Account>();
        List<Account> accList = [select id,Business_Paper_Expiration_Date__c,Tax_Practice_Expiration_Date__c,Medical_Equipment_Expiration_Date__c from Account where id in:accIdList];
        for(Account acc : accList){
            accMap.put(acc.id, acc);
        }
 
        Map<id, User> userMap = new Map<id, User>();
        List<User> uList = [select id,IsActive,Email from User where id in:userIdList];
        for(User u : uList){
            userMap.put(u.id, u);
        }
 
        List<Opportunity> OpportunitynewList = new List<Opportunity>();
        for(Opportunity Opp : OpportunityList){
            if(Opp.Agency1__r.id != null){
                Account acc = new Account();
                acc = accMap.get(Opp.Agency1__r.id);
 
                if((acc.Business_Paper_Expiration_Date__c != null && acc.Business_Paper_Expiration_Date__c <= Date.today().addDays(30))
                         || (acc.Tax_Practice_Expiration_Date__c != null && acc.Tax_Practice_Expiration_Date__c <= Date.today().addDays(30))
                         || (acc.Medical_Equipment_Expiration_Date__c != null && acc.Medical_Equipment_Expiration_Date__c <= Date.today().addDays(30))){
                    //过期
                    if(Opp.Sales_assistantWIN_ID__c != null){
                        User u = new User();
                        u = userMap.get(Opp.Sales_assistantWIN_ID__c);
 
                        if(u!= null && u.IsActive == true){
                            //无异常
                            Opp.AgencyQuolified_Flag__c = 10;
                        }else{
                            //异常
                            Opp.AgencyQuolified_Flag__c = 20;
                        }
                    }else{
                        //异常
                        Opp.AgencyQuolified_Flag__c = 20;
                    } 
 
                }
            }
            OpportunitynewList.add(Opp);
        }
        update OpportunitynewList;
 
    }
    
    global void finish(Database.BatchableContext BC) {
        // 今回はやることないです
        // chenjingwu Batch整理合并 20240304 start
        Id execBTId = Database.executeBatch(new AgencyIntelligenceExpireBatch(),20);
        // chenjingwu Batch整理合并 20240304 end
    }
}