public with sharing class RepairToPDFBatch implements Database.Batchable<SObject>, Database.Stateful {
    
    public List<String> ids;
    //add by rentongxiao 2020-09-27 start 
    public String iflog_Id;
    public BatchIF_Log__c rowDataSFDC;
    public List<String> repairIds;
    public String statu;
    //add by rentongxiao 2020-09-27 end

    //add by rentx 2020-10-27 通过月份同步R3数据
    public Integer monthNum;
    public RepairToPDFBatch(Integer tempmonth) {
        ids = new List<String>();
        monthNum = tempmonth;
    }
    //add by rentx 2020-10-27 通过月份同步R3数据
    
    public RepairToPDFBatch(List<String> argsIds) {
        ids = new List<String>();
        ids = argsIds;
        //add by rentx 2020-10-27 start
        repairIds = argsIds;
        //add by rentx 2020-10-27 end 
    }
    //add by rentongxiao 2020-09-27 start
    public RepairToPDFBatch(String iflog_Id,BatchIF_Log__c rowDataSFDC, List<String> repairIds,String statu) {
        ids = repairIds;
        this.iflog_Id = iflog_Id;
        this.rowDataSFDC = rowDataSFDC;
        this.repairIds = repairIds;
        this.statu = statu;
    }
    //add by rentongxiao 2020-09-27 end

    public RepairToPDFBatch(){
        ids = new List<String>();
        monthNum = null;
        statu = '';
    }

    public Database.QueryLocator start(Database.BatchableContext BC) {
        String staSql = 'select id,name,GeneratedPDFField__c,ProblemDescription__c,ASReportedCodeAC__c,AE_DetermineResult__c,PAE_Determine__c,PAE_DetermineAC__c from Repair__c where GeneratedPDFField__c = null';
        System.debug('ids.size()'+ids.size());
        if (ids.size() > 0) {
            staSql += ' and id in :ids';
        }else{
            // staSql += ' where AsyncData__c = false and Complaint_Number__c = null '; 
            staSql += ' and AsyncData__c = false and Complaint_Number__c = null '; 
            staSql += ' and Repair_Shipped_Date__c != null  and AE_DetermineResult__c != null and PAE_Determine__c != null and PAE_DetermineAC__c != null ';
        }
        if ((ids.size() == 0 || ids == null) && monthNum != null) {
            Date startDay = Date.newInstance(2020,monthNum,1);
            Date tempDay = Date.newInstance(2020,monthNum+1, 1);
            Date endDay = tempDay.addDays(-1);
            staSql += ' and SAP_Transfer_day__c >= :startDay and SAP_Transfer_day__c <= :endDay';
        }
        // if (ids.size() > 0) {
        //     staSql += '  and id in :ids';
        // }
        System.debug('staSQL'+staSql);
        return Database.getQueryLocator(staSql);
    }

    public void execute(Database.BatchableContext BC, List<Repair__c> repList) {

        System.debug('temp ---repList.size()='+repList.size());
        // 上线后课题 33 从IISE过来，直接同步到EtQ的时候，出现报错。需要修改逻辑。 start
        List<Repair__c> reList = new List<Repair__c>();
        // 上线后课题 33 从IISE过来，直接同步到EtQ的时候，出现报错。需要修改逻辑。 end
        // R1的情况需要考虑
        // R3 FUC是空或者包含UseRSA 不发送ETQ 20201203
        for (Repair__c re : repList) {
            if (!((re.AE_DetermineResult__c == 'nonAE' && re.PAE_Determine__c == 'nonPAE' && re.PAE_DetermineAC__c == 'nonPAE')
                   && (re.ASReportedCodeAC__c == null || re.ASReportedCodeAC__c.indexof('UseRSA') >= 0))) {
                reList.add(re);
            }
        }
        RepairAndQISToPDFController.generateAttachment(reList);
    }


    public void finish(Database.BatchableContext BC) {
       System.debug('finish');
       // Database.executeBatch(new QISToPDFBatch(),50);
       // Database.executeBatch(new RepairSendToETQBatch(iflog_Id, rowDataSFDC,repairIds,statu),50);
       Database.executeBatch(new RepairSendToETQBatch(iflog_Id, rowDataSFDC,repairIds,statu,monthNum),1);
       // Database.executeBatch(new RepairSendToETQBatch(),50);

    }
}