package com.szwl.controller; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.gexin.fastjson.JSON; import com.szwl.model.bo.JsonMessage; import com.szwl.model.dto.AdVo; import com.szwl.model.dto.TimeRuleVo; import com.szwl.model.entity.TAd; import com.szwl.model.entity.TEquipment; import com.szwl.model.entity.TTimeRule; import com.szwl.service.TAdService; import com.szwl.service.TEquipmentService; import com.szwl.service.TTimeRuleService; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; @Controller("ADIndexController") @RequestMapping("/api/app_ADIndex/ADIndex") public class ADIndexController { @Autowired TAdService aDService; @Autowired private TTimeRuleService timeRuleService; @Autowired private TEquipmentService equipmentService; @Resource private RedisTemplate redisTemplate; /** * 根据广告id获取广告 */ @GetMapping(value = "/getAdById.htm", produces = "text/html;charset=utf-8") @ResponseBody public String getAdById(String id) { if(StringUtils.isEmpty(id)){ // return JsonMessage.error("id为null"); return "id为null"; } TAd ad = aDService.getById(id); ArrayList list = new ArrayList<>(); AdVo adVo = new AdVo(); adVo.setAdType(String.valueOf(ad.getAdType())); adVo.setLocationType(String.valueOf(ad.getLocationType())); adVo.setName(ad.getName()); adVo.setOrder(ad.getOrders()); adVo.setUrl(ad.getUrl()); adVo.setEquipmentType(ad.getEquipmentType()); adVo.setCreateDate(ad.getCreateDate()); adVo.setId(ad.getId()); adVo.setMediaPreview(ad.getMediaPreview()); adVo.setDuration(ad.getDuration()); adVo.setScreenType(ad.getScreenType()); list.add(adVo); JSONObject jsonObject = new JSONObject(); jsonObject.put("code", 0); jsonObject.put("data", JSONObject.toJSON(list).toString()); jsonObject.put("errmsg", ""); return jsonObject.toJSONString(); } /** * 设备获取默认广告 */ @GetMapping(value = "/getAd.htm", produces = "text/html;charset=utf-8") @ResponseBody public String getAdNew(String clientId,String equimentType) { LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TEquipment::getClientId,clientId); List equipmentList = equipmentService.list(query); TEquipment equipment = equipmentList.get(0); String adminId = String.valueOf(equipment.getAdminId()); //1,获取 LambdaQueryWrapper query1 = Wrappers.lambdaQuery(); query1.eq(TAd::getType,"0"); query1.eq(TAd::getEquipmentType,equimentType); List ads = aDService.list(query1); ArrayList list = new ArrayList<>(); for (TAd ad : ads) { if(adminId.equals(ad.getAdminId())||ad.getAdminId()==null){ AdVo adVo = new AdVo(); adVo.setAdType(String.valueOf(ad.getAdType())); adVo.setLocationType(String.valueOf(ad.getLocationType())); adVo.setName(ad.getName()); adVo.setOrder(ad.getOrders()); adVo.setUrl(ad.getUrl()); adVo.setCreateDate(ad.getCreateDate()); adVo.setId(ad.getId()); adVo.setMediaPreview(ad.getMediaPreview()); adVo.setDuration(ad.getDuration()); adVo.setScreenType(ad.getScreenType()); list.add(adVo); } } JSONObject jsonObject = new JSONObject(); jsonObject.put("code", 0); jsonObject.put("data", JSONObject.toJSON(list).toString()); jsonObject.put("errmsg", ""); return jsonObject.toJSONString(); } /** * 设备获取广告 */ @GetMapping(value = "/getAdByEquipment.htm", produces = "text/html;charset=utf-8") @ResponseBody public String getAdByEquipment(String clientId,String equimentType) { LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TEquipment::getClientId,clientId); List equipmentList = equipmentService.list(query); TEquipment equipment = equipmentList.get(0); String adminId = String.valueOf(equipment.getAdminId()); //1,获取 LambdaQueryWrapper query1 = Wrappers.lambdaQuery(); query1.eq(TAd::getType,"0"); query1.eq(TAd::getEquipmentType,equimentType); query1.eq(TAd::getAdminId,adminId); List ads = aDService.list(query1); ArrayList list = new ArrayList<>(); for (TAd ad : ads) { if(adminId.equals(ad.getAdminId())||ad.getAdminId()==null){ AdVo adVo = new AdVo(); adVo.setAdType(String.valueOf(ad.getAdType())); adVo.setLocationType(String.valueOf(ad.getLocationType())); adVo.setName(ad.getName()); adVo.setOrder(ad.getOrders()); adVo.setUrl(ad.getUrl()); adVo.setCreateDate(ad.getCreateDate()); adVo.setId(ad.getId()); adVo.setMediaPreview(ad.getMediaPreview()); adVo.setDuration(ad.getDuration()); adVo.setScreenType(ad.getScreenType()); list.add(adVo); } } JSONObject jsonObject = new JSONObject(); jsonObject.put("code", 0); jsonObject.put("data", JSONObject.toJSON(list).toString()); jsonObject.put("errmsg", ""); return jsonObject.toJSONString(); } /** * 获取时间规则 * * @param id * @return */ @GetMapping(value = "/getTimeRule.htm", produces = "text/html;charset=utf-8") @ResponseBody public String getTimeRule(Long id) { TTimeRule timeRule = timeRuleService.getById(id); if (timeRule == null) { // return JsonMessage.error("该规则不存在"); return "该规则不存在"; } if(StringUtils.isEmpty(timeRule.getType())||timeRule.getType().equals("0")){ //旧规则 String rule = null; List timeRuleVos = JSON.parseArray(timeRule.getRule(), TimeRuleVo.class); for(TimeRuleVo timeRuleVo:timeRuleVos){ List ads = timeRuleVo.getAd(); rule = ads.toString(); break; } return rule; }else { //新规则 String rule = timeRule.getRule(); return rule; } } /** * 设备下载广告失败则更新设备的推送广告时间 * * @return */ @PostMapping(value = "/pushTimeUpdate.htm", produces = "text/html;charset=utf-8") @ResponseBody public String updatePushTimeUpdate(String clientId, String pushUpdateTime) { LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TEquipment::getClientId,clientId); List equipmentList = equipmentService.list(query); TEquipment equipment = equipmentList.get(0); if (equipment == null) { // return JsonMessage.error("该设备不存在"); return "该设备不存在"; } Date date = null; try { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); date = format.parse(pushUpdateTime); equipment.setPushUpdateTime(date); } catch (ParseException e) { e.printStackTrace(); // return JsonMessage.error("修改失败"); return "修改失败"; } equipmentService.updateById(equipment); // return JsonMessage.success("修改成功"); return "修改成功"; } /** * * * @param * @return */ @GetMapping(value = "/checkRedis.htm", produces = "application/json;charset=UTF-8") @ResponseBody public JsonMessage checkRedis(String str) { Boolean hasKey = redisTemplate.hasKey(str); return JsonMessage.success(hasKey); } /** * * * @param * @return */ @GetMapping(value = "/checkRedis2.htm", produces = "application/json;charset=UTF-8") @ResponseBody public JsonMessage checkRedis2(String key,String hashkey) { Object o = redisTemplate.boundHashOps(key).get(hashkey); return JsonMessage.success(o); } }