public without sharing class AfterUpdateReportHandler extends Oly_TriggerHandler{
	private Map<Id, Report__c> newMap;
    private Map<Id, Report__c> oldMap;
    private List<Report__c> newList;
    private List<Report__c> oldList;
	public AfterUpdateReportHandler() {
		this.newMap = (Map<Id, Report__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Report__c>) Trigger.oldMap;
        this.newList = (List<Report__c>) Trigger.new;
        this.oldList = (List<Report__c>) Trigger.old;
        Integer i = 0;
	}

	protected override void afterInsert() {
        InsertReport();
        //add by rentx 2021-03-31 start
         SplitOptionList();
        //add by rentx 2021-03-31 end
    }

	protected override void afterUpdate() {
		ChangeReport();
		UpdateReport();
        //add by rentx 2021-03-31 start
         SplitOptionList();
        //add by rentx 2021-03-31 end
		
	}

	private void InsertReport() {
		List<Rental_Apply__c> raList = new List<Rental_Apply__c>();
    	List<OPDPlan__c> oPDPlans = new List<OPDPlan__c>();

		for (Report__c newReprot : newList) { 
			if (newReprot.Loaner_request_no1__c != null) {
                Rental_Apply__c ra = new Rental_Apply__c(
                    Id = newReprot.Loaner_request_no1__c,
                    OPD__c = newReprot.Id
                );
                raList.add(ra);
            }
		}
		// SWAG-BA65JL 2019/03/22   XHL Start
		for (Report__c newReprot : newList) { 
			
			if (String.isNotBlank(newReprot.Report_OPDPlan__c)) {
                OPDPlan__c opdplan = new OPDPlan__c(
                	Id = newReprot.Report_OPDPlan__c,
                	OPDPlan_Report__c = newReprot.Id
                );
                oPDPlans.add(opdplan);
            }
		}

		if (raList.size() > 0) {
		    update raList;
		}
		if (oPDPlans.size() > 0) {
			update oPDPlans;
		}
		// SWAG-BA65JL 2019/03/22   XHL End
	}

	private void ChangeReport() {
		List<Rental_Apply__c> raList = new List<Rental_Apply__c>();
		for (Report__c newReprot : newList) { 
			Report__c oldReprot = oldMap.get(newReprot.Id);
            if (newReprot.Loaner_request_no1__c != null && oldReprot.Loaner_request_no1__c != newReprot.Loaner_request_no1__c) {
                Rental_Apply__c ra = new Rental_Apply__c(
                    Id = newReprot.Loaner_request_no1__c,
                    OPD__c = newReprot.Id
                );
                raList.add(ra);
            }
            
		}

		if (raList.size() > 0) {
		    update raList;
		}
	}
    // SWAG-BA65JL 2019/03/22   XHL Start
	private void UpdateReport(){
		List<OPDPlan__c> oPDPlanList = new List<OPDPlan__c>();
		// CHAN-BC6BW9 LHJ 20190520 Start
    	List<String> taskList = new List<String>();
    	// CHAN-BC6BW9 LHJ 20190520 End

		for (Report__c newReprot : newList) { 
			OPDPlan__c opdplan = new OPDPlan__c();
			if (oldMap.get(newReprot.Id).get('Status__c') != newReprot.Status__c 
				&& newReprot.Status__c == '申請中' 
				&& String.isNotBlank( newReprot.Report_OPDPlan__c)) {
            	opdplan.Id = newReprot.Report_OPDPlan__c;
        		opdplan.Status__c = '完毕';
        		oPDPlanList.add(opdplan);
			} else if (oldMap.get(newReprot.Id).get('Report_OPDPlan__c') != newReprot.Report_OPDPlan__c && String.isNotBlank( newReprot.Report_OPDPlan__c)) {
	            	            
        		opdplan.Id = newReprot.Report_OPDPlan__c;
        		opdplan.OPDPlan_Report__c = newReprot.Id;
        		oPDPlanList.add(opdplan);
	        } 
	        //else if (oldMap.get(newReprot.Id).get('Report_OPDPlan__c') != newReprot.Report_OPDPlan__c && String.isBlank( newReprot.Report_OPDPlan__c)) {
	        //	opdplan.Id = oldMap.get(newReprot.Id).Report_OPDPlan__c;
        	//	opdplan.OPDPlan_Report__c = null;
        	//	oPDPlanList.add(opdplan);
	        //}

            // CHAN-BC6BW9 LHJ 20190520 Start
            if (oldMap.get(newReprot.Id).get('Status__c') != newReprot.Status__c 
					&& oldMap.get(newReprot.Id).get('Status__c') == '草案中' ) {
            	taskList.add(newReprot.ID);
            }
            // CHAN-BC6BW9 LHJ 20190520 End
		}
		// CHAN-BC6BW9 LHJ 20190520 Start
		if (taskList != null && taskList.size() > 0) {
			List<Task> lstTask = [select id, status from task where whatid in :taskList and status != '完了'];

			if (lstTask != null && lstTask.size() > 0) {
	            List<Task> updTask = new List<Task>();
	            for(Task tsk : lstTask) {
	                tsk.status = '完了';
	                updTask.add(tsk);
	            }
	            if (updTask.size() > 0) {
	                update updTask;
	            }
	        }
        }
		// CHAN-BC6BW9 LHJ 20190520 End
		if (oPDPlanList.size() > 0) {
			update oPDPlanList;
		}
	}
	// SWAG-BA65JL 2019/03/22   XHL End

	//add by rentx 2021-03-31 start
	private void SplitOptionList(){
		//创建参数 key:Sobject对象 value:发生改变的选项列表字段集合 (哪个选项列表发生变化就把哪个当做参数,目前只有'产品优点(可多选）	','产品缺点(可多选）','此次OPD总结(可多选)	')
		Map<SObject,List<String>> data = new Map<SObject,List<String>>();
		for (Report__c rep : newList) {
			List<String> columsList = new List<String>();
			Report__c oldrep = null;
			if (Trigger.isUpdate) {
				oldrep = oldMap.get(rep.Id);
			}

			if (oldrep == null || oldrep.Product_Evaluation_Advantage1__c != rep.Product_Evaluation_Advantage1__c) {
				columsList.add('Product_Evaluation_Advantage1__c');
			}
			if (oldrep == null || oldrep.Product_Evaluation_Faults1__c != rep.Product_Evaluation_Faults1__c) {
				columsList.add('Product_Evaluation_Faults1__c');
			}
			if (oldrep == null || oldrep.This_OPD_Summarize__c != rep.This_OPD_Summarize__c) {
				columsList.add('This_OPD_Summarize__c');
			}

			data.put(rep, columsList);
		}

		//更新历史数据 add by rentx 2021-04-07 start
		/*Map<SObject,List<String>> data = new Map<SObject,List<String>>();
		List<String> columsList = new List<String>();
		columsList.add('Product_Evaluation_Advantage1__c');
		columsList.add('Product_Evaluation_Faults1__c');
		columsList.add('This_OPD_Summarize__c');
		for (Report__c rep : newList) {
			data.put(rep, columsList);
		}*/
		//更新历史数据 by rentx 2021-04-07 end

		SplitOptionListUtil util = new SplitOptionListUtil();
		util.makeObject(data);
	}
	//add by rentx 2021-03-31 end
}