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
public with sharing class CurrentFYMoneyTmsBatch implements Database.Batchable<sObject> {
    String query;
    String accid;
    public CurrentFYMoneyTmsBatch() {}
    public CurrentFYMoneyTmsBatch(String accid) {
        this.accid = accid;
    }
    public Database.QueryLocator start(Database.BatchableContext BC) {
 
        query = 'SELECT id,Intra_Trade_List_RMB__c,Consumable_Category3__c,Consumable_Category2__c,Product_Type__c,Deliver_date__c,Used_account__r.Agent_Ref__c,Used_account__c,Used_Account_Type__c '
            + 'FROM Consumable_order_details2__c where id !=null and Inventory_Status__c = \'经销商库存\' and Deliver_date__c >=2024-01-01  ' ;
        if (accid !=null) {
            query += ' and Used_account__c = :accid';
        }
        return Database.getQueryLocator(query);
    }
 
    public void execute(Database.BatchableContext BC, List<Consumable_order_details2__c> DcList) { 
        Map<String,double> map1= new Map<String,double>();
        List<Id> accList = new List<Id>();
        for (Consumable_order_details2__c dc : DcList) {
            if (dc.Intra_Trade_List_RMB__c !=0 && dc.Intra_Trade_List_RMB__c !=null) {
                String  key = '';
                Integer year = dc.Deliver_date__c.year();
                Integer month = dc.Deliver_date__c.month();
                if (dc.Used_Account_Type__c == '契約') {
                    key = dc.Used_account__r.Agent_Ref__c+'-'+year+'-'+month;
                    if (!accList.contains(dc.Used_account__r.Agent_Ref__c)) {
                        accList.add(dc.Used_account__r.Agent_Ref__c);
                    }
                    
                }else if(dc.Used_Account_Type__c == '販売店'){
                    key = dc.Used_account__c+'-'+year+'-'+month;
                    if (!accList.contains(dc.Used_account__c)) {
                        accList.add(dc.Used_account__c);
                    }
                }
                
                if (!map1.containsKey(key)) {
                    map1.put(key, dc.Intra_Trade_List_RMB__c);
                }else {
                    Double money = map1.get(key);
                    money += dc.Intra_Trade_List_RMB__c;
                    map1.put(key, money);
                }
            }
        }
        system.debug('map1='+map1);
        system.debug('accList='+accList);
        List<Dealer_ConsumableManage__c> dcmList = [select id,FY__c,Dealer_Name__c,FY_I_1__c,FY_I_10__c,FY_I_11__c,FY_I_12__c,FY_I_2__c,FY_I_3__c,FY_I_4__c,FY_I_5__c,FY_I_6__c,FY_I_7__c,FY_I_8__c,FY_I_9__c
                                                        from Dealer_ConsumableManage__c where Dealer_Name__c in :accList];
        List<Dealer_ConsumableManage__c> updateList = new List<Dealer_ConsumableManage__c>();
        for (Dealer_ConsumableManage__c dc : dcmList) {
            integer startIndex = dc.FY__c.indexOf('FY');
            String year = dc.FY__c.substring(startIndex+2, startIndex+6);
            Dealer_ConsumableManage__c dc1 = new Dealer_ConsumableManage__c();
            dc1.id = dc.id;
            for (Integer i=1;i<=12;i++) {
                String key = dc.Dealer_Name__c+'-'+year+'-'+i;
                if (map1.containsKey(key)) {
                    Double money = map1.get(key);
                    if (i==1) {
                        dc1.FY_I_1__c = money;
                    }
                    if (i==11) {
                        dc1.FY_I_11__c = money;
                    }
                    if (i==12) {
                        dc1.FY_I_12__c = money;
                    }
                    if (i==10) {
                        dc1.FY_I_10__c = money;
                    }
                    if (i==2) {
                        dc1.FY_I_2__c = money;
                    }
                    if (i==3) {
                        dc1.FY_I_3__c = money;
                    }
                    if (i==4) {
                        dc1.FY_I_4__c = money;
                    }
                    if (i==5) {
                        dc1.FY_I_5__c = money;
                    }
                    if (i==6) {
                        dc1.FY_I_6__c = money;
                    }
                    if (i==7) {
                        dc1.FY_I_7__c = money;
                    }
                    if (i==8) {
                        dc1.FY_I_8__c = money;
                    }
                    if (i==9) {
                        dc1.FY_I_9__c = money;
                    }
                }
            }
            updateList.add(dc1);
        }
        if (updateList.size()>0) {
            update updateList; 
        }
 
    }
 
    public void finish(Database.BatchableContext BC) {}
}