package com.ruoyi.demo.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.demo.constant.RedisContant;
import com.ruoyi.demo.entity.TopologicalBuildingWd;
import com.ruoyi.demo.entity.bo.BuildWdCategoryCount;
import com.ruoyi.demo.entity.bo.Histogram;
import com.ruoyi.demo.entity.vo.TopologicalWdAceeptVo;
import com.ruoyi.demo.service.TopologicalBuildWdSerevice;
import com.ruoyi.demo.valida.TopologicalWdListGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/poi/topologicalBuildWd")
public class TopologicalBuildWdController {
    @Autowired
    TopologicalBuildWdSerevice topologicalBuildWdSerevice;

    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("/buildingLevel")
    public R buildingLevel(@Validated(value = {TopologicalWdListGroup.class}) TopologicalWdAceeptVo topologicalWdAceeptVo){
        String md5 = topologicalWdAceeptVo.getHash();
        //2.查看redis中是否存在有缓存
        List<BuildWdCategoryCount> wdCount = (List<BuildWdCategoryCount>) redisTemplate.boundHashOps(RedisContant.TOPOLOGICAL_BUILD_WD_CATEGORY).get(md5);
        if (wdCount != null) {
            return R.ok(wdCount);
        }

        List<BuildWdCategoryCount> list = topologicalBuildWdSerevice.buildingLevel(topologicalWdAceeptVo);

        redisTemplate.boundHashOps(RedisContant.TOPOLOGICAL_BUILD_WD_CATEGORY).put(md5,list);
        redisTemplate.expire(RedisContant.TOPOLOGICAL_BUILD_WD_CATEGORY, RedisContant.TOPOLOGICAL_BUILD_WD_CATEGORY_TIME, TimeUnit.MINUTES); //30分钟

        return R.ok(list);
    }

    @RequestMapping("/list")
    public R list(@Validated(value = {TopologicalWdListGroup.class}) TopologicalWdAceeptVo topologicalWdAceeptVo){
        String md5 = topologicalWdAceeptVo.getHash();
        //2.查看redis中是否存在有缓存
        Page<TopologicalBuildingWd>  wdCount = (Page<TopologicalBuildingWd>)redisTemplate.boundHashOps(RedisContant.TOPOLOGICAL_BUILD_WD_LIST).get(md5);
        if (wdCount != null) {
            return R.ok(wdCount);
        }

        Page<TopologicalBuildingWd> list = topologicalBuildWdSerevice.list(topologicalWdAceeptVo);

        redisTemplate.boundHashOps(RedisContant.TOPOLOGICAL_BUILD_WD_LIST).put(md5,list);
        redisTemplate.expire(RedisContant.TOPOLOGICAL_BUILD_WD_LIST,RedisContant.TOPOLOGICAL_BUILD_WD_LIST_TIME, TimeUnit.MINUTES); //30分钟

        return R.ok(list);
    }

    @RequestMapping("/buildingHistogram")
    public R buildingHistogram(@Validated(value = {TopologicalWdListGroup.class}) TopologicalWdAceeptVo topologicalWdAceeptVo){
        String md5 = topologicalWdAceeptVo.getHash();
        //2.查看redis中是否存在有缓存
        List<Histogram> wdCount = (List<Histogram>) redisTemplate.boundHashOps(RedisContant.TOPOLOGICAL_BUILD_WD_BUILDING_HISTOGRAM).get(md5);
        if (wdCount != null) {
            return R.ok(wdCount);
        }

        List<Histogram> list = topologicalBuildWdSerevice.buildingHistogram(topologicalWdAceeptVo);

        redisTemplate.boundHashOps(RedisContant.TOPOLOGICAL_BUILD_WD_BUILDING_HISTOGRAM).put(md5,list);
        redisTemplate.expire(RedisContant.TOPOLOGICAL_BUILD_WD_BUILDING_HISTOGRAM, RedisContant.TOPOLOGICAL_BUILD_WD_BUILDING_HISTOGRAM_TIME, TimeUnit.MINUTES); //30分钟

        return R.ok(list);
    }
}
