package com.ruoyi.benyun.controller;

import com.ruoyi.benyun.entity.vo.TopologicalWdAceeptVo;
import com.ruoyi.benyun.service.TopologicalHouseWdService;
import com.ruoyi.benyun.valida.TopologicalWdGroup;
import com.ruoyi.benyun.valida.TopologicalWdListGroup;
import com.ruoyi.benyun.constant.RedisContant;
import com.ruoyi.benyun.entity.TopologicalHouseWd;
import com.ruoyi.benyun.entity.TopologicalHouseWdStatisticsData;
import com.ruoyi.common.core.domain.R;
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/topologicalHouseWd")
public class TopologicalHouseWdController {
    @Autowired
    TopologicalHouseWdService topologicalHouseWdService;

    @Autowired
    RedisTemplate redisTemplate;

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


        TopologicalHouseWdStatisticsData info = topologicalHouseWdService.info(topologicalWdAceeptVo);

        redisTemplate.boundHashOps(RedisContant.TOPOLOGICAL_HOUSE_WD_INFO).put(md5,info);
        redisTemplate.expire(RedisContant.TOPOLOGICAL_HOUSE_WD_INFO,RedisContant.TOPOLOGICAL_HOUSE_WD_INFO_TIME, TimeUnit.MINUTES); //30分钟


        return R.ok(info);
    }

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

        List<TopologicalHouseWd> list = topologicalHouseWdService.list(topologicalWdAceeptVo);

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

        return R.ok(list);
    }
}
