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;
    }
}