public without sharing class LoanerOrderState {

	public static String getOrderStater(String laId){

		Integer ngNum = [select count() from loaner_application_detail__c where loaner_application__c = :laId and Receipt_Status__c = 'NG' and Asset_Return_Date__c != null ];
		loaner_application__c la = [select id,Count_ApplicantReceived__c,Count_Received__c,Count_NG_SendBack__c,Detail_count__c,Count_NG__c from loaner_application__c where id = :laId];
		if([select count() from loaner_application_detail__c where loaner_application__c = :laId and RAESD_Status__c = '已批准'] > 0){
			return '已批准';
		}else if([select count() from loaner_application_detail__c where loaner_application__c = :laId and RAESD_Status__c = '已出库指示'] > 0){
			return '已出库指示';
		}else if([select count() from loaner_application_detail__c where loaner_application__c = :laId and RAESD_Status__c = '已下架'] > 0){
			return '已下架';
		}else if([select count() from loaner_application_detail__c where loaner_application__c = :laId and RAESD_Status__c = '出库前已检测'] > 0 
			&& [select count() from loaner_application_detail__c where loaner_application__c = :laId and Shipment_Request_Date__c != null] ==  0){
			return '出库前已检测';
		}else if([select count() from loaner_application_detail__c where loaner_application__c = :laId and RAESD_Status__c = '出库前已检测'] > 0 
			&& [select count() from loaner_application_detail__c where loaner_application__c = :laId and Shipment_Request_Date__c != null] > 0){
			return '部分发货';
		}else if([select count() from loaner_application_detail__c where loaner_application__c = :laId and RAESD_Status__c = '已出库'] > 0 
			&& [select count() from loaner_application_detail__c where loaner_application__c = :laId and Asset_Return_Date__c != null] == ngNum){
			return '全部发货';
		}else if([select count() from loaner_application_detail__c where loaner_application__c = :laId and RAESD_Status__c = '申请者已收货'] > 0 
			&& [select count() from loaner_application_detail__c where loaner_application__c = :laId and Asset_Return_Date__c != null] == ngNum){
			return '申请者已收货';
		}else if([select count() from loaner_application_detail__c where loaner_application__c = :laId and RAESD_Status__c = '申请者已装机确认'] > 0 
			&& [select count() from loaner_application_detail__c where loaner_application__c = :laId and Asset_Return_Date__c != null] == ngNum){
			return '申请者已装机确认';
		}else if([select count() from loaner_application_detail__c where loaner_application__c = :laId and RAESD_Status__c in ('申请者已装机确认','申请者收货NG','已出库','申请者已收货')] > 0 
			&& [select count() from loaner_application_detail__c where loaner_application__c = :laId and Asset_Return_Date__c != null] > 0){
			return '部分回寄';
		}else if([select count() from loaner_application_detail__c where loaner_application__c = :laId and RAESD_Status__c = '已回寄'] > 0){
			return '全部回寄';
		}else if([select count() from loaner_application_detail__c where loaner_application__c = :laId and RAESD_Status__c = '欠品中'] > 0){
			return '欠品确认中';
		}else{
			system.debug(la);
			system.debug(ngNum);
			if(la.Detail_count__c == la.Count_NG__c){
				if(la.Count_Received__c == la.Detail_count__c){

				}else if(la.Count_NG_SendBack__c == la.Detail_count__c){
					return '全部回寄';
				}else if(la.Count_ApplicantReceived__c == la.Detail_count__c && la.Count_NG_SendBack__c > 0){
					return '部分回寄';
				}else {
					return '申请者已收货';
				}
			}
			return '全部回收';
		}
	}

	public static Map<String ,String> getOrderStater1(List<String> laIdList){
		Map<String,List<loaner_application_detail__c>> laDatailMap = new Map<String,List<loaner_application_detail__c>>();
		Map<String,String> laStatuMap = new Map<String,String>();
		
		List<loaner_application_detail__c> allLadList = [select id,loaner_application__c,RAESD_Status__c,Shipment_Request_Date__c,Asset_Return_Date__c,Receipt_Status__c from loaner_application_detail__c where loaner_application__c = :laIdList];

		for(String laId : laIdList){
			List<loaner_application_detail__c> ladList = new List<loaner_application_detail__c>();
			for(loaner_application_detail__c lad : allLadList){
				if(lad.loaner_application__c == laId){
					ladList.add(lad);
				}
			}
			laDatailMap.put(laId, ladList);
		}

		for(String laId : laIdList){
			List<loaner_application_detail__c> ladList = laDatailMap.get(laId);
			Integer returnNgNum = 0;	//收货ng且回寄
			Integer requestDateNum = 0;	//出库日期
			Integer returnDateNum = 0;	//回寄日期

			Integer indicateNum = 0;	//出库指示
			Integer stockDownNum = 0; 	//下架
			Integer inspectionNum = 0;	//下架检查
			Integer shipmentNum = 0;	//出库
			Integer receiptNum = 0;		//收货
			Integer receiptNgNum =0; 	//收货Ng 
			Integer confirmNum = 0;		//装机确认
			Integer returnNum = 0;		//回寄
			Integer lostNum = 0;		//欠品

			for(loaner_application_detail__c lad : ladList){
				if(lad.RAESD_Status__c == '已出库指示'){
					indicateNum++;
				}else if(lad.RAESD_Status__c == '已下架'){
					stockDownNum++;
				}else if(lad.RAESD_Status__c == '出库前已检测'){
					inspectionNum++;
				}else if(lad.RAESD_Status__c == '已出库'){
					shipmentNum++;
				}else if(lad.RAESD_Status__c == '申请者已收货'){
					receiptNum++;
				}else if(lad.RAESD_Status__c == '申请者收货NG'){
					receiptNgNum++;
				}else if(lad.RAESD_Status__c == '申请者已装机确认'){
					confirmNum++;
				}else if(lad.RAESD_Status__c == '已回寄'){
					returnNum++;
				}else if(lad.RAESD_Status__c == '欠品中'){
					lostNum++;
				}

				if(lad.Shipment_Request_Date__c != null ){
					requestDateNum++;
				}

				if(lad.Asset_Return_Date__c != null ){
					returnDateNum++;
					if(lad.Receipt_Status__c == 'NG'){
						returnNgNum++;
					}
				}
			}
			if(indicateNum> 0){
				laStatuMap.put(laId,'已出库指示');
			}else if(stockDownNum > 0){
				laStatuMap.put(laId, '已下架');
			}else if(inspectionNum > 0 && requestDateNum ==  0){
				laStatuMap.put(laId, '出库前已检测') ;
			}else if(inspectionNum > 0 && requestDateNum > 0){
				laStatuMap.put(laId, '部分发货');
			}else if(shipmentNum > 0 && returnDateNum == returnNgNum){
				laStatuMap.put(laId, '全部发货');
			}else if(receiptNum > 0 && returnDateNum == returnNgNum){
				laStatuMap.put(laId, '申请者已收货');
			}else if(confirmNum > 0 && returnDateNum == returnNgNum){
				laStatuMap.put(laId, '申请者已装机确认');
			}else if((confirmNum+receiptNgNum+shipmentNum+receiptNum) > 0 && returnDateNum > 0){
				laStatuMap.put(laId, '部分回寄');
			}else if(returnNum > 0){
				laStatuMap.put(laId, '全部回寄');
			}else if(lostNum > 0){
				laStatuMap.put(laId, '欠品确认中');
			}else{
				laStatuMap.put(laId, '全部回收');
			}

		}
		system.debug(laStatuMap);
		return laStatuMap;
	}
}