jinpengyong
2021-12-24 5c427a35595e2deb2c1a025ff9a4c164d498e01d
行业贡献率:行业列表,传感器列表接口
3 files added
155 ■■■■■ changed files
screen-api/src/main/java/com/moral/api/controller/ProfessionController.java 56 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/ProfessionService.java 13 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/ProfessionServiceImpl.java 86 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/controller/ProfessionController.java
New file
@@ -0,0 +1,56 @@
package com.moral.api.controller;
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.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.moral.api.service.ProfessionService;
import com.moral.constant.ResponseCodeEnum;
import com.moral.constant.ResultMessage;
import com.moral.util.WebUtils;
@Slf4j
@Api(tags = {"行业贡献率模块"})
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/profession")
public class ProfessionController {
    @Autowired
    private ProfessionService professionService;
    @ApiOperation(value = "根据组织id获取所有设备行业列表", notes = "根据组织id获取所有设备行业列表")
    @GetMapping("getProfessionsByOrganizationId")
    public ResultMessage getProfessions(Integer organizationId) {
        if (ObjectUtils.isEmpty(organizationId)) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        Set<Map<String, Object>> response = professionService.getProfessionsByOrganizationId(organizationId);
        return ResultMessage.ok(response);
    }
    @ApiOperation(value = "获取该组织该行业所有设备的因子", notes = "获取该组织该行业所有设备的因子")
    @GetMapping("getSensorByProfessions")
    public ResultMessage getProfessions(HttpServletRequest request) {
        Map<String, Object> params = WebUtils.getParametersStartingWith(request, null);
        if (ObjectUtils.isEmpty(params.get("organizationId")) || ObjectUtils.isEmpty(params.get("professions"))) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        Set<Map<String, Object>> response = professionService.getSensorByProfessionsAndOrganizationId(params);
        return ResultMessage.ok(response);
    }
}
screen-api/src/main/java/com/moral/api/service/ProfessionService.java
New file
@@ -0,0 +1,13 @@
package com.moral.api.service;
import java.util.Map;
import java.util.Set;
public interface ProfessionService {
    //根据组织id获取该组织下所有设备的行业列表
    Set<Map<String, Object>> getProfessionsByOrganizationId(Integer organizationId);
    //根据组织和行业获取传感器列表
    Set<Map<String, Object>> getSensorByProfessionsAndOrganizationId(Map<String, Object> params);
}
screen-api/src/main/java/com/moral/api/service/impl/ProfessionServiceImpl.java
New file
@@ -0,0 +1,86 @@
package com.moral.api.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.Device;
import com.moral.api.entity.Sensor;
import com.moral.api.service.DeviceService;
import com.moral.api.service.ProfessionService;
import com.moral.constant.Constants;
import com.moral.constant.RedisConstants;
/**
 * <p>
 * 行业贡献率相关接口
 * </p>
 *
 * @author moral
 * @since 2021-12-23
 */
@Service
public class ProfessionServiceImpl implements ProfessionService {
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public Set<Map<String, Object>> getProfessionsByOrganizationId(Integer organizationId) {
        //获取该组织下所有设备
        List<Map<String, Object>> devices = deviceService.getDevicesByOrganizationId(organizationId);
        Set<Map<String, Object>> result = new HashSet<>();
        for (Map<String, Object> device : devices) {
            List<Map<String, Object>> professions = (List<Map<String, Object>>) device.get("professions");
            result.addAll(professions);
        }
        return result;
    }
    @Override
    public Set<Map<String, Object>> getSensorByProfessionsAndOrganizationId(Map<String, Object> params) {
        Set<Map<String, Object>> result = new HashSet<>();
        //获取属于该行业所有设备
        Integer orgId = Integer.parseInt(params.get("organizationId").toString());
        List<String> professions = Arrays.asList(params.get("professions").toString().split(","));
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("mac", "profession")
                .eq("organization_id", orgId)
                .eq("is_delete", Constants.NOT_DELETE);
        List<Device> devices = deviceService.list(queryWrapper);
        devices = devices.stream().filter(device -> {
            String[] list = device.getProfession().split(",");
            for (String s : list) {
                if (professions.contains(s)) {
                    return true;
                }
            }
            return false;
        }).collect(Collectors.toList());
        //从redis获取设备详细信息
        for (Device device : devices) {
            device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, device.getMac());
            List<Sensor> sensors = device.getVersion().getSensors();
            for (Sensor sensor : sensors) {
                Map<String, Object> sensorMap = new HashMap<>();
                sensorMap.put("sensorCode", sensor.getCode());
                sensorMap.put("sensorName", sensor.getName());
                result.add(sensorMap);
            }
        }
        return result;
    }
}