package com.ruoyi.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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 com.ruoyi.demo.valida.ChannelAreaGroup;
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.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;

    /**
     * 获取 网点城市等级分布统计
     * @param channelMapAceeptVo 渠道地图过滤请求接收体
     * @return
     */
    @GetMapping("/cityTier")
    public R cityTier(@Validated(value = ChannelAreaGroup.class) ChannelMapAceeptVo channelMapAceeptVo){
        //看redis中是否存在有缓存
        String md5 = channelMapAceeptVo.getHash();
        HashMap wdCount = (HashMap) redisTemplate.boundHashOps(RedisContant.CHANNEL_ANALYSE_CITYTIER).get(md5);
        if (wdCount != null) {
            return R.ok(wdCount);
        }

        //查询
        HashMap hashMap = channelAnalyseService.cityTier(channelMapAceeptVo);

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

    /**
     * 获取 品牌列表
     * @param channelAnalyseAceeptVo 渠道地图过滤请求接收体
     * @return
     */
    @GetMapping("/brandList")
    public R brandList(ChannelAnalyseAceeptVo channelAnalyseAceeptVo){
        //查看redis中是否存在有缓存
        String md5 = channelAnalyseAceeptVo.getHash();
        Page<Brand> wdCount = (Page<Brand>) redisTemplate.boundHashOps(RedisContant.CHANNEL_ANALYSE_BRANDLIST).get(md5);
        if (wdCount != null) {
            return R.ok(wdCount);
        }

        //查询
        Page<Brand> brandPageInfo = channelAnalyseService.brandList(channelAnalyseAceeptVo);

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

    /**
     * 获取 网点分类统计
     * @param channelMapAceeptVo 渠道地图过滤请求接收体
     * @return
     */
    @GetMapping("/category")
    public R category(@Validated(value = ChannelAreaGroup.class) ChannelMapAceeptVo channelMapAceeptVo){
        //查看redis中是否存在有缓存
        String md5 = channelMapAceeptVo.getHash();
        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);

        //存入redis
        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);
    }

    /**
     * 网点周边标签分析
     * @param channelMapAceeptVo
     * @return
     */
    @GetMapping("/tagAnalyse")
    public R tagAnalyse(@Validated(value = ChannelAreaGroup.class) 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);
    }

    /**
     * 获取 网点营业状态统计
     * @param channelMapAceeptVo 渠道地图过滤请求接收体
     * @return
     */
    @GetMapping("/businessStatusAnalyse")
    public R businessStatusAnalyse(@Validated(value = ChannelAreaGroup.class) ChannelMapAceeptVo channelMapAceeptVo){
        //查看redis中是否存在有缓存
        String md5 = channelMapAceeptVo.getHash();
        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);

        //保存到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);
    }


    /**
     * 获取 楼宇类型数量统计
     * @param channelMapAceeptVo
     * @return
     */
    @GetMapping("/aroundBuildAnalyse")
    public R aroundBuildAnalyse(@Validated(value = ChannelAreaGroup.class) 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);
    }

    /**
     * 获取 人均消费统计
     * @param channelMapAceeptVo
     * @return
     */
    @GetMapping("/perCapitaConsumpAnalyse")
    public R perCapitaConsumpAnalyse(@Validated(value = ChannelAreaGroup.class) ChannelMapAceeptVo channelMapAceeptVo){
        //查看redis中是否存在有缓存
        String md5 = channelMapAceeptVo.getHash();
        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);

        //保存到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);
    }

    /**
     * 获取 店龄/经营时长统计
     * @param channelMapAceeptVo
     * @return
     */
    @GetMapping("/operateTimeAnalyse")
    public R operateTimeAnalyse(@Validated(value = ChannelAreaGroup.class) ChannelMapAceeptVo channelMapAceeptVo){
        //查看redis中是否存在有缓存
        String md5 = channelMapAceeptVo.getHash();
        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);

        //保存到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);
    }



}
