高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
 * wangweipeng  2021/07/12 11:25    SWAG-C4P8CM
 * 每天更新 opd计划表的 if_Newest_HaveOpportunity__c 字段值
 */
global class OPDIfNewestHaveOpportunityBatch implements Database.Batchable<sObject> {
    public String query;
    private String errStr;
    private BatchIF_Log__c iflog;
    private final List<Id> TEST_ID = null;
 
   global OPDIfNewestHaveOpportunityBatch() {
        query = '';
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Is_Error__c = 0;
        iflog.Log__c = 'OPDIfNewestHaveOpportunityBatch start1\n';
        iflog.ErrorLog__c = '';
        insert iflog;
    }
    global OPDIfNewestHaveOpportunityBatch(List<Id> testId) {
        query = '';
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Is_Error__c = 0;
        iflog.Log__c = 'OPDIfNewestHaveOpportunityBatch start1\n';
        iflog.ErrorLog__c = '';
        insert iflog;
        TEST_ID = testId;
    }
    global Database.QueryLocator start(Database.BatchableContext bc) {
        Datetime startDate = Date.today();
        Datetime endDate = Date.today().addDays(1);
        Date dateToday = Date.today();
        Date yesterday = dateToday.addDays(-1);
        Integer year = yesterday.year();
        Integer month = yesterday.month();
        Integer buffer = Integer.valueOf(System.Label.update_If_Newest_HaveOpportunity);
        if (month < 4) {
          year -= 1;
        }
        String ocmYear = year + '年度';
 
        query = 'SELECT id,Name,Status__c,OPD_Customers_Target__c,OPD_Customers_Target_Last__c,NoNSupplementaryApplication__c,ModelLending__c,PlanProdDetail__c,if_Newest_HaveOpportunity__c'
                + ' from OPDPlan__c'
                + ' WHERE PlanProdDetail__c != null' //计划出借备品信息
                + ' AND OPD_Customers_Target__c != null '; //当期的客户-目标客户
            if(buffer == 1){//更新一下2021/7/28   到  2020年11月1号的数据
                query += ' AND Status__c != null '
                    + ' AND OPD_Customers_Target__r.OCM_Year__c in (\'2020年度\',\'2021年度\') '
                    + ' AND OPD_Customers_Target__r.CreatedDate >= 2020-11-01T00:01:01.000+0000 and OPD_Customers_Target__r.CreatedDate <= 2021-07-28T23:59:59.000+0000 ';
            }else{
                query+= ' AND Status__c in (\'计划中\',\'取消\',\'完毕未报告\',\'完毕\')'
                    + ' AND OPD_Customers_Target__r.OCM_Year__c = :ocmYear '
                    + ' AND OPD_Customers_Target__r.LastModifiedDate >= :startDate and OPD_Customers_Target__r.LastModifiedDate <= :endDate ';
            }
        if (TEST_ID <> null) {
            query += ' and Id IN: TEST_ID';
        }
        iflog.Log__c += query+'\n';
        update iflog;
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<OPDPlan__c> scope) {
        if(scope != null && scope.size() > 0){
            List<OPDPlan__c> dataOpdList = scope;
            Integer FIELDMAX = 144;
            //去mapping表中找到名字为targetKeyProduct的一条数据
            SS_Batch_Column_Mapping__c mpdMapping = SS_Batch_Column_Mapping__c.getValues('targetKeyProduct');
            //对应目标客户中的一系列 询价数  用来判断“是否有询价”
            List<String> columnOppCnt = new List<String>();
            Map<String, String> tempImportantOppCnt = new Map<String, String>();
            //获取到 客户-目标客户  表的询价的字段名
            for (Integer i = 129; i <= FIELDMAX; i++) {
                String lpadI = ('00' + i).right(3);
                String fromColumn = 'From_Column_' + lpadI + '__c';//假设执行此时 值是 From_Column_115__c
                String apiStr = String.valueOf(mpdMapping.get(fromColumn)); //取字段fromColumn上存储的数据为Rental_Cnt_USG400__c
                if (String.isBlank(apiStr) == false) {
                    String ssColumn = 'SS_Column_' + lpadI + '__c';//SS_Column_115__c
                    String ssApiStr = String.valueOf(mpdMapping.get(ssColumn));//取得是 字段SS_Column_115__c 上的存储的数据 USG-400
                    if (i >= 129 && i < 144) { //询价数 1,0
                        columnOppCnt.add(apiStr);
                        tempImportantOppCnt.put(apiStr, ssApiStr);
                    }
                }
            }
            Map<id, String> OpdTargetAcc = new Map<id, String>();
            for(Integer i = 0;i < dataOpdList.size() ;i++){
                if(dataOpdList[i].PlanProdDetail__c != null){
                    OpdTargetAcc.put(dataOpdList[i].id,dataOpdList[i].OPD_Customers_Target__c);
                }
            }
            Map<id, Account_Number_of_target__c> acctarList = new Map<id, Account_Number_of_target__c>([select id, OPD_CV290__c, OPD_EUME2__c, OPD_290Miro__c, OPD_GFUCT260__c, OPD_OTVS300S400__c, OPD_CV170__c, OPD_190Main__c, OPD_ENG__c, OPD_TB__c,
                    Sales_target_CV290__c,
                    Sales_target_290Miro__c,
                    Sales_target_EUME2__c,
                    Sales_target_GFUCT260__c,
                    Sales_target_OTVS300S400__c,
                    Sales_target_CV170__c,
                    Sales_target_190Main__c,
                    Sales_target_ENG__c,
                    Sales_target_TB__c,
                    Opp_cnt_290MiroBF__c,
                    Opp_cnt_290MiroGI__c,
                    Opp_cnt_290MiroGI_CF__c,
                    Opp_cnt_290MiroGI_Plus__c,
                    Opp_cnt_CV170__c,
                    Opp_cnt_CV190__c,
                    Opp_cnt_CV290__c,
                    Opp_cnt_ESG400__c,
                    Opp_cnt_EUME2__c,
                    Opp_cnt_GFUCT260__c,
                    Opp_cnt_OTVS190__c,
                    Opp_cnt_OTVS300__c,
                    Opp_cnt_OTVS400__c,
                    Opp_cnt_TB__c,
                    Opp_cnt_USG400__c,
                    Opp_cnt_190Main__c,
                    Asset_Cnt_290MiroBF__c,
                    Asset_Cnt_290MiroGI__c,
                    Asset_Cnt_290MiroGI_CF__c,
                    Asset_Cnt_290MiroGI_Plus__c,
                    Asset_Cnt_CV170__c,
                    Asset_Cnt_CV190__c,
                    Asset_Cnt_CV290__c,
                    Asset_Cnt_ESG400__c,
                    Asset_Cnt_EUME2__c,
                    Asset_Cnt_GFUCT260__c,
                    Asset_Cnt_OTVS190__c,
                    Asset_Cnt_OTVS300__c,
                    Asset_Cnt_OTVS400__c,
                    Asset_Cnt_TB__c,
                    Asset_Cnt_USG400__c,
                    Asset_Cnt_190Main__c,
                    OPD_amount_290MiroBF__c,
                    OPD_amount_290MiroGI__c,
                    OPD_amount_290MiroGI_CF__c,
                    OPD_amount_290MiroGI_Plus__c,
                    OPD_amount_CV170__c,
                    OPD_amount_CV190__c,
                    OPD_amount_CV290__c,
                    OPD_amount_ESG400__c,
                    OPD_amount_EUME2__c,
                    OPD_amount_GFUCT260__c,
                    OPD_amount_OTVS190__c,
                    OPD_amount_OTVS300__c,
                    OPD_amount_OTVS400__c,
                    OPD_amount_TB__c,
                    OPD_amount_USG400__c,
                    OPD_amount_190Main__c,
                    Rental_Cnt_290MiroBF__c,
                    Rental_Cnt_290MiroGI__c,
                    Rental_Cnt_290MiroGI_CF__c,
                    Rental_Cnt_290MiroGI_Plus__c,
                    Rental_Cnt_CV170__c,
                    Rental_Cnt_CV190__c,
                    Rental_Cnt_CV290__c,
                    Rental_Cnt_ESG400__c,
                    Rental_Cnt_EUME2__c,
                    Rental_Cnt_GFUCT260__c,
                    Rental_Cnt_OTVS190__c,
                    Rental_Cnt_OTVS300__c,
                    Rental_Cnt_OTVS400__c,
                    Rental_Cnt_TB__c,
                    Rental_Cnt_USG400__c,
                    Rental_Cnt_190Main__c
                    from Account_Number_of_target__c
                    where id in :OpdTargetAcc.values()]);//查询目标客户 本财年的
 
            //修改对象
            List <OPDPlan__c> ListUpdOpdplan = new List<OPDPlan__c>();
            for(Integer i = 0;i < dataOpdList.size() ;i++){
                OPDPlan__c opdplan = new OPDPlan__c();
                opdplan.id = dataOpdList[i].id;
                Integer ifNewestHaveOpportunity;
                //计划出借备品信息   不为空
                if(dataOpdList[i].PlanProdDetail__c != null){
                    if (acctarList.containskey(dataOpdList[i].OPD_Customers_Target__c)) {
                        //不能去掉所有空格,因为有一个特殊得(OTV-S400 4K主机).replaceAll(' ', ''); //GI-290镜子*3; GI-290镜子CF*1; OTV-S400 4K主机*3; OTV-S190*2; TB*8
                        String ppd = dataOpdList[i].PlanProdDetail__c;
                        //(GI-290镜子*3, GI-290镜子CF*1, OTV-S400 4K主机*3, OTV-S190*2, TB*8)
                        String[] strs = ppd.split(';');
                        //控制是否跳出循环
                        boolean flag = false;
                        for (Integer j = 0; j < strs.size(); j++) {
                            String strss = strs[j].split('\\*')[0].trim();
                            if (columnOppCnt != null) {
                                for (Integer o = 0; o < columnOppCnt.size(); o++) { //是否有询价数
                                    String  titleAccout2 = tempImportantOppCnt.get(columnOppCnt[o]);
                                    if (strss.indexOf(titleAccout2) != -1) {
                                        if (acctarList.get(dataOpdList[i].OPD_Customers_Target__c).get(columnOppCnt[o]) != null && acctarList.get(dataOpdList[i].OPD_Customers_Target__c).get(columnOppCnt[o]) != 0) {
                                            ifNewestHaveOpportunity = 1;
                                            flag = true;
                                            break;
                                        }
                                    }
                                }
                            }
                            if(flag){
                                break;
                            }
                        }
                    }
                }
                opdplan.if_Newest_HaveOpportunity__c = ifNewestHaveOpportunity;
                ListUpdOpdplan.add(opdplan);
            }
 
            Savepoint sp = Database.setSavepoint();
            try{
                update ListUpdOpdplan;
            }catch(exception e){
                errStr = e.getMessage()+'\n';
                errStr += e.getLineNumber()+'\n';
                Database.rollback(sp);
            }
        }
    }
 
    global void finish(Database.BatchableContext BC) {
        String tmp = '';
        if (String.isNotBlank(errStr)) {
            tmp = errStr;
        }
        iflog.Log__c += 'OPDIfNewestHaveOpportunityBatch finish()\n';
        iflog.Log__c += '\nOPDIfNewestHaveOpportunityBatch end';
 
        tmp += iflog.ErrorLog__c;
        if (tmp.length() > 60000) {
          tmp = tmp.substring(0, 60000);
          tmp += ' ...have more lines...';
          iflog.ErrorLog__c = tmp;
        }
        String tmp2 = iflog.Log__c;
        if (tmp2.length() > 60000) {
          tmp2 = tmp2.substring(0, 60000);
          tmp2 += ' ...have more lines...';
          iflog.Log__c = tmp2;
        }
        if (System.Label.Log_IO_Flag == 'Keep') {
          update iflog;
        } else if (System.Label.Log_IO_Flag == 'Auto') {
          if (iflog.Is_Error__c > 0) {
            update iflog;
          }
        }
    }
}