RemoteProductController.java 3.53 KB
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;
    }

}