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

import com.alibaba.fastjson.JSON;
import com.fzm.mall.server.front.goods.model.Sku;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fzm.mall.server.front.goods.model.vo.SkuVo;
import com.fzm.mall.server.front.order.model.vo.ShopInfoVo;
import org.apache.ibatis.annotations.Mapper;
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 fzm
 * @since 2021-03-09
 */
@Mapper
public interface SkuMapper extends BaseMapper<Sku> {
    @Select("select * from goods_sku where goods_id = #{goodsId} and status = 1")
    List<SkuVo> getByGoodsIs(@Param("goodsId") String goodsId);

    @Select("select goods_id from goods_sku where sku_id = #{skuId}")
    String getGoodsIdByskuId(@Param("skuId") String skuId);

    @Select("select a.*,b.type,b.name as name, b.merchant_id as merchantId,c.enterprise_name as merchantName  from goods_sku a left join goods_spu b on a.goods_id = b.goods_id left join  merchant c on b.merchant_id = c.merchant_id where a.sku_id = #{skuId}")
    SkuVo getBySkuId(@Param("skuId") String skuId);

    @Select("select address_name as addr,contact_name as name,contact_phone as phone from distribution_address where address_id = #{addressId} ")
    ShopInfoVo getShopInfoVo(@Param("addressId") String addressId);

    @Select("select exp_name from distribution_express where simple_name = #{returnExpressName}")
    String getExpressName(@Param("returnExpressName") String returnExpressName);

    @Select("select  b.merchant_id as merchantId  from goods_sku a left join goods_spu b on a.goods_id = b.goods_id left join  merchant c on b.merchant_id = c.merchant_id where a.sku_id = #{skuId}")
    String getSidBySkuId(@Param("skuId") String skuId);

    @Select("select token_id from external_token where label_name = #{coin} limit 1")
    Long getTrueCoin(@Param("coin") String coin);

    @Select("select  token_id from goods_sku a left join  external_token b on a.commodity_pass_id = b.commodity_pass_id where a.sku_id = #{skuId}")
    Long getTokenId(@Param("skuId") String skuId);

    @Select("select serial_no from goods_sku_usufruct where commodity_pass_id = #{commodity_pass_id}")
    String getSerialNo(@Param("commodity_pass_id") String tokenId);

    @Update("update goods_sku_usufruct set serial_no = #{toJSONString} where commodity_pass_id = #{commodityPassId}")
    int updateSerialNo(@Param("toJSONString") String toJSONString, @Param("commodityPassId") String commodityPassId);
}
