public without sharing class AssessmentReportHandler extends Oly_TriggerHandler {

	private Map<Id, AssessmentReport__C> newMap;
	private Map<Id, AssessmentReport__C> oldMap;
	private List<AssessmentReport__C> newList;
	private List<AssessmentReport__C> oldList;


	public AssessmentReportHandler() {
		this.newMap = (Map<Id, AssessmentReport__C>) Trigger.newMap;
		this.oldMap = (Map<Id, AssessmentReport__C>) Trigger.oldMap;
		this.newList = (List<AssessmentReport__C>) Trigger.new;
		this.oldList = (List<AssessmentReport__C>) Trigger.old;
	}


	protected override void afterUpdate() {
		SetupFinalGrade();
	}

	//设置最终考核成绩到服务技师培训主页面上去
	private void SetupFinalGrade() {
		System.debug( 'SetupFinalGrade start: ' );
		list<AssessmentReport__C> AssessReportlist = new list<AssessmentReport__C>();
		list<Campaign> updateCamlist = new list<Campaign>();
		for (AssessmentReport__C  temAR : newList) {
			AssessmentReport__C oldResult = oldMap.get(temAR.id);
			if (temAR.Status__c !=null &&
				!oldResult.Status__c.equals('批准')
			        && temAR.Status__c.equals('批准')
			        && temAR.Type__c.equals('考核')) {
				AssessReportlist.add(temAR);
				if (temAR.TeachingDate__c != null && temAR.TeachingDate__c.day() >= 15) {
					updateCamlist.add(new Campaign(id = temAR.Campaign__c, Mailflg_6Month_service__c = 7));
				} else if (temAR.TeachingDate__c != null && temAR.TeachingDate__c.day() <= 15) {
					updateCamlist.add(new Campaign(id = temAR.Campaign__c, Mailflg_6Month_service__c = 6));
				}
			}
		}
		list<CampaignMember__c> updateCMlist = new list<CampaignMember__c>();
		list<AssessmentReportStaff__c> AssessmentReportStafflist
		    = [select id , CampaignMember__c, Grade__c
		       from AssessmentReportStaff__c
		       where AssessmentReport__c in: AssessReportlist
		       and CampaignMember__c != null ];
		for (AssessmentReportStaff__c temARS : AssessmentReportStafflist) {
			CampaignMember__c temCAM =
			    new CampaignMember__c(id = temARS.CampaignMember__c,
			                          Grade__c = temARS.Grade__c );
			updateCMlist.add(temCAM);
		}
		if (updateCMlist.size() > 0) {
			update updateCMlist;
		}
		if (updateCamlist.size() > 0) {
			update updateCamlist;
		}


	}
}