HomeController.java 4.95 KB
package cn.com.glsx.admin.modules.controller;

import cn.com.glsx.admin.modules.model.ActiveDeviceVo;
import cn.com.glsx.admin.modules.model.DeviceBindVo;
import cn.com.glsx.admin.modules.service.HomeService;
import cn.com.glsx.auth.utils.ShieldContextHolder;
import com.glsx.plat.core.web.R;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页统计展示 ontroller
 *
 * @author yangll
 */
@Slf4j
@RestController
@RequestMapping(value = "/home")
@Api(value = "首页统计展示", tags = {"首页统计展示"})
public class HomeController {

    //查询对象+条件 做一个 用户ID ==> 上次查询时间
    private static Map<String, Long> DEVICE_COUNT_LAST_REFRESH_TIME = new HashMap<>();

    //查询对象+条件 做一个 用户ID ==> 总数
    private static Map<String, Map<String, Object>> DEVICE_COUNT_CACHE = new HashMap<>();

    @Autowired
    private HomeService homeService;

    @GetMapping("/authuserorg")
    @ApiOperation(value = "用户组织鉴权")
    public R<Map<String, Boolean>> authUserOrg() {
        Map<String, Boolean> authUserOrgMap = homeService.authUserOrg();
        return R.ok().data(authUserOrgMap);
    }

    @GetMapping("/getdevicenumber")
    @ApiOperation(value = "查询首页统计数据")
    public R<Map<String, Object>> getDeviceNumber() {
        String uniqueValue = hashQueryBean("DeviceNumber");
        Map<String, Object> deviceNumberMap;
        if (needRefreshTotal(uniqueValue)) {
            deviceNumberMap = homeService.getDeviceNumber();
            DEVICE_COUNT_LAST_REFRESH_TIME.put(uniqueValue, System.currentTimeMillis());
            DEVICE_COUNT_CACHE.put(uniqueValue, deviceNumberMap);
        } else {
            //从缓存获取数据
            log.info("DeviceNumber===从缓存中获取首页展示数据===" + ShieldContextHolder.getUserId());
            deviceNumberMap = DEVICE_COUNT_CACHE.get(uniqueValue);
        }

        return R.ok().data(deviceNumberMap);
    }

    @GetMapping("/getalarmnumber")
    @ApiOperation(value = "查询警情统计数据")
    public R<Map<String, Object>> getAlarmNumber() {
        String uniqueValue = hashQueryBean("AlarmNumber");
        Map<String, Object> alarmNumberMap;
        if (needRefreshTotal(uniqueValue)) {
            alarmNumberMap = homeService.getAlarmNumber();
            DEVICE_COUNT_LAST_REFRESH_TIME.put(uniqueValue, System.currentTimeMillis());
            DEVICE_COUNT_CACHE.put(uniqueValue, alarmNumberMap);
        } else {
            //从缓存获取数据
            log.info("AlarmNumber===从缓存中获取首页展示数据===" + ShieldContextHolder.getUserId());
            alarmNumberMap = DEVICE_COUNT_CACHE.get(uniqueValue);
        }

        return R.ok().data(alarmNumberMap);
    }

    /**
     * 查询设备激活趋势
     *
     * @param period
     * @return
     */
    @GetMapping("getactivedeviceinfo")
    @ApiOperation(value = "查询设备激活趋势")
    public R<List<ActiveDeviceVo>> getActiveDeviceInfo(@RequestParam Integer period) {
        List<ActiveDeviceVo> activeDeviceList = Lists.newArrayList();
        try {
            activeDeviceList = homeService.getActiveDeviceInfo(period);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().data(activeDeviceList);
    }

    @GetMapping("getnewbinddeviceinfo")
    @ApiOperation("设备绑定趋势")
    public R<List<DeviceBindVo>> getNewBindDeviceInfo() {
        List<DeviceBindVo> deviceBindList = Lists.newArrayList();
        try {
            deviceBindList = homeService.getNewBindDeviceInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().data(deviceBindList);
    }

    /**
     * 总数查询刷新间隔,判断是否需要重新刷新
     *
     * @param uniqueValue 唯一标识字符
     * @return 是否需要刷新
     */
    private boolean needRefreshTotal(String uniqueValue) {
        Long lastRefreshTime = DEVICE_COUNT_LAST_REFRESH_TIME.get(uniqueValue);
//        return lastRefreshTime == null || (Math.abs(lastRefreshTime - System.currentTimeMillis()) >= 120000);

        return true;
    }

    /**
     * 加密生成唯一标识符
     *
     * @param methodType 方法类型
     * @return 加密后的字符串
     */
    private String hashQueryBean(String methodType) {
        return DigestUtils.md5Hex((methodType + ShieldContextHolder.getUserId()).getBytes(StandardCharsets.UTF_8));
    }
}