package com.benyun.core.controller;

import com.benyun.core.constant.ChannelMapContant;
import com.benyun.core.constant.RedisContant;
import com.benyun.core.entity.WdCount;
import com.benyun.core.entity.WdInfo;
import com.benyun.core.entity.vo.ChannelMapAceeptVo;
import com.benyun.core.service.ChannelMapService;
import com.benyun.core.utils.HashUtil;
import com.benyun.core.valida.ChannelAreaGroup;
import com.benyun.core.valida.ChannelListGroup;
import com.benyun.core.valida.ChannelPointGroup;
import com.github.pagehelper.PageInfo;
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 javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/channelMap")
public class ChannelMapController {
    @Autowired
    ChannelMapService channelMapService;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/getArea")
    public R area(@Validated(value={ChannelAreaGroup.class}) ChannelMapAceeptVo channel) {
        String md5 = channel.getHash();

        //2.查看redis中是否存在有缓存
        WdCount wdCount = (WdCount) redisTemplate.boundHashOps(RedisContant.CHANNEL_MAP_AREA).get(md5);
        if (wdCount != null) {
            return R.ok(wdCount);
        }

        //3.redis中没有，直接请求
        WdCount area = channelMapService.area(Arrays.asList(channel.getChannel()), channel.getRankType(), channel.getSearchText(), channel.getAddrCode());
        if(area == null)
            return R.fail("请求参数错误");

        //4.保存到redis中
        redisTemplate.boundHashOps(RedisContant.CHANNEL_MAP_AREA).put(md5,area);
        redisTemplate.expire(RedisContant.CHANNEL_MAP_AREA,RedisContant.CHANNEL_MAP_AREA_TIME, TimeUnit.MINUTES); //30分钟
        return R.ok(area);
    }

    //TODO 这里还需要思考返回来的是什么数据
    @RequestMapping("/list")
    public R list(@Validated(value={ChannelListGroup.class}) ChannelMapAceeptVo channel){

        String md5 = channel.getHash();
        PageInfo<WdInfo> pageInfo = (PageInfo<WdInfo>) redisTemplate.boundHashOps(RedisContant.CHANNEL_MAP_LIST).get(md5);
        if(pageInfo != null){
            return R.ok(pageInfo);
        }

        PageInfo<WdInfo> list = channelMapService.list(channel.getChannel(), channel.getRankType(), channel.getSearchText(), channel.getOrderby(), channel.getAddrCode(), channel.getPageNum(), channel.getPageSize());
        if(list == null)
            return R.fail("请求参数错误");

        //4.保存到redis中
        redisTemplate.boundHashOps(RedisContant.CHANNEL_MAP_LIST).put(md5,list);
        redisTemplate.expire(RedisContant.CHANNEL_MAP_LIST,RedisContant.CHANNEL_MAP_LIST_TIME, TimeUnit.MINUTES); //30分钟
        return R.ok(list);
    }

    @RequestMapping("/point")
    public R point(@Validated(value={ChannelPointGroup.class}) ChannelMapAceeptVo channel){
        String md5 = channel.getHash();
        PageInfo<WdInfo> pageInfo = (PageInfo<WdInfo>) redisTemplate.boundHashOps(RedisContant.CHANNEL_MAP_POINT).get(md5);
        if(pageInfo != null){
            return R.ok(pageInfo);
        }

        PageInfo<WdInfo> point = channelMapService.point(channel.getChannel(), channel.getSearchText(), channel.getAddrCode(), channel.getPageNum(), channel.getPageSize());
        if(point == null)
            return R.fail("请求参数错误");

        //4.保存到redis中
        redisTemplate.boundHashOps(RedisContant.CHANNEL_MAP_POINT).put(md5,point);
        redisTemplate.expire(RedisContant.CHANNEL_MAP_POINT,RedisContant.CHANNEL_MAP_POINT_TIME, TimeUnit.MINUTES); //30分钟
        return R.ok(point);
    }

}
