SmartUserDeviceServiceImpl.java
3 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
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.SmartUserDevice;
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;
@Resource
private SmartUserDeviceMapper userDeviceMapper;
@Override
public List<SmartUserDevice> getList(SmartUserDevice userDevice) {
return userDeviceMapper.getList(userDevice);
}
@Override
@Transactional
public int save(SmartUserDevice userDevice) {
int rows;
Date now = new Date();
if (userDevice.getId() == null) {
userDevice.setCreateTime(now);
userDevice.setUpdateTime(now);
rows = userDeviceMapper.insertSelective(userDevice);
} else {
if (StringUtils.isBlank(userDevice.getDeviceName())) {
throw BusinessException.create(ResultEnum.PARAM_ERROR);
}
SmartUserDevice record = userDeviceMapper.selectByPrimaryKey(userDevice.getId());
if (record == null) {
throw BusinessException.create(ResultEnum.ID_NOT_FOUND);
}
record.setDeviceName(userDevice.getDeviceName());
record.setUpdateTime(now);
rows = userDeviceMapper.updateByPrimaryKeySelective(record);
}
return rows;
}
@Override
public int deleteDevice(SmartUserDevice userDevice) {
int rows;
rows = userDeviceMapper.deleteDevice(userDevice);
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, JSON.toJSONString(reqObj));
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;
}
}