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.common.result.HttpCode;
import com.heketai.website.entity.Cooperative;
import com.heketai.website.service.ICooperativeService;
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.*;




/**
 * @author hotking03
 */
@RestController
@RequestMapping("/cooperative")
public class CooperativeController {

    @Autowired
    ICooperativeService cooperativeService;

    @Autowired
    private RedisTemplate redisTemplate;

    private String fKey = "B3C4C9E1-1A6D-007B-C32E-3A2A50D4E694";

    @RequestMapping("/insert")
    @ApiOperation(value = "新增合作伙伴", notes = "新增合作伙伴")
    public BaseResult insert(@RequestBody Cooperative cooperative) {
        cooperativeService.insert(cooperative);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/del")
    @ApiOperation(value = "删除合作伙伴", notes = "删除合作伙伴")
    public BaseResult deleteById(@RequestBody Cooperative cooperative) {
        cooperativeService.deleteById(cooperative.getId());
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/update")
    @ApiOperation(value = "修改合作伙伴", notes = "修改合作伙伴")
    public BaseResult update(@RequestBody Cooperative cooperative) {

        if (StringUtils.isNull(cooperative.getId())) {
            BaseResult.fail(HttpCode.FAIL.getCode(), "参数错误");
        }
        cooperativeService.updateById(cooperative);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/findById")
    @ApiOperation(value = "合作伙伴id查", notes = "合作伙伴id查")
    public BaseResult findById(@RequestBody Cooperative cooperative) {
        String key = "E350A4E717DFA9EF"+JSON.toJSONString(cooperative);
        Object o = redisTemplate.boundHashOps(fKey).get(key);
        if (null != o) {
            Cooperative cooperative1 =JSON.parseObject(o.toString(), Cooperative.class);
            return BaseResult.successWithData(cooperative1);
        }
        if (StringUtils.isNull(cooperative.getId())) {
            BaseResult.fail(HttpCode.FAIL.getCode(), "参数错误");
        }
        Cooperative cooperative1 = cooperativeService.selectById(cooperative);
        redisTemplate.boundHashOps(fKey).put(key, JSON.toJSONString(cooperative1));
        return BaseResult.successWithData(cooperative1);
    }

    @RequestMapping("/findPage")
    @ApiOperation(value = "合作伙伴 分页", notes = "合作伙伴 分页")
    public BaseResult findPage(@RequestBody Page<Cooperative> page) {
        try {
            String key = "64E477967F3648FDAF7D" + JSON.toJSONString(page);
            Object o = redisTemplate.boundHashOps(fKey).get(key);
            if (null != o) {
                Page<Cooperative> list = JSON.parseObject(o.toString(), Page.class);
                return BaseResult.successWithData(list);
            }
            Cooperative cooperative = page.getRecords().get(0);
            EntityWrapper<Cooperative> wrapper = new EntityWrapper<>();
            wrapper.setEntity(cooperative);
            Page<Cooperative> list = cooperativeService.selectPage(page, wrapper);
            redisTemplate.boundHashOps(fKey).put(key, JSON.toJSONString(list));
            return BaseResult.successWithData(list);
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.FAIL.getCode(), "异常");
        }
    }
}
