package com.heketai.website.web;


import com.alibaba.fastjson.JSON;
import com.heketai.website.common.result.BaseResult;
import com.heketai.website.entity.dto.NavigationTree;
import com.heketai.website.entity.Navigation;
import com.heketai.website.service.INavigationService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 导航表 前端控制器
 * </p>
 *
 * @author xjl
 * @since 2019-11-18
 */
@RestController
@RequestMapping("/navigation")
public class NavigationController {
    @Autowired
    private INavigationService navigationService;
    @Autowired
    private RedisTemplate redisTemplate;
    private String fKey = "4B87B29A-F436-3264-A68E-13C137136773";

    @RequestMapping("/insert")
    @ApiOperation(value = "新增导航信息", notes = "新增导航信息")
    public BaseResult insert(@RequestBody Navigation navigation) {
        navigationService.insertInfo(navigation);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/deleteById")
    @ApiOperation(value = "删除导航信息", notes = "删除导航信息")
    public BaseResult deleteById(@RequestParam Integer id) {
        navigationService.deleteNavigationById(id);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/update")
    @ApiOperation(value = "新增导航信息", notes = "新增导航信息")
    public BaseResult update(@RequestBody Navigation navigation) {
        navigationService.updateById(navigation);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/getById")
    @ApiOperation(value = "导航信息 id查", notes = "导航信息 id查")
    public BaseResult<Navigation> getById(@RequestParam Integer id) {
        String key = "B11C3683-C477-D6A8-88FB-8056E98DD217"+id;
        Object o =  redisTemplate.boundHashOps(fKey).get(key);
        if (null != o){
            Navigation navigation =JSON.parseObject(o.toString(), Navigation.class);
            return BaseResult.successWithData(navigation);
        }
        Navigation navigation = navigationService.selectById(id);
        redisTemplate.boundHashOps(fKey).put(key,JSON.toJSONString(navigation));
        return BaseResult.successWithData(navigation);
    }

    @RequestMapping("/findTrees")
    @ApiOperation(value = "导航树查询", notes = "导航树查询")
    public BaseResult<List<NavigationTree>> findTrees() {
        String key = "269E46B6-6286-AE2D-9E8E-372AF43DBCE5";
        Object o =  redisTemplate.boundHashOps(fKey).get(key);
        if (null != o){
            List<NavigationTree> trees = JSON.parseArray(o.toString(),NavigationTree.class);
            return BaseResult.successWithData(trees);
        }
        List<NavigationTree> trees = navigationService.findTrees();
        redisTemplate.boundHashOps(fKey).put(key,JSON.toJSONString(trees));
        return BaseResult.successWithData(trees);
    }

}
