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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzm.mall.server.front.goods.model.GoodSpu;
import com.fzm.mall.server.front.goods.mapper.GoodSpuMapper;
import com.fzm.mall.server.front.goods.service.IGoodSpuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.front.redis.GoodRedis;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author fzm
 * @since 2021-03-02
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class GoodSpuServiceImpl extends ServiceImpl<GoodSpuMapper, GoodSpu> implements IGoodSpuService {
    private final GoodRedis goodRedis;

    @Autowired
    private GoodSpuMapper goodSpuMapper;

    /**
     * 获取商品基础信息
     */
    @Override
    public GoodSpu goodSpu(final String gid) {
        GoodSpu goodGoodSpuVO = goodRedis.getSpu(gid);
        if (goodGoodSpuVO != null) {
            return goodGoodSpuVO;
        }
        QueryWrapper<GoodSpu> qw = new QueryWrapper<>();
        qw.eq("goods_id", gid);
        goodGoodSpuVO = getOne(qw);
        if (goodGoodSpuVO != null) {
            goodRedis.setSpu(gid, goodGoodSpuVO);
        }
        return goodGoodSpuVO;
    }

    @Override
    public String getTemplateIdByGoodsId(String goodsId) {
        return goodSpuMapper.getTemplateIdByGoodsId(goodsId);
    }

    @Override
    public int getCommodityBySkuId(String skuId) {
        return goodSpuMapper.getCommodityBySkuId(skuId);
    }

}
