package com.yihu.jkedu.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.yihu.base.ConfigUtil; import com.yihu.utils.ApiUtil; import com.yihu.utils.StringUtil; import com.yihu.wsgw.api.ServiceBus; @Controller @RequestMapping("/behavior") public class BehaviorControl { /** * @Title: cancelBehavior * @Description: 取消点赞 * @param @param request * @param @param response * @param @throws Exception 设定文件 * @return void 返回类型 * @throws */ @RequestMapping(value = "/cancelBehavior") public void cancelBehavior(HttpServletRequest request, HttpServletResponse response) throws Exception { try { response.setContentType("application/json;charset=UTF-8"); String articleId = StringUtil.isEmpty(request.getParameter("articleId")) ? null : request.getParameter("articleId"); String userId = StringUtil.isEmpty(request.getParameter("userId")) ? null : request.getParameter("userId"); if(StringUtil.isEmpty(userId)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "用户userId不能为空").toString()); return; } if(StringUtil.isEmpty(articleId)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "文章articleId不能为空").toString()); return; } JSONObject params = new JSONObject(); params.put("articleId", articleId); params.put("userId", userId); String result = ServiceBus.getInstance( null, ConfigUtil.getInstance().getAppId()).call( "JkEdu.Behavior.cancelBehavior", params.toString(), false); JSONObject formResultObj = JSONObject.fromObject(result); int code = formResultObj.getInt("Code"); String msg = formResultObj.getString("Message"); JSONObject resultJson = new JSONObject(); if (code == 10000) {// 成功 resultJson.put("Code", 10000); resultJson.put("Message", msg); }else{ resultJson.put("Code", code); resultJson.put("Message", msg); } response.getWriter().write(resultJson.toString()); return; } catch (Exception e) { e.printStackTrace(); response.getWriter().write(ApiUtil.jsonResult(-14444, "取消点赞异常").toString()); return; } } /** * @Title: saveBehavior * @Description: 保存用户行为记录 * @param @param request * @param @param response * @param @throws Exception 设定文件 * @return void 返回类型 * @throws */ @RequestMapping(value = "/saveBehavior") public void saveBehavior(HttpServletRequest request, HttpServletResponse response) throws Exception { try { response.setContentType("application/json;charset=UTF-8"); String articleId = StringUtil.isEmpty(request.getParameter("articleId")) ? null : request.getParameter("articleId"); String behaviorAction = StringUtil.isEmpty(request.getParameter("behaviorAction")) ? null : request.getParameter("behaviorAction"); String cName = StringUtil.isEmpty(request.getParameter("cName")) ? null : request.getParameter("cName"); String userId = StringUtil.isEmpty(request.getParameter("userId")) ? null : request.getParameter("userId"); if(StringUtil.isEmpty(userId)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "用户userId不能为空").toString()); return; } if(StringUtil.isEmpty(cName)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "用户cName不能为空").toString()); return; } if(StringUtil.isEmpty(articleId)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "文章id不能为空").toString()); return; } if(StringUtil.isEmpty(behaviorAction)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "用户的行为类型不能为空").toString()); return; } JSONObject params = new JSONObject(); params.put("articleId", articleId); params.put("behaviorAction", behaviorAction); params.put("userId", userId); params.put("cName", cName); String result = ServiceBus.getInstance( null, ConfigUtil.getInstance().getAppId()).call( "JkEdu.Behavior.saveBehavior", params.toString(), false); JSONObject formResultObj = JSONObject.fromObject(result); int code = formResultObj.getInt("Code"); String msg = formResultObj.getString("Message"); JSONObject resultJson = new JSONObject(); if (code == 10000) {// 成功 resultJson.put("Code", 10000); resultJson.put("Message", msg); }else{ resultJson.put("Code", code); resultJson.put("Message", msg); } response.getWriter().write(resultJson.toString()); return; } catch (Exception e) { e.printStackTrace(); response.getWriter().write(ApiUtil.jsonResult(-14444, "保存用户的行为记录异常").toString()); return; } } /** * @Title: cancelArticle * @Description: 取消收藏 * @param @param request * @param @param response * @param @throws Exception 设定文件 * @return void 返回类型 * @throws */ @RequestMapping(value = "/cancelArticle") public void cancelArticle(HttpServletRequest request, HttpServletResponse response) throws Exception { try { response.setContentType("application/json;charset=UTF-8"); String articleId = StringUtil.isEmpty(request.getParameter("articleId")) ? null : request.getParameter("articleId"); String userType = StringUtil.isEmpty(request.getParameter("userType")) ? null : request.getParameter("userType"); String userId = StringUtil.isEmpty(request.getParameter("userId")) ? null : request.getParameter("userId"); if(StringUtil.isEmpty(userId)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "用户userId不能为空").toString()); return; } if(StringUtil.isEmpty(articleId)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "文章articleId不能为空").toString()); return; } if(StringUtil.isEmpty(userType)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "文章userType不能为空").toString()); return; } JSONObject params = new JSONObject(); params.put("articleId", articleId); params.put("userType", userType); params.put("userId", userId); String result = ServiceBus.getInstance( null, ConfigUtil.getInstance().getAppId()).call( "JkEdu.Behavior.cancelArticleCollection", params.toString(), false); JSONObject formResultObj = JSONObject.fromObject(result); int code = formResultObj.getInt("Code"); String msg = formResultObj.getString("Message"); JSONObject resultJson = new JSONObject(); if (code == 10000) {// 成功 resultJson.put("Code", 10000); resultJson.put("Message", msg); resultJson.put("articleId", articleId); }else{ resultJson.put("Code", code); resultJson.put("Message", msg); resultJson.put("articleId", articleId); } response.getWriter().write(resultJson.toString()); return; } catch (Exception e) { e.printStackTrace(); response.getWriter().write(ApiUtil.jsonResult(-14444, "取消收藏异常").toString()); return; } } /** * @Title: saveArticleCollection * @Description: 保存收藏 * @param @param request * @param @param response * @param @throws Exception 设定文件 * @return void 返回类型 * @throws */ @RequestMapping(value = "/saveArticleCollection") public void saveArticleCollection(HttpServletRequest request, HttpServletResponse response) throws Exception { try { response.setContentType("application/json;charset=UTF-8"); String articleId = StringUtil.isEmpty(request.getParameter("articleId")) ? null : request.getParameter("articleId"); String userType = StringUtil.isEmpty(request.getParameter("userType")) ? null : request.getParameter("userType"); String userId = StringUtil.isEmpty(request.getParameter("userId")) ? null : request.getParameter("userId"); String cName = StringUtil.isEmpty(request.getParameter("cName")) ? null : request.getParameter("cName"); String articleCategoryId = StringUtil.isEmpty(request.getParameter("articleCategoryId")) ? null : request.getParameter("articleCategoryId"); String articleCategoryName = StringUtil.isEmpty(request.getParameter("articleCategoryName")) ? null : request.getParameter("articleCategoryName"); if(StringUtil.isEmpty(userId)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "用户userId不能为空").toString()); return; } if(StringUtil.isEmpty(cName)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "用户cName不能为空").toString()); return; } if(StringUtil.isEmpty(articleId)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "文章articleId不能为空").toString()); return; } if(StringUtil.isEmpty(articleCategoryId)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "文章articleCategoryId不能为空").toString()); return; } if(StringUtil.isEmpty(articleCategoryName)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "文章articleCategoryName不能为空").toString()); return; } JSONObject params = new JSONObject(); params.put("articleId", articleId); params.put("userType", userType); params.put("userId", userId); params.put("cName", cName); params.put("articleCategoryId", articleCategoryId); params.put("articleCategoryName", articleCategoryName); String result = ServiceBus.getInstance( null, ConfigUtil.getInstance().getAppId()).call( "JkEdu.Behavior.saveArticleCollection", params.toString(), false); JSONObject formResultObj = JSONObject.fromObject(result); int code = formResultObj.getInt("Code"); String msg = formResultObj.getString("Message"); JSONObject resultJson = new JSONObject(); if (code == 10000) {// 成功 resultJson.put("Code", 10000); resultJson.put("Message", msg); }else{ resultJson.put("Code", code); resultJson.put("Message", msg); } response.getWriter().write(resultJson.toString()); return; } catch (Exception e) { e.printStackTrace(); response.getWriter().write(ApiUtil.jsonResult(-14444, "保存收藏异常").toString()); return; } } /** * @Title: getArticleCommentList * @Description: 查看评价 * @param @param request * @param @param response * @param @throws Exception 设定文件 * @return void 返回类型 * @throws */ @RequestMapping(value = "/getArticleCommentList") public void getArticleCommentList(HttpServletRequest request, HttpServletResponse response) throws Exception { try { response.setContentType("application/json;charset=UTF-8"); String articleId = StringUtil.isEmpty(request.getParameter("articleId")) ? null : request.getParameter("articleId"); int pageSize = StringUtil.isEmpty(request.getParameter("pageSize")) ? 0 : Integer.valueOf(request.getParameter("pageSize")); int pageIndex = StringUtil.isEmpty(request.getParameter("pageIndex")) ? 0 : Integer.valueOf(request.getParameter("pageIndex")); if(StringUtil.isEmpty(articleId)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "articleId参数不能为空").toString()); return; } JSONObject params = new JSONObject(); params.put("pageSize", pageSize); params.put("pageIndex", pageIndex); params.put("articleId", articleId); String formResult = ServiceBus.getInstance( null, ConfigUtil.getInstance().getAppId()).call( "JkEdu.Behavior.getArticleCommentList", params.toString(), false); JSONObject resultJson = new JSONObject(); JSONObject formResultObj = JSONObject.fromObject(formResult); int code = formResultObj.getInt("Code"); String msg = formResultObj.getString("Message"); if (code == 10000) {// 成功 int Count=formResultObj.getInt("Count"); if(Count>0){ JSONArray arrt=formResultObj.getJSONArray("Result"); resultJson.put("Code", 10000); resultJson.put("Message", msg); resultJson.put("Result", arrt); resultJson.put("Count", Count); response.getWriter().write(resultJson.toString()); return ; }else{ resultJson.put("Code", 10000); resultJson.put("Message", msg); resultJson.put("Result", null); resultJson.put("Count", Count); response.getWriter().write(resultJson.toString()); return ; } } resultJson.put("Code", -10000); resultJson.put("Message", msg); resultJson.put("Result", null); response.getWriter().write(resultJson.toString()); return ; } catch (Exception e) { e.printStackTrace(); response.getWriter().write(ApiUtil.jsonResult(-14444, "获取文章列表异常").toString()); return ; } } /** * @Title: saveArticleComment * @Description: 保存用户评价 * @param @param request * @param @param response * @param @throws Exception 设定文件 * @return void 返回类型 * @throws */ @RequestMapping(value = "/saveArticleComment") public void saveArticleComment(HttpServletRequest request, HttpServletResponse response) throws Exception { try { response.setContentType("application/json;charset=UTF-8"); String userId = StringUtil.isEmpty(request.getParameter("userId")) ? null : request.getParameter("userId"); String commentContent = StringUtil.isEmpty(request.getParameter("commentContent")) ? null : request.getParameter("commentContent"); String cName = StringUtil.isEmpty(request.getParameter("cName")) ? null : request.getParameter("cName"); String articleId = StringUtil.isEmpty(request.getParameter("articleId")) ? null : request.getParameter("articleId"); if(StringUtil.isEmpty(userId)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "用户userId不能为空").toString()); return; } if(StringUtil.isEmpty(cName)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "用户cName不能为空").toString()); return; } if(StringUtil.isEmpty(commentContent)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "评价内容不能为空").toString()); return; } if(StringUtil.isEmpty(articleId)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "文章id不能为空").toString()); return; } JSONObject params = new JSONObject(); String result = ServiceBus.getInstance( null, ConfigUtil.getInstance().getAppId()).call( "JkEdu.ConfigSys.getConfigSys", params.toString(), false); JSONObject formResultObj = JSONObject.fromObject(result); int code = formResultObj.getInt("Code"); String msg = formResultObj.getString("Message"); JSONObject resultjson = new JSONObject(); if (code == 10000) {// 成功 JSONArray array=formResultObj.getJSONArray("Result"); if(null!=array && array.size()>0){ JSONObject json=array.getJSONObject(0); if(null !=json){ int switchType=json.getInt("switchType"); int switchState=json.getInt("switchState"); if(switchType==1 && switchState==1){ JSONObject userparam = new JSONObject(); userparam.put("userID", userId); String userresult=queryUserInfo(userparam); JSONObject userResultObj = JSONObject.fromObject(userresult); int userCode=userResultObj.getInt("Code"); String usermsg=userResultObj.getString("Message"); if(userCode==10000){ JSONObject arrform = userResultObj.getJSONObject("Result"); String PhotoUri=""; if(null!=arrform && arrform.size()>0){ PhotoUri=arrform.getString("PhotoUri"); } JSONObject articleparams = new JSONObject(); articleparams.put("articleId", articleId); articleparams.put("commentContent", commentContent); articleparams.put("cName", cName); articleparams.put("userId", userId); if(StringUtils.isNotEmpty(PhotoUri)){ articleparams.put("userImg", PhotoUri); } String articleresult = ServiceBus.getInstance( null, ConfigUtil.getInstance().getAppId()).call( "JkEdu.Behavior.saveArticleComment", articleparams.toString(), false); JSONObject articleresultObj = JSONObject.fromObject(articleresult); int articleCode = articleresultObj.getInt("Code"); String articlemsg = articleresultObj.getString("Message"); if(articleCode == 10000){ resultjson.put("Code", 10000); resultjson.put("Message", articlemsg); response.getWriter().write(resultjson.toString()); return ; }else{ resultjson.put("Code", articleCode); resultjson.put("Message", articlemsg); response.getWriter().write(resultjson.toString()); return ; } }else{ response.getWriter().write(userresult); return; } }else if(switchType==1 && switchState==2){ response.getWriter().write(ApiUtil.jsonResult(-10000, "用户不能评价该文章").toString()); return; } }else{ response.getWriter().write(ApiUtil.jsonResult(-10000, "未设置评价配置").toString()); return; } }else{ response.getWriter().write(ApiUtil.jsonResult(-10000, "未设置评价配置").toString()); return; } }else{ resultjson.put("Code", code); resultjson.put("Message", msg); response.getWriter().write(resultjson.toString()); return; } } catch (Exception e) { e.printStackTrace(); response.getWriter().write(ApiUtil.jsonResult(-14444, "保存用户的评价异常").toString()); return; } } public String queryUserInfo(JSONObject json) throws Exception { try { String userID = StringUtil.isEmpty(json.getString("userID")) ? null : json.getString("userID"); JSONObject params = new JSONObject(); if(StringUtils.isNotEmpty(userID)){ params.put("userId", userID); } if(params.size() == 0){ return ApiUtil.jsonResult(-10000, "参数:userID,oaOperatorID,accountID,cardID,doctorUid,access_token不能全部为空").toString(); } String result = ServiceBus.getInstance( null, ConfigUtil.getInstance().getAppId()).call( "UserMgmt.User.queryUserInfoByID", params.toString(), false); JSONObject formResultObj = JSONObject.fromObject(result); int code = formResultObj.getInt("Code"); String msg = formResultObj.getString("Message"); JSONObject resultJson = new JSONObject(); if (code == 10000) {// 成功 JSONObject arrform = formResultObj.getJSONObject("Result"); resultJson.put("Code", 10000); resultJson.put("Message", msg); resultJson.put("Result", arrform); }else{ resultJson.put("Code", code); resultJson.put("Message", msg); resultJson.put("Result", null); } return resultJson.toString(); } catch (Exception e) { e.printStackTrace(); return ApiUtil.jsonResult(-14444, "获取用户信息异常").toString(); } } }