⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userbiz.java

📁 一个不错的bbs论坛系统.对初学者很有帮助
💻 JAVA
字号:
package com.yhbbs.user.biz;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.yhbbs.bbs.bean.WealthDtoIm;
import com.yhbbs.user.bean.UserImpl;
import com.yhbbs.user.bean.UserIndexIm;
import com.yhbbs.user.dao.UserDaoIm;
import com.yhbbs.user.itface.bean.User;
import com.yhbbs.user.itface.bean.UserIndex;
import com.yhbbs.user.itface.bean.UserLgDto;
import com.yhbbs.user.itface.bean.UserSession;
import com.yhbbs.user.itface.dao.UserDao;
import com.yhbbs.utils.Constants;
import com.yhbbs.utils.StringUtils;

/**
 * <p>Title:论坛用户相关信息操作</p>
 * <li> 论坛用户相关信息操作<br>
 * <br><b>WebSite: www.yyhweb.com</b>
 * <br><b>CopyRight: yyhweb[由由华网]</b>
 * @author stephen
 * @version YHBBS-2.0
 */
public class UserBiz {
	
	private static Logger bbslog = Logger.getLogger(UserBiz.class);
	
	private static UserDao userdao = UserDaoIm.getInstance();
	
	/** 根据用户Id取得一个用户
	 * @param userId 用户Id
	 * @return 论坛用户
	 */
	public static User getUser(int userId){
		
        try {
            return userdao.getUser(userId);
        }
        catch(SQLException e) {
            bbslog.error("Throws a SqlException when invoke getUser(userId):\n" + e.toString());
        }
        return new UserImpl();
	}
	
	/** 根据用户Id取得首页用户信息
	 * @param userId 用户Id
	 * @return 论坛首页用户信息
	 */
	public static UserIndex getIndexUser(int userId){
		
		try {
			return userdao.getIndexUser(userId);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getIndexUser(userId):\n" + e.toString());
		}
		return new UserIndexIm();
	}
	
	/** 根据用户名取得一个用户
	 * @param userName 用户名
	 * @return 论坛用户
	 */
	public static User getUser(String userName){
		
		try {
			return userdao.getUser(userName);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getUser(userName):\n" + e.toString());
		}
		return null;
	}
	
	/** 取得用户Id或判断用户是否存在
	 * @param userName 用户名
	 * @return 论坛用户Id
	 */
	public static int getUserId(String userName){
		
		try {
			return userdao.getUserId(userName);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getUserId(userName):\n" + e.toString());
		}
		return 0;
	}
	
	/** 取得用户名称
	 * @param userId 用户Id
	 * @return 论坛用户名称
	 */
	public static String getUserName(int userId){
		
		try {
			return userdao.getUserName(userId);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getUserName(int userId):\n" + e.toString());
		}
		return "";
	}
	
	/** 取得用户e_mail或判断e_mail是否存在
	 * @param email E_mail
	 * @return 该email已经被注册的用户数
	 */
	public static int getUserMail(String email){
		
		try {
			return userdao.getUserMail(email);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getUserMail(email):\n" + e.toString());
		}
		return 0;
	}
	/** 由用户Id取得用户e_mail
	 * @param userId 用户Id
	 * @return 该email已经被注册的用户数
	 */
	public static String getUserMail(int userId){
		
		try {
			return userdao.getUserMail(userId);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getUserMail(email):\n" + e.toString());
		}
		return "";
	}
	
	/** 取得用户最大Id
	 * @return 最大用户Id
	 */
	public static int getUserMaxId(){
		
		try {
			return userdao.getMaxUserId();
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getMaxUserId():\n" + e.toString());
		}
		return 0;
	}
	
	/** 取得论坛所有用户总数
	 * @return 用户总数
	 */
	public static int getUsersCount(){
		
		try {
			return userdao.getUsersCount();
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getUsersCount():\n" + e.toString());
		}
		return 0;
	}
	
