UserRelationController.java
4.65 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package cn.com.glsx.smart.modules.controller;
import cn.com.glsx.smart.common.constant.Constants;
import cn.com.glsx.smart.modules.entity.SmartUserChannel;
import cn.com.glsx.smart.modules.entity.SmartUserRelation;
import cn.com.glsx.smart.modules.service.SmartUserRelationService;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.web.R;
import com.glsx.plat.redis.utils.RedisUtils;
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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Title: UserRelationController.java
* @Description:用户关系控制类
* @author think.wang
* @version V1.0
* @Company: Didihu.com.cn
* @Copyright Copyright (c) 2017
*/
@Slf4j
@RestController
@RequestMapping("/user")
public class UserRelationController {
@Autowired
private RedisUtils redisUtils;
@Autowired
private SmartUserRelationService userRelationService;
@ResponseBody
@RequestMapping(value = "/getSingle", method = {RequestMethod.POST})
public SmartUserRelation getSingle(@RequestBody SmartUserRelation userRelation){
return userRelationService.getSingle(userRelation);
}
@ResponseBody
@RequestMapping(value = "/add", method = {RequestMethod.POST})
public Long add(@RequestBody SmartUserRelation userRelation){
log.info("/user/add保存数据参数:{}", JSON.toJSONString(userRelation));
return userRelationService.insertSelective(userRelation);
}
@ResponseBody
@RequestMapping(value = "/update", method = {RequestMethod.POST})
public Integer update(@RequestBody SmartUserRelation userRelation){
log.info("/user/update保存数据参数:{}", JSON.toJSONString(userRelation));
return userRelationService.updateByPrimaryKeySelective(userRelation);
}
@ResponseBody
@RequestMapping(value = "/getAqaraToken", method = {RequestMethod.POST})
public String getAqaraToken(@RequestBody SmartUserRelation userRelation){
log.info("/user/getAqaraToken参数:{}", JSON.toJSONString(userRelation));
if(StringUtils.isEmpty(userRelation.getPhone())){
return null;
}
Map<String, String> map = userRelationService.syncAqaraUserCore(userRelation.getPhone());
return map != null ? map.get("accessToken") : null;
}
@ResponseBody
@RequestMapping(value = "/login", method = {RequestMethod.POST})
public R login(@RequestBody SmartUserRelation userRelation){
log.info("/user/login保存数据参数:{}", JSON.toJSONString(userRelation));
String key = userRelation.getPhone();
if(redisUtils.hasKey(key)){
log.info("用户手机号 {} 重复请求", key);
return R.ok();
}
redisUtils.setex(key, key, Constants.REDIS_CACHE_TIME);
//获取用户数据
SmartUserRelation request = new SmartUserRelation();
request.setPhone(userRelation.getPhone());
SmartUserRelation user = userRelationService.getSingle(request);
//用户数据封装
request.setUserId(userRelation.getUserId());
request.setSource(userRelation.getSource());
request.setName(userRelation.getName());
if(user != null){
request.setId(user.getId());
userRelationService.updateByPrimaryKeySelective(request);
return R.ok().data(this.updateChannelUser(user));
}
String password = MD5Utils.md5Hex(request.getPhone(), "utf-8");
request.setPassword(password);
request.setCreateTime(new Date());
Long userId = userRelationService.insertSelective(request);
if(userId > 0L){
userRelationService.syncAqaraUser(request.getPhone(), userId);
String tuYaId = userRelationService.syncTuYaUser(request.getPhone(), password, userId);
return R.ok().data(tuYaId);
}
return R.error();
}
/**
* 用户更新操作
* @param user
* @return
*/
private String updateChannelUser(SmartUserRelation user){
List<SmartUserChannel> list = user.getChannelList();
String tuYaId;
if(CollectionUtils.isEmpty(list)){
userRelationService.syncAqaraUser(user.getPhone(), user.getId());
tuYaId = userRelationService
.syncTuYaUser(user.getPhone(), user.getPassword(), user.getId());
return tuYaId;
} else{
Map<Byte, String> map = new HashMap<>();
list.forEach(index -> map.put(index.getChannel(), index.getThirdId()));
tuYaId = map.get(Constants.USER_CHANNEL_TUYA);
if(tuYaId == null){
tuYaId = userRelationService
.syncTuYaUser(user.getPhone(), user.getPassword(), user.getId());
}
if(map.get(Constants.USER_CHANNEL_AQARA) == null){
userRelationService.syncAqaraUser(user.getPhone(), user.getId());
}
}
return tuYaId;
}
}