package com.ruoyi.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.demo.constant.RedisContant;
import com.ruoyi.demo.entity.AttentionPool;
import com.ruoyi.demo.entity.WdInfo;
import com.ruoyi.demo.entity.bo.*;
import com.ruoyi.demo.entity.vo.ChannelMapAceeptVo;
import com.ruoyi.demo.mapper.AttentionPoolDao;
import com.ruoyi.demo.service.ChannelMapService;
import com.ruoyi.demo.valida.ChannelAreaGroup;
import com.ruoyi.demo.valida.ChannelListGroup;
import com.ruoyi.demo.valida.ChannelPointGroup;
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.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/channelMap")

public class ChannelMapController {
    @Autowired
    private ChannelMapService channelMapService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    AttentionPoolDao attentionPoolDao;


    @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(channel);
        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);
    }


    @RequestMapping("/list")
    public R list(@Validated(value={ChannelListGroup.class}) ChannelMapAceeptVo channel){

        String md5 = channel.getHash();
        HashMap pageInfo = (HashMap) redisTemplate.boundHashOps(RedisContant.CHANNEL_MAP_LIST).get(md5);
        if(pageInfo != null){
            List<WdTopologicalInfoBo> data = (List<WdTopologicalInfoBo>) pageInfo.get("data");
            attention(data);
            pageInfo.put("data",data);
            return R.ok(pageInfo);
        }

        HashMap hashMap = channelMapService.list(channel);
        if(hashMap == null)
            return R.fail("请求参数错误");

        //4.保存到redis中
        List<WdTopologicalInfoBo> data = (List<WdTopologicalInfoBo>) hashMap.get("data");
        attention(data);
        hashMap.put("data",data);
        redisTemplate.boundHashOps(RedisContant.CHANNEL_MAP_LIST).put(md5,hashMap);
        redisTemplate.expire(RedisContant.CHANNEL_MAP_LIST,RedisContant.CHANNEL_MAP_LIST_TIME, TimeUnit.MINUTES); //30分钟
        return R.ok(hashMap);
    }



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

    //Todo 标记网点关注
    public void attention(List<WdTopologicalInfoBo> wdTopologicalInfoBos){
        List<String> collect = wdTopologicalInfoBos.stream().map(wdTopologicalInfoBo -> {
            return wdTopologicalInfoBo.getWdId();
        }).collect(Collectors.toList());

        //标志是否关注
        QueryWrapper<AttentionPool> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.select("wd_id");
        queryWrapper1.in("wd_id",collect);
        if(collect.size() > 0){
            List<AttentionPool> attentionPools = attentionPoolDao.selectList(queryWrapper1);
            List<String> collect1 = attentionPools.stream().map(attentionPool -> {
                return attentionPool.getWdId();
            }).collect(Collectors.toList());

            for (WdTopologicalInfoBo wdTopologicalInfoBo : wdTopologicalInfoBos) {
                boolean contains = collect1.contains(wdTopologicalInfoBo.getWdId());
                if (contains)
                    wdTopologicalInfoBo.setAttention(1);
                else
                    wdTopologicalInfoBo.setAttention(0);
            }
        }
    }

    @GetMapping("/getWdInfo")
    public R getWdInfo(String wdId){
        WdInfo wdInfo = channelMapService.getWdInfo(wdId);
        if(wdInfo == null)
            return R.ok(null);
        if("1".equals(wdInfo.getWdTypeCode())){
            return R.ok((StoreWdInfoBo)wdInfo);
        }else if("2".equals(wdInfo.getWdTypeCode())){
            return R.ok((HouseWdInfoBo)wdInfo);
        }else if("3".equals(wdInfo.getWdTypeCode())){
            return R.ok((BuildWdInfoBo)wdInfo);
        }else if("4".equals(wdInfo.getWdTypeCode())){
            return R.ok((TrafficWdInfoBo)wdInfo);
        }else if("5".equals(wdInfo.getWdTypeCode())){
            return R.ok((EnterpriseWdInfoBo)wdInfo);
        }
        return R.ok(wdInfo);
    }

}
