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.TopologicalHouseWd;
import com.ruoyi.demo.entity.TopologicalHouseWdStatisticsData;
import com.ruoyi.demo.entity.vo.TopologicalWdAceeptVo;
import com.ruoyi.demo.service.TopologicalHouseWdService;
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.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/poi/topologicalHouseWd")
public class TopologicalHouseWdController {
    @Autowired
    TopologicalHouseWdService topologicalHouseWdService;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 获取小区网点关键信息
     * @param topologicalWdAceeptVo
     * @return
     */
    @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);
    }

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

        Page<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);
    }
}
