global class updateQISPDAddFour implements Database.Batchable<sObject>, Database.Stateful  {
    public String query;
    //153p财年的开始日
    Date fromDate = null;

    Date addFourDate = null;
    public String id;
    public List<String> ids;

    global updateQISPDAddFour() {
        this.query = query;
    }

    global updateQISPDAddFour(String id) {
        this.id = id;
    }

    global updateQISPDAddFour(List<String> ids) {
        this.ids = ids;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        String et = 'ET';
        String eng = 'ENG';
        String  action = '送回';
        //检索市场部判定日不为空的、市场部意见未填写 、   的 QIS
        query = 'select id,SC_Judge_Date__c,SC_JudgeDate_Four__c,SC_Judge_Date_Four__c from QIS_Report__c ';
        query +=' where SC_Judge_Date__c !=null and Market_Opinion__c = null   ';
        //QIS判断分类为ET 或者ENG
        // query += ' and ( QIS_SC_Place__c=\'' + et + '\'  or QIS_SC_Place__c=\'' + eng + '\' )';
        //对应方法为送回
        query += ' and next_action__c =\'' + action + '\' ' ;
        if(!string.isblank(id)){
            query += ' and id = :id';
        }
        if(ids != null){
            query += ' and id in :ids';
        }
        system.debug('sql:'+query);
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<Sobject> scope) {
        List<QIS_Report__c> qisList =new List < QIS_Report__c > ();
        qisList = scope;
 
        //用于存储本财年奥林巴斯日历的工作日
        List < Date > workDayNum = new List < Date > ();
        //LJPH-BQ8DQ4 精琢技术 wql 20200624 start
        Date todate = Date.today();
        //根据传入的财年 去判断上财年的开始日期
        fromDate =Date.newInstance(2020, 4, 1);

        //因为市场部判定日是153p时上线的，所以此处获取奥林巴斯日历在153p开始以后的工作日
        List<OlympusCalendar__c> WorkDayList = [select Date__c, IsWorkDay__c from OlympusCalendar__c where  Date__c >= :fromDate  order by Date__c  asc];
        //用于存放最后更新的QIS
        List<QIS_Report__c> qList = new List<QIS_Report__c>();

        for(QIS_Report__c qis :qisList){

            if(WorkDayList.size() >0){
                //①先确定判断日在数组中的位置
                //②判定日+4个工作日
                for(OlympusCalendar__c oly : WorkDayList){
                        if(oly.Date__c > qis.SC_Judge_Date__c){
                            if (oly.IsWorkDay__c == 1) {
                                workDayNum.add(oly.Date__c);
                            }
                        }
                    }
                }
            //①如果判定日那天工作日，当天默认不算+4个工作日
            //②如果判定日那天是非工作日，取最近的4个工作日
            if(workDayNum.size()>5){
                addFourDate   =workDayNum[4];
            }else{
                 addFourDate  =null;
            }
             System.debug('数组:'+workDayNum);
             System.debug('判定日+4个工作日：'+addFourDate);
             //判断  今天是不是大于判定日+4个工作日
             if(  todate >= addFourDate){
                 QIS_Report__c q = new QIS_Report__c();
                 q.SC_Judge_Date_Four__c = addFourDate;
                 q.Id =qis.Id;
                 q.SC_JudgeDate_Four__c = true;
                 qList.add(q);
            }

        }
             
        if(qList.size() > 0){
            update qList;
        }
        
    }

    global void finish(Database.BatchableContext BC) {

    }
}