package com.fzm.mall.server.front.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzm.mall.server.front.enums.GoodsRedisEnum;
import com.fzm.mall.server.front.goods.model.Banner;
import com.fzm.mall.server.front.goods.mapper.BannerMapper;
import com.fzm.mall.server.front.goods.service.IBannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.front.user.service.IRedisService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fzm
 * @since 2021-03-02
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements IBannerService {

    @Autowired
    private IRedisService redisService;

    @Override
    public List<Banner> getList() {
        String goodsBanners = redisService.getKey(GoodsRedisEnum.GOODS_REDIS_BANNER_KEY.value());
        List<Banner> banners = new ArrayList<>();
        if(StringUtil.isNullOrEmpty(goodsBanners)){
            QueryWrapper<Banner> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status",1);
            queryWrapper.orderByDesc("indexs");
            queryWrapper.last("limit 0,5");
            banners = list(queryWrapper);
            redisService.setKey(GoodsRedisEnum.GOODS_REDIS_BANNER_KEY.value(), JSON.toJSONString(banners));
            return banners;
        }else{
            try{
                banners =  JSON.parseArray(goodsBanners,Banner.class);
            }catch (Exception e){
                log.error("banner 转化异常");
                return  banners;
            }
            return banners;
        }

    }
}
