ADIndexController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * 获取时间规则 ByClientId
  182. *
  183. * @param
  184. * @return
  185. */
  186. @GetMapping(value = "/getTimeRuleByClientId.htm", produces = "text/html;charset=utf-8")
  187. @ResponseBody
  188. public String getTimeRuleByClientId(String clientId) {
  189. if(StringUtils.isEmpty(clientId)){
  190. return "编号为空";
  191. }
  192. LambdaQueryWrapper<TEquipment> query = Wrappers.lambdaQuery();
  193. query.eq(TEquipment::getClientId,clientId);
  194. List<TEquipment> equipmentList = equipmentService.list(query);
  195. if(equipmentList.size()>0){
  196. TEquipment equipment = equipmentList.get(0);
  197. if(equipment.getTimeRuleId()!=null){
  198. TTimeRule timeRule = timeRuleService.getById(equipment.getTimeRuleId());
  199. if (timeRule == null) {
  200. // return JsonMessage.error("该规则不存在");
  201. return "该规则不存在";
  202. }
  203. if(StringUtils.isEmpty(timeRule.getType())||timeRule.getType().equals("0")){
  204. //旧规则
  205. String rule = null;
  206. List<TimeRuleVo> timeRuleVos = JSON.parseArray(timeRule.getRule(), TimeRuleVo.class);
  207. for(TimeRuleVo timeRuleVo:timeRuleVos){
  208. List<String> ads = timeRuleVo.getAd();
  209. rule = ads.toString();
  210. break;
  211. }
  212. return rule;
  213. }else {
  214. //新规则
  215. String rule = timeRule.getRule();
  216. return rule;
  217. }
  218. }
  219. }
  220. return "该规则不存在";
  221. }
  222. /**
  223. * 设备下载广告失败则更新设备的推送广告时间
  224. *
  225. * @return
  226. */
  227. @PostMapping(value = "/pushTimeUpdate.htm", produces = "text/html;charset=utf-8")
  228. @ResponseBody
  229. public String updatePushTimeUpdate(String clientId, String pushUpdateTime) {
  230. LambdaQueryWrapper<TEquipment> query = Wrappers.lambdaQuery();
  231. query.eq(TEquipment::getClientId,clientId);
  232. List<TEquipment> equipmentList = equipmentService.list(query);
  233. TEquipment equipment = equipmentList.get(0);
  234. if (equipment == null) {
  235. // return JsonMessage.error("该设备不存在");
  236. return "该设备不存在";
  237. }
  238. Date date = null;
  239. try {
  240. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  241. date = format.parse(pushUpdateTime);
  242. equipment.setPushUpdateTime(date);
  243. } catch (ParseException e) {
  244. e.printStackTrace();
  245. // return JsonMessage.error("修改失败");
  246. return "修改失败";
  247. }
  248. equipmentService.updateById(equipment);
  249. // return JsonMessage.success("修改成功");
  250. return "修改成功";
  251. }
  252. /**
  253. *
  254. *
  255. * @param
  256. * @return
  257. */
  258. @GetMapping(value = "/checkRedis.htm", produces = "application/json;charset=UTF-8")
  259. @ResponseBody
  260. public JsonMessage checkRedis(String str) {
  261. Boolean hasKey = redisTemplate.hasKey(str);
  262. return JsonMessage.success(hasKey);
  263. }
  264. /**
  265. *
  266. *
  267. * @param
  268. * @return
  269. */
  270. @GetMapping(value = "/checkRedis2.htm", produces = "application/json;charset=UTF-8")
  271. @ResponseBody
  272. public JsonMessage checkRedis2(String key,String hashkey) {
  273. Object o = redisTemplate.boundHashOps(key).get(hashkey);
  274. return JsonMessage.success(o);
  275. }
  276. }