BehaviorApi.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package com.yihu.jk.api;
  2. import java.sql.SQLException;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.List;
  7. import net.sf.json.JSONArray;
  8. import net.sf.json.JSONObject;
  9. import com.common.json.JSONException;
  10. import com.yihu.jk.dao.BehaviorDao;
  11. import com.yihu.jk.utils.ApiUtil;
  12. import com.yihu.jk.utils.RelativeDateFormat;
  13. import com.yihu.jk.utils.StringUtil;
  14. import com.yihu.jk.vo.ArticleCollection;
  15. import com.yihu.jk.vo.ArticleComment;
  16. import com.yihu.jk.vo.ArticleStatistic;
  17. import com.yihu.jk.vo.Behavior;
  18. import com.yihu.wsgw.api.InterfaceMessage;
  19. public class BehaviorApi {
  20. public BehaviorDao subDao = new BehaviorDao();
  21. /**
  22. * 添加 浏览 转发 分享 点赞
  23. */
  24. public String saveBehavior(InterfaceMessage im) {
  25. JSONObject json = JSONObject.fromObject(im.getParam());
  26. String articleId = StringUtil.isEmpty(json.get("articleId")) ? null : json.getString("articleId");
  27. String cName = StringUtil.isEmpty(json.get("cName")) ? null : json.getString("cName");
  28. String userId = StringUtil.isEmpty(json.get("userId")) ? null : json.getString("userId");
  29. // 1、点赞 2、转发3、分享 4、浏览
  30. Integer behaviorAction = StringUtil.isEmpty(json.get("behaviorAction")) ? null : json.getInt("behaviorAction");
  31. if (StringUtil.isEmpty(articleId) || StringUtil.isEmpty(behaviorAction) || StringUtil.isEmpty(cName)
  32. || StringUtil.isEmpty(userId)) {
  33. return ApiUtil.getRespJSON(-10000, "ArticleId,BehaviorAction,CName,UserId 为必填项,入参为:" + json.toString())
  34. .toString();
  35. }
  36. Behavior vo = new Behavior();
  37. vo.setArticleId(articleId);
  38. vo.setBehaviorAction(behaviorAction);
  39. vo.setcName(cName);
  40. vo.setUserId(userId);
  41. ArticleStatistic arvo = new ArticleStatistic();
  42. arvo.setArticleId(articleId);
  43. try {
  44. subDao.saveBehavior(vo);
  45. int number = 1;
  46. ArticleApi.updateArticleStatistic(articleId, behaviorAction, number);
  47. return ApiUtil.getRespJSON(10000, "成功").toString();
  48. } catch (SQLException e) {
  49. e.printStackTrace();
  50. return ApiUtil.getRespJSON(-14444, e.getMessage()).toString();
  51. }
  52. }
  53. /**
  54. * 医生居民收藏
  55. */
  56. public String saveArticleCollection(InterfaceMessage im) {
  57. JSONObject json = JSONObject.fromObject(im.getParam());
  58. String articleId = StringUtil.isEmpty(json.get("articleId")) ? null : json.getString("articleId");
  59. String cName = StringUtil.isEmpty(json.get("cName")) ? null : json.getString("cName");
  60. String userId = StringUtil.isEmpty(json.get("userId")) ? null : json.getString("userId");
  61. int userType = StringUtil.isEmpty(json.get("userType")) ? 2 : json.getInt("userType");// 1医生
  62. String orgName = StringUtil.isEmpty(json.get("orgName")) ? null : json.getString("orgName");
  63. String orgId = StringUtil.isEmpty(json.get("orgId")) ? null : json.getString("orgId");
  64. String articleCategoryId = StringUtil.isEmpty(json.get("articleCategoryId")) ? null : json
  65. .getString("articleCategoryId");
  66. String articleCategoryName = StringUtil.isEmpty(json.get("articleCategoryName")) ? null : json
  67. .getString("articleCategoryName");
  68. if (StringUtil.isEmpty(articleId) || StringUtil.isEmpty(userId) || StringUtil.isEmpty(articleCategoryId)
  69. || StringUtil.isEmpty(userType)) {
  70. return ApiUtil.getRespJSON(-10000, "ArticleId,userType,CName,UserId 为必填项,入参为:" + json.toString())
  71. .toString();
  72. }
  73. ArticleCollection vo = new ArticleCollection();
  74. vo.setArticleId(articleId);
  75. vo.setArticleCategoryId(articleCategoryId);
  76. vo.setArticleCategoryName(articleCategoryName);
  77. vo.setOrgId(orgId);
  78. vo.setOrgName(orgName);
  79. vo.setUserType(userType);
  80. vo.setcName(cName);
  81. vo.setUserId(userId);
  82. ArticleStatistic arvo = new ArticleStatistic();
  83. arvo.setArticleId(articleId);
  84. try {
  85. subDao.saveArticleCollection(vo);
  86. // if (userType == 2) {
  87. int behaviorAction = 6;
  88. int number = 1;
  89. ArticleApi.updateArticleStatistic(articleId, behaviorAction, number);
  90. // }
  91. Behavior behavior = new Behavior();
  92. behavior.setArticleId(articleId);
  93. behavior.setBehaviorAction(behaviorAction);
  94. behavior.setcName(cName);
  95. behavior.setUserId(userId);
  96. // ArticleStatistic arvo = new ArticleStatistic();
  97. arvo.setArticleId(articleId);
  98. subDao.saveBehavior(behavior);
  99. return ApiUtil.getRespJSON(10000, "成功").toString();
  100. } catch (SQLException e) {
  101. e.printStackTrace();
  102. return ApiUtil.getRespJSON(-14444, e.getMessage()).toString();
  103. }
  104. }
  105. /**
  106. * 取消收藏
  107. *
  108. * @param im
  109. * @return
  110. */
  111. public String cancelArticleCollection(InterfaceMessage im) {
  112. JSONObject json = JSONObject.fromObject(im.getParam());
  113. String articleId = StringUtil.isEmpty(json.get("articleId")) ? null : json.getString("articleId");
  114. String userId = StringUtil.isEmpty(json.get("userId")) ? null : json.getString("userId");
  115. int userType = StringUtil.isEmpty(json.get("userType")) ? 2 : json.getInt("userType");// 1医生
  116. if (StringUtil.isEmpty(articleId) || StringUtil.isEmpty(userId) || StringUtil.isEmpty(userType)) {
  117. return ApiUtil.getRespJSON(-10000, "ArticleId,userType,CName,UserId 为必填项,入参为:" + json.toString())
  118. .toString();
  119. }
  120. ArticleCollection vo = new ArticleCollection();
  121. vo.setArticleId(articleId);
  122. vo.setUserType(userType);
  123. vo.setUserId(userId);
  124. ArticleStatistic arvo = new ArticleStatistic();
  125. arvo.setArticleId(articleId);
  126. try {
  127. int count = subDao.deleteArticleCollection(vo);
  128. // if (userType == 2) {
  129. int behaviorAction = 6;
  130. int number = -1;
  131. ArticleApi.updateArticleStatistic(articleId, behaviorAction, number);
  132. // }
  133. if (count == 0) {
  134. return ApiUtil.getRespJSON(-10001, "收藏失败").toString();
  135. }
  136. return ApiUtil.getRespJSON(10000, "成功").toString();
  137. } catch (SQLException e) {
  138. e.printStackTrace();
  139. return ApiUtil.getRespJSON(-14444, e.getMessage()).toString();
  140. }
  141. }
  142. /**
  143. * 添加评论
  144. *
  145. * @param im
  146. * @return
  147. */
  148. public String saveArticleComment(InterfaceMessage im) {
  149. JSONObject json = JSONObject.fromObject(im.getParam());
  150. String articleId = StringUtil.isEmpty(json.get("articleId")) ? null : json.getString("articleId");
  151. String commentContent = StringUtil.isEmpty(json.get("commentContent")) ? null : json
  152. .getString("commentContent");
  153. String userImg = StringUtil.isEmpty(json.get("userImg")) ? null : json.getString("userImg");
  154. String cName = StringUtil.isEmpty(json.get("cName")) ? null : json.getString("cName");
  155. String userId = StringUtil.isEmpty(json.get("userId")) ? null : json.getString("userId");
  156. if (StringUtil.isEmpty(articleId) || StringUtil.isEmpty(commentContent) || StringUtil.isEmpty(cName)
  157. || StringUtil.isEmpty(userId)) {
  158. return ApiUtil.getRespJSON(-10000, "ArticleId,commentContent,CName,UserId 为必填项,入参为:" + json.toString())
  159. .toString();
  160. }
  161. ArticleComment vo = new ArticleComment();
  162. vo.setCommentId(StringUtil.createUUID());
  163. vo.setArticleId(articleId);
  164. vo.setcName(cName);
  165. vo.setCommentContent(commentContent);
  166. vo.setUserId(userId);
  167. vo.setUserImg(userImg);
  168. ArticleStatistic arvo = new ArticleStatistic();
  169. arvo.setArticleId(articleId);
  170. try {
  171. subDao.saveArticleComment(vo);
  172. int behaviorAction = 5;
  173. int number = 1;
  174. ArticleApi.updateArticleStatistic(articleId, behaviorAction, number);
  175. return ApiUtil.getRespJSON(10000, "成功").toString();
  176. } catch (SQLException e) {
  177. e.printStackTrace();
  178. return ApiUtil.getRespJSON(-14444, e.getMessage()).toString();
  179. }
  180. }
  181. /**
  182. * 获取评论列表
  183. *
  184. * @param im
  185. * @return
  186. * @throws JSONException
  187. */
  188. public String getArticleCommentList(InterfaceMessage im) throws JSONException {
  189. JSONObject json = JSONObject.fromObject(im.getParam());
  190. String articleId = StringUtil.isEmpty(json.get("articleId")) ? null : json.getString("articleId");
  191. Integer pageIndex = StringUtil.isEmpty(json.get("pageIndex")) ? 0 : json.getInt("pageIndex");
  192. Integer pageSize = StringUtil.isEmpty(json.get("pageSize")) ? 5 : json.getInt("pageSize");
  193. if (StringUtil.isEmpty(articleId)) {
  194. return ApiUtil.getRespJSON(-10000, "ArticleId 为必填项,入参为:" + json.toString()).toString();
  195. }
  196. ArticleComment vo = new ArticleComment();
  197. vo.setArticleId(articleId);
  198. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:m:s");
  199. JSONObject rv = ApiUtil.getRespJSON(10000, "成功");
  200. JSONArray array = new JSONArray();
  201. Date date;
  202. try {
  203. List<ArticleComment> result = subDao.getArticleCommentList(vo, pageIndex, pageSize);
  204. if (result.size() > 0) {
  205. for (ArticleComment vo1 : result) {
  206. JSONObject jsonObj = new JSONObject();
  207. jsonObj.put("articleId", StringUtil.getJSONValue(vo1.getArticleId()));
  208. jsonObj.put("commentContent", StringUtil.getJSONValue(vo1.getCommentContent()));
  209. jsonObj.put("commentId", StringUtil.getJSONValue(vo1.getCommentId()));
  210. jsonObj.put("cName", StringUtil.getJSONValue(vo1.getcName()));
  211. jsonObj.put("userImg", StringUtil.getJSONValue(vo1.getUserImg()));
  212. jsonObj.put("dateTime", StringUtil.getJSONValue(vo1.getcName()));
  213. try {
  214. date = format.parse(vo1.getInsertTime());
  215. jsonObj.put("dateTime", RelativeDateFormat.format(date));
  216. array.add(jsonObj);
  217. } catch (ParseException e) {
  218. // TODO Auto-generated catch block
  219. e.printStackTrace();
  220. return ApiUtil.getRespJSON(-10000, "获取时间数据:" + e.getMessage()).toString();
  221. }
  222. }
  223. }
  224. int count = subDao.getArticleCommentCount(vo);
  225. rv.put("Result", array);
  226. rv.put("Count", count);
  227. return rv.toString();
  228. } catch (SQLException e) {
  229. e.printStackTrace();
  230. return ApiUtil.getRespJSON(-10000, "获取数据异常:" + e.getMessage()).toString();
  231. }
  232. }
  233. /**
  234. * 取消点赞
  235. *
  236. * @param im
  237. * @return
  238. */
  239. public String cancelBehavior(InterfaceMessage im) {
  240. JSONObject json = JSONObject.fromObject(im.getParam());
  241. String articleId = StringUtil.isEmpty(json.get("articleId")) ? null : json.getString("articleId");
  242. String userId = StringUtil.isEmpty(json.get("userId")) ? null : json.getString("userId");
  243. if (StringUtil.isEmpty(articleId) || StringUtil.isEmpty(userId)) {
  244. return ApiUtil.getRespJSON(-10000, "ArticleId,UserId 为必填项,入参为:" + json.toString()).toString();
  245. }
  246. Behavior vo = new Behavior();
  247. vo.setArticleId(articleId);
  248. vo.setUserId(userId);
  249. ArticleStatistic arvo = new ArticleStatistic();
  250. arvo.setArticleId(articleId);
  251. try {
  252. subDao.deleteBehavior(vo);
  253. return ApiUtil.getRespJSON(10000, "成功").toString();
  254. } catch (SQLException e) {
  255. e.printStackTrace();
  256. return ApiUtil.getRespJSON(-14444, e.getMessage()).toString();
  257. }
  258. }
  259. }