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.AboutUs;
import com.heketai.website.service.IAboutUsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * <p>
 * 关于我们前端控制器
 * </p>
 *
 * @author xjl
 * @since 2019-12-11
 */
@RestController
@RequestMapping("/aboutUs")
@Api(value = "关于我们理接口", tags = "关于我们页面")
@Slf4j
public class AboutUsController {
    @Autowired
    private IAboutUsService aboutUsService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private String fKey = "getAllAboutUsList";

    @RequestMapping("/insert")
    @ApiOperation(value = "新增关于我们", notes = "新增关于我们")
    @Transactional(propagation = Propagation.REQUIRED)
    public BaseResult insert(@RequestBody AboutUs aboutUs) {
        aboutUsService.insert(aboutUs);
        stringRedisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/deleteById")
    @ApiOperation(value = "删除关于我们", notes = "删除关于我们")
    public BaseResult deleteById(@RequestParam Integer id) {
        aboutUsService.deleteById(id);
        stringRedisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/update")
    @ApiOperation(value = "修改关于我们", notes = "修改关于我们")
    public BaseResult update(@RequestBody AboutUs aboutUs) {
        aboutUsService.updateById(aboutUs);
        stringRedisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/getById")
    @ApiOperation(value = "关于我们 id查", notes = "关于我们 id查")
    public BaseResult<AboutUs> getById(@RequestParam Integer id) {
        return BaseResult.successWithData(aboutUsService.selectById(id));
    }

    @RequestMapping("/findAll")
    @ApiOperation(value = "查所有关于我们", notes = "查所有关于我们")
    public BaseResult findAll() {
        try {
            String key = "findAllAboutUs";
            Object aboutUsObject = stringRedisTemplate.boundHashOps(fKey).get(key);
            if (aboutUsObject != null) {
                List<AboutUs> aboutUses = JSON.parseArray(aboutUsObject.toString(), AboutUs.class);
                return BaseResult.successWithData(aboutUses);
            }
            List<AboutUs> aboutUsList = aboutUsService.selectList(new EntityWrapper<>());
            String s = JSON.toJSONString(aboutUsList);
            stringRedisTemplate.boundHashOps(fKey).put(key, s);
            return BaseResult.successWithData(aboutUsList);
        } catch (Exception e) {
            return BaseResult.fail("查所有关于我们失败");
        }

    }

    @RequestMapping("/findPage")
    @ApiOperation(value = "关于我们 分页", notes = "关于我们 分页")
    public BaseResult<Page<AboutUs>> findPage(@RequestBody Page<AboutUs> page) {
        try {
            String key = "findPageAboutUs" + JSON.toJSONString("page");
            Object aboutUsList = stringRedisTemplate.boundHashOps(fKey).get(key);
            if (aboutUsList != null) {
                Page<AboutUs> aboutUsPage = JSON.parseObject(aboutUsList.toString(), Page.class);
                return BaseResult.successWithData(aboutUsPage);
            }
            Page<AboutUs> aboutUsPage = aboutUsService.selectPage(page);
            stringRedisTemplate.boundHashOps(fKey).put("key", aboutUsPage);
            return BaseResult.successWithData(aboutUsPage);
        } catch (Exception e) {
            return BaseResult.fail("关于我们,分页查询失败");
        }
    }

}
