UserKafkaController.java
4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.glsx.smart.controller;
import com.glsx.smart.dao.entity.SmartScene;
import com.glsx.smart.dao.entity.SmartUserRelation;
import com.glsx.smart.service.SmartSceneDataService;
import com.glsx.smart.service.SmartSceneService;
import com.glsx.smart.service.SmartUserRelationService;
import com.glsx.smart.service.UserKafkaService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @author think.wang
* @version 1.0.0
* @Title UserKafkaController
* @Package com.glsx.smart.controller
* @Description 嘀嘀虎用户数据推送处理controller
* @date 2021/4/10
*/
@Slf4j
@RestController
@RequestMapping("/userKafka")
public class UserKafkaController {
@Autowired
private UserKafkaService userKafkaService;
@Autowired
private SmartSceneService sceneService;
@Autowired
private SmartUserRelationService userRelationService;
@Autowired
private SmartSceneDataService sceneDataService;
/**
* 更新用户场景
*
* @param userId
* @param phone
* @param lngAndLat
* @return
*/
@ResponseBody
@RequestMapping(value = "/updateUserAddress", method = {RequestMethod.POST, RequestMethod.GET})
public boolean updateUserAddress(@RequestParam("userId") Long userId, @RequestParam("phone") String phone,
@RequestParam("lngAndLat") String lngAndLat){
SmartUserRelation relation = getUserRelation(userId, phone);
if(relation == null){
return false;
}
SmartScene record = new SmartScene();
record.setUserId(relation.getId());
List<SmartScene> sceneList = sceneService.getList(record);
if(sceneList.isEmpty()){
return false;
}
int len = sceneList.size();
for (int i = 0; i < len; i++) {
SmartScene scene = sceneList.get(i);
List<String> deviceList = userKafkaService.getSendDeviceList(
sceneDataService.getBySceneId(scene.getId()));
if(CollectionUtils.isNotEmpty(deviceList)){
for (String deviceId : deviceList) {
//更新设备家位置
userKafkaService.pushUserDeviceData(deviceId, lngAndLat, 2);
}
}
}
return true;
}
@ResponseBody
@RequestMapping(value = "/logoutUserScene", method = {RequestMethod.POST, RequestMethod.GET})
public boolean logoutUserScene(@RequestParam("userId") Long userId, @RequestParam("phone") String phone) {
SmartUserRelation relation = getUserRelation(userId, phone);
if (relation != null) {
SmartScene record = new SmartScene();
record.setUserId(relation.getId());
List<SmartScene> sceneList = sceneService.getList(record);
if (CollectionUtils.isNotEmpty(sceneList)) {
int len = sceneList.size();
for (int j = 0; j < len; j++) {
SmartScene scene = sceneList.get(j);
List<String> deviceList = userKafkaService.getSendDeviceList(
sceneDataService.getBySceneId(scene.getId()));
if (CollectionUtils.isNotEmpty(deviceList)) {
for (String deviceId : deviceList) {
userKafkaService.pushUserDeviceData(deviceId, null, 3); //删除设备信息
}
}
}
//todo 删除用户场景数据
}
}
return true;
}
/**
* 获取车家智能用户关系
* @param userId
* @param phone
* @return
*/
private SmartUserRelation getUserRelation(Long userId, String phone){
SmartUserRelation record = new SmartUserRelation();
record.setUserId(userId);
record.setPhone(phone);
record.setChannel((byte)1);
return userRelationService.getSingle(record);
}
}