public without sharing class LoanerExpressTriggerHandler {
	
	public static void updateExpress(List<Loaner_Express__c> newList, Map<Id, Loaner_Express__c> newMap, List<Loaner_Express__c> oldList, Map<Id, Loaner_Express__c> oldMap){
		List<String> expressIDList = new List<String>();
		List<String> returnIDList = new List<String>();
		List<String> lendOrderList = new List<String>();
		Loaner_Express__c old = null;
		for(Loaner_Express__c express : newList){
			old = oldMap.get(express.Id);
			if(old.consignor__c != express.consignor__c || old.Consignor_LogisticsCompany__c != express.Consignor_LogisticsCompany__c || old.Consignor_LogisticsNumber__c != express.Consignor_LogisticsNumber__c){
				expressIDList.add(express.Id);
			}
			if(old.Return_Sender__c != express.Return_Sender__c || old.Return_LogisticsCompany__c != express.Return_LogisticsCompany__c || old.Return_LogisticsNumber__c != express.Return_LogisticsNumber__c){
				returnIDList.add(express.Id);
				if(express.IsLendOrder__c && express.LendOrder__c != null){
					lendOrderList.add(express.Id);
				}
			}
		}
		List<loaner_application_detail__c> updateList = new List<loaner_application_detail__c>();
		if(expressIDList.size() != 0){
			List<loaner_application_detail__c> ladList =[select id,Loaner_Express__c from loaner_application_detail__c where Loaner_Express__c = :expressIDList]; 
			for(loaner_application_detail__c lad : ladList){
				Loaner_Express__c express = newMap.get(lad.Loaner_Express__c);
				if(express.IsDeliveryLogistics__c){
					lad.Return_to_wh_staff__c = express.consignor__c;
					lad.delivery_company__c = express.Consignor_LogisticsCompany__c;
					lad.Tracking_Number__c = express.Consignor_LogisticsNumber__c;
				}
				updateList.add(lad);
			}
		}
		if(returnIDList.size() != 0){
			List<loaner_application_detail__c> ladList =[select id,Return_Express__c from loaner_application_detail__c where Return_Express__c = :returnIDList]; 
			system.debug(ladList.size());
			for(loaner_application_detail__c lad: ladList){
				Loaner_Express__c express = newMap.get(lad.Return_Express__c);
				if(!express.IsDeliveryLogistics__c){
					lad.Return_Trake_Staff__c = express.Return_Sender__c;
					lad.Return_Track_Company__c = express.Return_LogisticsCompany__c;
					lad.Return_Track_Number__c = express.Return_LogisticsNumber__c;
				}
				updateList.add(lad);
			}
		}
		List<Loaner_Express__c> upLeList = new List<Loaner_Express__c>();
		if(lendOrderList.size()>0){
			for(String leId:lendOrderList){
				Loaner_Express__c temp = newMap.get(leId);
				Loaner_Express__c le = new Loaner_Express__c();
				le.Id = temp.LendOrder__c;
				le.Consignor_LogisticsCompany__c = temp.Return_LogisticsCompany__c;
				le.Consignor_LogisticsNumber__c = temp.Return_LogisticsNumber__c;
				upLeList.add(le);
			}
		}
		ControllerUtil.updateExpress(upLeList);
		//update upLeList;
		update updateList;
	}

	public static void emailDate(List<Loaner_Express__c> newList, Map<Id, Loaner_Express__c> newMap, List<Loaner_Express__c> oldList, Map<Id, Loaner_Express__c> oldMap){
		List<String> autoId = new List<String>();
		List<String> remindId = new List<String>();

		for(Loaner_Express__c le : newList){
			Loaner_Express__c old = oldMap.get(le.id);
			system.debug('leName:'+le.Name+'------'+le.Automatic_Received_Date__c+'======'+le.Shipment_Request_Date__c);
			if(le.Automatic_Received_Date__c == null && le.Shipment_Request_Date__c != null){
				autoId.add(le.id);
			}
			if(le.Automatic_Received_Date__c != old.Automatic_Received_Date__c){
				remindId.add(le.id);
			}
		}
		system.debug(autoId.size());
		Map<String,OlympusCalendar__c> oCalendar = calendarUtil.getCalendarMap(Date.today().addDays(-10),Date.today().addDays(240));
		List<Loaner_Express__c> autoList = new List<Loaner_Express__c>();
		List<Loaner_Express__c> remindList = new List<Loaner_Express__c>();
		List<loaner_application_detail__c> ladList = new List<loaner_application_detail__c>();
		if(autoId.size()>0){
			autoList = [select id,Shipment_Request_Date__c,Loaner_type__c from Loaner_Express__c where id= :autoId];
			for(Loaner_Express__c auto : autoList){
				if(auto.Loaner_type__c == 'NDT' || auto.Loaner_type__c == 'ANI'){
					auto.Automatic_Received_Date__c = calendarUtil.getCorrespondingDate(oCalendar,auto.Shipment_Request_Date__c,3);
					auto.Remind_Received_Date__c = calendarUtil.getCorrespondingDate(oCalendar,auto.Automatic_Received_Date__c,-2);
				}else{
					auto.Automatic_Received_Date__c = calendarUtil.getCorrespondingDate(oCalendar,auto.Shipment_Request_Date__c,5);
					auto.Remind_Received_Date__c = calendarUtil.getCorrespondingDate(oCalendar,auto.Automatic_Received_Date__c,-3);
				}
				
			}
		}
		system.debug(autoList);
		if(remindId.size()>0){
			remindList = [select id,Automatic_Received_Date__c,Loaner_type__c from Loaner_Express__c where id= :remindId];
			
			for(Loaner_Express__c remind : remindList){
				if(remind.Loaner_type__c == 'NDT' || remind.Loaner_type__c == 'ANI'){
					remind.Remind_Received_Date__c = calendarUtil.getCorrespondingDate(oCalendar,remind.Automatic_Received_Date__c,-2);
				}else{
					remind.Remind_Received_Date__c = calendarUtil.getCorrespondingDate(oCalendar,remind.Automatic_Received_Date__c,-3);
				}
			}
			for(loaner_application_detail__c lad : [select id,Loaner_Express__c from loaner_application_detail__c where Loaner_Express__c= :remindId]){
				lad.Automatic_Received_Date__c = newMap.get(lad.Loaner_Express__c).Automatic_Received_Date__c;

				ladList.add(lad);
			}
		}
		if(autoList.size()>0) update autoList;
		if(remindList.size()>0) {
			update remindList;
			update ladList;
		}
	}

	public static void getApprover(List<Loaner_Express__c> newList, Map<Id, Loaner_Express__c> newMap, List<Loaner_Express__c> oldList, Map<Id, Loaner_Express__c> oldMap){
		List<Loaner_Express__c> leIdList = new List<Loaner_Express__c>();
		for(Loaner_Express__c le : newList){
			if(le.Approver__c == null){
				leIdList.add(le);
			}
		}

		if(leIdList.size() >0){
			List<Loaner_Express__c> leList = new List<Loaner_Express__c>();
			for(Loaner_Express__c le : [select id,loaner_application__r.loaner_Manager__c,loaner_application__r.Approver__c,loaner_application__r.OwnerId from Loaner_Express__c where id=:leIdList]){
				le.Approver__c = le.loaner_application__r.loaner_Manager__c;
				le.sales_User__c = le.loaner_application__r.Approver__c;
				le.Loaner_Agent__c = le.loaner_application__r.OwnerId;
				leList.add(le);
			}
			if(leList.size()>0){
				update leList;
			}
		}
	}

	public static void updateName(List<Loaner_Express__c> newList, Map<Id, Loaner_Express__c> newMap, List<Loaner_Express__c> oldList, Map<Id, Loaner_Express__c> oldMap){
		List<String> leList1 = new List<String>();
		List<String> leList2 = new List<String>();
		List<Loaner_Express__c> updateList = new List<Loaner_Express__c>();
		for(Loaner_Express__c le : newList){
			Loaner_Express__c old = oldMap.get(le.id);
			if((le.Return_LogisticsNumber__c != old.Return_LogisticsNumber__c ||le.Return_LogisticsCompany__c != old.Return_LogisticsCompany__c) && le.IsDeliveryLogistics__c == false ){
				leList1.add(le.id);
			}

			if(( le.Consignor_LogisticsNumber__c != old.Consignor_LogisticsNumber__c || le.Consignor_LogisticsCompany__c != old.Consignor_LogisticsCompany__c) && le.IsDeliveryLogistics__c == true){
				leList2.add(le.id);
 
			}
		}
		if(leList1.size()>0){
			for(Loaner_Express__c le : [select id,Asset_Return_Date__c,Return_LogisticsNumber__c,IsLendOrder__c,LendOrder__c,Return_LogisticsCompany__c from Loaner_Express__c where id=:leList1]){
				le.Name =le.Asset_Return_Date__c.day()+'日回寄物流单-'+ le.Return_LogisticsCompany__c+le.Return_LogisticsNumber__c;
				updateList.add(le);
				if(le.IsLendOrder__c && le.LendOrder__c != null){
					Loaner_Express__c temp = new Loaner_Express__c();
					temp.id = le.LendOrder__c;
					temp.Name = le.Asset_Return_Date__c.day()+'日发货流单-'+ le.Return_LogisticsCompany__c+le.Return_LogisticsNumber__c;
					updateList.add(temp);
				}
			}
		}
		if(leList2.size()>0){
			for(Loaner_Express__c le :  [select id,Shipment_Request_Date__c,Consignor_LogisticsNumber__c,Consignor_LogisticsCompany__c from Loaner_Express__c where id=:leList2]){
				le.Name = le.Shipment_Request_Date__c.day()+'日发货物流单-'+le.Consignor_LogisticsCompany__c+le.Consignor_LogisticsNumber__c;
				updateList.add(le);
			}
			
		}
		if(updateList.size()>0) ControllerUtil.updateExpress(updateList);
	}


}