public with sharing class RepairQuoteTrigger {

	public static void ChangeRepair(List<Repair_Quotation__c> newList, Map<Id, Repair_Quotation__c> newMap, List<Repair_Quotation__c> oldList, Map<Id, Repair_Quotation__c> oldMap) {
		System.debug('===========> start');
		List<String> rqIds = new List<String>(); 
		Map<id,Repair_Quotation__c> RqMap = new Map<id,Repair_Quotation__c>();
		if (Trigger.isUpdate) {
			Boolean flag = false;
			for(Repair_Quotation__c rq : newList){
				if(Trigger.isUpdate && rq.ListPrice__c != oldMap.get(rq.id).ListPrice__c){
					//rqIds.add(rq.id);
					flag = true;
					rqIds.add(rq.id);
					RqMap.put(rq.id, rq);
				}
			}
			if(flag == false){
                return;
            }
            for(Repair_Quotation__c rq : newList){
				if(Trigger.isUpdate && rq.ListPrice__c != oldMap.get(rq.id).ListPrice__c){
					// rqIds.add(rq.id);
					// RqMap.put(rq.id, rq);
				}
			}
		}else if(Trigger.isInsert){
			for(Repair_Quotation__c rq : newList){
				rqIds.add(rq.id);
				RqMap.put(rq.id, rq);
			}
		}

		List<Repair__c> RepairUpdateList = new List<Repair__c>();
		if(rqIds.size() > 0){
			//修理的同期中的修理报价de报价金额改变的修理
			List<Repair__c> RpList = [select id,Repair_Quotation_Id__c from Repair__c where Repair_Quotation_Id__c in :rqIds];
			if(RpList.size() > 0){
				for(Repair__c rp : RpList){
					Repair_Quotation__c Rqa = RqMap.get(rp.Repair_Quotation_Id__c);
					rp.Repair_Quotation_Price__c = Rqa.ListPrice__c;
					RepairUpdateList.add(rp);
				}
				update RepairUpdateList;
			}
		}
		System.debug('===========> end');
	}


	public static void SendEmailForRentalApproval(List<Repair_Quotation__c> newList, Map<Id, Repair_Quotation__c> newMap, List<Repair_Quotation__c> oldList, Map<Id, Repair_Quotation__c> oldMap) {
		Set<Id> repairIds = new Set<Id>();
		List<Repair_Quotation__c> repairQuotationList = new List<Repair_Quotation__c>();

		for(Repair_Quotation__c rq : newList) {
			if(Trigger.isUpdate 
				&& rq.Rental_Apply_Discount_Status__c != oldMap.get(rq.id).Rental_Apply_Discount_Status__c
				&& rq.Rental_Apply_Discount_Status__c == '批准'){
				repairIds.add(rq.Repair__c);
				repairQuotationList.add(rq);
			}
		}

		if(repairIds.size() > 0){
			//查询修理中对应的选择的服务方式和维修中心
			Map<Id, Repair__c> repairMap = new Map<Id, Repair__c>([
						select id,On_site_repair__c,work_location_select__c 
						from Repair__c 
						where Id in :repairIds]);

			List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
			// 获取Email Template
			EmailTemplate et = [Select id from EmailTemplate where name = '备品减价申请---批准通过' limit 1];
			// Email
			String userEmail = null;

			for (Repair_Quotation__c rqTemp :repairQuotationList) {
				String addToEmail = null;
				userEmail = rqTemp.RentalApplyDiscountApplyPersonEmail_sys__c;
				if (!repairMap.isEmpty() && repairMap.containsKey(rqTemp.Repair__c)) {
					// 服务方式
					String siteRepair = repairMap.get(rqTemp.Repair__c).On_site_repair__c;
					// 维修中心
					String workLocation = repairMap.get(rqTemp.Repair__c).work_location_select__c;

					if (siteRepair == '直送OGZ修理') {
						// 办事处通知组群设定OGZ
						addToEmail = System.Label.EmailForRentalApproval_OGZ;
					} else if (siteRepair == '直送SORC修理') {
						// 办事处通知组群设定SORC
						addToEmail = System.Label.EmailForRentalApproval_SORC;
					} else if (siteRepair == 'RC修理') {
						if (workLocation == '上海办事处') {
							// 上海办事处
							addToEmail = System.Label.EmailForRentalApproval_RC_SH;
						} else if (workLocation == '杭州办事处') {
							// 杭州办事处 + 上海办事处
							addToEmail = System.Label.EmailForRentalApproval_RC_HZ + ';' + System.Label.EmailForRentalApproval_RC_SH;
						} else if (workLocation == '广东办事处') {
							// 广东办事处
							addToEmail = System.Label.EmailForRentalApproval_RC_GZ;
						} else if (workLocation == '成都办事处') {
							// 成都办事处 + 广东办事处
							addToEmail = System.Label.EmailForRentalApproval_RC_CD + ';' + System.Label.EmailForRentalApproval_RC_GZ;
						} else if (workLocation == '北京办事处') {
							// 北京办事处
							addToEmail = System.Label.EmailForRentalApproval_RC_BJ;
						} else if (workLocation == '沈阳办事处') {
							// 沈阳办事处
							addToEmail = System.Label.EmailForRentalApproval_RC_SY;
						} else if (workLocation == '西安办事处') {
							// 西安办事处
							addToEmail = System.Label.EmailForRentalApproval_RC_XA;
						}
					}
				}

				if (String.isNotBlank(addToEmail)) {
					userEmail = userEmail + ';' + addToEmail;
				}

				String[] toAddresses = userEmail.split(';');
				Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
				mail = Messaging.renderStoredEmailTemplate(et.Id, null , rqTemp.Id);
				mail.setSaveAsActivity(false);
				mail.setToAddresses(toAddresses);
				emails.add(mail);
			}

			Messaging.sendEmail(emails);
		}
	}
}