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 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(); } } }