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.AdCategory;
import com.heketai.website.entity.Advertisement;
import com.heketai.website.service.IAdCategoryService;
import com.heketai.website.service.IAdvertisementService;
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.List;

/**
 * <p>
 * 广告表 前端控制器
 * </p>
 *
 * @author xjl
 * @since 2019-11-20
 */
@RestController
@RequestMapping("/advertisement")
@Api(value = "广告管理接口", tags = "广告管理页面")
public class AdvertisementController {
    @Autowired
    private IAdvertisementService advertisementService;

    @Autowired
    private IAdCategoryService adCategoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    private String fKey = "findByAdvertisementAndAdCategory";

    @RequestMapping("/insert")
    @ApiOperation(value = "新增广告", notes = "新增广告")
    public BaseResult insert(@RequestBody Advertisement advertisement) {
        advertisementService.insert(advertisement);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/deleteById")
    @ApiOperation(value = "删除广告", notes = "删除广告")
    public BaseResult deleteById(@RequestParam Integer id) {
        advertisementService.deleteById(id);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/update")
    @ApiOperation(value = "修改广告", notes = "修改广告")
    public BaseResult update(@RequestBody Advertisement advertisement) {
        advertisementService.updateById(advertisement);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/getById")
    @ApiOperation(value = "广告 id查", notes = "广告 id查")
    public BaseResult<Advertisement> getById(@RequestParam Integer id) {
        return BaseResult.successWithData(advertisementService.selectById(id));
    }

    @RequestMapping("/findPage")
    @ApiOperation(value = "广告 分页", notes = "广告 分页")
    public BaseResult<Page<Advertisement>> findPage(@RequestBody Page<Advertisement> page) {
        try {
            String key = "findPageAdvertisement" + JSON.toJSONString(page);
            Object redisPage = redisTemplate.boundHashOps(fKey).get(key);
            if (redisPage != null) {
                Page<Advertisement> advertisementPage = JSON.parseObject(redisPage.toString(),Page.class);
                return BaseResult.successWithData(advertisementPage);
            }

            Page<Advertisement> advertisementPage = advertisementService.selectPage(page);
            List<AdCategory> adCategories = adCategoryService.selectList(new EntityWrapper<>());
            for (Advertisement at : advertisementPage.getRecords()) {
                for (AdCategory ac : adCategories) {
                    if (at.getCategoryId().equals(ac.getId() + "")) {
                        at.setCategoryId(ac.getName());
                    }
                }
            }
            redisTemplate.boundHashOps(fKey).put(key, JSON.toJSONString(advertisementPage));
            return BaseResult.successWithData(advertisementPage);
        } catch (Exception e) {
            return BaseResult.fail("广告分页查询失败");
        }

    }

    @RequestMapping("/getByType")
    @ApiOperation(value = "广告 type查", notes = "广告 type查")
    public BaseResult<List<Advertisement>> getByType(@RequestParam Integer type) {
        try{
            String key ="getByTypeAdvertisement"+String.valueOf(type);
            Object redisGet = redisTemplate.boundHashOps(fKey).get(key);
            if (redisGet!=null){
                List<Advertisement> advertisements = JSON.parseArray(redisGet.toString(), Advertisement.class);
                return BaseResult.successWithData(advertisements);
            }

            EntityWrapper wrapper = new EntityWrapper();
            Advertisement advertisement = new Advertisement();
            if (type == 0) {
                advertisement.setCategoryId("1");
            } else {
                advertisement.setCategoryId("2");
            }
            wrapper.setEntity(advertisement);
            List<Advertisement> advertisementList = advertisementService.selectList(wrapper);
            redisTemplate.boundHashOps(fKey).put(key,JSON.toJSONString(advertisementList));
            return BaseResult.successWithData(advertisementList);
        }catch (Exception e){
            return BaseResult.fail("广告分类查询失败");
        }

    }
}
