PageRenderTime 65ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/lehuo/lsm/controller/ActivityController.java

https://gitlab.com/Simon0622/web
Java | 202 lines | 167 code | 32 blank | 3 comment | 11 complexity | c45cde5e687c9d88909d831095342c9f MD5 | raw file
  1. package lehuo.lsm.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import lehuo.lsm.global.Global;
  4. import lehuo.lsm.model.*;
  5. import lehuo.lsm.service.impl.*;
  6. import lehuo.lsm.util.ImgDownload;
  7. import org.apache.commons.lang.StringUtils;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.stereotype.Controller;
  11. import org.springframework.ui.ModelMap;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RequestParam;
  14. import org.springframework.web.bind.annotation.ResponseBody;
  15. import org.springframework.web.multipart.MultipartFile;
  16. import javax.annotation.Resource;
  17. import javax.servlet.http.HttpSession;
  18. import java.io.InputStream;
  19. import java.io.UnsupportedEncodingException;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. /**
  23. * Created by simonliu on 2014/10/26.
  24. */
  25. @Controller
  26. @RequestMapping(value = "/activity")
  27. public class ActivityController {
  28. protected static Logger logger = LoggerFactory.getLogger(RankController.class);
  29. private static final int PAGE = 10;
  30. @Resource
  31. private ActivityService activityService;
  32. @Resource
  33. private UserService userService;
  34. @Resource
  35. private ImagesService imagesService;
  36. @Resource
  37. private ActivityCommentService activityCommentService;
  38. @RequestMapping(value = "/index")
  39. public String index(ModelMap map,Integer currentPage,String querytype,String hasloc,
  40. String hasphoto,String keyword){
  41. List<Activity> list = null;
  42. if(currentPage==null){
  43. currentPage=1;
  44. }
  45. if(!(StringUtils.isBlank(querytype)||StringUtils.isBlank(hasloc))){
  46. if(!hasloc.equals("on")){
  47. hasloc = null;
  48. }
  49. if(!StringUtils.isBlank(keyword)){
  50. try {
  51. keyword = new String(keyword.trim().getBytes("ISO-8859-1"), "utf8");
  52. } catch (UnsupportedEncodingException e) {
  53. logger.error("UnsupportedEncodingException",e);
  54. }
  55. }
  56. list = activityService.simpleQuery(keyword,querytype, hasloc, (currentPage - 1) * PAGE, PAGE);
  57. }else{
  58. list = activityService.selectList(null,(currentPage-1)*PAGE,PAGE);
  59. }
  60. map.put("currentPage",currentPage);
  61. map.put("keyword",keyword);
  62. map.put("querytype",querytype);
  63. map.put("list",list);
  64. return "activityindex";
  65. }
  66. @RequestMapping(value = "/addacpage")
  67. public String addActivityPage(){
  68. return "addacpage";
  69. }
  70. @RequestMapping(value = "/activitydetail")
  71. public String activityDetail(Integer id,ModelMap map){
  72. Activity activity = new Activity();
  73. activity.setId(id);
  74. Activity result = activityService.select(activity);
  75. List<Images> imglist = null;
  76. if(result==null){
  77. return "activitydetail";
  78. }else {
  79. Images where = new Images();
  80. where.setLinks_id(result.getId());
  81. where.setCatagory(result.getActivitytype());
  82. imglist = imagesService.selectList(where,null,null);
  83. }
  84. ActivityComment ac = new ActivityComment();
  85. ac.setActivity_id(id);
  86. List<ActivityComment> comments = activityCommentService.selectList(ac,0,200);
  87. map.put("activity",result);
  88. map.put("images",imglist);
  89. map.put("comment",comments);
  90. return "activitydetail";
  91. }
  92. @RequestMapping(value = "/activityedit")
  93. public String activityEdit(Integer id,ModelMap map){
  94. Activity activity = new Activity();
  95. activity.setId(id);
  96. Activity result = activityService.select(activity);
  97. List<Images> imglist = null;
  98. if(result==null){
  99. return "activityedit";
  100. }else {
  101. Images where = new Images();
  102. where.setLinks_id(result.getId());
  103. where.setCatagory(result.getActivitytype());
  104. imglist = imagesService.selectList(where,null,null);
  105. }
  106. map.put("activity",result);
  107. map.put("images",imglist);
  108. return "activityedit";
  109. }
  110. @RequestMapping(value = "/addactivity")
  111. public String addactivity(Activity activity,HttpSession session){
  112. String sessionid = session.getId();
  113. String ipaddr = (String)session.getAttribute("ip");
  114. User item = new User();
  115. item.setSessionid(sessionid);
  116. item.setIpaddr(ipaddr);
  117. User user = userService.select(item);
  118. logger.info("activity extra {}",activity.getExtra());
  119. if(!StringUtils.isEmpty(activity.getExtra())){
  120. String extra = activity.getExtra();
  121. extra = extra.substring(0,extra.length()-1);
  122. activity.setExtra(extra);
  123. }
  124. if(user==null){
  125. User u = new User("","",sessionid,ipaddr);
  126. userService.insert(u);
  127. activity.setUserid(u.getId());
  128. }else{
  129. activity.setUserid(user.getId());
  130. }
  131. logger.info("activity {}",activity.getActivityname());
  132. activityService.insert(activity);
  133. String extra = activity.getExtra();
  134. if(!StringUtils.isEmpty(extra)){
  135. String[] extraarray = extra.split(",");
  136. for(int i=0;i<extraarray.length;i++){
  137. try{
  138. Integer imgid = Integer.parseInt(extraarray[i]);
  139. Images where = new Images();
  140. where.setId(imgid);
  141. Images update = new Images();
  142. update.setLinks_id(activity.getId());
  143. imagesService.update(update,where);
  144. }catch (Exception e){
  145. logger.error("addactivity error",e);
  146. }
  147. }
  148. }
  149. return "redirect:/activity/index";
  150. }
  151. @RequestMapping(value = "/addcomment")
  152. @ResponseBody
  153. public String comment(String activityId,String comment,HttpSession session){
  154. if(StringUtils.isBlank(comment)){
  155. return "";
  156. }else{
  157. try{
  158. logger.info("add activity comment {}",comment);
  159. String sessionid = session.getId();
  160. User param = new User();
  161. param.setSessionid(sessionid);
  162. User user = userService.select(param);
  163. activityCommentService.insert(new ActivityComment(Integer.parseInt(activityId),comment,user.getId()));
  164. }catch(Exception e){
  165. logger.error("add comment error",e);
  166. }
  167. }
  168. return "success";
  169. }
  170. }