package com.ruoyi.demo.controller;


import com.ruoyi.common.core.domain.R;
import com.ruoyi.demo.constant.RedisContant;
import com.ruoyi.demo.entity.TopologicalStoreWd;
import com.ruoyi.demo.entity.bo.Histogram;
import com.ruoyi.demo.entity.bo.StoreWdCategoryCount;
import com.ruoyi.demo.entity.vo.TopologicalWdAceeptVo;
import com.ruoyi.demo.service.TopologicalStoreWdService;
import com.ruoyi.demo.valida.TopologicalWdGroup;
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.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/poi/topologicalStoreWd")
public class TopologicalStoreWdController {

    @Autowired
    TopologicalStoreWdService topologicalStoreWdService;

    @Autowired
    RedisTemplate redisTemplate;

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

        List<StoreWdCategoryCount> category = topologicalStoreWdService.category(topologicalWdAceeptVo);

        redisTemplate.boundHashOps(RedisContant.TOPOLOGICAL_STORE_WD_CATEGORY).put(md5,category);
        redisTemplate.expire(RedisContant.TOPOLOGICAL_STORE_WD_CATEGORY, RedisContant.TOPOLOGICAL_STORE_WD_CATEGORY_TIME, TimeUnit.MINUTES); //30分钟

        return R.ok(category);
    }

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


        List<TopologicalStoreWd> list = topologicalStoreWdService.list(topologicalWdAceeptVo);

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

        return R.ok(list);
    }

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


        List<Histogram> list = topologicalStoreWdService.storeHistogram(topologicalWdAceeptVo);

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

        return R.ok(list);
    }

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


        List<Histogram> list = topologicalStoreWdService.perCapitaConsumpHistogram(topologicalWdAceeptVo);

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

        return R.ok(list);
    }
}
