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.Company;
import com.heketai.website.service.ICompanyService;
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.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-11-20
 */
@RestController
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private ICompanyService companyService;

    @Autowired
    private RedisTemplate redisTemplate;

    private String fKey = "E947CFDF-ED26-F209-DB7C-254EF082B01D";


    @RequestMapping("/insert")
    @ApiOperation(value = "新增公司信息", notes = "新增公司信息")
    public BaseResult insert(@RequestBody Company company) {
        companyService.insert(company);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/deleteById")
    @ApiOperation(value = "删除公司信息", notes = "删除公司信息")
    public BaseResult deleteById(@RequestParam Integer id) {
        companyService.deleteById(id);
        redisTemplate.delete(fKey);
        return BaseResult.success();

    }

    @RequestMapping("/update")
    @ApiOperation(value = "修改公司信息", notes = "修改公司信息")
    public BaseResult update(@RequestBody Company company) {
        companyService.updateById(company);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/getById")
    @ApiOperation(value = "公司信息 id查", notes = "公司信息 id查")
    public BaseResult<Company> getById(@RequestParam Integer id) {
        String key = "3194CFF4-E3F3-A158-6FB1-9926C8EFDCE7"+id;
        Object o = redisTemplate.boundHashOps(fKey).get(key);
        if (null != o) {
            Company company = JSON.parseObject(o.toString(), Company.class);
            return BaseResult.successWithData(company);
        }
        Company company = companyService.selectById(id);
        redisTemplate.boundHashOps(fKey).put(key, JSON.toJSONString(company));
        return BaseResult.successWithData(company);
    }

    @RequestMapping("/findAll")
    @ApiOperation(value = "查所有公司信息", notes = "查所有公司信息")
    public BaseResult findAll() {
        String key = "9F6B2D74-CCFB-BD59-FDFE-20FFF7A0CB6E";
        Object o = redisTemplate.boundHashOps(fKey).get(key);
        if (null != o) {
            List<Company> companies = JSON.parseArray(o.toString(), Company.class);
            return BaseResult.successWithData(companies);
        }
        List<Company> companies = companyService.selectList(new EntityWrapper<>());
        redisTemplate.boundHashOps(fKey).put(key, JSON.toJSONString(companies));
        return BaseResult.successWithData(companies);
    }

    @RequestMapping("/findPage")
    @ApiOperation(value = "公司信息 分页查", notes = "公司信息 分页查")
    public BaseResult<Page<Company>> findPage(@RequestBody Page<Company> page) {
        String key = "03880575E288CD68A558" + JSON.toJSONString(page);
        Object o = redisTemplate.boundHashOps(fKey).get(key);
        if (null != o) {
            Page<Company> companyPage = JSON.parseObject(o.toString(), Page.class);
            return BaseResult.successWithData(companyPage);
        }
        Page<Company> companyPage = companyService.selectPage(page);
        redisTemplate.boundHashOps(fKey).put(key, JSON.toJSONString(companyPage));
        return BaseResult.successWithData(companyPage);
    }
}
