global class AccountWebService {
	WebService static String AccountCancel(String accountId) {
		Account account = new Account();
		List<Account> accountList = [select Id, OwnerId, AccountStatus__c, CancelReason__c from Account where Id =:accountId];
		if (accountList.size() == 0) {
			return '无效的客户';
		} else {
			account = accountList[0];
		}
		if (account.AccountStatus__c == 'Cancel') {
			return '客户已经取消';
		}
		if (account.CancelReason__c == null || account.CancelReason__c.length() == 0) {
			return '必须输入取消理由';
		}
		account.AccountStatus__c = 'Cancel';
		account.OwnerId = System.Label.BatchUserId;
		update account;
		
		return '1';
	}

	WebService static String UpdateShare(String accountId, String ownerOld, String ownerNew) {
		try {
			Account acc = new Account(
				Id = accountId,
				OwnerId = ownerNew
			);
			update acc;

			List<AccountShare> asList = [select Id, AccountId, UserOrGroupId, AccountAccessLevel, RowCause from AccountShare where AccountId = :accountId and UserOrGroupId = :ownerOld and RowCause = 'Team' and AccountAccessLevel = 'Read'];
			for (AccountShare ash : asList) {
				ash.AccountAccessLevel = 'Edit';
			}
			if (asList.size() > 0) {
				update asList;
			}
		} catch (Exception ex) {
			return ex.getMessage();
		}
		
		return '1';
	}
	// 技术服务部担当、技术服务部(部长、副部长)、IE 样机管理者 简档 创建客户跳过必填字段验证，改为提交审批时验证
	// CHAN-BU98HT XHL 20201014
	WebService static String RequiredField(String accountId) {
		String result = '1';

		List<String> errorMessageList = new List<String>();
		// 王鹏伟修改前 SQL 备份
		//List<Account> accList = [select Id,Name,FacilityNameD__c,DivisionName_D__c,DealerSelectOwner__c,Province__c,CityD__c,Address1D__c,Sub_UseD__c,AccountStatusD__c,PostCodeD__c,DepartmentNameD__c,IndustryCD__c,MarketVerticalsD__c,UseD__c,EnglishNameD__c,TargetCustomer__c,CustomerSource__c,UserType__c,RecordTypeId from Account where  Id = :accountId ];
		List<Account> accList = [select Id,Name,FacilityNameD__c,DivisionName_D__c,DealerSelectOwner__c,Province__c,CityD__c,Address1D__c,Sub_UseD__c,AccountStatusD__c,PostCodeD__c,DepartmentNameD__c,IndustryCD__c,MarketVerticalsD__c,UseD__c,EnglishNameD__c,TargetCustomer__c,CustomerSource__c,UserType__c,RecordTypeId,DealerAccount__c,AssetRVI1D__c,serviceCreate__c from Account where  Id = :accountId ];
		if (accList.size() > 0) {

			// 共同部分8个字段
			Account account = accList[0];
			// 客户/代理商名称.
			if (String.isBlank(account.FacilityNameD__c)) {
				String errorMessage = '客户/代理商名称.';
				errorMessageList.add(errorMessage);
			}
			// 部门名称.
			if (String.isBlank(account.DivisionName_D__c)) {
				String errorMessage = '部门名称.';
				errorMessageList.add(errorMessage);
			}
			// 审批人
			if (String.isBlank(account.DealerSelectOwner__c)) {
				String errorMessage = '审批人';
				errorMessageList.add(errorMessage);
			}
			// 省
			if (String.isBlank(account.Province__c)) {
				String errorMessage = '省';
				errorMessageList.add(errorMessage);
			}
			// 城市.
			if (String.isBlank(account.CityD__c)) {
				String errorMessage = '城市.';
				errorMessageList.add(errorMessage);
			}
			// 地址1.	
			if (String.isBlank(account.Address1D__c)) {
				String errorMessage = '地址1.';
				errorMessageList.add(errorMessage);
			}
			// 账户状态.
			/*if (String.isBlank(account.AccountStatusD__c)) {
				String errorMessage = '账户状态. 必填 请确认;';
				errorMessageList.add(errorMessage);
			}*/
			// 邮编.
			/*if (String.isBlank(account.PostCodeD__c)) {
				String errorMessage = '邮编. 必填 请确认;';
				errorMessageList.add(errorMessage);
			}*/
			// Sub Use.
			if (String.isBlank(account.Sub_UseD__c)) {
				String errorMessage = 'Sub Use.';
				errorMessageList.add(errorMessage);
			}
			// ANI NDT RVI IE
			/*if ('01228000000TdFL'.equals(account.RecordTypeId) || '01228000000TdFG'.equals(account.RecordTypeId) || 
				'01228000000TdFB'.equals(account.RecordTypeId) || '01228000000TdF6'.equals(account.RecordTypeId)) {
				// Sub Use.
				if (String.isBlank(account.Sub_UseD__c)) {
					String errorMessage = 'Sub Use. 必填 请确认;';
					errorMessageList.add(errorMessage);
				}
			}*/
			// RVI IE 王鹏伟新加
			if(('01228000000TdFB'.equals(account.RecordTypeId) || '01228000000TdF6'.equals(account.RecordTypeId)) && account.serviceCreate__c == true){
				//代理商
				if(!String.isNotBlank(String.valueOf(account.DealerAccount__c))){
					String errorMessage = '代理商';
					errorMessageList.add(errorMessage);
				}
			}
			// RVI 判断RVI产品信息
			if('01228000000TdFB'.equals(account.RecordTypeId)){
				//
				if(!String.isNotBlank(account.AssetRVI1D__c)){
					String errorMessage = 'RVI产品信息';
					errorMessageList.add(errorMessage);
				}
			}
			// IE
			if ('01228000000TdF6'.equals(account.RecordTypeId)) {
				// 英文名称.
				/*if (String.isBlank(account.EnglishNameD__c)) {
					String errorMessage = '英文名称. 必填 请确认;';
					errorMessageList.add(errorMessage);
				}*/
				// 客户类型
				/*if (String.isBlank(account.TargetCustomer__c)) {
					String errorMessage = '客户类型 必填 请确认;';
					errorMessageList.add(errorMessage);
				}*/
				// 客户来源.
				if (String.isBlank(account.CustomerSource__c)) {
					String errorMessage = '客户来源.';
					errorMessageList.add(errorMessage);
				}
				// 用户属性
				if (String.isBlank(account.UserType__c)) {
					String errorMessage = '用户属性';
					errorMessageList.add(errorMessage);
				}
			}
			// BS
			/*if ('01228000000TdF1'.equals(account.RecordTypeId)) {
				// 科室.
				if (String.isBlank(account.DepartmentNameD__c)) {
					String errorMessage = '科室. 必填 请确认;';
					errorMessageList.add(errorMessage);
				}
				// 行业.
				if (String.isBlank(account.IndustryCD__c)) {
					String errorMessage = '行业. 必填 请确认;';
					errorMessageList.add(errorMessage);
				}
				// 市场分类.
				if (String.isBlank(account.MarketVerticalsD__c)) {
					String errorMessage = '市场分类. 必填 请确认;';
					errorMessageList.add(errorMessage);
				}
				// 用途.
				if (String.isBlank(account.UseD__c)) {
					String errorMessage = '用途. 必填 请确认;';
					errorMessageList.add(errorMessage);
				}
			}
*/
			if (errorMessageList.size() > 0){
				result = '';
				for (String error :errorMessageList) {
					result += error +'\n';
				}
				result += '必填，请确认！';
			}
			
		}
		return result;
	}
	// 技术服务部担当、技术服务部(部长、副部长)、IE 样机管理者 简档 创建联系人跳过必填字段验证，改为提交审批时验证
	// CHAN-BU98HT XHL 20201014
	WebService static String ContactRequiredField(String contactId) {
		String result = '1';
		List<String> errorMessageList = new List<String>();
		List<Contact> contactList = [Select Id,Name,MobilePhone,MobilePhoneD__c,Postcode__c,PostcodeD__c,Address1__c,Address1D__c,CreatedById,ContactStatusD__c,RecordTypeId,CreatedBy.ProfileId,ProductSegmentBS__c  From Contact where Id = : contactId ];
		String userId = UserInfo.getUserId();
		if (contactList.size() > 0) {
			Contact contact = contactList[0];
			String profileId = contact.CreatedBy.ProfileId;
			//技术服务部担当
			if ('00e28000001sZE6'.equals(profileId.subString(0,15)) ||
				'00e28000000eN67'.equals(profileId.subString(0,15))) {
				if (String.isBlank(contact.MobilePhoneD__c)) {
					String errorMessage = '手机.';
					errorMessageList.add(errorMessage);
				}
				if (String.isBlank(contact.Name)) {
					String errorMessage = '姓名';
					errorMessageList.add(errorMessage);
				}
				
				if (String.isBlank(contact.Address1D__c)) {
					String errorMessage = '地址1.';
					errorMessageList.add(errorMessage);
				}
				if(contact.ProductSegmentBS__c == true){
					if (String.isBlank(contact.PostcodeD__c)) {
					String errorMessage = '邮政编码.';
					errorMessageList.add(errorMessage);
					}
					if (String.isBlank(contact.ContactStatusD__c)) {
						String errorMessage = '状态.	';
						errorMessageList.add(errorMessage);
					}
				}
			}
			
			// SSBG IE 样机管理者，SSBG技术服务部（部长・副部长）
			if ('00e28000000YQMx'.equals(profileId.subString(0,15)) || 
				'00e0K000001l0l9'.equals(profileId.subString(0,15)) ||
				'00e28000000eN67'.equals(profileId.subString(0,15))) {

				if (String.isBlank(contact.MobilePhone)) {
					String errorMessage = '手机';
					errorMessageList.add(errorMessage);
				}
				if (String.isBlank(contact.Name)) {
					String errorMessage = '姓名';
					errorMessageList.add(errorMessage);
				}
				
				if (String.isBlank(contact.Address1__c)) {
					String errorMessage = '地址1';
					errorMessageList.add(errorMessage);
				}
				
				if(contact.ProductSegmentBS__c == true){
					if (String.isBlank(contact.PostcodeD__c)) {
					String errorMessage = '邮政编码.';
					errorMessageList.add(errorMessage);
					}
					if (String.isBlank(contact.ContactStatusD__c)) {
						String errorMessage = '状态.	';
						errorMessageList.add(errorMessage);
					}
				}
			}
		}

		if (errorMessageList.size() > 0){
			result = '';
			for (String error :errorMessageList) {
				result += error +'\n';
			}
			result += '必填，请确认！';
		}
		return result;
	}

}