RemoteProductController.java
3.53 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
package com.glsx.smart.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
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.SmartProduct;
import com.glsx.smart.dao.entity.SmartUserRelation;
import com.glsx.smart.service.SmartProductService;
import com.glsx.smart.service.SmartUserRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
*
* 远程产品服务controller
* 暂时只有涂鸦
*
*/
@Slf4j
@RestController
@RequestMapping("/remoteProduct")
public class RemoteProductController {
@Value("${device_remote_url}")
private String remoteUrl;
@Autowired
private SmartProductService productService;
@Autowired
private SmartUserRelationService userRelationService;
/**
* 用户获取远程产品列表数据
*
* @return 结果
*/
@RequestMapping(value = "/getList", method = {RequestMethod.GET, RequestMethod.POST})
public JSONArray getList(@RequestBody SmartUserRelation request) {
SmartUserRelation user = userRelationService.getSingle(request);
return this.getRemoteDevice(user.getThirdId());
}
private JSONArray getRemoteDevice(String thirdId){
StringBuffer url = new StringBuffer(remoteUrl);
url.append("/device/").append(thirdId).append("/devices");
log.info("请求远程设备列表接口地址:{}", url.toString());
try{
String result = HttpUtils.get(url.toString());
log.info("获取用户设备列表数据返回结果:{}", result);
if (StringUtils.isNotEmpty(result)) {
JSONObject json = JSON.parseObject(result);
if (json.getInteger("code").equals(200)) {
JSONArray list = new JSONArray();
JSONArray array = json.getJSONArray("message");
int len = array.size();
for (int i = 0; i < len; i++) {
JSONObject object = array.getJSONObject(i);
JSONObject val = new JSONObject();
String productId = object.getString("product_id");
val.put("productId", productId);
val.put("uuid", object.getString("uuid"));
val.put("productName", object.getString("product_name"));
val.put("iconUrl", this.getProductIcon(productId));
list.add(val);
}
return list;
}
}
}catch (Exception e){
log.error("获取用户设备列表数据失败:{}", e);
}
return null;
}
/**
* 通过平台产品id获取产品图片
* @param productId
* @return
*/
private String getProductIcon(String productId){
SmartProduct req = new SmartProduct();
req.setPlatformProductId(productId);
List<SmartProduct> list = productService.getListByCondition(req);
if(!list.isEmpty()){
return list.get(0).getIconUrl();
}
return null;
}
}