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
global class SortUpdateOpp2Batch implements Database.Batchable<SObject> , Database.Stateful{
    
    private final List<String> sdcName = null;
    private BatchIF_Log__c iflog;
 
    global SortUpdateOpp2Batch(){
    }
 
    global SortUpdateOpp2Batch(List<String> str){
        sdcName = str;
    }
 
    global Database.QueryLocator start(Database.BatchableContext BC){
 
        //20230724 lt 添加邮件提醒 start
        system.debug('执行start');
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'SortUpdateOpp2Batch';
        iflog.Log__c  = 'SortUpdateOpp2Batch start\n';
        iflog.ErrorLog__c = '';
        insert iflog;
        //20230724 lt 添加邮件提醒 end
 
        String sql = 'Select id,Name,type__c From opp2AuxiliarySort__c ';
        if(sdcName <> null){
            sql += 'Where Name in :sdcName';
        }
        sql += ' order by type__c';
        System.debug('=sql='+sql);
        return Database.getQueryLocator(sql);
 
    }
 
    global void execute(Database.BatchableContext BC, List<opp2AuxiliarySort__c> opp2AuxiliarySortList){
        try{
            if(opp2AuxiliarySortList.size()>0){
                List<Opportunity2__c> GIcheckList = new List<Opportunity2__c>();
                List<Opportunity2__c> SPcheckList = new List<Opportunity2__c>();
                String sql = opp2SQLHead('GI');
                String sql2 = opp2SQLHead('SP');
                if(opp2AuxiliarySortList[0].type__c == 1){
                    sql +='and Opportunity__r.OCM_man_province__c = \''+opp2AuxiliarySortList[0].Name +'\'';
                    sql +=' Order by Opportunity__r.Amount_Without_Tax_Thousand_F__c Desc Limit 100';
                    GIcheckList = Database.query(sql);
                    Integer GInum = 0;
                    for (Opportunity2__c GIoppObj : GIcheckList) {
                        GInum++;
                        GIoppObj.RankNum_State__c = GInum;
                    }
    
                    sql2 +='and Opportunity__r.OCM_man_province__c = \''+opp2AuxiliarySortList[0].Name +'\'';
                    sql2 +=' Order by Opportunity__r.Amount_Without_Tax_Thousand_F__c Desc Limit 100';
                    SPcheckList = Database.query(sql2);
                    Integer SPnum = 0;
                    for (Opportunity2__c SPoppObj : SPcheckList) {
                        SPnum++;
                        SPoppObj.RankNum_State__c = SPnum;
                    }
                }else if(opp2AuxiliarySortList[0].type__c == 2){
                    sql +='and Opportunity__r.SalesdepartmentForecast__c = \''+opp2AuxiliarySortList[0].Name +'\'';
                    sql +=' Order by Opportunity__r.Amount_Without_Tax_Thousand_F__c Desc Limit 100';
                    GIcheckList = Database.query(sql);
                    Integer GInum = 0;
                    for (Opportunity2__c GIoppObj : GIcheckList) {
                        GInum++;
                        GIoppObj.RankNum_Dept__c = GInum;
                    }
    
                    sql2 +='and Opportunity__r.SalesdepartmentForecast__c = \''+opp2AuxiliarySortList[0].Name +'\'';
                    sql2 +=' Order by Opportunity__r.Amount_Without_Tax_Thousand_F__c Desc Limit 100';
                    SPcheckList = Database.query(sql2);
                    Integer SPnum = 0;
                    for (Opportunity2__c SPoppObj : SPcheckList) {
                        SPnum++;
                        SPoppObj.RankNum_Dept__c = SPnum;
                    }
    
                }else if(opp2AuxiliarySortList[0].type__c == 3){
                    sql +='and Opportunity__r.SalesdepartmentForecast__c != \'市场\' and Opportunity__r.SalesdepartmentForecast__c != \'其他\' and Opportunity__r.SalesdepartmentForecast__c != \'7.能量\' and Opportunity__r.SalesdepartmentForecast__c != \'外科\'  and Opportunity__r.SalesdepartmentForecast__c != \'经销商\' ';
                    sql +=' Order by Opportunity__r.Amount_Without_Tax_Thousand_F__c Desc Limit 100';
                    GIcheckList = Database.query(sql);
                    Integer GInum = 0;
                    for (Opportunity2__c GIoppObj : GIcheckList) {
                        GInum++;
                        GIoppObj.RankNum_Country__c = GInum;
                    }
    
                    sql2 +='and Opportunity__r.SalesdepartmentForecast__c != \'市场\' and Opportunity__r.SalesdepartmentForecast__c != \'其他\' and Opportunity__r.SalesdepartmentForecast__c != \'7.能量\' and Opportunity__r.SalesdepartmentForecast__c != \'外科\'  and Opportunity__r.SalesdepartmentForecast__c != \'经销商\' ';
                    sql2 +=' Order by Opportunity__r.Amount_Without_Tax_Thousand_F__c Desc Limit 100';
                    SPcheckList = Database.query(sql2);
                    Integer SPnum = 0;
                    for (Opportunity2__c SPoppObj : SPcheckList) {
                        SPnum++;
                        SPoppObj.RankNum_Country__c = SPnum;
                    }
                }
                Database.SaveResult[] saveTenderResults = Database.update(GIcheckList, false);
                Database.SaveResult[] saveTenderResults2 = Database.update(SPcheckList, false);
    
    
            }
        }catch(Exception ex){
            iflog.ErrorLog__c = ex.getLineNumber()+'  行错误 :   '+ex.getMessage();
            return;
        }
 
       
 
    }
 
    global void finish(Database.BatchableContext BC){
        //20230724 lt 添加邮件提醒 start
        iflog.Log__c += '\nSortUpdateOpp2Batch end';
        String tmp = iflog.ErrorLog__c;
        if (tmp.length() > 65000) {
            tmp = tmp.substring(0, 65000);
            tmp += ' ...have more lines...';
            iflog.ErrorLog__c = tmp;
        }
        update iflog;
        //20230724 lt 添加邮件提醒 end
    }
 
    public String opp2SQLHead (String Type){
        String sql = 'Select id,name, StageName__c,Opportunity__r.Product_Sales_Category__c,'
            +'Opportunity__r.Amount_Without_Tax_Thousand_F__c,'
            +'Opportunity__r.OCM_man_province__c,Opportunity__r.SalesdepartmentForecast__c,'
            +'RankNum_State__c,RankNum_Dept__c,RankNum_Country__c '
            +'From Opportunity2__c '
            +'where StageName__c = \'询价\' '
            +'and Opportunity__r.Amount_Without_Tax_Thousand_F__c != null ';
            if(Type == 'GI'){
                sql +='and Opportunity__r.Product_Sales_Category__c = \'GI\' ';
            }else if(Type == 'SP'){
                sql +='and Opportunity__r.Product_Sales_Category__c = \'SP\' ';
            }
        return sql;
    }
 
 
}