package com.szwl.controller; import com.gexin.fastjson.JSON; import com.gexin.fastjson.JSONObject; import com.szwl.model.bo.JsonMessage; import com.szwl.model.entity.TEquipment; import com.szwl.model.utils.PushUtils; import com.szwl.service.TAdminService; import com.szwl.service.TEquipmentService; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang.StringUtils; import org.springframework.amqp.core.MessageProperties; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.LinkedHashMap; import java.util.Map; @RestController @RequestMapping("/Log") public class LogController { @Autowired TEquipmentService tEquipmentService; @Autowired TAdminService tAdminService; @Autowired RabbitTemplate rabbitTemplate; @ApiOperation(value = "上传日志") @PostMapping("/uploadLog") public JsonMessage uploadLog(@RequestBody Map body) { String deviceId = String.valueOf(body.get("deviceId")); Object logDate = body.get("logDate"); LinkedHashMap map = new LinkedHashMap<>(); map.put("logDate", logDate); String jsonString = JSON.toJSONString(map); JSONObject jsonObject = JSON.parseObject(jsonString); String day = jsonObject.getJSONObject("logDate").getString("_value"); if(day.length()!=8){ return JsonMessage.success("日期格式错误"); } TEquipment equipment = tEquipmentService.getById(deviceId); if (equipment == null) { return JsonMessage.error("该设备不存在"); } String kind = day+"-"+deviceId; String channel = equipment.getChannel(); String equimentType = equipment.getEquimentType(); if(StringUtils.isEmpty(channel)||channel.equals("1")||StringUtils.isEmpty(equimentType)){ //用个推 PushUtils.push(equipment.getGtClientId(), "", "", PushUtils.buildJson("log", kind).toString()); } if(StringUtils.isNotEmpty(channel)&&channel.equals("2")&&StringUtils.isNotEmpty(equimentType)){ //用Mq //1 创建消息 MessageProperties messageProperties = new MessageProperties(); messageProperties.setContentType("application/json"); org.springframework.amqp.core.Message message = new org.springframework.amqp.core.Message(PushUtils.buildJson("log", kind).toString().getBytes(), messageProperties); rabbitTemplate.send(equimentType, deviceId, message); } return JsonMessage.success("发送成功"); } }