AdminBannersApiController.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package com.goafanti.banners.controller;
  2. import java.util.Calendar;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import javax.annotation.Resource;
  6. import javax.servlet.http.HttpServletRequest;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.util.Assert;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import com.goafanti.banners.enums.BannersType;
  13. import com.goafanti.banners.service.BannersService;
  14. import com.goafanti.common.bo.Result;
  15. import com.goafanti.common.constant.ErrorConstants;
  16. import com.goafanti.common.controller.BaseApiController;
  17. import com.goafanti.common.enums.AttachmentType;
  18. import com.goafanti.common.model.Banners;
  19. import com.goafanti.common.utils.StringUtils;
  20. import com.goafanti.core.mybatis.JDBCIdGenerator;
  21. @Controller
  22. @RequestMapping(value = "/api/admin/banners")
  23. public class AdminBannersApiController extends BaseApiController {
  24. @Resource
  25. BannersService bannersService;
  26. @Autowired
  27. JDBCIdGenerator idGenerator;
  28. @RequestMapping(value = "/upload", method = RequestMethod.POST)
  29. public Result upload(String sign, HttpServletRequest req) {
  30. Assert.isTrue(StringUtils.isNotBlank(sign) && BannersType.containsType(sign), "广告图位置标示不正确。");
  31. Result res = res();
  32. Calendar c = Calendar.getInstance();
  33. res.data(handleFile(res, false, req, AttachmentType.BANNERS_PIC.getCode(), String.valueOf(c.get(Calendar.YEAR)),
  34. String.valueOf(c.get(Calendar.MONTH) + 1), idGenerator.generateId().toString()));
  35. return res;
  36. }
  37. /*@RequestMapping(value = "/save", method = RequestMethod.POST)
  38. public Result save(@Valid Banners banners, BindingResult bindingResult) {
  39. Assert.notNull(banners, "广告图参数不正确");
  40. Assert.isTrue(StringUtils.isNotBlank(banners.getSign()) && BannersType.containsType(banners.getSign()),
  41. "广告图位置标示不正确。");
  42. Assert.hasText(banners.getImgurl(), "广告图url参数不正确");
  43. Assert.isTrue(!bindingResult.hasFieldErrors(),
  44. bindingResult.hasFieldErrors() ? bindingResult.getFieldError().getDefaultMessage() : "");
  45. Result res = res();
  46. if (banners.getId() == null) {
  47. banners.setId(idGenerator.generateId());
  48. res.data(bannersService.insertBanner(banners));
  49. } else {
  50. res.data(bannersService.updateBanner(banners));
  51. }
  52. return res;
  53. }
  54. @RequestMapping(value = "/list", method = RequestMethod.GET)
  55. public Result portalBannersList(String key, String pageNo, String pageSize) {
  56. Assert.isTrue(StringUtils.isBlank(key) || BannersType.containsType(key), "广告图位置标示不正确。");
  57. Integer pN = getPageNo(pageNo);
  58. Integer pS = getPageSize(pageSize);
  59. Map<String, Object> result = new HashMap<>();
  60. result.put("list", bannersService.findBannersList(key, (pN - 1) * pS, pS));
  61. result.put("total", bannersService.findBannersListCount(key));
  62. return res().data(result);
  63. }
  64. @RequestMapping(value = "/delete", method = RequestMethod.POST)
  65. public Result delete(String id) {
  66. Assert.isTrue(StringUtils.isNumeric(id), "广告图id不正确。");
  67. return res().data(bannersService.deleteBanner(Long.valueOf(id)));
  68. }
  69. @RequestMapping(value = "/clearCache", method = RequestMethod.GET)
  70. public Result clearCache(String key) {
  71. Assert.isTrue(StringUtils.isNotBlank(key) && BannersType.containsType(key), "广告图位置标示不正确。");
  72. bannersService.cleanList(key);
  73. return res().data(bannersService.findPortalBanners(key));
  74. }*/
  75. @RequestMapping(value = "/keys", method = RequestMethod.GET)
  76. public Result portalBannersKeys() {
  77. Map<String, String> types = new HashMap<>();
  78. for (BannersType type : BannersType.values()) {
  79. types.put(type.getKey(), type.getDesc());
  80. }
  81. return res().data(types);
  82. }
  83. /**
  84. * 新增轮播图
  85. * @param b
  86. * @return
  87. */
  88. @RequestMapping(value="/save" ,method=RequestMethod.POST)
  89. public Result save(Banners b){
  90. Result result=new Result();
  91. result.setData(bannersService.insertBanner(b));
  92. return result;
  93. }
  94. /**
  95. * 删除轮播图
  96. * @param id
  97. * @return
  98. */
  99. @RequestMapping(value="/delete" ,method=RequestMethod.POST)
  100. public Result delete(String id){
  101. Result result=new Result();
  102. result.setData(bannersService.deleteBanner(id));
  103. return result;
  104. }
  105. /**
  106. * 轮播图详情
  107. * @param id
  108. * @return
  109. */
  110. @RequestMapping(value="/details" ,method=RequestMethod.GET)
  111. public Result details(String id){
  112. Result result=new Result();
  113. result.setData(bannersService.selectByPrimaryKey(id));
  114. return result;
  115. }
  116. /**
  117. * 修改轮播图
  118. * @param b
  119. * @return
  120. */
  121. @RequestMapping(value="/update" ,method=RequestMethod.POST)
  122. public Result details(Banners b){
  123. Result result=new Result();
  124. result.setData(bannersService.updateBanner(b));
  125. return result;
  126. }
  127. /**
  128. * 轮播图列表
  129. * @param apiUrl
  130. * @param client
  131. * @param pageSize
  132. * @param pageNo
  133. * @param request
  134. * @return
  135. */
  136. @RequestMapping(value="/list" ,method=RequestMethod.GET)
  137. public Result List(String apiUrl,Integer client,Integer pageSize,Integer pageNo,HttpServletRequest request){
  138. Result result=new Result();
  139. result.setData(bannersService.findAdminBannersList( apiUrl,client, pageSize, pageNo));
  140. return result;
  141. }
  142. /**
  143. * 图片上传
  144. */
  145. @RequestMapping(value = "/uploadPicture", method = RequestMethod.POST)
  146. public Result uploadPicture(HttpServletRequest req, String sign) {
  147. Result res = new Result();
  148. AttachmentType attachmentType = AttachmentType.getField(sign);
  149. if (attachmentType == AttachmentType.BANNERS_PIC) {
  150. res.setData(handleFiles(res, "/banners/", false, req, sign, ""));
  151. } else {
  152. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "图片标示"));
  153. }
  154. return res;
  155. }
  156. }