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


}