advertiseAction.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package com.happy.action;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.google.gson.Gson;
  4. import com.google.gson.reflect.TypeToken;
  5. import com.happy.Model.Advertise;
  6. import com.happy.Model.app.Around_product;
  7. import com.happy.Model.app.Arounds;
  8. import com.happy.Until.GetHttpParam;
  9. import com.happy.Until.ResUtil;
  10. import com.happy.Until.ResponseUtil;
  11. import com.happy.Until.TimeExchange;
  12. import com.happy.service.AdvertiseService;
  13. import com.opensymphony.xwork2.ActionSupport;
  14. import org.apache.struts2.ServletActionContext;
  15. import org.apache.struts2.interceptor.ServletRequestAware;
  16. import javax.annotation.Resource;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import java.util.List;
  20. import java.util.Objects;
  21. public class advertiseAction extends ActionSupport implements ServletRequestAware {
  22. private HttpServletRequest request;
  23. public HttpServletResponse response;
  24. @Resource
  25. public AdvertiseService advertiseService;
  26. public Integer id;
  27. public int page; // 当前页
  28. public int rows;// 每页显示的行数rows
  29. public String title;
  30. public Integer state;
  31. public String startTime;
  32. public String endTime;
  33. public HttpServletRequest getRequest() {
  34. return request;
  35. }
  36. public void setRequest(HttpServletRequest request) {
  37. this.request = request;
  38. }
  39. public void setServletRequest(HttpServletRequest request) {
  40. this.request = request;
  41. }
  42. public HttpServletResponse getResponse() {
  43. return response;
  44. }
  45. public void setResponse(HttpServletResponse response) {
  46. this.response = response;
  47. }
  48. public Integer getId() {
  49. return id;
  50. }
  51. public void setId(Integer id) {
  52. this.id = id;
  53. }
  54. public int getPage() {
  55. return page;
  56. }
  57. public void setPage(int page) {
  58. this.page = page;
  59. }
  60. public int getRows() {
  61. return rows;
  62. }
  63. public void setRows(int rows) {
  64. this.rows = rows;
  65. }
  66. public String getTitle() {
  67. return title;
  68. }
  69. public void setTitle(String title) {
  70. this.title = title;
  71. }
  72. public Integer getState() {
  73. return state;
  74. }
  75. public void setState(Integer state) {
  76. this.state = state;
  77. }
  78. public String getStartTime() {
  79. return startTime;
  80. }
  81. public void setStartTime(String startTime) {
  82. this.startTime = startTime;
  83. }
  84. public String getEndTime() {
  85. return endTime;
  86. }
  87. public void setEndTime(String endTime) {
  88. this.endTime = endTime;
  89. }
  90. public String insert() {
  91. JSONObject resultjson = new JSONObject();
  92. Gson gson = new Gson();
  93. com.alibaba.fastjson.JSONObject json = GetHttpParam.getHttpParam(request);
  94. System.out.println(json);
  95. if (json == null) {
  96. resultjson.put("message", "请传入参数");
  97. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  98. return null;
  99. }
  100. Advertise advertise = null;
  101. try {
  102. advertise = gson.fromJson(json.toString(), new TypeToken<Advertise>() {}.getType());
  103. if (advertise == null) {
  104. resultjson.put("message", "数据为空");
  105. resultjson.put("code", 500);
  106. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  107. return null;
  108. }
  109. if (advertise.getTitle()==null || advertise.getLogoUrl()==null || advertise.getImageUrl()==null || advertise.getJumpWay()==null || advertise.getJumpDirection()==null || advertise.getJumpPoint()==null){
  110. resultjson.put("message", "各参数不能为空");
  111. resultjson.put("code", 500);
  112. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  113. return null;
  114. }
  115. Advertise listc = advertiseService.queryByTitle(advertise.getTitle());
  116. if (listc != null) {
  117. resultjson.put("message", "该广告已存在");
  118. resultjson.put("code", 500);
  119. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  120. return null;
  121. }
  122. advertise.setCreateTime(TimeExchange.getTime());
  123. int m = advertiseService.insertAdvertise(advertise);
  124. if (m > 0) {
  125. resultjson.put("message", "添加成功");
  126. resultjson.put("code", 200);
  127. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  128. return null;
  129. } else {
  130. resultjson.put("message", "添加失败");
  131. resultjson.put("code", 502);
  132. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  133. return null;
  134. }
  135. } catch (Exception e) {
  136. e.printStackTrace();
  137. }
  138. resultjson.put("message", "未知异常");
  139. resultjson.put("code", 205);
  140. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  141. return null;
  142. }
  143. public String update() {
  144. JSONObject resultjson = new JSONObject();
  145. Gson gson = new Gson();
  146. JSONObject json = GetHttpParam.getHttpParam(request);
  147. System.out.println(json);
  148. if (json == null) {
  149. resultjson.put("code", 205);
  150. resultjson.put("message", "请传入参数");
  151. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  152. return null;
  153. }
  154. Advertise advertise = null;
  155. try {
  156. advertise = gson.fromJson(json.toString(), new TypeToken<Advertise>() {}.getType());
  157. if (advertise == null) {
  158. resultjson.put("message", "数据为空");
  159. resultjson.put("code", 500);
  160. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  161. return null;
  162. }
  163. if (advertise.getTitle()==null || advertise.getLogoUrl()==null || advertise.getImageUrl()==null || advertise.getJumpWay()==null || advertise.getJumpDirection()==null || advertise.getJumpPoint()==null){
  164. resultjson.put("message", "各参数不能为空");
  165. resultjson.put("code", 500);
  166. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  167. return null;
  168. }
  169. Advertise listy = advertiseService.queryById(advertise.getId());
  170. if (listy==null){
  171. resultjson.put("message", "该条数据已删除");
  172. resultjson.put("code", 500);
  173. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  174. return null;
  175. }
  176. Advertise listc = advertiseService.queryByTitle(advertise.getTitle());
  177. if (listc!=null && !Objects.equals(listc.getId(), advertise.getId())){
  178. resultjson.put("message", "广告已存在");
  179. resultjson.put("code", 500);
  180. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  181. return null;
  182. }
  183. advertise.setCreateTime(TimeExchange.getTime());
  184. int m = advertiseService.updateAdvertise(advertise);
  185. if (m > 0) {
  186. resultjson.put("message", "修改成功");
  187. resultjson.put("code", 200);
  188. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  189. return null;
  190. } else {
  191. resultjson.put("message", "修改失败");
  192. resultjson.put("code", 502);
  193. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  194. return null;
  195. }
  196. } catch (Exception e) {
  197. e.printStackTrace();
  198. }
  199. resultjson.put("message", "未知异常");
  200. resultjson.put("code", 205);
  201. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  202. return null;
  203. }
  204. public String del() {
  205. JSONObject resultjson = new JSONObject();
  206. if (id == null) {
  207. resultjson.put("message", "请传入id");
  208. resultjson.put("code", 500);
  209. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  210. return null;
  211. }
  212. Advertise advertise = advertiseService.queryById(id);
  213. if (advertise==null){
  214. resultjson.put("message", "该数据已删除");
  215. resultjson.put("code", 500);
  216. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  217. return null;
  218. }
  219. int m = advertiseService.delAdvertise(id);
  220. if (m > 0) {
  221. resultjson.put("message", "删除成功");
  222. resultjson.put("code", 200);
  223. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  224. return null;
  225. }
  226. resultjson.put("message", "未知异常");
  227. resultjson.put("code", 205);
  228. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  229. return null;
  230. }
  231. public String listPage() {
  232. JSONObject resultJson = new JSONObject();
  233. StringBuilder s1 = new StringBuilder("");
  234. if (state!=null){
  235. s1.append(" and state = '").append(this.state).append("' ");
  236. }
  237. if (title!=null){
  238. s1.append(" and title like '%").append(this.title).append("%' ");
  239. }
  240. if (startTime!=null){
  241. s1.append(" and create_time > '").append(this.startTime).append("' ");
  242. }
  243. if (endTime!=null){
  244. s1.append(" and create_time < '").append(this.endTime).append(" 24:60:60' ");
  245. }
  246. int total = advertiseService.queryAdvertiseTotal(s1.toString()); // 查询表中的总记录数
  247. List<Advertise> listPage = advertiseService.queryAdvertisePage(page,rows,s1.toString()); // 查询分页
  248. if (listPage == null) {
  249. resultJson.put("code", 200);
  250. resultJson.put("rows", "");
  251. resultJson.put("total", 0);
  252. } else {
  253. resultJson.put("code", 200);
  254. resultJson.put("rows", listPage);
  255. // int total = listAll.size();
  256. resultJson.put("total", total);// 总记录数
  257. int totalPage = total % rows == 0 ? (total / rows)
  258. : (total / rows) + 1;// 总页数
  259. resultJson.put("totalPage", totalPage);
  260. resultJson.put("currentPage", page);// 当前页
  261. resultJson.put("numPerPage", rows);// 每页数
  262. resultJson.put("nextPage", totalPage - page == 0 ? page : page + 1);// 下一页
  263. resultJson.put("previousPage", page - 0 == 1 ? page : page - 1);// 上一页
  264. resultJson.put("hasPreviousPage", true);// 有上一页
  265. resultJson.put("hasNextPage", true);// 有下一页
  266. resultJson.put("firstPage", true);// 首页
  267. resultJson.put("lastPage", true);// 尾页
  268. }
  269. ResponseUtil.writeJson(ServletActionContext.getResponse(),
  270. resultJson.toString());
  271. return null;
  272. }
  273. }