	/** 取得论坛最新用户(只有Id和名称)
	 * @return 最新用户
	 */
	public static UserSession getNewUser(){
		
		try {
			return userdao.getNewUser();
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getNewUser():\n" + e.toString());
		}
		return null;
	}
	
	/** 增加一个论坛用户
	 * @param user 论坛用户
	 * @return 成功:true 失败:false
	 */
	public static boolean addUser(User user){
		
		boolean flag = true;
		try {
			userdao.addUser(user);
		}
		catch(SQLException e) {
			flag = false;
			bbslog.error("Throws a SqlException when invoke addUser(user):\n" + e.toString());
		}
		return flag;
	}
	
	/** 编辑论坛用户信息
	 * @param user 论坛用户
	 * @param flag true:用户修改 false:管理员修改
	 * @return 成功:true 失败:false
	 */
	public static boolean editUser(User user,boolean flag){

		boolean rtflag = true;
		try {
			userdao.editUser(user,flag);
		}
		catch(SQLException e) {
			rtflag = false;
			bbslog.error("Throws a SqlException when invoke editUser(user):\n" + e.toString());
		}
		return rtflag;
	}
	
	/** 判断用户登录是否成功
	 * @param userMap userMap 正登录的用户名和密码
	 * @param user 登录用户相关信息
	 * @return 用户Id
	 */
	public static int userLogin(HashMap<String,String> userMap,UserLgDto user){
		int userId = 0;
		try {
			userId = userdao.userLogin(userMap);
		} catch (SQLException e) {
			bbslog.error("Throws a SqlException when invoke userLogin(userMap):\n" + e.toString());
		}
		if(userId>0){
			try {
				user.setId(userId);
				userdao.upUserLogin(user);
			} catch (SQLException e) {
				bbslog.error("Throws a SqlException when invoke upUserLogin(user):\n" + e.toString());
			}
		}
		return userId;	
	}
	/** 取系统Cookie判断用户登录是否成功
	 * @param request 一次请求
	 * @return 用户Id
	 */
	public static int userLogin(HttpServletRequest request){
		int userId = 0;
		Cookie[] cks = request.getCookies();
		if(cks==null)
			return 0;			
		String username = "";
		for(int i=0;i<cks.length;i++){
			Cookie ck = cks[i];
			if(ck.getName().equalsIgnoreCase(Constants.cookiename))
				username = ck.getValue();
		}
		if(username==null || username.length()<1){
			return 0;
		}
		
		try {
			userId = userdao.getUserId(StringUtils.decodeString(username));
			if(userId>0){ // 设置其在线,不过期经验值不变化
				userdao.upUserLogin(userId);
			}
		} catch (SQLException e) {
			bbslog.error("Throws a SqlException when invoke userLogin(request):\n" + e.toString());
		}
		
		return userId;
	}
	
	/** 判断用户退出是否成功
	 * @param userId 用户名
	 */
	public static void userLogout(int userId){

		try {
			userdao.upUserlogout(userId);
		} catch (SQLException e) {
			bbslog.error("Throws a SqlException when invoke userLogout(int userId):\n" + e.toString());
		}
		return;	
	}
	
	
	/** 发表\删除帖子\设置和取消精华贴后更新用户财富和发贴数
	 *  同时检查是否满足升级和降级条件
	 * @param userWth 用户财富信息
	 * @param typeId 判断操作类型 1:post 2:replay 3:elite 4:delete article 5:delete replay 6:celelite
	 */
	public static void postArticle(WealthDtoIm userWth,int typeId){
		
		try {
			switch (typeId) {
				case 1:userdao.postArticle(userWth,true);
					   userUpGrade(userWth.getId());
					   break;
				case 2:userdao.postArticle(userWth,false);
				       userUpGrade(userWth.getId());
					   break;
				case 3:userdao.eliteArticle(userWth,true);
					   break;
				case 4:userdao.deleteArticle(userWth,true);
					   userUpGrade(userWth.getId());
					   break;
				case 5:userdao.deleteArticle(userWth,false);
					   userUpGrade(userWth.getId());
					   break;
				case 6:userdao.eliteArticle(userWth,false);
					   break;
				default:break;
			}
			
		} catch (SQLException e) {
			bbslog.error("Throws a SqlException when invoke postArticle(WealthDtoIm userWth,int typeId):\n" + e.toString());
		}
		return;	
	}
	
