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.Brief;
import com.heketai.website.service.IBriefService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 专利 荣誉简介表 前端控制器
 * </p>
 *
 * @author xjl
 * @since 2019-11-20
 */
@RestController
@RequestMapping("/brief")
public class BriefController {
    @Autowired
    private IBriefService briefService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private String fKey = "F1B7B33E-2C2D-AF33-E83C-E7E20FF8916E";

    @RequestMapping("/insert")
    @ApiOperation(value = "新增荣誉简介", notes = "新增荣誉简介")
    public BaseResult insert(@RequestBody Brief brief) {
        if (brief.getLanguage() == 1 && brief.getCategory() == 0) {
            brief.setLanguage(0);
        }
        if (brief.getLanguage() == 2 && brief.getCategory() == 0) {
            brief.setLanguage(1);
        }
        briefService.insert(brief);
        stringRedisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/deleteById")
    @ApiOperation(value = "删除荣誉简介", notes = "删除荣誉简介")
    public BaseResult deleteById(@RequestParam Integer id) {
        briefService.deleteById(id);
        stringRedisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/update")
    @ApiOperation(value = "修改荣誉简介", notes = "修改荣誉简介")
    public BaseResult update(@RequestBody Brief brief) {
        briefService.updateById(brief);
        stringRedisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/getById")
    @ApiOperation(value = "荣誉简介 id查", notes = "荣誉简介 id查")
    public BaseResult<Brief> getById(@RequestParam Integer id) {
        String key = "F7DC8287-B9AC-B825-CD3BB738BCFE"+id;
        Object o = stringRedisTemplate.boundHashOps(fKey).get(key);
        if (null != o){
            Brief brief = JSON.parseObject(o.toString(), Brief.class);
            return BaseResult.successWithData(brief);
        }
        Brief brief = briefService.selectById(id);
        stringRedisTemplate.boundHashOps(fKey).put(key, JSON.toJSONString(brief));
        return BaseResult.successWithData(brief);
    }

    @RequestMapping("/findAll")
    @ApiOperation(value = "查所有荣誉简介", notes = "查所有荣誉简介")
    public BaseResult findAll() {
        String key = "5ABD6B7D-7B11-4086-9D63-E45C788190EE";
        Object o = stringRedisTemplate.boundHashOps(fKey).get(key);
        if (null != o){
            List<Brief> briefs = JSON.parseArray(o.toString(), Brief.class);
            return BaseResult.successWithData(briefs);
        }
        List<Brief> briefs = briefService.selectList(new EntityWrapper<>());
        stringRedisTemplate.boundHashOps(fKey).put(key, JSON.toJSONString(briefs));
        return BaseResult.successWithData(briefs);
    }

    @RequestMapping("/findPage")
    @ApiOperation(value = "荣誉简介 分页", notes = "荣誉简介 分页")
    public BaseResult<Page<Brief>> findPage(@RequestBody Page<Brief> page) {
        String key = "ADC6350A-B9A54D2F9A40"+page.toString();
//        Object o = stringRedisTemplate.boundHashOps(fKey).get(key);
//        if (null != o){
//            Page<Brief> briefPage = JSON.parseObject(o.toString(),  Page.class);
//            return BaseResult.successWithData(briefPage);
//        }
        EntityWrapper<Brief> entityWrapper = new EntityWrapper<>();
        List<Brief> records = page.getRecords();
        Brief brief = records.get(0);
        Integer language = brief.getLanguage();
        if (brief.getLanguage() == 1  && page.getSize() == 10){
            brief.setLanguage(0);
        }
        if (brief.getLanguage() == 2 && page.getSize() == 10){
            brief.setLanguage(1);
        }
        entityWrapper.orderBy("briefTime",false);
        entityWrapper.setEntity(page.getRecords().get(0));
        Page<Brief> briefPage = briefService.selectPage(page, entityWrapper);
        stringRedisTemplate.boundHashOps(fKey).put(key, JSON.toJSONString(briefPage));
        return BaseResult.successWithData(briefPage);
    }

    @RequestMapping("/briefList")
    @ApiOperation(value = "荣誉简介 分页", notes = "荣誉简介 分页")
    public BaseResult briefList(@RequestBody Page<Brief> page) {
        String key = "D748F13A-5B9D7571488A"+page.toString();
        Object o = stringRedisTemplate.boundHashOps(fKey).get(key);
//        if (null != o){
//            Map<String,Object> map = JSON.parseObject(o.toString(),  Map.class);
//            return BaseResult.successWithData(map);
//        }
        EntityWrapper<Brief> entityWrapper = new EntityWrapper<>();
        entityWrapper.setEntity(page.getRecords().get(0));
        Page<Brief> briefPage = briefService.selectPage(page, entityWrapper);
        List<Brief> records = briefPage.getRecords();
        List<Brief> zhiliang = new ArrayList<>();
        List<Brief> rohs = new ArrayList<>();
        List<Brief> zhuanli = new ArrayList<>();
        for(Brief bf : records){
            if(bf.getTyped() == 1){
                zhiliang.add(bf);
            }else if(bf.getTyped() == 2){
                rohs.add(bf);
            }else {
                zhuanli.add(bf);
            }
        }
        Map<String,Object> map = new HashMap();
        map.put("zhiliang",zhiliang);
        map.put("rohs",rohs);
        map.put("zhuanli",zhuanli);
        stringRedisTemplate.boundHashOps(fKey).put(key, JSON.toJSONString(map));
        return BaseResult.successWithData(map);
    }
}
