userserviceimpl.java

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

JAVA
529
字号
/* 
 * Created on 2007-1-14
 * Last modified on 2008-1-1
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.club.service.user;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.yeqiangwei.club.service.ServiceLocator;
import com.yeqiangwei.club.service.ServiceWrapper;
import com.yeqiangwei.club.model.BasicInfo;
import com.yeqiangwei.club.model.Group;
import com.yeqiangwei.club.model.ManageLog;
import com.yeqiangwei.club.model.User;
import com.yeqiangwei.club.service.security.GroupService;
import com.yeqiangwei.club.service.util.BasicInfoService;
import com.yeqiangwei.club.service.util.CounterService;
import com.yeqiangwei.club.service.util.CountersService;
import com.yeqiangwei.cache.Cache;
import com.yeqiangwei.cache.CacheFactory;
import com.yeqiangwei.club.cache.CacheKeys;
import com.yeqiangwei.club.cache.CacheRegion;
import com.yeqiangwei.club.dao.DAOLocator;
import com.yeqiangwei.club.dao.DAOWrapper;
import com.yeqiangwei.club.dao.UserDAO;
import com.yeqiangwei.club.model.Counter;
import com.yeqiangwei.club.model.Counters;
import com.yeqiangwei.club.model.Register;
import com.yeqiangwei.club.exception.ClubException;
import com.yeqiangwei.club.exception.ClubRuntimeException;
import com.yeqiangwei.club.param.CounterParameter;
import com.yeqiangwei.club.param.UserParameter;
import com.yeqiangwei.club.util.BeanUtils;
import com.yeqiangwei.club.util.MessageUtils;
import com.yeqiangwei.club.controller.form.AProfileForm;
import com.yeqiangwei.club.controller.form.UpdateUserParamterForm;
import com.yeqiangwei.util.Validator;
import com.yeqiangwei.util.FormatDateTime;
import com.yeqiangwei.util.StringHelper;
import com.yeqiangwei.util.TypeChange;

public class UserServiceImpl implements UserService{ 
	
	private static final Logger logger = Logger.getLogger(UserServiceImpl.class.getName());

	private static Cache<User> CACHE_USER = CacheFactory.<User>creator(CacheRegion.USER);
	
	private static Map<Integer, Integer> VIEWS_MAP;
	
	static{
		if(VIEWS_MAP==null){
			VIEWS_MAP = new Hashtable<Integer, Integer>();
		}
	}
	
	public static void main(String args[]){
		Map<Integer, Integer> VIEWS_MAP = new Hashtable<Integer, Integer>();
		Iterator<Integer> it = VIEWS_MAP.keySet().iterator();  
		while(it.hasNext()){
			Integer userId = it.next();
			System.out.println(userId);
		}
	}
	
	@Override
	public int updateSignaturesByUserId(int userId, String sig)
			throws ClubException {
		if(userId==0){
			throw new ClubException(MessageUtils.getMessage("error_notfind_user"));
		}
		else if(StringHelper.length(sig)>500){
			throw new ClubException(MessageUtils.getMessage("error_toolong"));
		}
		CACHE_USER.remove(CacheKeys.getUserKey(userId));
		return this.getUserDAO().updateSignaturesByUserId(userId, sig);
	}
	
	@Override
	public User updateUserParameter(UpdateUserParamterForm form) throws ClubException {
		User user = this.findById(form.getUserId());
		if(Validator.isEmpty(user)){
			throw new ClubException(MessageUtils.getMessage("error_notfind_user"));
		}
		user.setCredit(user.getCredit()+form.getCredit());
		user.setScore(user.getScore()+form.getScore());
		user.setMoney(user.getMoney()+form.getMoney());
		user.setViews(user.getViews()+form.getViews());
		this.getUserDAO().update(user);
		CACHE_USER.put(CacheKeys.getUserKey(user.getUserId()), user);
		return user;
	}

	@Override
	public void setViews(int userId,int views){
		if(userId==0||views==0){
			return ;
		}
		if(VIEWS_MAP.get(userId)==null){
			User user = this.findById(userId);
			if(!Validator.isEmpty(user)){
				VIEWS_MAP.put(user.getUserId(), views);
			}else{
				logger.error("setViews error, User is null");
			}
		}else{
			VIEWS_MAP.put(userId, VIEWS_MAP.get(userId)+views);
		}
	}
	
	/**
	 * 计划定时启动
	 */
	public void setViewsToDB(){
		Iterator<Integer> it = VIEWS_MAP.keySet().iterator();
		List<Integer> list = new ArrayList<Integer>();
		while(it.hasNext()){
			int userId = it.next();
			User user = this.findById(userId);
			if(!Validator.isEmpty(user)){
				user.setViews(VIEWS_MAP.get(user.getUserId())+user.getViews());
				this.getUserDAO().updateViewsByUserId(user.getUserId(), user.getViews());
				CACHE_USER.put(CacheKeys.getUserKey(user.getUserId()), user);
			}
			list.add(userId);
		}
		for(Integer userId : list){
			VIEWS_MAP.remove(userId);
		}
	}
	
	public Cache<User> getCache(){
		return CACHE_USER;
	}
	
	public int updateIsIndexedByUserId(int userId, boolean isIndexed){
		if(userId>0){
			return this.getUserDAO().updateIsIndexedByUserId(userId,isIndexed);
		}else{
			return 0;
		}
	}
	
	public User update(AProfileForm form) throws ClubException {
		User user = this.findById(form.getUserId());
		if(Validator.isEmpty(user)){
			throw new ClubException(MessageUtils.getMessage("error_notfind_user"));
		}
		if(!user.getPassword().equals(form.getPassword())){
			form.setPassword(StringHelper.encodeString(this.getBasicInfo().getPasswordEncrypt(),form.getPassword()));
		}
		BeanUtils.copyProperties(user,form);
		user.setIsIndexed(false);
		this.update(user,false);
		return user;
	}

	public void createOrUpdate(User model) throws ClubException {
		if(model.getUserId()<=0){
			this.create(model);
		}else{
			this.update(model);
		}
	}

	public void create(User model) throws ClubException {
		//统计相关开始
		int ymd = TypeChange.stringToInt(FormatDateTime.formatDateTime("yyyyMMdd"));
		CountersService countersService = ServiceWrapper.<CountersService>getSingletonInstance(ServiceLocator.COUNTERS);
		CounterParameter param = new CounterParameter();
		param.setYmd(ymd);
		Counters counters = countersService.findOnlyByParameter(param);
		if(Validator.isEmpty(counters)){
			counters = new Counters();
		}
		counters.setYmd(ymd);
		CounterService counterService = ServiceWrapper.<CounterService>getSingletonInstance(ServiceLocator.COUNTER);
		Counter counter = counterService.findOnly();
		if(Validator.isEmpty(counter)){
			counter = new Counter();
		}
		if(model.getSex()==1){
			counter.setBoys(counter.getBoys()+1);
			counters.setBoys(counters.getBoys()+1);
		}else{
			counter.setGirls(counter.getGirls()+1);
			counters.setGirls(counters.getGirls()+1);
		}
		//统计相关结束
		
		//用户初始化参数开始
		Group group = this.getGroupService().findByRegisterDefault();
		if(!Validator.isEmpty(group)){//初始化用户等级结束
			model.setGroupId(group.getGroupId());
		}else{
			//this.setMessage(MessageUtils.getMessage("error_reg_group"));
			throw new ClubException(MessageUtils.getMessage("error_reg_group")); //无法分配默认用户组
		}
		
		BasicInfo basicInfo = this.getBasicInfoService().findOnly();
		model.setFileUploadMaxSize(basicInfo.getFileUploadUserSize()); //默认空间
		model.setFileUploadUseSize(0);
		if(basicInfo.getFileUploadIsOpen()){
			model.setFileUploadIsOpen((byte)1);
		}else{
			model.setFileUploadIsOpen((byte)0);
		}
		model.setNoAllowed(basicInfo.getUserAllowedType());
		model.setPassword(StringHelper.encodeString(basicInfo.getPasswordEncrypt()
				,model.getPassword()));
		if(basicInfo.getUserAllowedType()==2){
			RegisterService registerService = ServiceWrapper.<RegisterService>getSingletonInstance(ServiceLocator.REGISTER);
			Register register = registerService.findByKeyAndNumber(model.getEmailAddress(), model.getRegisterNumber());
			if(Validator.isEmpty(register)){
				//this.setMessage(MessageUtils.getMessage("error_reg_number"));
				throw new ClubException(MessageUtils.getMessage("error_reg_number"));
			}else if(register.getUsed()){
				//this.setMessage(MessageUtils.getMessage("error_reg_number"));
				throw new ClubException(MessageUtils.getMessage("error_reg_number"));
			}else{
				model.setNoAllowed((byte)0);
				register.setUsed(true);
				registerService.update(register);
			}
		}
		//初始化用户等级
		this.ruleUtils(model,0); //按积分制度更新参数
		//用户初始化参数结束
		counterService.createOrUpdate(counter);
		countersService.createOrUpdate(counters);
		this.getUserDAO().create(model);
		CACHE_USER.put(CacheKeys.getKey(model),model);
		//this.setMessage(MessageUtils.getMessage("reg",model));
	}
	
	private User filter(User model, boolean passwordEncrypt) throws ClubException{
		if(Validator.isEmpty(model)||model.getUserId()==0){
			throw new ClubException(MessageUtils.getMessage("error_update_noid"));
		}
		if(passwordEncrypt){
			model.setPassword(StringHelper.encodeString(
					this.getBasicInfoService().findOnly().getPasswordEncrypt()
					,model.getPassword()));
		}
		return model;
	}

	public void update(User model, boolean passwordEncrypt) throws ClubException{
		this.filter(model,passwordEncrypt);
		User item = this.getUserDAO().findById(model.getUserId());
		if(Validator.isEmpty(item)){
			throw new ClubRuntimeException(MessageUtils.getMessage("error_notfind_user"));
		}else{
			BeanUtils.copyProperties(item,model);
			this.getUserDAO().update(item);
			CACHE_USER.put(CacheKeys.getUserKey(item.getUserId()),item);
		}
	}
	

⌨️ 快捷键说明

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