ADIndexController.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package com.szwl.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  5. import com.gexin.fastjson.JSON;
  6. import com.szwl.model.bo.JsonMessage;
  7. import com.szwl.model.dto.AdVo;
  8. import com.szwl.model.dto.TimeRuleVo;
  9. import com.szwl.model.entity.TAd;
  10. import com.szwl.model.entity.TEquipment;
  11. import com.szwl.model.entity.TTimeRule;
  12. import com.szwl.service.TAdService;
  13. import com.szwl.service.TEquipmentService;
  14. import com.szwl.service.TTimeRuleService;
  15. import org.apache.commons.lang.StringUtils;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.data.redis.core.RedisTemplate;
  18. import org.springframework.stereotype.Controller;
  19. import org.springframework.web.bind.annotation.GetMapping;
  20. import org.springframework.web.bind.annotation.PostMapping;
  21. import org.springframework.web.bind.annotation.RequestMapping;
  22. import org.springframework.web.bind.annotation.ResponseBody;
  23. import javax.annotation.Resource;
  24. import java.text.ParseException;
  25. import java.text.SimpleDateFormat;
  26. import java.util.ArrayList;
  27. import java.util.Date;
  28. import java.util.List;
  29. @Controller("ADIndexController")
  30. @RequestMapping("/api/app_ADIndex/ADIndex")
  31. public class ADIndexController {
  32. @Autowired
  33. TAdService aDService;
  34. @Autowired
  35. private TTimeRuleService timeRuleService;
  36. @Autowired
  37. private TEquipmentService equipmentService;
  38. @Resource
  39. private RedisTemplate redisTemplate;
  40. /**
  41. * 根据广告id获取广告
  42. */
  43. @GetMapping(value = "/getAdById.htm", produces = "text/html;charset=utf-8")
  44. @ResponseBody
  45. public String getAdById(String id) {
  46. if(StringUtils.isEmpty(id)){
  47. // return JsonMessage.error("id为null");
  48. return "id为null";
  49. }
  50. TAd ad = aDService.getById(id);
  51. ArrayList<AdVo> list = new ArrayList<>();
  52. AdVo adVo = new AdVo();
  53. adVo.setAdType(String.valueOf(ad.getAdType()));
  54. adVo.setLocationType(String.valueOf(ad.getLocationType()));
  55. adVo.setName(ad.getName());
  56. adVo.setOrder(ad.getOrders());
  57. adVo.setUrl(ad.getUrl());
  58. adVo.setEquipmentType(ad.getEquipmentType());
  59. adVo.setCreateDate(ad.getCreateDate());
  60. adVo.setId(ad.getId());
  61. adVo.setMediaPreview(ad.getMediaPreview());
  62. adVo.setDuration(ad.getDuration());
  63. adVo.setScreenType(ad.getScreenType());
  64. list.add(adVo);
  65. JSONObject jsonObject = new JSONObject();
  66. jsonObject.put("code", 0);
  67. jsonObject.put("data", JSONObject.toJSON(list).toString());
  68. jsonObject.put("errmsg", "");
  69. return jsonObject.toJSONString();
  70. }
  71. /**
  72. * 设备获取默认广告
  73. */
  74. @GetMapping(value = "/getAd.htm", produces = "text/html;charset=utf-8")
  75. @ResponseBody
  76. public String getAdNew(String clientId,String equimentType) {
  77. LambdaQueryWrapper<TEquipment> query = Wrappers.lambdaQuery();
  78. query.eq(TEquipment::getClientId,clientId);
  79. List<TEquipment> equipmentList = equipmentService.list(query);
  80. TEquipment equipment = equipmentList.get(0);
  81. String adminId = String.valueOf(equipment.getAdminId());
  82. //1,获取
  83. LambdaQueryWrapper<TAd> query1 = Wrappers.lambdaQuery();
  84. query1.eq(TAd::getType,"0");
  85. query1.eq(TAd::getEquipmentType,equimentType);
  86. List<TAd> ads = aDService.list(query1);
  87. ArrayList<AdVo> list = new ArrayList<>();
  88. for (TAd ad : ads) {
  89. if(adminId.equals(ad.getAdminId())||ad.getAdminId()==null){
  90. AdVo adVo = new AdVo();
  91. adVo.setAdType(String.valueOf(ad.getAdType()));
  92. adVo.setLocationType(String.valueOf(ad.getLocationType()));
  93. adVo.setName(ad.getName());
  94. adVo.setOrder(ad.getOrders());
  95. adVo.setUrl(ad.getUrl());
  96. adVo.setCreateDate(ad.getCreateDate());
  97. adVo.setId(ad.getId());
  98. adVo.setMediaPreview(ad.getMediaPreview());
  99. adVo.setDuration(ad.getDuration());
  100. adVo.setScreenType(ad.getScreenType());
  101. list.add(adVo);
  102. }
  103. }
  104. JSONObject jsonObject = new JSONObject();
  105. jsonObject.put("code", 0);
  106. jsonObject.put("data", JSONObject.toJSON(list).toString());
  107. jsonObject.put("errmsg", "");
  108. return jsonObject.toJSONString();
  109. }
  110. /**
  111. * 设备获取广告
  112. */
  113. @GetMapping(value = "/getAdByEquipment.htm", produces = "text/html;charset=utf-8")
  114. @ResponseBody
  115. public String getAdByEquipment(String clientId,String equimentType) {
  116. LambdaQueryWrapper<TEquipment> query = Wrappers.lambdaQuery();
  117. query.eq(TEquipment::getClientId,clientId);
  118. List<TEquipment> equipmentList = equipmentService.list(query);
  119. TEquipment equipment = equipmentList.get(0);
  120. String adminId = String.valueOf(equipment.getAdminId());
  121. //1,获取
  122. LambdaQueryWrapper<TAd> query1 = Wrappers.lambdaQuery();
  123. query1.eq(TAd::getType,"0");
  124. query1.eq(TAd::getEquipmentType,equimentType);
  125. query1.eq(TAd::getAdminId,adminId);
  126. List<TAd> ads = aDService.list(query1);
  127. ArrayList<AdVo> list = new ArrayList<>();
  128. for (TAd ad : ads) {
  129. if(adminId.equals(ad.getAdminId())||ad.getAdminId()==null){
  130. AdVo adVo = new AdVo();
  131. adVo.setAdType(String.valueOf(ad.getAdType()));
  132. adVo.setLocationType(String.valueOf(ad.getLocationType()));
  133. adVo.setName(ad.getName());
  134. adVo.setOrder(ad.getOrders());
  135. adVo.setUrl(ad.getUrl());
  136. adVo.setCreateDate(ad.getCreateDate());
  137. adVo.setId(ad.getId());
  138. adVo.setMediaPreview(ad.getMediaPreview());
  139. adVo.setDuration(ad.getDuration());
  140. adVo.setScreenType(ad.getScreenType());
  141. list.add(adVo);
  142. }
  143. }
  144. JSONObject jsonObject = new JSONObject();
  145. jsonObject.put("code", 0);
  146. jsonObject.put("data", JSONObject.toJSON(list).toString());
  147. jsonObject.put("errmsg", "");
  148. return jsonObject.toJSONString();
  149. }
  150. /**
  151. * 获取时间规则
  152. *
  153. * @param id
  154. * @return
  155. */
  156. @GetMapping(value = "/getTimeRule.htm", produces = "text/html;charset=utf-8")
  157. @ResponseBody
  158. public String getTimeRule(Long id) {
  159. TTimeRule timeRule = timeRuleService.getById(id);
  160. if (timeRule == null) {
  161. // return JsonMessage.error("该规则不存在");
  162. return "该规则不存在";
  163. }
  164. if(StringUtils.isEmpty(timeRule.getType())||timeRule.getType().equals("0")){
  165. //旧规则
  166. String rule = null;
  167. List<TimeRuleVo> timeRuleVos = JSON.parseArray(timeRule.getRule(), TimeRuleVo.class);
  168. for(TimeRuleVo timeRuleVo:timeRuleVos){
  169. List<String> ads = timeRuleVo.getAd();
  170. rule = ads.toString();
  171. break;
  172. }
  173. return rule;
  174. }else {
  175. //新规则
  176. String rule = timeRule.getRule();
  177. return rule;
  178. }
  179. }
  180. /**
  181. * 设备下载广告失败则更新设备的推送广告时间
  182. *
  183. * @return
  184. */
  185. @PostMapping(value = "/pushTimeUpdate.htm", produces = "text/html;charset=utf-8")
  186. @ResponseBody
  187. public String updatePushTimeUpdate(String clientId, String pushUpdateTime) {
  188. LambdaQueryWrapper<TEquipment> query = Wrappers.lambdaQuery();
  189. query.eq(TEquipment::getClientId,clientId);
  190. List<TEquipment> equipmentList = equipmentService.list(query);
  191. TEquipment equipment = equipmentList.get(0);
  192. if (equipment == null) {
  193. // return JsonMessage.error("该设备不存在");
  194. return "该设备不存在";
  195. }
  196. Date date = null;
  197. try {
  198. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  199. date = format.parse(pushUpdateTime);
  200. equipment.setPushUpdateTime(date);
  201. } catch (ParseException e) {
  202. e.printStackTrace();
  203. // return JsonMessage.error("修改失败");
  204. return "修改失败";
  205. }
  206. equipmentService.updateById(equipment);
  207. // return JsonMessage.success("修改成功");
  208. return "修改成功";
  209. }
  210. /**
  211. *
  212. *
  213. * @param
  214. * @return
  215. */
  216. @GetMapping(value = "/checkRedis.htm", produces = "application/json;charset=UTF-8")
  217. @ResponseBody
  218. public JsonMessage checkRedis(String str) {
  219. Boolean hasKey = redisTemplate.hasKey(str);
  220. return JsonMessage.success(hasKey);
  221. }
  222. /**
  223. *
  224. *
  225. * @param
  226. * @return
  227. */
  228. @GetMapping(value = "/checkRedis2.htm", produces = "application/json;charset=UTF-8")
  229. @ResponseBody
  230. public JsonMessage checkRedis2(String key,String hashkey) {
  231. Object o = redisTemplate.boundHashOps(key).get(hashkey);
  232. return JsonMessage.success(o);
  233. }
  234. }