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.NewsCategory;
import com.heketai.website.service.INewsCategoryService;
import io.swagger.annotations.Api;
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.*;

/**
 * <p>
 * 资讯分类列表 前端控制器
 * </p>
 *
 * @author xjl
 * @since 2019-11-19
 */
@RestController
@RequestMapping("/newsCategory")
@Api(value = "资讯分类列表",tags = "新闻中心和应用方案")
public class NewsCategoryController {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private INewsCategoryService newsCategoryService;

    private String fKey = "6537F4C5-EB00-4DF1-8C24-B861EA2EAB13";

    @RequestMapping("/insert")
    @ApiOperation(value = "新增资讯分类", notes = "新增资讯分类")
    public BaseResult insert(@RequestBody NewsCategory newsCategory) {
        if(newsCategory.getSort() ==11){
            newsCategory.setType(0);
        }
        newsCategoryService.insert(newsCategory);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/deleteById")
    @ApiOperation(value = "删除资讯分类", notes = "删除资讯分类")
    public BaseResult deleteById(@RequestParam Integer id) {
        newsCategoryService.deleteById(id);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/update")
    @ApiOperation(value = "修改资讯分类", notes = "修改资讯分类")
    public BaseResult update(@RequestBody NewsCategory newsCategory) {
        newsCategoryService.updateById(newsCategory);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/getById")
    @ApiOperation(value = "咨讯分类 id查", notes = "资讯分类 id查")
    public BaseResult<NewsCategory> getById(@RequestParam Integer id) {
        String key = "06AA8EBF-67C8-3A50-B22C-A196837AAEF6"+id;
        Object obj = redisTemplate.boundHashOps(fKey).get(key);
        if (null != obj){
            NewsCategory newsCategory = JSON.parseObject(obj.toString(), NewsCategory.class);
            return BaseResult.successWithData(newsCategory);
        }
        NewsCategory newsCategory = newsCategoryService.selectById(id);
        redisTemplate.boundHashOps(fKey).put(key,JSON.toJSONString(newsCategory));
        return BaseResult.successWithData(newsCategory);
    }

    @RequestMapping("/findAll")
    @ApiOperation(value = "查所有资讯分类", notes = "查所有资讯分类")
    public BaseResult<List<NewsCategory>> findAll(@RequestParam Integer language) {
        String key ="e545948deb0f4873b8361c9165afdfe3"+language;
        Object obj = redisTemplate.boundHashOps(fKey).get(key);
        if (null != obj){
            List<NewsCategory> newsCategories = JSON.parseArray(obj.toString(), NewsCategory.class);
            return BaseResult.successWithData(newsCategories);
        }
        NewsCategory newsCategory=new NewsCategory();
        newsCategory.setLanguage(language);
        List<NewsCategory> newsCategories = newsCategoryService.selectList(new EntityWrapper<>(newsCategory));
        redisTemplate.boundHashOps(fKey).put(key,JSON.toJSONString(newsCategories));
        return BaseResult.successWithData(newsCategories);
    }

    @RequestMapping("/findPage")
    @ApiOperation(value = "资讯分类分页", notes = "资讯分类分页")
    public BaseResult<Page<NewsCategory>> findPage(@RequestBody Page<NewsCategory> page) {
        String key = "f7802357b6c645d7ae0fea7b1c90b8e3"+JSON.toJSONString(page);
        Object obj = redisTemplate.boundHashOps(fKey).get(key);
        if (null != obj){
            Page<NewsCategory> newsCategoryPage = JSON.parseObject(obj.toString(), Page.class);
            return BaseResult.successWithData(newsCategoryPage);
        }
        EntityWrapper<NewsCategory> entityWrapper = new EntityWrapper<>();
        NewsCategory newsCategory = page.getRecords().get(0);
        if(Objects.nonNull(newsCategory)){
            entityWrapper.setEntity(newsCategory);
        }
        Page<NewsCategory> newsCategoryPage = newsCategoryService.selectPage(page, entityWrapper);
        redisTemplate.boundHashOps(fKey).put(key,JSON.toJSONString(newsCategoryPage));
        return BaseResult.successWithData(newsCategoryPage);
    }

    @RequestMapping("/findListByPage")
    @ApiOperation(value = "资讯分类分页", notes = "资讯分类分页")
    public Object findListByPage(@RequestBody Page<NewsCategory> page) {
        String key = "9dd45bd333c044018b94fce2b0754d22"+JSON.toJSONString(page);
        Object obj = redisTemplate.boundHashOps(fKey).get(key);
        if (null != obj){
            Map<String,Object> map = JSON.parseObject(obj.toString(), HashMap.class);
            return BaseResult.successWithData(map);
        }
        EntityWrapper<NewsCategory> entityWrapper = new EntityWrapper<>();
        NewsCategory newsCategory = page.getRecords().get(0);
        if(Objects.nonNull(newsCategory)){
            entityWrapper.setEntity(newsCategory);
        }
        Page<NewsCategory> newsCategoryPage = newsCategoryService.selectPage(page, entityWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("total",newsCategoryPage.getTotal());
        map.put("current",newsCategoryPage.getCurrent());
        map.put("size",newsCategoryPage.getSize());
        map.put("pages",newsCategoryPage.getPages());
        List<NewsCategory> records = newsCategoryPage.getRecords();
        if(newsCategory.getType() == 1){
            List<NewsCategory> hyNews = new ArrayList<>();
            List<NewsCategory> jsNews = new ArrayList<>();
            for(NewsCategory nc : records){
                if(nc.getSort() == 1) {
                    hyNews.add(nc);
                }else {
                    jsNews.add(nc);
                }
            }
            map.put("hyNews",hyNews);
            map.put("jsNews",jsNews);
        }else {
            List<NewsCategory> gywmNews = new ArrayList<>();
            List<NewsCategory> gsjsNews = new ArrayList<>();
            List<NewsCategory> joinNews = new ArrayList<>();
            for(NewsCategory nc : records){
                if(nc.getSort() == 11) {
                    gywmNews.add(nc);
                }else if(nc.getSort() == 12) {
                    gsjsNews.add(nc);
                }else {
                    joinNews.add(nc);
                }
            }
            map.put("gywmNews",gywmNews);
            map.put("gsjsNews",gsjsNews);
            map.put("joinNews",joinNews);
        }
        redisTemplate.boundHashOps(fKey).put(key,JSON.toJSONString(map));
        return BaseResult.successWithData(map);
    }
}
