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
global class ASPPriceYearBatch3 implements Database.Batchable<sObject> {
    public String query;
    public String FYyear;
    public Date startDate;
    public Date endDate;
    public Set<String> s;
    //当前时间,默认xxxx年1月1日,这时跑上一年的4月1日到上一年底的数据
    global ASPPriceYearBatch3(Date currentDate) {
        
        Integer year = currentDate.year();
        Integer fyear=year+1;
        this.FYyear='FY'+fyear;
        this.startDate=Date.newInstance(year-1, 4, 1);
        this.endDate=Date.newInstance(year-1, 12, 31);
        this.s = new Set<String>{'停止','有効','有効(再申請中)','失効(期限内生産済在庫対応)'};
        this.query = 'SELECT id,Intra_Trade_List_RMB__c,ConsumCategory2__c,ConsumCategory3__c,Estimation_Entry_Possibility__c '+
                     ' from Product2 where ConsumCategory2__c != null and ConsumCategory3__c != null '+
                     ' AND (Dealer_Object__c=true OR ENG_DeaerProFlag__c=true) '+
                     ' and Category3__c <> \'\' ' +
                     ' and Category5__c !=\'竞争对手\''+
                     ' and SFDA_Status__c in :s';
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<Product2> scope) {
        Map<String,List<Product2>> product2Map = new Map<String,List<Product2>>();
        for(Product2 product2:scope){
            String key = product2.ConsumCategory2__c+'-'+product2.ConsumCategory3__c;
            if(product2Map.containsKey(key)){
                product2Map.get(key).add(product2);
            }else{
                List<Product2> product2List = new List<Product2>();
                product2List.add(product2);
                product2Map.put(key,product2List);
            }
        }
        List<ET_ASP__c> etASPList=[SELECT id,Salesdepartment__c,ConsumCategory2__c,ConsumCategory3__c,yearly__c,ASP_year_ListRMB__c,ASP_year__c,List_Num__c,List_RMB__c from ET_ASP__c WHERE yearly__c=:FYyear ];
        for(ET_ASP__c etASP:etASPList){
            String key = etASP.ConsumCategory2__c+'-'+etASP.ConsumCategory3__c;
            if(etASP.List_Num__c==null){
                etASP.List_Num__c=0;
            }
            if(etASP.List_RMB__c==null){
                etASP.List_RMB__c=0;
            }
            if(product2Map.containsKey(key)){
                List<Product2> product2List = product2Map.get(key);
                for(Product2 product2:product2List){
                    if(product2.Intra_Trade_List_RMB__c!=null){
                        etASP.List_RMB__c+=product2.Intra_Trade_List_RMB__c;
                        etASP.List_Num__c+=1;
                    }
                }
            }
            if(etASP.List_RMB__c!=0 && etASP.List_Num__c!=0){
                etASP.ASP_year_ListRMB__c = etASP.List_RMB__c / etASP.List_Num__c * 0.5 / 1.13 /1000;
            }else{
                etASP.ASP_year_ListRMB__c = 0;
            }
        }
        update etASPList;
    }
 
    global void finish(Database.BatchableContext BC) {
        List<ET_ASP__c> etASPListQG=[SELECT id,Salesdepartment__c,ConsumCategory2__c,ConsumCategory3__c,yearly__c,ASP_year__c,DosageAmountTotal__c,DosageNumTotal__c from ET_ASP__c WHERE yearly__c=:FYyear AND Salesdepartment__c='0.全国' ];
        List<ET_ASP__c> etASPList=[SELECT id,Salesdepartment__c,ConsumCategory2__c,ConsumCategory3__c,yearly__c,ASP_year__c,DosageAmountTotal__c,DosageNumTotal__c from ET_ASP__c WHERE yearly__c=:FYyear AND Salesdepartment__c!='0.全国' AND ASP_year__c=0];
        Map<String,ET_ASP__c> etQGMap = new Map<String,ET_ASP__c>();
        for(ET_ASP__c etASP:etASPListQG){
            String key = etASP.ConsumCategory2__c+'-'+etASP.ConsumCategory3__c;
            etQGMap.put(key, etASP);
        }
        for(ET_ASP__c etASP:etASPList){
            String key = etASP.ConsumCategory2__c+'-'+etASP.ConsumCategory3__c;
            ET_ASP__c etASPQG = etQGMap.get(key);
            etASP.ASP_year__c = etASPQG.ASP_year__c;
        }
        update etASPList;
    }
}