package com.fzm.mall.server.front.goods.mapper;

import com.fzm.mall.server.front.asset.model.vo.good.GoodSkuVO;
import com.fzm.mall.server.front.asset.model.vo.good.GoodSpuVO;
import com.fzm.mall.server.front.goods.model.GoodSku;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fzm.mall.server.front.goods.model.vo.SkuVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 商品规格表 Mapper 接口
 * </p>
 *
 * @author tdz
 * @since 2021-03-02
 */
public interface GoodSkuMapper extends BaseMapper<GoodSku> {
    /**
     * 通过币种查询编号
     */
    @Select("select goods_sku.goods_id from goods_sku left join goods_spu on goods_sku.goods_id = goods_spu.goods_id " +
            "where goods_sku.coin = #{coin} and goods_sku.is_active = 1 and goods_spu.type = 0")
    String skuId(String coin);

    @Select("select a.*,p.thumb picture ,b.shop_name enterpriseName,b.merchant_id merchantId,p.name goodsName,p.description from goods_sku a left join goods_spu p on a.goods_id = p.goods_id left join shop b on b.merchant_id=p.merchant_id  where a.sku_id = #{skuId}")
    GoodSkuVO getGoodSkuVo(String skuId);


    /**
     * 根据token名称查询是否存在，全新
     */
    @Select("select goods_sku.* from goods_sku left join goods_spu on goods_sku.goods_id = goods_spu.goods_id " +
            "where goods_sku.coin = #{coin} and goods_sku.is_active = 1 and goods_spu.type = 0")
    GoodSku skuUnitByCoin(String coin);

    /**
     * 根据token名称查询是否存在
     */
    @Select("select * from goods_sku where goods_id = #{gid} and coin = #{token} and is_active = 1")
    GoodSku skuUnitByGidCoin(@Param("gid") String gid, @Param("token") String token);


    /**
     * 获取商品销售信息
     */
    @Select("select * from goods_sku where goods_id = #{gid} and is_active = 1")
    List<GoodSku> goodSku(String gid);

    /**
     * 减少商品库存
     */
    @Update("update goods_sku set stock = stock - #{number} where goods_id = #{gid} and coin = #{coin} and is_active = 1 and (stock >= #{number})")
    Integer goodStockMinus(@Param("gid") String gid, @Param("coin") String coin, @Param("number") Integer number);

    /**
     * 恢复商品库存
     */
    @Update("update goods_sku set stock = stock + #{number} where goods_id = #{gid} and coin = #{coin} and is_active = 1")
    Integer goodRestore(@Param("gid") String gid, @Param("coin") String coin, @Param("number") String number);

    @Select("select a.merchant_id as merchantId,a.template_id as templateId,a.name as name,a.goods_id as goodsId,b.* from goods_spu a left join goods_sku b on a.goods_id = b.goods_id where b.sku_id = #{skuId}")
    SkuVo getSkuVoBySkuId(@Param("skuId") String skuId);

    @Update("update goods_sku set stock =stock - #{num} where sku_id = #{skuId} and stock >= #{num}")
    int delStock(@Param("skuId") String skuId, @Param("num") Integer num);
    @Update("update goods_sku set stock =stock + #{num} where sku_id = #{skuId}")
    void addStock(@Param("skuId") String skuId, @Param("num") Integer number);

    @Update("update goods_sku set sales =sales - #{num} where sku_id = #{skuId} and sales >= #{number}")
    void delSales(@Param("skuId")String skuId, @Param("num")Integer number);

    @Update("update goods_sku set sales =sales + #{num} where sku_id = #{skuId}")
    void addSales(@Param("skuId")String skuId, @Param("num")Integer number);

    @Select("select prop_val from goods_sku_prop where sku_id = #{skuId}")
    List<String> listProval(String skuId);

    @Select("select name from goods_spu where goods_id = #{goodsId}")
    String getGoodsName(@Param("goodsId") String goodsId);
}