	/** 取得在线用户
	 * @param userId 用户Id
	 * @return 在线用户
	 */
	public static UserSession getOnlineUser(int userId){
		
		try {
			return userdao.getOnlineUser(userId);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getOnlineUser(userId):\n" + e.toString());
		}
		return null;
	}
	
	/** 根据用户发表文章取得用户列表
	 * @param userpage 
	 * @return 用户列表
	 */
	public static List getUserByPost(HashMap userpage){
		
		try {
			return userdao.getUserByPost(userpage);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getUserByPost(userpage):\n" + e.toString());
		}
		return null;
	}
	
	/** 根据用户注册时间取得用户列表
	 * @param userpage 
	 * @return 用户列表
	 */
	public static List getUserByRegTime(HashMap userpage){
		
		try {
			return userdao.getUserByRegTime(userpage);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getUserByRegTime(userpage):\n" + e.toString());
		}
		return null;
	}
	
	/** 取得发表帖子最大的前20名用户
	 * @return 用户列表
	 */
	public static List get20PostUsers(){
		
		try {
			return userdao.get20PostUsers();
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke get20PostUsers():\n" + e.toString());
		}
		return null;
	}
	
	/** 取得最新注册20名用户
	 * @return 用户列表
	 */
	public static List get20RegUsers(){
		
		try {
			return userdao.get20RegUsers();
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke get20RegUsers():\n" + e.toString());
		}
		return null;
	}
	
	/** 判断用户是否满足升级条件,满足则升级。
	 *  在用户帖子被删时候,判断用户是否满足当前等级条件,不满足则降级。
	 * @param userId 用户ID 
	 */
	public static void userUpGrade(int userId){
		try {
			User user = userdao.getPostGrade(userId);
			int postnum = (user.getPostnum()+user.getReplaynum());
			int gid = user.getGrade();
			int nextGrade = UserLevelBiz.getGradePost(gid+1);
			int curGrade = UserLevelBiz.getGradePost(gid);
			
			if(postnum>=nextGrade){
				userdao.userUpGrade(userId);
			}else if(postnum<curGrade){
				userdao.userDownGrade(userId);
			}
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke get20RegUsers():\n" + e.toString());
		}
		return;
	}
	
	/** 以下为管理后台用到 */
	
	/** 取得用户列表
	 * @param start 查询开始位置
	 * @param end 查询长度
	 * @return 用户列表
	 */
	public static List getUsers(int start,int end){
		
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		map.put("start", start);
		map.put("end", end);
		try {
			return userdao.getUsers(map);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getUsers(int start,int end):\n" + e.toString());
		}
		return null;
	}
	/** 删除一个用户
	 * @param userId 用户Id
	 * @return 成功:ture  失败:false
	 */
	public static boolean deleteUser(int userId){
		boolean flag = true;
		try {
			flag = userdao.deleteUser(userId);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke deleteUser(int userId):\n" + e.toString());
			flag = true;
		}
		return flag;
	}
	
	/** 当多个用户被设置为版主时候,更新其类型。
	 * @param userNames 用户名称(多个用户)
	 * @param userType 用户类型
	 * @return 成功:ture  失败:false
	 */
	public static boolean updateUserType(String userNames,int userType){
		boolean flag = true;
		try {
			String[] admins = StringUtils.getAllAdmin(userNames);
			for(int i=0;i<admins.length;i++){
				if(!userdao.updateUserType(admins[i], userType))
					return false;
			}
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke updateUserType(String userNames,int userType):\n" + e.toString());
			flag = true;
		}
		return flag;
	}
}

⌨️ 快捷键说明

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