global class PrototypeToThaw {

	/**
	 * 样机申请者自动解冻
	 * @Author   XHL
	 * @DateTime 2020-02-26
	 * @param    loanerId   样机借用申请Id
	 */
	public static void UserUnfreeze(String loanerId) {

		Map<String, String> userIdViolationIdMap = new Map<String, String>();
		//查找样机借用申请相关的"样机用户违规信息"
		List<User_Violations__c> ulList = [select id, Violations_User__c, Violations_reason__c
		                                   from User_Violations__c
		                                   where Is_effective__c = true and Violations_reason__c = '超期未归还'
		                                           and Violations_Loaner__c  = :loanerId];
		if ( ulList.size() > 0) {
			for (User_Violations__c violation : ulList) {
				userIdViolationIdMap.put(violation.Violations_User__c, violation.Id);
			}
		}

		if (userIdViolationIdMap.size() > 0) {

			Map<String, Map<String, Integer>> userIdReasonQuantityMap = new Map<String, Map<String, Integer>>();
			Map<String, Integer> reasonQuantityMap = new Map<String, Integer>();
			//用户的"样机用户违规信息"
			ulList = [SELECT Id, Is_effective__c, Violations_User__c, Is_locking__c, Violations_reason__c,is_Alone__c
			          FROM User_Violations__c
			          WHERE Violations_User__c in :userIdViolationIdMap.keySet() AND Is_effective__c = true];
			if (ulList.size() > 0) {
				Map<String,User_Violations__c> upsertUserViolationsMap = new Map<String,User_Violations__c>();
				String userId = '';
				for (User_Violations__c violation : ulList) {
					userId = violation.Violations_User__c;
					//判断是否与当前"样机借用申请"的"样机用户违规信息"相同
					if (userIdViolationIdMap.get(userId) != violation.Id) {
						//不同→统计样机借用的用户其他违规信息的数量
						String key = userId + ';' + violation.Id + ';' + violation.Violations_reason__c;
						if (!userIdReasonQuantityMap.containsKey(userId)) {
							reasonQuantityMap.put(key, 1);
							userIdReasonQuantityMap.put(userId, reasonQuantityMap);

						} else {
							if (!reasonQuantityMap.containsKey(key)) {
								reasonQuantityMap.put(key, 1);
							} else {
								reasonQuantityMap.put(key, reasonQuantityMap.get(key) + 1);
							}
							userIdReasonQuantityMap.put(userId, reasonQuantityMap);
						}
					} else {
						//相同→将当前的"样机用户违规信息"设置为无效
						upsertUserViolationsMap.put(violation.Id,violation);
						violation.Is_effective__c = false;
						if (violation.Is_locking__c == true) {
							violation.Is_locking__c = false;
							violation.Violations_Stop_Date__c = Date.today();
							violation.is_Alone__c = true;
						}
					}
				}

				if (userIdReasonQuantityMap.size() > 0) {

					List<User> userList = [SELECT Id, ViolationsID__c FROM User WHERE Id in :userIdReasonQuantityMap.keySet()];
					if (userList.size() > 0 && userList != null) {
						Map<String,Integer> reasonMap = new Map<String,Integer>();
						for (User user : userList) {
							for (String keys : userIdReasonQuantityMap.get(user.Id).keySet()) {
								List<String> keyList =  keys.split(';');
								//判断剩余的"样机用户违规信息"的数量是否大于或等于1
								if (userIdReasonQuantityMap.get(user.Id).size() >= 1) {
									//判断用户的"冻结信息Id"是否与"样机借用申请"的"样机用户违规信息"相同
									if (userIdViolationIdMap.get(user.Id) == user.ViolationsID__c) {

										user.ViolationsID__c = keyList[1];
										User_Violations__c uViolations = new User_Violations__c();
										uViolations.Id = keyList[1];
										uViolations.Is_locking__c = true;
										uViolations.Locking_Start_Date__c = date.today();
										upsertUserViolationsMap.put(keyList[1],uViolations);
									}
								} else {
									user.ViolationsID__c = null;
								}

								//统计违规类型数量
								if (!reasonMap.containsKey(keyList[2]) && '超期未归还'.equals(keyList[2])) {
									reasonMap.put(keyList[2],1);
									user.Return_Timeout_Number__c = 1;
								} else if ('超期未归还'.equals(keyList[2])){
									user.Return_Timeout_Number__c += 1;
								} else if (!reasonMap.containsKey(keyList[2]) && '系统自动收货'.equals(keyList[2])) {
									reasonMap.put(keyList[2],1);
									user.Automatic_Received_Number__c = 1;
								} else if ('系统自动收货'.equals(keyList[2])){
									user.Automatic_Received_Number__c += 1;
								}
							}
							//2020-05-08 AddStart
							//当违规类型"超期未归还"不存在时，用户的"超期未归还次数"字段置为0
							if ( reasonMap.size() > 0) {
								if (!reasonMap.containsKey('超期未归还')) {
									user.Return_Timeout_Number__c = 0;
								} 
							}
							//2020-05-08 AddEnd
						}


						update userList;
					}

				} else {
					if (String.isNotBlank(userId)) {
						User user = new User();
						user.Id = userId;
						user.Return_Timeout_Number__c = 0;
						user.ViolationsID__c = null;
						update user;
					}
				}

				if ( upsertUserViolationsMap.size() > 0 && upsertUserViolationsMap != null) {
					upsert upsertUserViolationsMap.values();
				}
				//upsert ulList;
			}


		}
	}
}