package com.heketai.website.web;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.heketai.website.common.result.BaseResult;
import com.heketai.website.entity.AuthorizeCompany;
import com.heketai.website.service.ICompanyAuthorizeService;
import com.heketai.website.common.utils.StringUtils;
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.RestController;

/**
 * @author hotking03
 */
@RestController
@RequestMapping("authorize")
public class CompanyAuthorizeController {

    @Autowired
    private ICompanyAuthorizeService companyAuthorizeService;
    @Autowired
    private RedisTemplate redisTemplate;

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

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


    @RequestMapping("/deleteById")
    @ApiOperation(value = "删除公司信息", notes = "删除公司信息")
    public BaseResult deleteById(@RequestBody AuthorizeCompany company) {
        companyAuthorizeService.deleteById(company.getId());
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

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

    @RequestMapping("/getById")
    @ApiOperation(value = "公司信息id查", notes = "公司信息id查")
    public BaseResult<AuthorizeCompany> getById(@RequestBody AuthorizeCompany company) {
        String key = "89E1A3F3-3DCE" + company.toString();
        Object o = redisTemplate.boundHashOps(fKey).get(key);
        if (null != o) {
            AuthorizeCompany authorizeCompany = JSON.parseObject(o.toString(), AuthorizeCompany.class);
            return BaseResult.successWithData(authorizeCompany);
        }
        AuthorizeCompany authorizeCompany = companyAuthorizeService.selectById(company.getId());
        redisTemplate.boundHashOps(fKey).put(key, JSON.toJSONString(authorizeCompany));
        return BaseResult.successWithData(authorizeCompany);
    }

    @RequestMapping("/findPage")
    @ApiOperation(value = "公司信息分页查", notes = "公司信息分页查")
    public Object findPage(@RequestBody Page<AuthorizeCompany> page) {
        String key = "6FC60187833F770B79F5" + JSON.toJSONString(page);

        Object o = redisTemplate.boundHashOps(fKey).get(key);
        if (null != o) {
            return BaseResult.successWithData(o);
        }
        AuthorizeCompany authorizeCompany = page.getRecords().get(0);
        if (!StringUtils.isNull(authorizeCompany.getCompanyName())) {
            authorizeCompany.setCompanyCode(authorizeCompany.getCompanyName());
        }
        Object object = companyAuthorizeService.selectByPage(page);
        return BaseResult.successWithData(object);
    }
}
