SmartUserDeviceServiceImpl.java
4.46 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
package com.glsx.smart.service.impl;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.common.utils.HttpUtils;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.smart.dao.entity.SmartUserChannel;
import com.glsx.smart.dao.entity.SmartUserDevice;
import com.glsx.smart.dao.mapper.SmartUserChannelMapper;
import com.glsx.smart.dao.mapper.SmartUserDeviceMapper;
import com.glsx.smart.exception.BusinessException;
import com.glsx.smart.exception.ResultEnum;
import com.glsx.smart.service.BaseService;
import com.glsx.smart.service.SmartUserDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
* <p>Description: 用户设备业务
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-04-23
*/
@Slf4j
@Service
public class SmartUserDeviceServiceImpl extends BaseService implements SmartUserDeviceService {
@Value("${device_remote_url}")
private String remoteUrl;
@Value("${user_bind_key}")
private String userBindKeyApi;
@Value("${user_send_command}")
private String userSendCommandApi;
@Resource
private SmartUserDeviceMapper userDeviceMapper;
@Resource
private SmartUserChannelMapper userChannelMapper;
@Override
public List<SmartUserDevice> getList(SmartUserDevice req) {
return userDeviceMapper.getList(req);
}
@Override
public SmartUserDevice getByDeviceId(SmartUserDevice req) {
List<SmartUserDevice> userDeviceList = userDeviceMapper.select(req);
if (userDeviceList.isEmpty()) {
throw BusinessException.create(ResultEnum.DEVICE_NOT_FOUND);
}
return userDeviceList.get(0);
}
@Override
@Transactional
public int save(SmartUserDevice req) {
int rows;
Date now = new Date();
SmartUserDevice record = new SmartUserDevice();
record.setDeviceId(req.getDeviceId());
record.setChannel(req.getChannel());
SmartUserDevice savedUserDevice = userDeviceMapper.selectOne(record);
if (savedUserDevice == null) {
if (StringUtils.isBlank(req.getUid())) {
throw BusinessException.create(ResultEnum.PARAM_ERROR);
}
SmartUserChannel ucRecord = new SmartUserChannel();
ucRecord.setThirdId(req.getUid());
SmartUserChannel savedUserChannel = userChannelMapper.selectOne(ucRecord);
if (savedUserChannel == null) {
throw BusinessException.create(ResultEnum.USER_NOT_FOUND);
}
req.setUserId(savedUserChannel.getUserId());
req.setCreateTime(now);
req.setUpdateTime(now);
rows = userDeviceMapper.insertSelective(req);
} else {
if (StringUtils.isBlank(req.getDeviceName())) {
throw BusinessException.create(ResultEnum.PARAM_ERROR);
}
savedUserDevice.setDeviceName(req.getDeviceName());
savedUserDevice.setUpdateTime(now);
rows = userDeviceMapper.updateByPrimaryKeySelective(savedUserDevice);
}
return rows;
}
@Override
public int deleteDevice(SmartUserDevice req) {
int rows;
rows = userDeviceMapper.deleteDevice(req);
return rows;
}
@Override
public String getUserBindKey(String username) {
JSONObject reqObj = new JSONObject();
reqObj.put("username", username);
String url = remoteUrl + userBindKeyApi;
log.info("$$$$ getUserBindKey url = {}, req = {}", url, reqObj.toJSONString());
String respJson = HttpUtils.get(url, reqObj);
log.info("$$$$ getUserBindKey resp = {}", respJson);
JSONObject respObj = JSON.parseObject(respJson);
String bindKey = "200".equals(respObj.getString("code"))
? respObj.getJSONObject("message").getString("bindKey") : null;
return bindKey;
}
@Override
public String sendCommand(JSONObject req) {
String url = remoteUrl + userSendCommandApi;
log.info("$$$$ sendCommand url = {}, param: {}", url, req.toJSONString());
String respJson = HttpUtils.post(url, req.toJSONString());
log.info("$$$$ sendCommand resp = {}", respJson);
JSONObject respObj = JSON.parseObject(respJson);
return respObj.getString("result");
}
}