|
package com.ekexiu.portal.leavemsg;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.jfw.apt.annotation.Autowrie;
import org.jfw.apt.annotation.DefaultValue;
import org.jfw.apt.web.annotation.Path;
import org.jfw.apt.web.annotation.operate.Get;
import org.jfw.apt.web.annotation.operate.Post;
import org.jfw.apt.web.annotation.param.AfterCommit;
import org.jfw.apt.web.annotation.param.FieldParam;
import org.jfw.apt.web.annotation.param.JdbcConn;
import org.jfw.apt.web.annotation.param.RequestParam;
import org.jfw.util.DateUtil;
import org.jfw.util.StringUtil;
import org.jfw.util.exception.JfwBaseException;
import org.jfw.util.jdbc.JdbcUtil;
import org.jfw.util.jdbc.PreparedStatementConfig;
import com.ekexiu.portal.dao.ArticleDao;
import com.ekexiu.portal.dao.PaperAuthorDao;
import com.ekexiu.portal.dao.PatentAuthorDao;
import com.ekexiu.portal.dao.PpaperDao;
import com.ekexiu.portal.dao.PpatentDao;
import com.ekexiu.portal.notify.NotifyService;
import com.ekexiu.portal.notify.NotifyType;
import com.ekexiu.portal.po.Article;
import com.ekexiu.portal.po.PaperAuthor;
import com.ekexiu.portal.po.PatentAuthor;
import com.ekexiu.portal.po.Ppaper;
import com.ekexiu.portal.po.Ppatent;
import com.ekexiu.portal.question.Answer;
import com.ekexiu.portal.question.Question;
import com.ekexiu.portal.question.QuestionDao;
@Path("/leavemsg")
public class Service {
@Autowrie
private LeaveMsgDao leaveMsgDao;
@Autowrie
private ArticleDao articleDao;
@Autowrie
private PpatentDao ppatentDao;
@Autowrie
private PpaperDao ppaperDao;
@Autowrie
private QuestionDao questionDao;
@Autowrie
private NotifyService notifyService;
@Autowrie
private PaperAuthorDao paperAuthorDao;
@Autowrie
private PatentAuthorDao patentAuthorDao;
public PaperAuthorDao getPaperAuthorDao() {
return paperAuthorDao;
}
public void setPaperAuthorDao(PaperAuthorDao paperAuthorDao) {
this.paperAuthorDao = paperAuthorDao;
}
public PatentAuthorDao getPatentAuthorDao() {
return patentAuthorDao;
}
public void setPatentAuthorDao(PatentAuthorDao patentAuthorDao) {
this.patentAuthorDao = patentAuthorDao;
}
public ArticleDao getArticleDao() {
return articleDao;
}
public void setArticleDao(ArticleDao articleDao) {
this.articleDao = articleDao;
}
public PpatentDao getPpatentDao() {
return ppatentDao;
}
public void setPpatentDao(PpatentDao ppatentDao) {
this.ppatentDao = ppatentDao;
}
public PpaperDao getPpaperDao() {
return ppaperDao;
}
public void setPpaperDao(PpaperDao ppaperDao) {
this.ppaperDao = ppaperDao;
}
public QuestionDao getQuestionDao() {
return questionDao;
}
public void setQuestionDao(QuestionDao questionDao) {
this.questionDao = questionDao;
}
public NotifyService getNotifyService() {
return notifyService;
}
public void setNotifyService(NotifyService notifyService) {
this.notifyService = notifyService;
}
public LeaveMsgDao getLeaveMsgDao() {
return leaveMsgDao;
}
public void setLeaveMsgDao(LeaveMsgDao leaveMsgDao) {
this.leaveMsgDao = leaveMsgDao;
}
// 1:文章 2:论文 3:专利 4:回答
public void notify(Connection con, String type, String id, String reciver, String uid, String uname, String lid, boolean agree, List<Runnable> runs)
throws SQLException, JfwBaseException {
if (type.equals("1")) {
Article article = this.articleDao.queryOne(con, id);
if (article == null) {
throw new JfwBaseException(50000, "article[" + id + "] not found");
}
if (article.getArticleType().equals("1")) {
if (reciver == null) {
if (!uid.equals(article.getProfessorId())) {
this.notifyService.notify(con, article.getProfessorId(), uid, uname, lid, article.getArticleTitle(), NotifyType.LEAVE_MSG_AT_ARTICLE,
runs);
}
} else {
if (!uid.equals(reciver)) {
this.notifyService.notify(con, reciver, uid, uname, lid, article.getArticleTitle(),
agree ? NotifyType.AGREE_LEAVE_MSG_AT_ARTICLE : NotifyType.REPLY_LEAVE_MSG_AT_ARTICLE, runs);
}
}
}
} else if (type.equals("2")) {
Ppaper paper = this.ppaperDao.query(con, id);
if (paper == null) {
throw new JfwBaseException(50000, "paper[" + id + "] not found");
}
if (reciver == null) {
for (PaperAuthor pa : this.paperAuthorDao.query(con, id)) {
if (!pa.getProfessorId().startsWith("#") && (!uid.equals(pa.getProfessorId()))) {
this.notifyService.notify(con, pa.getProfessorId(), uid, uname, lid, paper.getName(), NotifyType.LEAVE_MSG_AT_PAPER, runs);
}
}
} else {
if (!uid.equals(reciver)) {
this.notifyService.notify(con, reciver, uid, uname, lid, paper.getName(),
agree ? NotifyType.AGREE_LEAVE_MSG_AT_PAPER : NotifyType.REPLY_LEAVE_MSG_AT_PAPER, runs);
}
}
} else if (type.equals("3")) {
Ppatent patent = this.ppatentDao.query(con, id);
if (patent == null) {
throw new JfwBaseException(50000, "patent[" + id + "] not found");
}
if (reciver == null) {
for (PatentAuthor pa : this.patentAuthorDao.query(con, id)) {
if (!pa.getProfessorId().startsWith("#") && (!uid.equals(pa.getProfessorId()))) {
this.notifyService.notify(con, pa.getProfessorId(), uid, uname, lid, patent.getName(), NotifyType.LEAVE_MSG_AT_PARENT, runs);
}
}
} else {
if (!uid.equals(reciver)) {
this.notifyService.notify(con, reciver, uid, uname, lid, patent.getName(),
agree ? NotifyType.AGREE_LEAVE_MSG_AT_PARENT : NotifyType.REPLY_LEAVE_MSG_AT_PARENT, runs);
}
}
} else if (type.equals("4")) {
Answer a = this.questionDao.queryAnswer(con, id);
if (a == null) {
throw new JfwBaseException(50000, "answer[" + id + "] not found");
}
Question q = this.questionDao.query(con, a.getQid());
if (q == null) {
throw new JfwBaseException(50000, "answer[" + id + "]'question not found");
}
if (reciver == null) {
if (!uid.equals(a.getUid())) {
this.notifyService.notify(con, a.getUid(), uid, uname, lid, q.getTitle(), NotifyType.LEAVE_MSG_AT_ANSWER, runs);
}
} else {
if (!uid.equals(reciver)) {
this.notifyService.notify(con, reciver, uid, uname, lid, q.getTitle(),
agree ? NotifyType.AGREE_LEAVE_MSG_AT_ANSWER : NotifyType.REPLY_LEAVE_MSG_AT_ANSWER, runs);
}
}
} else {
throw new IllegalArgumentException();
}
}
/**
* 新增一顶级留言
*
* @param con
* @param msg
* cnt
* @param title
* 文章标题 论文标题 专利标题 提问
* @param uname
* 发送人名称
* @param runs
* @return
* @throws SQLException
* @throws JfwBaseException
*/
@Path()
@Post
public String save(@JdbcConn(true) Connection con,
@RequestParam(fields = { @FieldParam(value = "cnt", valueClass = String.class), @FieldParam(value = "refId", valueClass = String.class),
@FieldParam(value = "refType", valueClass = String.class), @FieldParam(value = "sender", valueClass = String.class) }) LeaveMsg msg,
String uname, @AfterCommit List<Runnable> runs) throws SQLException, JfwBaseException {
String id = StringUtil.buildUUID();
String time = DateUtil.formatDateTime(System.currentTimeMillis());
msg.setId(id);
msg.setTop(id);
msg.setState("1");
msg.setParent(null);
msg.setAgreeCount(0);
msg.setCreateTime(time);
msg.setTimeDesc(time + id);
leaveMsgDao.insert(con, msg);
this.notify(con, msg.getRefType(), msg.getRefId(), null, msg.getSender(), uname, id, false, runs);
return id;
}
/**
* 回复留言
*
* @param con
* @param cnt
* 留言内容
* @param id
* 被回复留言ID
* @param uid
* 用户ID
* @param title
* 留言主体 文章标题 论文标题 专利标题 提问
* @param uname
* 用户姓名
* @param runs
* @return
* @throws SQLException
* @throws JfwBaseException
*/
@Path("/reply")
@Post
public String reply(@JdbcConn(true) Connection con, String cnt, String id, String uid, String uname, @AfterCommit List<Runnable> runs)
throws SQLException, JfwBaseException {
final LeaveMsg msg = leaveMsgDao.query(con, id);
if (msg == null)
throw new JfwBaseException(50000, "leave message[id:" + id + "] not found");
if (msg.getSender().equals(uid)) {
throw new JfwBaseException(50001, "can't reply self");
}
String nid = StringUtil.buildUUID();
msg.setParent(id);
msg.setReciver(msg.getSender());
msg.setSender(uid);
msg.setState("1");
msg.setCnt(cnt);
msg.setId(nid);
msg.setAgreeCount(0);
String time = DateUtil.formatDateTime(System.currentTimeMillis());
msg.setCreateTime(time);
msg.setTimeDesc(time + nid);
leaveMsgDao.insert(con, msg);
this.notify(con, msg.getRefType(), msg.getRefId(), msg.getReciver(), uid, uname, id, false, runs);
return nid;
}
/**
* 查询某主体下的留言
*
* @param con
* @param stype
* 主体类型 1:文章 2:论文 3:专利 4:提问
* @param sid
* 留言主体ID , 文章ID,论文ID,专利ID,提问ID
* @param time
* 留言时间 time > createTime
* @param id
* 留言ID time > createTime && id >leaveMsg.id
* @param rows
* 返回的最大条数
* @return
* @throws SQLException
*/
@Get
@Path("/subject")
public List<LeaveMsg> query(@JdbcConn Connection con, String stype, String sid, @DefaultValue("\"0\"") String time, @DefaultValue("\"0\"") String id,
int rows) throws SQLException {
return leaveMsgDao.query(con, sid, stype, time + id, rows);
}
@Get
@Path("/count")
public int query(@JdbcConn Connection con, String stype, String sid) throws SQLException {
return leaveMsgDao.queryCount(con, stype, sid);
}
@Get
@Path("/counts")
public List<Map<String, Object>> query(@JdbcConn Connection con, final String stype, final String[] ids) throws SQLException {
StringBuilder sb = new StringBuilder();
sb.append("SELECT REF_ID rid,COUNT(1) num FROM LEAVE_MSG WHERE STATE='1' AND REF_TYPE =? AND REF_ID IN (?");
for (int i = 1; i < ids.length; ++i) {
sb.append(",?");
}
sb.append(") GROUP BY rid");
return JdbcUtil.queryMaps(con, sb.toString(), new PreparedStatementConfig() {
@Override
public void config(PreparedStatement ps) throws SQLException {
int idx = 1;
ps.setString(idx++, stype);
for (int i = 0; i < ids.length; ++i) {
ps.setString(idx++, ids[i]);
}
}
});
}
/**
* 删除留言
*
* @param con
* @param id
* 留言ID
* @return
* @throws SQLException
*/
@Get
@Path("/del")
public int delete(@JdbcConn(true) Connection con, String id) throws SQLException {
return leaveMsgDao.logicDelete(con, id);
}
/**
* 点赞留言
*
* @param con
* @param id
* 留言ID
* @param uid
* 点赞用户ID * @param uname 点赞用户姓名
* @param runs
* @throws SQLException
* @throws JfwBaseException
*/
@Post
@Path("/agree")
public void agree(@JdbcConn(true) Connection con, String id, String uid, String uname, @AfterCommit List<Runnable> runs)
throws SQLException, JfwBaseException {
LeaveMsg msg = leaveMsgDao.query(con, id);
if (msg == null) {
throw new JfwBaseException(50000, "leave message[id:" + id + "] not found");
}
LeaveMsgAgreeRec lmar = new LeaveMsgAgreeRec();
lmar.setId(id);
lmar.setUid(uid);
try {
leaveMsgDao.insert(con, lmar);
} catch (SQLException e) {
if ("23505".equals(e.getSQLState())) {
con.rollback();
throw new JfwBaseException(50001, "leave message duplicate agree");
}
throw e;
}
if (leaveMsgDao.incAgree(con, id) > 0) {
this.notify(con, msg.getRefType(), msg.getRefId(), msg.getSender(), uid, uname, msg.getId(), true, runs);
}
}
/**
* 是否对指定留言点过赞
*
* @param con
* @param id
* 留言 ID
* @param uid
* 用户ID
* @return
* @throws SQLException
*/
@Get
@Path("/agree")
public boolean agree(@JdbcConn Connection con, String id, String uid) throws SQLException {
return null != leaveMsgDao.queryAgreeRec(con, id, uid);
}
/**
* 查贸询一个指定的留言
*
* @param con
* @param id
* 留言ID
* @return
* @throws SQLException
*/
@Get
@Path("/qo")
public LeaveMsg query(@JdbcConn Connection con, String id) throws SQLException {
return leaveMsgDao.query(con, id);
}
// @Get
// @Path("/parent")
// public List<LeaveMsg> parent(@JdbcConn Connection con, String parent,
// @DefaultValue("\"0\"") String time, @DefaultValue("\"0\"") String id,
// @DefaultValue("10") int rows) throws SQLException {
// return leaveMsgDao.queryWithParent(con, parent, time, id, rows);
// }
//
// @Get
// @Path("/top")
// public List<LeaveMsg> top(@JdbcConn Connection con, String top, String
// sid, String rid, @DefaultValue("\"0\"") String time,
// @DefaultValue("\"0\"") String id,
// @DefaultValue("10") int rows) throws SQLException {
// return leaveMsgDao.queryWithTop(con, top, sid, rid, time, id, rows);
// }
/**
* 留言对话框
*
* @param con
* @param id
* 留言ID
* @return
* @throws SQLException
* @throws JfwBaseException
*/
@Get
@Path("/dialog")
public List<LeaveMsg> dialog(@JdbcConn Connection con, String id) throws SQLException, JfwBaseException {
List<LeaveMsg> ret = null;
LeaveMsg msg = leaveMsgDao.query(con, id);
if (msg == null)
throw new JfwBaseException(50000, "leave message[id:" + id + "] not found");
if (msg.getId().equals(msg.getTop())) {
ret = leaveMsgDao.queryWithParent(con, msg.getId(), "0", Integer.MAX_VALUE);
ret.add(0, msg);
} else {
ret = leaveMsgDao.queryWithTop(con, msg.getTop(), msg.getSender(), msg.getReciver(), "0", Integer.MAX_VALUE);
}
return ret;
}
}
|