package com.heketai.website.web;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.heketai.website.common.result.BaseResult;
import com.heketai.website.entity.AdCategory;
import com.heketai.website.service.IAdCategoryService;
import io.swagger.annotations.Api;
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-12-12
 */
@RestController
@RequestMapping("/adCategory")
@Api(value = "广告分类管理接口", tags = "广告分类页面")
public class AdCategoryController {
    @Autowired
    private IAdCategoryService adCategoryService;
    @Autowired
    private RedisTemplate redisTemplate;

    private String fKey = "findByAdCategoryList";

    @RequestMapping("/insert")
    @ApiOperation(value = "新增广告分类", notes = "新增广告分类")
    public BaseResult insert(@RequestBody AdCategory adCategory) {
        adCategoryService.insert(adCategory);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/deleteById")
    @ApiOperation(value = "删除广告分类", notes = "删除广告分类")
    public BaseResult deleteById(@RequestParam Integer id) {
        adCategoryService.deleteById(id);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/update")
    @ApiOperation(value = "修改广告分类", notes = "修改广告分类")
    public BaseResult update(@RequestBody AdCategory adCategory) {
        adCategoryService.updateById(adCategory);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/getById")
    @ApiOperation(value = "广告分类 id查", notes = "广告分类 id查")
    public BaseResult<AdCategory> getById(@RequestParam Integer id) {
        return BaseResult.successWithData(adCategoryService.selectById(id));
    }

    @RequestMapping("/findAll")
    @ApiOperation(value = "查所有广告分类", notes = "查所有广告分类")
    public BaseResult findAll() {
        try {
            String key = "findAllAdCategory";
            Object findRedis = redisTemplate.boundHashOps(fKey).get(key);
            if (findRedis != null) {
                List<AdCategory> adCategories = JSON.parseArray(findRedis.toString(), AdCategory.class);
                return BaseResult.successWithData(adCategories);
            }
            List<AdCategory> adCategories = adCategoryService.selectList(new EntityWrapper<>());
            String s = JSON.toJSONString(adCategories);
            redisTemplate.boundHashOps(fKey).put(key, s);
            return BaseResult.successWithData(adCategories);
        } catch (Exception e) {
            return BaseResult.fail("查所有广告分类失败");
        }

    }

    @RequestMapping("/findPage")
    @ApiOperation(value = "广告分类 分页", notes = "广告分类 分页")
    public BaseResult<Page<AdCategory>> findPage(@RequestBody Page<AdCategory> page) {
        try {
            String key = "findPageAdCategory"+JSON.toJSONString(page);
            Object findRedis = redisTemplate.boundHashOps(fKey).get(key);
            if (findRedis != null) {
                Page<AdCategory> adCategoryPage = JSON.parseObject(findRedis.toString(), Page.class);
                return BaseResult.successWithData(adCategoryPage);
            }
            Page<AdCategory> adCategoryPage = adCategoryService.selectPage(page);
            String s = JSON.toJSONString(adCategoryPage);
            redisTemplate.boundHashOps(fKey).put(key, s);
            return BaseResult.successWithData(adCategoryPage);
        } catch (Exception e) {
            return BaseResult.fail("查分页广告分类失败");
        }

    }
}
