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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
global class DealerConsumableManageBatch3 implements Database.Batchable<sObject> {
    public Date currentMonth;
    public Date endDate;
    public Date startDate;
    public String queryStr;
    public String year;
    public Boolean isIncrementOnly; //是否只跑增量
    global DealerConsumableManageBatch3(Date startDate,Date endDate,Boolean isIncrementOnly) {
        //当年
        this.startDate=startDate;
        this.endDate=endDate;
        this.isIncrementOnly=isIncrementOnly;
        queryStr = 'SELECT OCM_man_province__c,Product_Type__c,Dealer_Info_ID__c,Detail_Count__c,Intra_Trade_KRMB__c,Product_OutDate__c '+
        ' FROM Consumable_order_details2__c '+
        ' WHERE Lose_Flag__c = FALSE AND Dealer_Returned__c = FALSE AND Dealer_Saled__c=true AND Product_OutDate__c >= :startDate AND Product_OutDate__c  <= :endDate' +
        ' AND (Inventory_Status__c = \'直销到医院\' OR Inventory_Status__c = \'给二级到医院\')  AND ShipmentAccount__c != \'\' AND Consumable_product__r.Product2__c!=null AND Consumable_Category1__c!=null AND Intra_Trade_KRMB__c!=null AND Hospital_ID__c!=null AND Dealer_Info_ID__c!=null and Product_Type__c!=\'\' ';
        // ' and Consumable_product__r.Product2__r.ConsumCategory3__c=\'高频治疗钳\' and Consumable_product__r.Product2__r.ConsumCategory2__c=\'ESD\' and Hospital_ID__c=\'0011000000V9RHzAAN\''+
        // ' limit 3000';
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        //数据超过一万条会报错
        if(!isIncrementOnly){
            Integer year = startDate.year();
            Integer FYear = year;
            if(startDate>=Date.newInstance(year, 4, 1)){
                FYear = FYear+1;
            }
            String  OCSMYear= 'FY'+FYear;
            List<Dealer_ConsumableManage__c> ctmList= [SELECT  id FROM Dealer_ConsumableManage__c WHERE FY__c = :OCSMYear ];
            if(ctmList.size()>0){
                for(Dealer_ConsumableManage__c ctm:ctmList){
                    ctm.IMS_Shipments_1__c=0;
                    ctm.IMS_Shipments_2__c=0;
                    ctm.IMS_Shipments_3__c=0;
                    ctm.IMS_Shipments_4__c=0;
                    ctm.IMS_Shipments_5__c=0;
                    ctm.IMS_Shipments_6__c=0;
                    ctm.IMS_Shipments_7__c=0;
                    ctm.IMS_Shipments_8__c=0;
                    ctm.IMS_Shipments_9__c=0;
                    ctm.IMS_Shipments_10__c=0;
                    ctm.IMS_Shipments_11__c=0;
                    ctm.IMS_Shipments_12__c=0;
                }
                update ctmList;
            }
            
        }
 
        return Database.getQueryLocator(queryStr);
    }
 
    global void execute(Database.BatchableContext BC,  list<Consumable_order_details2__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<Consumable_order_details2__c>> codMap = new Map<String,List<Consumable_order_details2__c>>();
        for(Consumable_order_details2__c cod:scope){
            String key = cod.Dealer_Info_ID__c+'-'+cod.Product_Type__c+'-'+OCSMYear+'-'+cod.OCM_man_province__c;
            ctmKeySet.add(cod.Dealer_Info_ID__c+'-'+cod.Product_Type__c+'-'+OCSMYear+'-'+cod.OCM_man_province__c);
            if(codMap.containsKey(key)){
                codMap.get(key).add(cod);
            }else{
                List<Consumable_order_details2__c> codList = new List<Consumable_order_details2__c>();
                codList.add(cod);
                codMap.put(key,codList);
            }
        }
 
 
        List<Dealer_ConsumableManage__c> ctmList= [SELECT  TMS_Province__c,IMS_Shipments_1__c,IMS_Shipments_2__c,IMS_Shipments_3__c,IMS_Shipments_4__c
                                                    ,IMS_Shipments_5__c,IMS_Shipments_6__c,IMS_Shipments_7__c,IMS_Shipments_8__c
                                                    ,IMS_Shipments_9__c,IMS_Shipments_10__c,IMS_Shipments_11__c,IMS_Shipments_12__c
                                                    ,TMS_Key__c
                                                     FROM Dealer_ConsumableManage__c WHERE  TMS_Key__c in :ctmKeySet];
        System.debug('获取到现存IMS:');
        System.debug(ctmList);
 
        for(Dealer_ConsumableManage__c ctm:ctmList){
            String key = ctm.TMS_Key__c;
            if(ctm.IMS_Shipments_1__c==null){
                ctm.IMS_Shipments_1__c=0;
            }
            if(ctm.IMS_Shipments_2__c==null){
                ctm.IMS_Shipments_2__c=0;
            }
            if(ctm.IMS_Shipments_3__c==null){
                ctm.IMS_Shipments_3__c=0;
            }
            if(ctm.IMS_Shipments_4__c==null){
                ctm.IMS_Shipments_4__c=0;
            }
            if(ctm.IMS_Shipments_5__c==null){
                ctm.IMS_Shipments_5__c=0;
            }
            if(ctm.IMS_Shipments_6__c==null){
                ctm.IMS_Shipments_6__c=0;
            }
            if(ctm.IMS_Shipments_7__c==null){
                ctm.IMS_Shipments_7__c=0;
            }
            if(ctm.IMS_Shipments_8__c==null){
                ctm.IMS_Shipments_8__c=0;
            }
            if(ctm.IMS_Shipments_9__c==null){
                ctm.IMS_Shipments_9__c=0;
            }
            if(ctm.IMS_Shipments_10__c==null){
                ctm.IMS_Shipments_10__c=0;
            }
            if(ctm.IMS_Shipments_11__c==null){
                ctm.IMS_Shipments_11__c=0;
            }
            if(ctm.IMS_Shipments_12__c==null){
                ctm.IMS_Shipments_12__c=0;
            }
            if(codMap.containsKey(key)){
                List<Consumable_order_details2__c> codList=codMap.get(key);
                for(Consumable_order_details2__c cod:codList){
                    if(cod.Product_OutDate__c >= Date.newInstance(FYear, 1, 1) && cod.Product_OutDate__c < Date.newInstance(FYear, 2, 1)){
                        ctm.IMS_Shipments_1__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear, 2, 1) && cod.Product_OutDate__c < Date.newInstance(FYear, 3, 1)){
                        ctm.IMS_Shipments_2__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear, 3, 1) && cod.Product_OutDate__c < Date.newInstance(FYear, 4, 1)){
                        ctm.IMS_Shipments_3__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 4, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 5, 1)){
                        ctm.IMS_Shipments_4__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 5, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 6, 1)){
                        ctm.IMS_Shipments_5__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 6, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 7, 1)){
                        ctm.IMS_Shipments_6__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 7, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 8, 1)){
                        ctm.IMS_Shipments_7__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 8, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 9, 1)){
                        ctm.IMS_Shipments_8__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 9, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 10, 1)){
                        ctm.IMS_Shipments_9__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 10, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 11, 1)){
                        ctm.IMS_Shipments_10__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 11, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 12, 1)){
                        ctm.IMS_Shipments_11__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 12, 1) && cod.Product_OutDate__c < Date.newInstance(FYear, 1, 1)){
                        ctm.IMS_Shipments_12__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }
                }
                codMap.remove(key);
            }
        }
 
        List<Dealer_ConsumableManage__c> newCtm=new List<Dealer_ConsumableManage__c>();
        for(String key:codMap.keySet()){
            List<Consumable_order_details2__c> codList=codMap.get(key);
            Dealer_ConsumableManage__c ctm=new Dealer_ConsumableManage__c();
            ctm.Dealer_Name__c = codList[0].Dealer_Info_ID__c;
            if(codList[0].Product_Type__c == 'ET'){
                ctm.ET_ENG__c='ET';
            }else{
                ctm.ET_ENG__c='ENG';
            }
            ctm.FY__c = OCSMYear;
            ctm.TMS_Key__c = ctm.Dealer_Name__c+'-'+ctm.ET_ENG__c+'-'+ctm.FY__c+'-'+codList[0].OCM_man_province__c;
            ctm.IMS_Shipments_1__c=0;
            ctm.IMS_Shipments_2__c=0;
            ctm.IMS_Shipments_3__c=0;
            ctm.IMS_Shipments_4__c=0;
            ctm.IMS_Shipments_5__c=0;
            ctm.IMS_Shipments_6__c=0;
            ctm.IMS_Shipments_7__c=0;
            ctm.IMS_Shipments_8__c=0;
            ctm.IMS_Shipments_9__c=0;
            ctm.IMS_Shipments_10__c=0;
            ctm.IMS_Shipments_11__c=0;
            ctm.IMS_Shipments_12__c=0;
 
            for(Consumable_order_details2__c cod:codList){
                if(cod.Product_OutDate__c >= Date.newInstance(FYear, 1, 1) && cod.Product_OutDate__c < Date.newInstance(FYear, 2, 1)){
                    ctm.IMS_Shipments_1__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear, 2, 1) && cod.Product_OutDate__c < Date.newInstance(FYear, 3, 1)){
                    ctm.IMS_Shipments_2__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear, 3, 1) && cod.Product_OutDate__c < Date.newInstance(FYear, 4, 1)){
                    ctm.IMS_Shipments_3__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 4, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 5, 1)){
                    ctm.IMS_Shipments_4__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 5, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 6, 1)){
                    ctm.IMS_Shipments_5__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 6, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 7, 1)){
                    ctm.IMS_Shipments_6__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 7, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 8, 1)){
                    ctm.IMS_Shipments_7__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 8, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 9, 1)){
                    ctm.IMS_Shipments_8__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 9, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 10, 1)){
                    ctm.IMS_Shipments_9__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 10, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 11, 1)){
                    ctm.IMS_Shipments_10__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 11, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 12, 1)){
                    ctm.IMS_Shipments_11__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 12, 1) && cod.Product_OutDate__c < Date.newInstance(FYear, 1, 1)){
                    ctm.IMS_Shipments_12__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }
            }
            newCtm.add(ctm);
        }
        if(ctmList.size()>0){
            update ctmList;
        }
        if(newCtm.size()>0){
            insert newCtm;
        }
 
    }
 
    global void finish(Database.BatchableContext BC) {
        
        Database.executeBatch(new DealerConsumableManageBatch4(startDate,endDate,this.isIncrementOnly),200);
    }
}