public without sharing class SWOTriggerHandler {
    public static void setDateTimeSendEmail(List<SWO__C> newList, Map<Id, SWO__C> newMap, List<SWO__C> oldList, Map<Id, SWO__C> oldMap) {
        for(SWO__C newSwo : newList){
        	SWO__C oldSwo = (oldMap == null?null:oldMap.get(newSwo.Id));
        	if(oldSwo == null || (oldSwo.SWO_STATUS__c != newSwo.SWO_STATUS__c)){
        		if(newSwo.SWO_STATUS__c == 'Quotation for approval'){
				newSwo.Quotation_for_Approval__c = Datetime.now();
				}
				//当SWO Status 改变为Quotation approved时,此时间点自动抓取并填充此处
				if(newSwo.SWO_STATUS__c == 'Quotation approved'){
					newSwo.Quotation_Approved__c = Datetime.now();
				}
				//谷伟祎新加三个时间框 start
				//当SWO Status 改变为Parts request for diagnosis时,此时间点自动抓取并填充此处
				if(newSwo.SWO_STATUS__c == 'Parts request for diagnosis'){
					newSwo.Parts_request_for_diagnosis_date__c = Datetime.now();
				}
				//当SWO Status 改变为Parts ready for diagnosis时,此时间点自动抓取并填充此处
				if(newSwo.SWO_STATUS__c == 'Parts ready for diagnosis'){
					newSwo.Parts_ready_for_diagnosis_date__c = Datetime.now();
				}
				//当SWO Status 改变为Ready to ship时,此时间点自动抓取并填充此处
				if(newSwo.SWO_STATUS__c == 'Ready to ship'){
					newSwo.Ready_to_ship_date__c = Datetime.now();
				}
				//谷伟祎新加三个时间框 end
				//工程师把SWO状态改为Ready for Quote 时，数据自动填充该日期。
				if(newSwo.SWO_STATUS__c == 'Ready for Quote'){
					newSwo.Ready_for_Quote__c = Datetime.now();
				}
				//工程师把SWO状态改为Parts Hold 时，数据自动填充该日期。
				if(newSwo.SWO_STATUS__c == 'Parts Hold'){
					newSwo.Part_Hold_Date__c = Datetime.now();
				}
				//Delivered to service由 SWO 状态设置为deliver to technician 时抓取。
				if (newSwo.SWO_STATUS__c == 'Delivered to Technician') {
					newSwo.DELIVERED_TO_SERVICE__c = Datetime.now();
				}
				//Ready to Quote由SWO 状态设置为ready to quote时抓取
				if (newSwo.SWO_STATUS__c == 'Ready to Quote') {
					newSwo.READY_TO_QUOTE__c = Datetime.now();
				}
				//Ready for Sales order由SWO 状态设置为ready for sales order时抓取
				if (newSwo.SWO_STATUS__c == 'Ready for Sales order') {
					newSwo.READY_FOR_SALES_ORDER__c = Datetime.now();
				}
				//repair SWO 状态设置为 repair 时抓取
				if(newSwo.SWO_STATUS__c == 'Repair'){
					newSwo.Repair_DateTime__c = Datetime.now();
					//哪个技术人员把SWO状态更改为Repair 状态，把该技术人员的名字填入该位置
					newSwo.TECHNICIAN__c = UserInfo.getUserId();
				}
				//工程师把SWO状态改为In Queue 时，数据自动填充该日期
				if(newSwo.SWO_STATUS__c == 'In Queue'){
					newSwo.In_Queue_Date__c = Datetime.now();
				}
				//如果该SWO有被设置为 Close cancel或者close denied的状态, 把状态值 close cancel 或者close denied 填入到该位置。
				if (newSwo.SWO_STATUS__c == 'Close cancel') {
					newSwo.Returned_Without_Repair_Flag__c = 'Close cancel';
				}
				//如果该SWO有被设置为 Close cancel或者close denied的状态, 把状态值 close cancel 或者close denied 填入到该位置。
				if (newSwo.SWO_STATUS__c == 'Close Denied') {
					newSwo.Returned_Without_Repair_Flag__c = 'Close Denied';
				}
				//如果该SWO有被设置为 Ready for customer pick up的状态, 把状态值Ready for customer pick up 填入到该位置。
				if (newSwo.SWO_STATUS__c == 'Ready for Customer pickup') {
					newSwo.Specified_Delivery_Flag__c = 'Ready for Customer pickup';
				}
        	}
        	if(oldSwo != null && (oldSwo.ASSIGNEDTO__c != newSwo.ASSIGNEDTO__c)){
				doSendEmail(newSwo.ASSIGNEDTO__c,newSwo);
        	}
        }
    }

    public static void sendEmail(List<SWO__C> newList, Map<Id, SWO__C> newMap, List<SWO__C> oldList, Map<Id, SWO__C> oldMap) {
    	for(SWO__C newSwo : newList){
    		if(String.isNotBlank(newSwo.ASSIGNEDTO__c)){
    			doSendEmail(newSwo.ASSIGNEDTO__c,newSwo);
    		}
    	}
    }

    //发送邮件
	private static void doSendEmail(String id,SWO__c swo){
		if(String.isBlank(id)){
			return;
		}
		User user = [select Name,Email from User where id=:id];
		if(user!=null && String.isNotBlank(user.Email)){
			String subject = 'Service Work Order ~SWO:'+swo.Name;
			String message = 'SWO status Changed to: '+ swo.SWO_STATUS__c+', Please continue to proceed this SWO.'+'\r';
			//上线正式环境时链接需要修改为正式环境链接
			//message += ('https://ssbg.my.salesforce.com/apex/SWOPageRead?id='+swo.Id);
			//测试环境
			//message += ('https://ssbg--ssbgtest.my.salesforce.com/apex/SWOPageRead?id='+swo.Id);
			//正式环境链接地址 https://ssbg--c.ap6.visual.force.com/apex/SWOPageRead?id=a0r0K00006aylRy&sfdc.override=1
			message += ('https://ssbg.my.salesforce.com/apex/SWOPageRead?id='+swo.Id);
			List<String> ccAddresses = new List<String>();
			List<String> toAddresses = new List<String>();
			Messaging.SingleEmailMessage sendMail = new Messaging.SingleEmailMessage();
			toAddresses.add(user.Email);
	        sendMail.setToAddresses(toAddresses);
	        sendMail.setSubject(subject);
	        sendMail.setPlainTextBody(message);
	        sendMail.setBccSender(false);
	        sendMail.setUseSignature(false);
	        // ccAddresses.add('swo_listen@z-23gt0jbtzuxf7uka2keyfl65gxjqimwuqbl07g9u0govga7hm6.0t-8aptuaq.cs112.apex.sandbox.salesforce.com');
	        ccAddresses.add(System.label.EmailComeBackListen);
	        ccAddresses.add('dai_Prectech@olympus.com.cn');
	        sendMail.setCcAddresses(ccAddresses);
			Messaging.SendEmailResult[] result_list = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { sendMail });
		}
	}
}