userserviceimpl.java

来自「社区文章采用的是平板、树形自由选择的两种展示方式」· Java 代码 · 共 529 行 · 第 1/2 页

JAVA
529
字号
	public void update(User model) throws ClubException {
		this.update(model,false);
	}

	public int delete(User model) throws ClubException {		
		CounterService counterService = ServiceWrapper.<CounterService>getSingletonInstance(ServiceLocator.COUNTER);
		Counter counter = counterService.findOnly();
		int c = 0;
		if(!Validator.isEmpty(model)){
			if(model.getUserId()==0){
				model = this.findByUserName(model.getUserName());
			}
			if(model!=null){
				c = this.getUserDAO().delete(model);
				CACHE_USER.remove(CacheKeys.getUserKey(model.getUserId()));
			}
			if(!Validator.isEmpty(counter)){
				if(model.getSex()==1){
					counter.setBoys(counter.getBoys()-1);
				}
				else if(model.getSex()==2){
					counter.setGirls(counter.getGirls()-1);
				}
				counterService.update(counter);
			}
		}else{
			throw new ClubException(MessageUtils.getMessage("error_notfind_user"));
		}
		CACHE_USER.remove(CacheKeys.getKey(model));
		/*
		if(c>0){
			CACHE_USER.remove(CacheKeys.getKey(model));
			this.setMessage(MessageUtils.getMessage("success"));
		}else{
			this.setMessage(MessageUtils.getMessage("error"));
		}
		*/
		return c;
	}

	public int delete(String[] ids) throws ClubException {
		int success = 0;
		int error = 0;
		int c = 0;
		if(ids!=null&&ids.length>0){
			for(int i=0; i<ids.length; i++){
				int id = TypeChange.stringToInt(ids[i]);
				User model = new User();
				model.setUserId(id);
				try {
					c = this.delete(model);
				} catch (ClubException e) {
					logger.error(e.toString());
				}
				if(c>0){
					success++;
				}else{
					error++;
				}
			}
			logger.info("delete(String[] ids) 成功:"+success+"  失败:"+error);
		}else{
			logger.warn("delete(String[] ids) 传入空的ID集合");
			throw new ClubException(MessageUtils.getMessage("error_delete_noid"));
		}		
		return success;
	}
	
	/**
	 * have not cache
	 */
	public User findByUserName(String userName) {
		User model = null;
		if(!Validator.isEmpty(userName)){
			model = this.getUserDAO().findByUserName(userName);
			if(!Validator.isEmpty(model)){
				CACHE_USER.put(CacheKeys.getKey(model),model);
			}
		}
		return model;
	}
	
	public User findByUserNameAndPassword(User model) throws ClubException{
		return this.findByUserNameAndPassword(model, true);
	}


	public User findByUserIdAndPassword(int userId, String password) {
		return this.findByUserIdAndPassword(userId, password, true);
	}

	public User findByUserNameAndPassword(User model, boolean passwordEncrypt) throws ClubException
	{
		if(!Validator.isEmpty(model.getUserName())&&!Validator.isEmpty(model.getPassword()))
		{
			String password = model.getPassword();
			//用户初始化参数开始
			if(this.getBasicInfo()!=null && passwordEncrypt){
				password = StringHelper.encodeString(this.getBasicInfo().getPasswordEncrypt(),model.getPassword());
			}
			model = this.getUserDAO().findByUserNameAndPassword(model.getUserName(),password);
			if(!Validator.isEmpty(model)){
				model.setLastLoginDateTime(FormatDateTime.now());
				logger.debug("model.getLastLoginDateTime():"+model.getLastLoginDateTime());
				model.setLastLoginIp(model.getLastLoginIp());
				model.setLoginTimes(model.getLoginTimes()+1);
				this.ruleUtils(model,1); //按积分制度更新参数
				this.getUserDAO().update(model);
				CACHE_USER.put(CacheKeys.getKey(model),model); // Cache
			}
			return model;
		}else{
			return null;
		}
	}

	public User findByUserIdAndPassword(int userId, String password, boolean passwordEncrypt) {
		User model = null;
		if(userId>0&&!Validator.isEmpty(password)){
			if(passwordEncrypt){
				password = StringHelper.encodeString(this.getBasicInfo().getPasswordEncrypt(),password);
			}
			model = CACHE_USER.get(CacheKeys.getUserKey(userId));
			if(!Validator.isEmpty(model)){ //如果缓存存在
				if(!model.getPassword().equals(password)){ //如果密码不符则置空对象
					return null;
				}
			}else{//如果缓存不存在则通过DAO读取数据库数据
				model = this.getUserDAO().findByUserIdAndPassword(userId,password);
				if(!Validator.isEmpty(model)){
					CACHE_USER.put(CacheKeys.getKey(model),model);
				}
			}
		}
		return model;
	}

	public User findByEmailAddress(String emailAddress) {
		User model = null;
		if(!Validator.isEmpty(emailAddress)){
			User item = this.getUserDAO().findByEmailAddress(emailAddress);
			if(!Validator.isEmpty(item)){
				model = new User();
				BeanUtils.copyProperties(model,item); //PO->BO
				CACHE_USER.put(CacheKeys.getKey(model),model);
			}
		}
		return model;
	}


	public List<User> findByParameter(UserParameter param) {
		if(!Validator.isEmpty(param)){
			return this.getUserDAO().findByParameter(param);
		}
		else{
			return null;
		}
	}

	public long countByParameter(UserParameter param) {
		if(!Validator.isEmpty(param)){
			return this.getUserDAO().countByParameter(param);
		}
		return 0;
	}
	
	/**
	 * 如果缓存存在则向数据库内读取数据
	 */
	public User findById(int id) {
		User model = CACHE_USER.get(CacheKeys.getUserKey(id));
		if(Validator.isEmpty(model)&&id>0){
			model = this.getUserDAO().findById(id);
			if(!Validator.isEmpty(model)){
				CACHE_USER.put(CacheKeys.getUserKey(id),model);
			}
		}
		return model;
	}
	
	public int updateGroupIdByUserIds(int groupId, String[] userIds) throws ClubException {
		int c = 0;
		if(Validator.isEmpty(userIds)){
			//this.setMessage(MessageUtils.getMessage("error_update_noid"));
			throw new ClubException(MessageUtils.getMessage("error_update_noid"));
		}else{
			for(int i=0; i<userIds.length; i++){
				int id = TypeChange.stringToInt(userIds[i]);
				User model = this.findById(id);
				if(!Validator.isEmpty(model)){
					model.setGroupId(groupId);
					try {
						this.update(model);
					} catch (ClubException e1) {
						logger.error(e1.toString());
					}
				}
			}
		}
		return c;
	}
	
	private User ruleUtils(User model, int locator){
		return this.ruleUtils(model,0,locator);
	}
	
	public ManageLog ruleUtils(ManageLog model, int forumId, int locator) {
		if(Validator.isEmpty(model)){
			return null;
		}
		double score = this.getRuleService().getElement(forumId,RuleService.SCORE,locator);
		double money = this.getRuleService().getElement(forumId,RuleService.MONEY,locator);
		double credit = this.getRuleService().getElement(forumId,RuleService.CREDIT,locator);
		Double d = new Double(this.getRuleService().getElement(forumId, RuleService.VIEWS, locator));
		int views = d.intValue();
		model.setScore(score);
		model.setMoney(money);
		model.setCredit(credit);
		model.setViews(views);
		return model;
	}
	
	public User ruleUtils(User model, int forumId, int locator){
		double score = this.getRuleService().getElement(forumId,RuleService.SCORE,locator);
		double money = this.getRuleService().getElement(forumId,RuleService.MONEY,locator);
		double credit = this.getRuleService().getElement(forumId,RuleService.CREDIT,locator);
		int views = (int) this.getRuleService().getElement(forumId, RuleService.VIEWS, locator);
		model.setScore(model.getScore()+score);
		model.setMoney(model.getMoney()+money);
		model.setCredit(model.getCredit()+credit);
		model.setViews(model.getViews()+views);
		logger.debug("积分变更:"+score+" 金币变更:"+money+" 信誉变更:"+credit+" 人气变更:"+views);
		return model;
	}
	
	/************************************************************/

	public BasicInfoService getBasicInfoService() {
		return ServiceWrapper.<BasicInfoService>getSingletonInstance(ServiceLocator.BASICINFO);
	}

	public UserDAO getUserDAO() {
		return DAOWrapper.<UserDAO>getSingletonInstance(DAOLocator.USER);
	}

	
	private GroupService getGroupService() {
		return ServiceWrapper.<GroupService>getSingletonInstance(ServiceLocator.GROUP);
	}

	private RuleService getRuleService() {
		return ServiceWrapper.<RuleService>getSingletonInstance(ServiceLocator.RULE);
	}
	
	public BasicInfo getBasicInfo() {
		return this.getBasicInfoService().findOnly();
	}

	public User update_fileUpload(User model) {
		return null;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?