package com.yhbbs.article.biz;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import org.apache.log4j.Logger;
import com.yhbbs.article.bean.ArticleDtoIm;
import com.yhbbs.article.bean.ArticleIm;
import com.yhbbs.article.bean.ContentIm;
import com.yhbbs.article.dao.ArticleDaoIm;
import com.yhbbs.article.itface.Article;
import com.yhbbs.article.itface.ArticleDao;
import com.yhbbs.article.itface.ArticleDto;
import com.yhbbs.article.itface.Content;
import com.yhbbs.article.search.biz.BbsWriterIm;
import com.yhbbs.bbs.bean.WealthDtoIm;
import com.yhbbs.bbs.biz.BbsPropBiz;
import com.yhbbs.bbs.biz.BbsStatBiz;
import com.yhbbs.bbs.biz.WealthBiz;
import com.yhbbs.bbs.itface.Parameter;
import com.yhbbs.forum.bean.ForumArtPostIm;
import com.yhbbs.forum.biz.ForumBiz;
import com.yhbbs.forum.itface.ForumArtPost;
import com.yhbbs.user.biz.UserBiz;
import com.yhbbs.utils.Constants;
import com.yhbbs.utils.DateUtils;
import com.yhbbs.vote.biz.VoteBiz;
/**
* <p>Title:论坛帖子相关操作</p>
* <li> 论坛帖子的相关操作
* <p>Company: www.yyhweb.com</p>
* <br><b>CopyRight: yyhweb[由由华网]</b>
* @author stephen
* @version YHBBS-2.0
*/
public class ArticleBiz {
private static Logger bbslog = Logger.getLogger(ArticleBiz.class);
private static ArticleDao artdao = ArticleDaoIm.getInstance();
/** 取得帖子标题(用于导航)
* @param artId 帖子Id
* @return 帖子标题
*/
public static String getArtTitle(int artId){
try {
return artdao.getArtTitle(artId);
}
catch(SQLException e) {
e.toString();
}
return "";
}
/** 取得帖子发表时间(用于判断用户是否可再编辑帖子)
* @param artId 帖子Id
* @return true:可以 false:不可以
*/
public static boolean isEditAgain(int artId){
// 系统设置的编辑帖子过期时间
Parameter parameter = BbsPropBiz.getParameter();
if(parameter.getArtEdit()==0)
return false;
int hours = parameter.getArtHour();
if(hours==0)
return true;
long pt = 0;
long ht = hours*3600000l;
long ct = System.currentTimeMillis();
try {
pt = artdao.getArtPostTime(artId).getTime();
return (ct-pt-ht)<0;
}catch(SQLException e) {
bbslog.error("Throws a SqlException when invoke isEditAgain(int artId):\n" + e.toString());
}
return false;
}
/** 取得某回复帖子的主题帖子Id
* @param artId 帖子Id
* @return 发表帖子用户Id
*/
public static int getReplayParentId(int artId){
try {
return artdao.getReplayParentId(artId);
}
catch(SQLException e) {
bbslog.error("Throws a SqlException when invoke getReplayParentId(int artId):\n" + e.toString());
}
return 0;
}
/** 取得发表帖子用户Id
* @param artId 帖子Id
* @return 发表帖子用户Id
*/
public static int getArtUserId(int artId){
try {
return artdao.getArtUserId(artId);
}
catch(SQLException e) {
bbslog.error("Throws a SqlException when invoke getArtUserId(int artId):\n" + e.toString());
}
return 0;
}
/** 取得帖子最大Id
* @return 帖子最大Id
*/
public static int getArtMaxId(){
try {
return (artdao.getMaxArtId()+1);
}
catch(SQLException e) {
bbslog.error("Throws a SqlException when invoke getMaxArtId():\n" + e.toString());
}
return 1;
}
/** 根据帖子Id取得帖子Dto(无内容)
* @param artId 帖子Id
* @return 一个帖子
*/
public static ArticleDto getArticle(int artId){
ArticleDto article = null;
try {
article = artdao.getArticle(artId);
}
catch(SQLException e) {
bbslog.error("Throws a SqlException when invoke getArticle(artId):\n" + e.toString());
}
if(article==null)
article = new ArticleDtoIm();
return article;
}
/** 发表文章,根据parent判断是主题贴还是回帖
* <li>若是回复贴更新主题贴信息
* @param article 帖子信息
* @param content 帖子内容
* @param forum 需要更新的论坛信息
* @return 一个帖子
*/
public static boolean postArticle(Article article,Content content,ForumArtPost forum){
boolean flag = false;
WealthDtoIm wealth = null;
HashMap<String, Comparable> userMap = null;
int artId = article.getId();
int userId = article.getUserid();
int parent = article.getParentid();
try {
flag = artdao.addArticle(article); // 添加帖子信息
if(flag){
flag = artdao.addContent(content); // 添加帖子内容
}
if(flag){ // 更新帖子\论坛\系统\用户相关信息
if(parent==0){ // 主题贴
wealth = (WealthDtoIm) WealthBiz.getPostArtWth();
wealth.setId(userId);
UserBiz.postArticle(wealth,1); // 更新用户财富和帖子数目
ForumBiz.forumPostArt(forum,true); // 更新论坛信息
BbsStatBiz.upArtCount(true); // 更新系统统计信息
}else{ // 回复贴
wealth = (WealthDtoIm) WealthBiz.getRepArtWth();
wealth.setId(userId);
UserBiz.postArticle(wealth,2); // 更新用户财富和帖子数目
userMap = new HashMap<String, Comparable>();
userMap.put("artId",parent);
userMap.put("userId",userId);
userMap.put("userName",article.getUser());
artdao.replayArt(userMap); // 更新主题帖子信息
ForumBiz.forumPostArt(forum,false); // 更新论坛信息
BbsStatBiz.upArtCount(false); // 更新系统统计信息
}
article.setContent(content); // 该帖子建立索引
BbsWriterIm.indexArticle(article);
}else{
artdao.delArticle(artId,false);
bbslog.error("Post an article unsuccessfully!");
}
}catch(SQLException e) {
bbslog.error("Throws a SqlException when invoke postArticle(article,content,forum):\n" + e.toString());
}
return flag;
}
/** 更新文章,如果该文章是论坛的最新文章则需要更新其标题.
* @param article 帖子信息
* @param content 帖子内容
* @return true:更新成功 false:更新失败
*/
public static boolean updateArticle(Article article,Content content){
boolean flag = true;
int artId = article.getId();
int forumId = article.getForum();
String title = article.getTitle();
try {
flag = artdao.editArticle(article);
if(flag){
if(artId==ForumBiz.getForumLArtId(forumId))
ForumBiz.upForumLArtTitle(forumId,title);
flag = artdao.editContent(content);
article.setContent(content); // 更新该帖子索引
BbsWriterIm.updateIndex(article);
}else{
bbslog.error("Edit artilce unsuccessfully!The article id is:"+artId);
}
}catch(SQLException e) {
bbslog.error("Throws a SqlException when invoke updateArticle(Article article,Content content):\n" + e.toString());
}
return flag;
}
/** 删除帖子,论坛统计和系统统计都要更新
* @param artId 帖子Id
* @param userId 帖子作者Id
* @param forumId 帖子所在论坛Id
* @param flag true:主题帖 false:回复贴
* @return true:删除成功 false:删除失败
*/
public static boolean deleteArticle(int artId,int userId,int forumId,boolean flag){
String isVote = "";
boolean del = false;
boolean isToday = isTodayArt(artId);/** 判断是否为今日帖子 */
WealthDtoIm wealth = null;
try {
isVote =getArticle(artId).getIsvote();
del = artdao.delArticle(artId,flag);
if(del){
if(flag){
wealth = (WealthDtoIm) WealthBiz.getDelArtWth();
wealth.setId(userId);
UserBiz.postArticle(wealth,4);
}else{
wealth = (WealthDtoIm) WealthBiz.getDelArtWth();
wealth.setId(userId);
UserBiz.postArticle(wealth,5);
}
BbsStatBiz.deleteArticle(flag);
if(!isVote.equals("0")){ // 删除投票贴还需要删除其投票用户等信息
VoteBiz.deleteVote(artId);