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.CustomerMessage;
import com.heketai.website.service.ICustomerMessageService;
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-10
 */
@RestController
@RequestMapping("/customerMessage")
public class CustomerMessageController {
    @Autowired
    private ICustomerMessageService customerMessageService;

    @Autowired
    private RedisTemplate redisTemplate;

    private String fKey = "CF4073D2-10EF-936C-33DF-824989FA640A";

    @RequestMapping("/insert")
    @ApiOperation(value = "新增用户留言简介", notes = "新增用户留言简介")
    public BaseResult insert(@RequestBody CustomerMessage customerMessage) {
        customerMessageService.insert(customerMessage);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/deleteById")
    @ApiOperation(value = "删除用户留言", notes = "删除用户留言")
    public BaseResult deleteById(@RequestParam Integer id) {
        customerMessageService.deleteById(id);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/update")
    @ApiOperation(value = "修改用户留言", notes = "修改用户留言")
    public BaseResult update(@RequestBody CustomerMessage customerMessage) {
        customerMessageService.updateById(customerMessage);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/getById")
    @ApiOperation(value = "用户留言 id查", notes = "用户留言 id查")
    public BaseResult<CustomerMessage> getById(@RequestParam Integer id) {
        String key = "92FF0574-8B7B-E0A1-4B78-CFAF60ED5228"+ id;
        Object o = redisTemplate.boundHashOps(fKey).get(key);
        if (null != o) {
            CustomerMessage customerMessage =JSON.parseObject(o.toString(), CustomerMessage.class);
            return BaseResult.successWithData(customerMessage);
        }
        CustomerMessage customerMessage = customerMessageService.selectById(id);
        redisTemplate.boundHashOps(fKey).put(key, JSON.toJSONString(customerMessage));
        return BaseResult.successWithData(customerMessage);
    }

    @RequestMapping("/findAll")
    @ApiOperation(value = "查所有用户留言", notes = "查所有用户留言")
    public BaseResult findAll() {
        String key = "5FDC93D0-9B0F-64D6-E965-B46CC47018CC";
        Object o = redisTemplate.boundHashOps(fKey).get(key);
        if (null != o) {
            List<CustomerMessage> customerMessages =  JSON.parseArray(o.toString(), CustomerMessage.class);
            return BaseResult.successWithData(customerMessages);
        }
        List<CustomerMessage> customerMessages = customerMessageService.selectList(new EntityWrapper<>());
        redisTemplate.boundHashOps(fKey).put(key, JSON.toJSONString(customerMessages ));
        return BaseResult.successWithData(customerMessages);
    }

    @RequestMapping("/findPage")
    @ApiOperation(value = "用户留言 分页", notes = "用户留言 分页")
    public BaseResult<Page<CustomerMessage>> findPage(@RequestBody Page<CustomerMessage> page) {

        String key = "517CB0B45FBED034DC35" + JSON.toJSONString(page);
        Object o = redisTemplate.boundHashOps(fKey).get(key);
        if (null != o) {
            Page<CustomerMessage> customerMessagePage =JSON.parseObject(o.toString(), Page.class);
            return BaseResult.successWithData(customerMessagePage);
        }
        Page<CustomerMessage> customerMessagePage = customerMessageService.selectPage(page);
        redisTemplate.boundHashOps(fKey).put(key, JSON.toJSONString(customerMessagePage));
        return BaseResult.successWithData(customerMessagePage);
    }
}
