/***
 * 基建项目 最早订货日&最早发货日
 * WYL DB202401246060 2024/1/15
 */
global class InfrastructureDevelopmentDateBatch implements Database.Batchable<SObject> , Database.Stateful{
    public String query;
    public String ipId;
    private BatchIF_Log__c iflog;


    global InfrastructureDevelopmentDateBatch() {
        this.query = query;
    }
    global InfrastructureDevelopmentDateBatch(String ipId) {
        this.query = query;
        this.ipId = ipId;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        //添加提醒 start
        system.debug('执行start');
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'InfrastructureDevelopmentDateBatch';
        iflog.Log__c  = 'InfrastructureDevelopmentDateBatch start\n';
        insert iflog;
        //添加提醒 end
        query = 'select id,name from Infrastructure_Project__c where IfFollow__c = 1 and RelatedInquiryDate__c != null';
        if (this.ipId != null && this.ipId != '') {
            query += '  and id =:ipId';
        }
        return Database.getQueryLocator(query);
    }

  global void execute(Database.BatchableContext BC, list<Infrastructure_Project__c> IPList) {
    List<String> ipIdList = new List<String>();
    for (Infrastructure_Project__c ip : IPList) {
        ipIdList.add(ip.id);
    }
    try {
        List<Opportunity> OpportunityList = [select id,name,order_Date_For_Report__c,
        Shipping_Date_For_Report__c,Infrastructure_Project__c from Opportunity 
        where Infrastructure_Project__c in :ipIdList
        order by order_Date_For_Report__c,Shipping_Date_For_Report__c asc];
        Map<ID, Opportunity> oppMap = new Map<ID, Opportunity>(); 
        for (Opportunity opp : OpportunityList) {
        if (!oppMap.containsKey(opp.Infrastructure_Project__c)) {
            oppMap.put(opp.Infrastructure_Project__c, opp);
        }
        }
        //打印
        List<Infrastructure_Project__c> InfrastructureList = new List<Infrastructure_Project__c>();
        for (ID key : oppMap.keySet()) {
        Opportunity opp = oppMap.get(key);
        Infrastructure_Project__c ip= new Infrastructure_Project__c();
        ip.id  = key;
        ip.Earliest_order_date__c = opp.order_Date_For_Report__c;
        ip.Earliest_shipping_date__c =opp.Shipping_Date_For_Report__c;
        InfrastructureList.add(ip);
        System.debug('基建ID: ' + key + ', 询价信息: ' + opp);
        }
        Database.SaveResult[] saveTenderResults = Database.update(InfrastructureList, false);

        for (Integer save = 0; save < saveTenderResults.size(); save++) {
        Database.SaveResult sr = saveTenderResults[save];
        if (!sr.isSuccess()) {
        Database.Error emsg = sr.getErrors()[0];
        iflog.ErrorLog__c += 'ERROR  ' + saveTenderResults[save].id + 'SS_oli:' + emsg + '\n';
        }else {
        iflog.Log2__c += saveTenderResults[save].id +',';
        }
        }
    } catch (Exception e) {
        String errorMessage = e.getMessage();
            iflog.ErrorLog__c += errorMessage;
            update iflog;
            return;
    } 
   
  }

  global void finish(Database.BatchableContext BC) {
    iflog.Log__c += '\n InfrastructureDevelopmentDateBatch end';
     update iflog;
  }
}