package com.ruoyi.benyun.controller;

import com.ruoyi.benyun.entity.bo.StoreWdCategoryCount;
import com.ruoyi.benyun.entity.vo.ChannelAnalyseAceeptVo;
import com.ruoyi.benyun.entity.vo.ChannelMapAceeptVo;
import com.ruoyi.benyun.entity.vo.TagAnalyse;
import com.ruoyi.benyun.service.ChannelAnalyseService;
import com.ruoyi.benyun.constant.RedisContant;
import com.ruoyi.benyun.entity.Brand;
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.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("/channelAnalyse")
public class ChannelAnalyseController {

    @Autowired
    ChannelAnalyseService channelAnalyseService;
    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("/cityTier")
    public R cityTier(ChannelMapAceeptVo channelMapAceeptVo){
        String md5 = channelMapAceeptVo.getHash();

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

        HashMap hashMap = channelAnalyseService.cityTier(channelMapAceeptVo);

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

    @RequestMapping("/category")
    public R category(ChannelMapAceeptVo channelMapAceeptVo){

        List<StoreWdCategoryCount> category = channelAnalyseService.category(channelMapAceeptVo);

        return R.ok(category);
    }

    @RequestMapping("/brandList")
    public R brandList(ChannelAnalyseAceeptVo channelAnalyseAceeptVo){
        String md5 = channelAnalyseAceeptVo.getHash();

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

        PageInfo<Brand> brandPageInfo = channelAnalyseService.brandList(channelAnalyseAceeptVo);

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

    @RequestMapping("/tagAnalyse")
    public R tagAnalyse(ChannelMapAceeptVo channelMapAceeptVo){
        int page = channelMapAceeptVo.getPageNum();
        channelMapAceeptVo.setPageNum(0);
        String md5 = channelMapAceeptVo.getHash();

        //2.查看redis中是否存在有缓存
        HashMap<String, Object> result = new HashMap<>();
        HashMap<String,Object> wdCount = (HashMap<String,Object>) redisTemplate.boundHashOps(RedisContant.CHANNEL_ANALYSE_TAG_ANLYSE).get(md5);
        if (wdCount != null) {
            result.put("data",(List<TagAnalyse>) wdCount.get(page + ""));
            result.put("pages",(int)wdCount.get("pages"));
            result.put("total",(int)wdCount.get("total"));
            return R.ok(result);
        }
        HashMap<String, Object> hashMap = channelAnalyseService.tagAnalyse(channelMapAceeptVo);

        //4.保存到redis中
        redisTemplate.boundHashOps(RedisContant.CHANNEL_ANALYSE_TAG_ANLYSE).put(md5,hashMap);
        redisTemplate.expire(RedisContant.CHANNEL_ANALYSE_TAG_ANLYSE,RedisContant.CHANNEL_ANALYSE_TAG_ANLYSE_TIME, TimeUnit.MINUTES); //30分钟

        result.put("data",(List<TagAnalyse>) hashMap.get(page + ""));
        result.put("pages",(int)hashMap.get("pages"));
        result.put("total",(int)hashMap.get("total"));
        return R.ok(result);
    }

//    @RequestMapping("/businessAnalyse")
//    public R businessAnalyse(ChannelMapAceeptVo channelMapAceeptVo){
//        channelAnalyseService.businessAnalyse(channelMapAceeptVo);
//        return R.ok();
//    }
}
