123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- package com.yihu.jk.api;
- import java.sql.SQLException;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.List;
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
- import com.common.json.JSONException;
- import com.yihu.jk.dao.BehaviorDao;
- import com.yihu.jk.utils.ApiUtil;
- import com.yihu.jk.utils.RelativeDateFormat;
- import com.yihu.jk.utils.StringUtil;
- import com.yihu.jk.vo.ArticleCollection;
- import com.yihu.jk.vo.ArticleComment;
- import com.yihu.jk.vo.ArticleStatistic;
- import com.yihu.jk.vo.Behavior;
- import com.yihu.wsgw.api.InterfaceMessage;
- public class BehaviorApi {
- public BehaviorDao subDao = new BehaviorDao();
- /**
- * 添加 浏览 转发 分享 点赞
- */
- public String saveBehavior(InterfaceMessage im) {
- JSONObject json = JSONObject.fromObject(im.getParam());
- String articleId = StringUtil.isEmpty(json.get("articleId")) ? null : json.getString("articleId");
- String cName = StringUtil.isEmpty(json.get("cName")) ? null : json.getString("cName");
- String userId = StringUtil.isEmpty(json.get("userId")) ? null : json.getString("userId");
- // 1、点赞 2、转发3、分享 4、浏览
- Integer behaviorAction = StringUtil.isEmpty(json.get("behaviorAction")) ? null : json.getInt("behaviorAction");
- if (StringUtil.isEmpty(articleId) || StringUtil.isEmpty(behaviorAction) || StringUtil.isEmpty(cName)
- || StringUtil.isEmpty(userId)) {
- return ApiUtil.getRespJSON(-10000, "ArticleId,BehaviorAction,CName,UserId 为必填项,入参为:" + json.toString())
- .toString();
- }
- Behavior vo = new Behavior();
- vo.setArticleId(articleId);
- vo.setBehaviorAction(behaviorAction);
- vo.setcName(cName);
- vo.setUserId(userId);
- ArticleStatistic arvo = new ArticleStatistic();
- arvo.setArticleId(articleId);
- try {
- subDao.saveBehavior(vo);
- int number = 1;
- ArticleApi.updateArticleStatistic(articleId, behaviorAction, number);
- return ApiUtil.getRespJSON(10000, "成功").toString();
- } catch (SQLException e) {
- e.printStackTrace();
- return ApiUtil.getRespJSON(-14444, e.getMessage()).toString();
- }
- }
- /**
- * 医生居民收藏
- */
- public String saveArticleCollection(InterfaceMessage im) {
- JSONObject json = JSONObject.fromObject(im.getParam());
- String articleId = StringUtil.isEmpty(json.get("articleId")) ? null : json.getString("articleId");
- String cName = StringUtil.isEmpty(json.get("cName")) ? null : json.getString("cName");
- String userId = StringUtil.isEmpty(json.get("userId")) ? null : json.getString("userId");
- int userType = StringUtil.isEmpty(json.get("userType")) ? 2 : json.getInt("userType");// 1医生
- String orgName = StringUtil.isEmpty(json.get("orgName")) ? null : json.getString("orgName");
- String orgId = StringUtil.isEmpty(json.get("orgId")) ? null : json.getString("orgId");
- String articleCategoryId = StringUtil.isEmpty(json.get("articleCategoryId")) ? null : json
- .getString("articleCategoryId");
- String articleCategoryName = StringUtil.isEmpty(json.get("articleCategoryName")) ? null : json
- .getString("articleCategoryName");
- if (StringUtil.isEmpty(articleId) || StringUtil.isEmpty(userId) || StringUtil.isEmpty(articleCategoryId)
- || StringUtil.isEmpty(userType)) {
- return ApiUtil.getRespJSON(-10000, "ArticleId,userType,CName,UserId 为必填项,入参为:" + json.toString())
- .toString();
- }
- ArticleCollection vo = new ArticleCollection();
- vo.setArticleId(articleId);
- vo.setArticleCategoryId(articleCategoryId);
- vo.setArticleCategoryName(articleCategoryName);
- vo.setOrgId(orgId);
- vo.setOrgName(orgName);
- vo.setUserType(userType);
- vo.setcName(cName);
- vo.setUserId(userId);
- ArticleStatistic arvo = new ArticleStatistic();
- arvo.setArticleId(articleId);
- try {
- subDao.saveArticleCollection(vo);
- // if (userType == 2) {
- int behaviorAction = 6;
- int number = 1;
- ArticleApi.updateArticleStatistic(articleId, behaviorAction, number);
- // }
- Behavior behavior = new Behavior();
- behavior.setArticleId(articleId);
- behavior.setBehaviorAction(behaviorAction);
- behavior.setcName(cName);
- behavior.setUserId(userId);
- // ArticleStatistic arvo = new ArticleStatistic();
- arvo.setArticleId(articleId);
- subDao.saveBehavior(behavior);
- return ApiUtil.getRespJSON(10000, "成功").toString();
- } catch (SQLException e) {
- e.printStackTrace();
- return ApiUtil.getRespJSON(-14444, e.getMessage()).toString();
- }
- }
- /**
- * 取消收藏
- *
- * @param im
- * @return
- */
- public String cancelArticleCollection(InterfaceMessage im) {
- JSONObject json = JSONObject.fromObject(im.getParam());
- String articleId = StringUtil.isEmpty(json.get("articleId")) ? null : json.getString("articleId");
- String userId = StringUtil.isEmpty(json.get("userId")) ? null : json.getString("userId");
- int userType = StringUtil.isEmpty(json.get("userType")) ? 2 : json.getInt("userType");// 1医生
- if (StringUtil.isEmpty(articleId) || StringUtil.isEmpty(userId) || StringUtil.isEmpty(userType)) {
- return ApiUtil.getRespJSON(-10000, "ArticleId,userType,CName,UserId 为必填项,入参为:" + json.toString())
- .toString();
- }
- ArticleCollection vo = new ArticleCollection();
- vo.setArticleId(articleId);
- vo.setUserType(userType);
- vo.setUserId(userId);
- ArticleStatistic arvo = new ArticleStatistic();
- arvo.setArticleId(articleId);
- try {
- int count = subDao.deleteArticleCollection(vo);
- // if (userType == 2) {
- int behaviorAction = 6;
- int number = -1;
- ArticleApi.updateArticleStatistic(articleId, behaviorAction, number);
- // }
- if (count == 0) {
- return ApiUtil.getRespJSON(-10001, "收藏失败").toString();
- }
- return ApiUtil.getRespJSON(10000, "成功").toString();
- } catch (SQLException e) {
- e.printStackTrace();
- return ApiUtil.getRespJSON(-14444, e.getMessage()).toString();
- }
- }
- /**
- * 添加评论
- *
- * @param im
- * @return
- */
- public String saveArticleComment(InterfaceMessage im) {
- JSONObject json = JSONObject.fromObject(im.getParam());
- String articleId = StringUtil.isEmpty(json.get("articleId")) ? null : json.getString("articleId");
- String commentContent = StringUtil.isEmpty(json.get("commentContent")) ? null : json
- .getString("commentContent");
- String userImg = StringUtil.isEmpty(json.get("userImg")) ? null : json.getString("userImg");
- String cName = StringUtil.isEmpty(json.get("cName")) ? null : json.getString("cName");
- String userId = StringUtil.isEmpty(json.get("userId")) ? null : json.getString("userId");
- if (StringUtil.isEmpty(articleId) || StringUtil.isEmpty(commentContent) || StringUtil.isEmpty(cName)
- || StringUtil.isEmpty(userId)) {
- return ApiUtil.getRespJSON(-10000, "ArticleId,commentContent,CName,UserId 为必填项,入参为:" + json.toString())
- .toString();
- }
- ArticleComment vo = new ArticleComment();
- vo.setCommentId(StringUtil.createUUID());
- vo.setArticleId(articleId);
- vo.setcName(cName);
- vo.setCommentContent(commentContent);
- vo.setUserId(userId);
- vo.setUserImg(userImg);
- ArticleStatistic arvo = new ArticleStatistic();
- arvo.setArticleId(articleId);
- try {
- subDao.saveArticleComment(vo);
- int behaviorAction = 5;
- int number = 1;
- ArticleApi.updateArticleStatistic(articleId, behaviorAction, number);
- return ApiUtil.getRespJSON(10000, "成功").toString();
- } catch (SQLException e) {
- e.printStackTrace();
- return ApiUtil.getRespJSON(-14444, e.getMessage()).toString();
- }
- }
- /**
- * 获取评论列表
- *
- * @param im
- * @return
- * @throws JSONException
- */
- public String getArticleCommentList(InterfaceMessage im) throws JSONException {
- JSONObject json = JSONObject.fromObject(im.getParam());
- String articleId = StringUtil.isEmpty(json.get("articleId")) ? null : json.getString("articleId");
- Integer pageIndex = StringUtil.isEmpty(json.get("pageIndex")) ? 0 : json.getInt("pageIndex");
- Integer pageSize = StringUtil.isEmpty(json.get("pageSize")) ? 5 : json.getInt("pageSize");
- if (StringUtil.isEmpty(articleId)) {
- return ApiUtil.getRespJSON(-10000, "ArticleId 为必填项,入参为:" + json.toString()).toString();
- }
- ArticleComment vo = new ArticleComment();
- vo.setArticleId(articleId);
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:m:s");
- JSONObject rv = ApiUtil.getRespJSON(10000, "成功");
- JSONArray array = new JSONArray();
- Date date;
- try {
- List<ArticleComment> result = subDao.getArticleCommentList(vo, pageIndex, pageSize);
- if (result.size() > 0) {
- for (ArticleComment vo1 : result) {
- JSONObject jsonObj = new JSONObject();
- jsonObj.put("articleId", StringUtil.getJSONValue(vo1.getArticleId()));
- jsonObj.put("commentContent", StringUtil.getJSONValue(vo1.getCommentContent()));
- jsonObj.put("commentId", StringUtil.getJSONValue(vo1.getCommentId()));
- jsonObj.put("cName", StringUtil.getJSONValue(vo1.getcName()));
- jsonObj.put("userImg", StringUtil.getJSONValue(vo1.getUserImg()));
- jsonObj.put("dateTime", StringUtil.getJSONValue(vo1.getcName()));
- try {
- date = format.parse(vo1.getInsertTime());
- jsonObj.put("dateTime", RelativeDateFormat.format(date));
- array.add(jsonObj);
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return ApiUtil.getRespJSON(-10000, "获取时间数据:" + e.getMessage()).toString();
- }
- }
- }
- int count = subDao.getArticleCommentCount(vo);
- rv.put("Result", array);
- rv.put("Count", count);
- return rv.toString();
- } catch (SQLException e) {
- e.printStackTrace();
- return ApiUtil.getRespJSON(-10000, "获取数据异常:" + e.getMessage()).toString();
- }
- }
- /**
- * 取消点赞
- *
- * @param im
- * @return
- */
- public String cancelBehavior(InterfaceMessage im) {
- JSONObject json = JSONObject.fromObject(im.getParam());
- String articleId = StringUtil.isEmpty(json.get("articleId")) ? null : json.getString("articleId");
- String userId = StringUtil.isEmpty(json.get("userId")) ? null : json.getString("userId");
- if (StringUtil.isEmpty(articleId) || StringUtil.isEmpty(userId)) {
- return ApiUtil.getRespJSON(-10000, "ArticleId,UserId 为必填项,入参为:" + json.toString()).toString();
- }
- Behavior vo = new Behavior();
- vo.setArticleId(articleId);
- vo.setUserId(userId);
- ArticleStatistic arvo = new ArticleStatistic();
- arvo.setArticleId(articleId);
- try {
- subDao.deleteBehavior(vo);
- return ApiUtil.getRespJSON(10000, "成功").toString();
- } catch (SQLException e) {
- e.printStackTrace();
- return ApiUtil.getRespJSON(-14444, e.getMessage()).toString();
- }
- }
- }
|