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.HeatMapBo;
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.GetMapping;
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/topologicalStoreWd")
public class TopologicalStoreWdController {

    @Autowired
    TopologicalStoreWdService topologicalStoreWdService;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 获取周边网点分类
     * @param topologicalWdAceeptVo
     * @return
     */
    @GetMapping("/category")
    public R category(@Validated(value = {TopologicalWdGroup.class}) TopologicalWdAceeptVo topologicalWdAceeptVo){
        //1.查看redis中是否存在有缓存
        String md5 = topologicalWdAceeptVo.getHash();
        List<StoreWdCategoryCount>wdCount = (List<StoreWdCategoryCount>) redisTemplate.boundHashOps(RedisContant.TOPOLOGICAL_STORE_WD_CATEGORY).get(md5);
        if (wdCount != null) {
            return R.ok(wdCount);
        }

        //2.查询结果
        List<StoreWdCategoryCount> category = topologicalStoreWdService.category(topologicalWdAceeptVo);

        //3.存入redis
        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);
    }

    /**
     * 获取周边门店网点列表
     * @param topologicalWdAceeptVo
     * @return
     */
    @GetMapping("/list")
    public R list(@Validated(value = {TopologicalWdListGroup.class}) TopologicalWdAceeptVo topologicalWdAceeptVo){
        //1.查看redis中是否存在有缓存
        String md5 = topologicalWdAceeptVo.getHash();
        HashMap<String,Object> wdCount = (HashMap<String,Object>) redisTemplate.boundHashOps(RedisContant.TOPOLOGICAL_STORE_WD_LIST).get(md5);
        if (wdCount != null) {
            return R.ok(wdCount);
        }

        //2.获取数据
        HashMap<String,Object> list = topologicalStoreWdService.list(topologicalWdAceeptVo);

        //3.存储redis
        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);
    }

    /**
     * 获取商业网点分类数量统计
     * @param topologicalWdAceeptVo
     * @return
     */
    @GetMapping("/storeHistogram")
    public R storeHistogram(@Validated(value = {TopologicalWdGroup.class}) TopologicalWdAceeptVo topologicalWdAceeptVo){
        //1.查看redis中是否存在有缓存
        String md5 = topologicalWdAceeptVo.getHash();
        List<Histogram> wdCount = (List<Histogram>) redisTemplate.boundHashOps(RedisContant.TOPOLOGICAL_STORE_WD_STORE_HISTOGRAM).get(md5);
        if (wdCount != null) {
            return R.ok(wdCount);
        }

        //2.获取数据
        List<Histogram> list = topologicalStoreWdService.storeHistogram(topologicalWdAceeptVo);

        //3.存入redis
        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);
    }

    /**
     * 获取门店人均消费
     * @param topologicalWdAceeptVo
     * @return
     */
    @GetMapping("/perCapitaConsumpHistogram")
    public R perCapitaConsumpHistogram(@Validated(value = {TopologicalWdGroup.class}) TopologicalWdAceeptVo topologicalWdAceeptVo){
        //1.查看redis中是否存在有缓存
        String md5 = topologicalWdAceeptVo.getHash();
        List<Histogram> wdCount = (List<Histogram>) redisTemplate.boundHashOps(RedisContant.TOPOLOGICAL_STORE_WD_PER_CAPITA_CONSUMP_HISTOGRAM).get(md5);
        if (wdCount != null) {
            return R.ok(wdCount);
        }

        //2.获取数据
        List<Histogram> list = topologicalStoreWdService.perCapitaConsumpHistogram(topologicalWdAceeptVo);

        //存入redis
        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);
    }

    /**
     * 获取门店人均消费
     * @param topologicalWdAceeptVo
     * @return
     */
    @GetMapping("/heatMap")
    public R heatMap(@Validated(value = {TopologicalWdGroup.class}) TopologicalWdAceeptVo topologicalWdAceeptVo){
        //1.查看redis中是否存在有缓存
        String md5 = topologicalWdAceeptVo.getHash();
        List<HeatMapBo> wdCount = (List<HeatMapBo>) redisTemplate.boundHashOps(RedisContant.TOPOLOGICAL_STORE_WD_HEAT_MAP).get(md5);
        if (wdCount != null) {
            return R.ok(wdCount);
        }

        //2.获取数据
        List<HeatMapBo> heatMapBos = topologicalStoreWdService.heatMap(topologicalWdAceeptVo);

        //存入redis
        redisTemplate.boundHashOps(RedisContant.TOPOLOGICAL_STORE_WD_HEAT_MAP).put(md5,heatMapBos);
        redisTemplate.expire(RedisContant.TOPOLOGICAL_STORE_WD_HEAT_MAP,RedisContant.TOPOLOGICAL_STORE_WD_HEAT_MAP_TIME, TimeUnit.MINUTES); //30分钟
        return R.ok(heatMapBos);
    }
}
