package com.ruoyi.demo.controller;

import com.github.pagehelper.PageInfo;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.demo.constant.RedisContant;
import com.ruoyi.demo.entity.Brand;
import com.ruoyi.demo.entity.bo.Histogram;
import com.ruoyi.demo.entity.bo.StoreWdCategoryCount;
import com.ruoyi.demo.entity.vo.ChannelAnalyseAceeptVo;
import com.ruoyi.demo.entity.vo.ChannelMapAceeptVo;
import com.ruoyi.demo.entity.vo.TagAnalyse;
import com.ruoyi.demo.service.ChannelAnalyseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/channelAnalyse")
public class ChannelAnalyseController {

    @Autowired
    ChannelAnalyseService channelAnalyseService;
    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/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);
    }

    @GetMapping("/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);
    }

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

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


        List<StoreWdCategoryCount> category = channelAnalyseService.category(channelMapAceeptVo);
        redisTemplate.boundHashOps(RedisContant.CHANNEL_ANALYSE_CATEGORY).put(md5,category);
        redisTemplate.expire(RedisContant.CHANNEL_ANALYSE_CATEGORY,RedisContant.CHANNEL_ANALYSE_CATEGORY_TIME, TimeUnit.MINUTES); //30分钟
        return R.ok(category);
    }

    @GetMapping("/tagAnalyse")
    public R tagAnalyse(ChannelMapAceeptVo channelMapAceeptVo){
        Integer page = channelMapAceeptVo.getPageNum() == null ? 1: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);
    }

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

        //2.查看redis中是否存在有缓存
        HashMap<String,Integer> wdCount = (HashMap<String,Integer>) redisTemplate.boundHashOps(RedisContant.CHANNEL_ANALYSE_BUSINESS_STATUS_ANALYSE).get(md5);
        if (wdCount != null) {
            return R.ok(wdCount);
        }
        HashMap<String,Integer> hashMap = channelAnalyseService.businessStatusAnalyse(channelMapAceeptVo);

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

        return R.ok(hashMap);
    }

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

        //2.查看redis中是否存在有缓存
        ArrayList<Histogram> wdCount = (ArrayList<Histogram>) redisTemplate.boundHashOps(RedisContant.CHANNEL_ANALYSE_AROUND_BUILD_ANALYSE).get(md5);
        if (wdCount != null) {
            return R.ok(wdCount);
        }
        ArrayList<Histogram> hashtable = channelAnalyseService.aroundBuildAnalyse(channelMapAceeptVo);

        //4.保存到redis中
        redisTemplate.boundHashOps(RedisContant.CHANNEL_ANALYSE_AROUND_BUILD_ANALYSE).put(md5,hashtable);
        redisTemplate.expire(RedisContant.CHANNEL_ANALYSE_AROUND_BUILD_ANALYSE,RedisContant.CHANNEL_ANALYSE_AROUND_BUILD_ANALYSE_TIME, TimeUnit.MINUTES); //30分钟

        return R.ok(hashtable);
    }

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

        //2.查看redis中是否存在有缓存
        ArrayList<Histogram> wdCount = (ArrayList<Histogram>) redisTemplate.boundHashOps(RedisContant.CHANNEL_ANALYSE_PER_CAPITA_CONSUMP_ANALYSE).get(md5);
        if (wdCount != null) {
            return R.ok(wdCount);
        }
        ArrayList<Histogram> hashtable = channelAnalyseService.perCapitaConsumpAnalyse(channelMapAceeptVo);

        //4.保存到redis中
        redisTemplate.boundHashOps(RedisContant.CHANNEL_ANALYSE_PER_CAPITA_CONSUMP_ANALYSE).put(md5,hashtable);
        redisTemplate.expire(RedisContant.CHANNEL_ANALYSE_PER_CAPITA_CONSUMP_ANALYSE,RedisContant.CHANNEL_ANALYSE_PER_CAPITA_CONSUMP_ANALYSE_TIME, TimeUnit.MINUTES); //30分钟

        return R.ok(hashtable);
    }

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

        //2.查看redis中是否存在有缓存
        ArrayList<Histogram> wdCount = (ArrayList<Histogram>) redisTemplate.boundHashOps(RedisContant.CHANNEL_ANALYSE_OPERATE_TIME_ANALYSE).get(md5);
        if (wdCount != null) {
            return R.ok(wdCount);
        }
        ArrayList<Histogram> hashtable = channelAnalyseService.operateTimeAnalyse(channelMapAceeptVo);

        //4.保存到redis中
        redisTemplate.boundHashOps(RedisContant.CHANNEL_ANALYSE_OPERATE_TIME_ANALYSE).put(md5,hashtable);
        redisTemplate.expire(RedisContant.CHANNEL_ANALYSE_OPERATE_TIME_ANALYSE,RedisContant.CHANNEL_ANALYSE_OPERATE_TIME_ANALYSE_TIME, TimeUnit.MINUTES); //30分钟

        return R.ok(hashtable);
    }



}
