RemoteProductController.java
5.79 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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.constant.Constants;
import com.glsx.smart.dao.entity.SmartProduct;
import com.glsx.smart.dao.entity.SmartProductType;
import com.glsx.smart.dao.entity.SmartUserRelation;
import com.glsx.smart.service.SmartProductService;
import com.glsx.smart.service.SmartProductTypeService;
import com.glsx.smart.service.SmartUserRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.*;
import java.util.ArrayList;
import java.util.List;
/**
* 远程产品服务controller
* 暂时只有涂鸦
*/
@Slf4j
@RestController
@RequestMapping("/remoteProduct")
@Api(value = "设备管理", tags = {"设备管理"})
public class RemoteProductController {
@Value("${device_remote_url}")
private String remoteUrl;
@Autowired
private SmartProductService productService;
@Autowired
private SmartProductTypeService productTypeService;
@Autowired
private SmartUserRelationService userRelationService;
@Autowired
private SmartProductTypeService smartProductTypeService;
/**
* 用户获取远程产品列表数据
*
* @return 结果
*/
@ResponseBody
@RequestMapping(value = "/getList", method = {RequestMethod.GET, RequestMethod.POST})
public JSONArray getList(@RequestBody SmartUserRelation request) {
log.info("用户参数:{}", request);
// request.setUserId(91712228L);
SmartUserRelation user = userRelationService.getSingle(request);
if(user == null){
return null;
}
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 array = json.getJSONArray("message");
if(array == null || array.size() <= 0){
return array;
}
return this.remoteDataHandle(array);
}
}
} catch (Exception e) {
log.error("获取用户设备列表数据失败:{}", e);
}
return null;
}
private JSONArray remoteDataHandle(JSONArray array){
int len = array.size();
List<String> prodIdList = new ArrayList<>(len);
for (int i = 0; i < len; i++) {
prodIdList.add(array.getJSONObject(i).getString("product_id"));
}
List<SmartProduct> productList = productService.getByPlatProductIdList(prodIdList);
List<Long> typeIdList = new ArrayList<>(productList.size());
for (SmartProduct product : productList) {
typeIdList.add(product.getTypeId());
}
List<SmartProductType> productTypeList = new ArrayList<>();
if(!typeIdList.isEmpty()){
productTypeList = productTypeService.getByIdList(typeIdList);
}
JSONArray list = new JSONArray();
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("id"));
val.put("productName", object.getString("product_name"));
SmartProduct product = this.compareProductId(productList, productId);
if(product != null){
val.put("iconUrl", product.getIconUrl());
val.put("flag", this.compareTypeId(productTypeList, product.getTypeId()));
}
list.add(val);
}
return list;
}
/**
* 比较获取产品
* @param list
* @param productId
* @return
*/
private SmartProduct compareProductId(List<SmartProduct> list, String productId){
int len = list.size();
for (int i = 0; i < len; i++) {
SmartProduct product = list.get(i);
if(productId.equals(product.getPlatformProductId())){
return product;
}
}
return null;
}
/**
* 比较是否产品类型为万能遥控器
* @param list
* @param id
* @return
*/
private boolean compareTypeId(List<SmartProductType> list, Long id){
int len = list.size();
for (int i = 0; i < len; i++) {
SmartProductType productType = list.get(i);
if(id == productType.getId()
&& productType.getTypeName().contains(Constants.DEFAULT_DEVICE)){
return true;
}
}
return false;
}
/**
* 查询红外设备下的遥控器
*
* @return 结果
*/
@ResponseBody
@RequestMapping(value = "/findRemotes", method = {RequestMethod.GET, RequestMethod.POST})
@ApiOperation(value = "查询红外设备下的遥控器")
public String findRemotes(@RequestParam String infrared_id) {
return smartProductTypeService.findRemotesById(infrared_id);
}
}