RemoteProductController.java 3.93 KB
package com.glsx.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.glsx.api.exception.BusinessException;
import com.glsx.api.exception.ResultEnum;
import com.glsx.api.pojo.request.ProductReq;
import com.glsx.api.pojo.request.UserRelationReq;
import com.glsx.api.pojo.response.ProductResp;
import com.glsx.api.pojo.response.UserRelationResp;
import com.glsx.api.remote.ProductFeignClient;
import com.glsx.api.remote.UserFeignClient;
import com.glsx.plat.common.utils.HttpUtils;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.web.R;
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("/remoteDevice")
public class RemoteProductController {

    @Value("${device_remote_url}")
    private String remoteUrl;

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private ProductFeignClient productFeignClient;

  /**
     * 用户获取远程产品列表数据
     *
     * @return 结果
     */
    @RequestMapping(value = "/getList", method = {RequestMethod.POST})
    public R getList(@RequestBody UserRelationReq req) {
        Long id = req.getId();
        if(id == null || id <= 0L){
            throw BusinessException.create(ResultEnum.PARAM_ERROR);
        }
        UserRelationResp user = userFeignClient.getSingle(req);
        if(user == null){
            throw BusinessException.create(ResultEnum.USER_NOT_EXIST);
        }
        return R.ok().data(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){
        ProductReq req = new ProductReq();
        req.setPlatformProductId(productId);
        List<ProductResp> list = productFeignClient.getListByCondition(req);
        if(!list.isEmpty()){
            return  list.get(0).getIconUrl();
        }
        return null;
    }

}