Newer
Older
br-data-forwarding / src / main / java / com / casic / api / DeviceInfoController.java
package com.casic.api;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.casic.dao.model.BusDevice;
import com.casic.dao.service.IBusDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/device")
public class DeviceInfoController {

    @Resource
    private IBusDeviceService deviceService;

    @RequestMapping("aepExt")
    public Object aepExt(@RequestBody List<Map<String, Object>> extInfoList) {
        log.info(JSONObject.toJSONString(extInfoList));
        Map<String, Object> retObj = new HashMap<>();
        List<Map<String, Object>> retSuccList = new ArrayList<>();
        int idx = 1;
        for (Map<String, Object> devExt : extInfoList) {
            Map<String, Object> retItem = new HashMap<>();
            retItem.put("index", idx);

            if (devExt.containsKey("DEVCODE")) {
                String devCode = StrUtil.toString(devExt.get("DEVCODE"));
                BusDevice device = deviceService.getDeviceByCode(devCode);
                if (ObjectUtil.isNotNull(device)) {
                    device.setNbAppKey(StrUtil.toString(devExt.getOrDefault("NB_APP_KEY", "")));
                    device.setNbAppSecret(StrUtil.toString(devExt.getOrDefault("NB_APP_SECRET", "")));
                    device.setMasterApiKey(StrUtil.toString(devExt.getOrDefault("MASTER_API_KEY", "")));
                    device.setNbDeviceId(StrUtil.toString(devExt.getOrDefault("NB_DEVICE_ID", "")));
                    device.setNbProductId(StrUtil.toString(devExt.getOrDefault("NB_PRODUCT_ID", "")));

                    boolean succ = deviceService.updateById(device);
                    retItem.put("success", succ);
                    if (!succ) {
                        retItem.put("msg", "updateById数据库操作失败");
                    }
                } else {
                    retItem.put("success", false);
                    retItem.put("msg", "devCode[" + devCode + "]的设备未找到");
                }
            } else {
                retItem.put("success", false);
                retItem.put("msg", "devCode必传,不能为空");
            }
            retSuccList.add(retItem);
            idx++;
        }
        retObj.put("data", retSuccList);
        retObj.put("code", 200);
        return retObj;
    }
}