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
//获取追溯日报的活动区分,经销商状态
global class ConsumableTargetManageBatch4 implements Database.Batchable<sObject> {
    public Date currentMonth;
    public Date endDate;
    public Date startDate;
    public String queryStr;
    public String year;
    global ConsumableTargetManageBatch4(Date startDate,Date endDate) {
        //当年
        this.startDate=startDate;
        this.endDate=endDate;
        queryStr = 'SELECT id,WorkRecord__c,WarlockClassification__c,ProductClassification__c,Agency_Hospital__c,Agency_Hospital__r.Hospital__c,Report_Date__c'
                  +' FROM Agency_Report__c ' 
                  +' WHERE WarlockClassification__c!=null AND WorkRecord__c != null AND Agency_Hospital__c!=null AND Agency_Hospital__r.Hospital__c!=null AND ProductClassification__c!=null'
                  +' AND Report_Date__c>=:startDate AND Report_Date__c<=:endDate';
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(queryStr);
    }
 
    global void execute(Database.BatchableContext BC, list<Agency_Report__c> scope) {
        Integer year = startDate.year();
        Integer FYear = year;
        if(startDate>=Date.newInstance(year, 4, 1)){
            FYear = FYear+1;
        }
        String  OCSMYear= 'FY'+FYear;
        Set<String> ctmKeySet=new Set<String>();
        Map<String,List<Agency_Report__c>> arMap = new Map<String,List<Agency_Report__c>>();
        for(Agency_Report__c ar:scope){
            String hpId='';
            if(ar.Agency_Hospital__c!=null&&ar.Agency_Hospital__r.Hospital__c!=null){
                hpId=ar.Agency_Hospital__r.Hospital__c;
            }
            String key=ar.ProductClassification__c+'-'+ar.WarlockClassification__c+'-'+hpId;
            ctmKeySet.add(hpId+'-'+ar.ProductClassification__c+'-'+ar.WarlockClassification__c+'-'+OCSMYear);
            if(arMap.containsKey(key)){
                arMap.get(key).add(ar);
            }else{
                List<Agency_Report__c> arList = new List<Agency_Report__c>();
                arList.add(ar);
                arMap.put(key,arList);
            }
        }
 
        List<ConsumableTargetManage__c> ctmList= [SELECT  DealerActivities_time__c,Hospital__c,OCM_Year__c,Category3__c,Category4__c 
                                                FROM ConsumableTargetManage__c WHERE OCM_Year__c = :OCSMYear  AND ConsumableTargetManageKey__c in :ctmKeySet];
        
        Map<String, Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe();
        Schema.SObjectType objType = globalDescribe.get('ConsumableTargetManage__c');
        Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
        Map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap();
        List<Schema.PicklistEntry> SalesdepartmentMap=fieldMap.get('DealerActivities__c').getDescribe().getPicklistValues();
        List<String> DealerActivitiesList=new List<String>();
        for(Schema.PicklistEntry st:SalesdepartmentMap){
            if(st.isActive()){
                DealerActivitiesList.add(st.getValue());
            }
        }
 
        for(ConsumableTargetManage__c ctm:ctmList){
            String hpId='';
            if(ctm.Hospital__c!=null){
                hpId=ctm.Hospital__c;
            }
            String key=ctm.Category3__c+'-'+ctm.Category4__c+'-'+ hpId;
            if(arMap.containsKey(key)){
                List<Agency_Report__c> arList=arMap.get(key);
                String statu='';
                Date lastDate=Date.newInstance(FYear-1, 4, 1);
                if(ctm.DealerActivities_time__c!=null){
                    lastDate=ctm.DealerActivities_time__c;
                }
                for(Agency_Report__c ar:arList){
                    if(ar.Report_Date__c>=lastDate && DealerActivitiesList.contains(ar.WorkRecord__c)){
                        lastDate=ar.Report_Date__c;
                        statu=ar.WorkRecord__c;
                        ctm.DealerActivities_time__c=lastDate;
                        ctm.DealerActivities__c=ar.WorkRecord__c;
                    }
                }
            }
        }
 
        if(ctmList.size()>0){
            update ctmList;
        }
    }
 
    global void finish(Database.BatchableContext BC) {
        Id batjobId = Database.executeBatch(new ConsumableTargetManageBatch5(startDate,endDate), 200);
 
    }
}