/*2021-07-08 mzy  点检任务
  给任务发送时点是今天的点检计划生成被动任务
*/
global class CreateInspectupTaskBatch implements Database.Batchable<sObject> {
    public List<String> tempInspectupPlanId = new List<String>();
    
    public boolean isexcuteAll = false;

    public boolean IsNeedExecute=false;  //2021-03-08 mzy  WLIG-BYHD79  SFDC环境batch合并调查 


    BatchIF_log__c log1 = new BatchIF_Log__c();

    global CreateInspectupTaskBatch() {
        
    }
     
    //无条件生成所有点检计划的任务
    global CreateInspectupTaskBatch(Boolean isexAll) {
        this.isexcuteAll = isexAll;
    }

    //指定点检计划无条件生成任务
    global CreateInspectupTaskBatch(List<String> tempInspectupPlanId) {
        this.tempInspectupPlanId = tempInspectupPlanId;
    }

    //Batch合并用
    global CreateInspectupTaskBatch(String way,Boolean IsNeedExecute) {
        this.IsNeedExecute = IsNeedExecute;
    }
    
    global Database.QueryLocator start(Database.BatchableContext bc) {

        //Date today0 = Date.today();
        //Date ThisMonthFirstDay = Date.newInstance(today0.year(),today0.month(),1);
        Date today = Date.today();
        String query = 'SELECT Id,Maintenance_Contract__c,Planned_Start_Date__c,Planned_End_Date__c,Actual_Execution_Quantity_Inplan__c, ';
               query += 'Maintenance_Contract__r.Service_Contract_Staff__c,Maintenance_Contract__r.Department__c,Maintenance_Contract__r.Department__r.Name, ';
               query += 'taskFrequency__c,task__c,InspctupDate__c ';
               query += 'FROM Inspectup_Plan__c ';
                if(isexcuteAll == false){                 
                    if(tempInspectupPlanId.size()>0){
                        query += 'WHERE Id in :tempInspectupPlanId';
                    }else {
                        query += 'WHERE  ActualNumNoEqualsPlanNum__c = true AND Task_sending_time__c = :today ';                       
                        query += 'AND (task__c = null OR (task__c != null and task__r.taskStatus__c in (\'02 接受\',\'03 完成\',\'04 取消\') ) )'; 
                    }
                }
        System.debug('SQL语句:'+query);
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<Inspectup_Plan__c> InspectupPlanList) {
       
        //定义变量保存生成的点检任务(被动任务)
        List<Task__c> NeedInsertTask = new List<Task__c>();
        //定义变量保存记录类型
        Id InspectUp_TaskId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('InspectUp_Task').getRecordTypeId();
        //定义变量保存要更新的点检计划
        //List<Inspectup_Plan__c> updateInspectupPlanList = new List<Inspectup_Plan__c>();
        //定义Map保存点检计划
        Map<String,Inspectup_Plan__c> updateInPlanMap = new  Map<String,Inspectup_Plan__c>();

        //根据点检计划生成 对应的点检任务
        for(Inspectup_Plan__c InspectupPlan :InspectupPlanList){
            //生成任务
            Task__c tempTask = new Task__c();
            tempTask.RecordTypeId = InspectUp_TaskId;
            tempTask.taskDifferent__c = '被动任务';
            tempTask.taskStatus__c = '01 分配';
            tempTask.Maintenance_Contract__c = InspectupPlan.Maintenance_Contract__c;
            tempTask.assignee__c = InspectupPlan.Maintenance_Contract__r.Service_Contract_Staff__c;
            tempTask.account__c = InspectupPlan.Maintenance_Contract__r.Department__c;
            tempTask.Inspectup_Plan__c = InspectupPlan.id;
            tempTask.PlanEndDate__c = InspectupPlan.Planned_End_Date__c;
            //tempTask.Name = '点检任务（' + InspectupPlan.Maintenance_Contract__r.Department__r.Name + '）';
            Integer num = Integer.valueOf(InspectupPlan.taskFrequency__c == null ? 0 : InspectupPlan.taskFrequency__c) + 1;
            if(num>=10){
                tempTask.Name = 'FWCheck-'+num;
            }else{
                tempTask.Name = 'FWCheck-0'+num;
            }
            tempTask.OwnerId = InspectupPlan.Maintenance_Contract__r.Service_Contract_Staff__c;
            NeedInsertTask.add(tempTask);

            InspectupPlan.taskFrequency__c = num;
            updateInPlanMap.put(InspectupPlan.Id,InspectupPlan);
        }

        if(NeedInsertTask.size()>0){
            insert NeedInsertTask;
             
            List<Inspectup_Plan__c> InsPlanList = new List<Inspectup_Plan__c>();
            for(Task__c t : NeedInsertTask){
                if(updateInPlanMap.get(t.Inspectup_Plan__c)!= null){
                    Inspectup_Plan__c i = updateInPlanMap.get(t.Inspectup_Plan__c);

                    //if(i.task__c != null && ('01 分配'.equals(i.task__r.taskStatus__c) 
                    //    || '03 完成'.equals(i.task__r.taskStatus__c) 
                    //    || '04 取消'.equals(i.task__r.taskStatus__c) ){
                    //    i.task__c = t.id;
                    //}

                    i.task__c = t.id;

                    updateInPlanMap.put(t.Inspectup_Plan__c,i);
                }
            }

            update updateInPlanMap.values();
        }
    }

    global void finish(Database.BatchableContext BC) {

    }
}