<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.tmerclub.cloud.product.mapper.SpuMapper">
    <resultMap id="spuMap" type="com.tmerclub.cloud.product.model.Spu">
        <id column="spu_id" property="spuId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="shop_id" property="shopId"/>
        <result column="brand_id" property="brandId"/>
        <result column="category_id" property="categoryId"/>
        <result column="shop_category_id" property="shopCategoryId"/>
        <result column="img_urls" property="imgUrls"/>
        <result column="main_img_url" property="mainImgUrl"/>
        <result column="video" property="video"/>
        <result column="price_fee" property="priceFee"/>
        <result column="market_price_fee" property="marketPriceFee"/>
        <result column="status" property="status"/>
        <result column="delivery_mode" property="deliveryMode"/>
        <result column="delivery_template_id" property="deliveryTemplateId"/>
        <result column="seq" property="seq"/>
        <result column="is_top" property="isTop"/>
        <result column="is_refund" property="isRefund"/>
        <result column="activity_id" property="activityId"/>
        <result column="spu_type" property="spuType"/>
        <result column="spu_mold" property="spuMold"/>
        <result column="has_sku_img" property="hasSkuImg"/>
        <result column="spu_code" property="spuCode"/>
        <result column="supplier_spu_type" property="supplierSpuType"/>
        <result column="supplier_spu_id" property="supplierSpuId"/>
        <result column="supplier_id" property="supplierId"/>
        <result column="sku_choice_status" property="skuChoiceStatus"/>
        <result column="supplier_delivery_type" property="supplierDeliveryType"/>
        <result column="measure_unit" property="measureUnit"/>
        <result column="pre_sale_type" property="preSaleType"/>
        <result column="deposit_type" property="depositType"/>
        <result column="deposit_amount" property="depositAmount"/>
        <result column="deposit_expansion_amount" property="depositExpansionAmount"/>
        <result column="deposit_start_time" property="depositStartTime"/>
        <result column="deposit_end_time" property="depositEndTime"/>
        <result column="balance_start_time" property="balanceStartTime"/>
        <result column="balance_end_time" property="balanceEndTime"/>
        <result column="pre_sale_delivery_type" property="preSaleDeliveryType"/>
        <result column="pre_sale_delivery_time" property="preSaleDeliveryTime"/>
        <result column="virtual_remark" property="virtualRemark"/>
        <result column="write_off_num" property="writeOffNum"/>
        <result column="write_off_multiple_count" property="writeOffMultipleCount"/>
        <result column="write_off_time" property="writeOffTime"/>
        <result column="write_off_start" property="writeOffStart"/>
        <result column="write_off_end" property="writeOffEnd"/>
        <result column="delivery_amount" property="deliveryAmount"/>
    </resultMap>
    <resultMap id="supplierSpuExcelVO" type="com.tmerclub.cloud.product.vo.SupplierSpuExcelVO">
        <result column="spu_id" property="spuId"/>
        <result column="category_id" property="categoryId"/>
        <result column="shop_category_id" property="shopCategoryId"/>
        <result column="status" property="status"/>
        <result column="delivery_mode" property="deliveryMode"/>
        <result column="delivery_template_id" property="deliveryTemplateId"/>
        <result column="supplier_id" property="supplierId"/>
        <result column="sku_choice_status" property="skuChoiceStatus"/>
        <result column="supplier_delivery_type" property="supplierDeliveryType"/>
        <collection property="brandLangList" ofType="com.tmerclub.cloud.common.product.vo.BrandLangVO">
            <id column="b_lang" property="lang"/>
            <result column="brand_name" property="name"/>
        </collection>
        <collection property="spuLangList" ofType="com.tmerclub.cloud.common.product.vo.SpuLangVO">
            <id column="lang" property="lang"/>
            <result column="spu_name" property="spuName"/>
            <result column="selling_point" property="sellingPoint"/>
        </collection>
    </resultMap>
    <resultMap id="spuDetailMap" type="com.tmerclub.cloud.common.product.vo.SpuVO"
               extends="com.tmerclub.cloud.product.mapper.SpuMapper.spuMap">
        <result column="score_fee" property="scoreFee"/>
        <result column="is_syn_update" property="isSynUpdate"/>
        <result column="supplier_spu_type" property="supplierSpuType"/>
        <result column="is_update_msg" property="isUpdateMsg"/>
        <result column="name" property="name"/>
        <result column="supplier_delivery_type" property="supplierDeliveryType"/>
        <collection property="detailList" ofType="com.tmerclub.cloud.common.product.vo.SpuDetailVO">
            <id column="detail_lang" property="lang"/>
            <result column="detail" property="detail"/>
        </collection>
        <collection property="spuLangList" ofType="com.tmerclub.cloud.common.product.vo.SpuLangVO">
            <id column="lang" property="lang"/>
            <result column="spu_name" property="spuName"/>
            <result column="selling_point" property="sellingPoint"/>
        </collection>
        <collection property="skus" ofType="com.tmerclub.cloud.common.product.vo.SkuVO">
            <id column="sku_id" property="skuId"/>
            <result column="supplier_sku_id" property="supplierSkuId"/>
            <result column="stock_point_id" property="stockPointId"/>
        </collection>
    </resultMap>
    <resultMap id="spuAndSkuMap" type="com.tmerclub.cloud.common.product.vo.SpuVO"
             extends="com.tmerclub.cloud.product.mapper.SpuMapper.spuMap">
    <result column="spu_name" property="name"/>
    <result column="detail" property="detail"/>
    <collection property="skus" ofType="com.tmerclub.cloud.common.product.vo.SkuVO">
      <id column="sku_id" property="skuId"/>
      <result column="create_time" property="createTime"/>
      <result column="update_time" property="updateTime"/>
      <result column="spu_id" property="spuId"/>
      <result column="sku_name" property="skuName"/>
      <result column="img_url" property="imgUrl"/>
      <result column="price_fee" property="priceFee"/>
      <result column="market_price_fee" property="marketPriceFee"/>
      <result column="status" property="status"/>
      <result column="party_code" property="partyCode"/>
      <result column="model_id" property="modelId"/>
      <result column="weight" property="weight"/>
      <result column="volume" property="volume"/>
      <result column="score_fee" property="scoreFee"/>
    </collection>
  </resultMap>
    <resultMap id="spuExcelVO" type="com.tmerclub.cloud.product.vo.SpuExcelVO">
        <result column="spu_id" property="spuId"/>
        <result column="spu_mold" property="spuMold"/>
        <result column="spu_type" property="spuType"/>
        <result column="is_refund" property="isRefund"/>
        <result column="main_img_url" property="mainImgUrl"/>
        <result column="imgs" property="imgs"/>
        <result column="video" property="video"/>
        <result column="virtual_remark" property="virtualRemark"/>
        <result column="category_id" property="categoryId"/>
        <result column="shop_category_id" property="shopCategoryId"/>
        <result column="status" property="status"/>
        <result column="delivery_mode" property="deliveryMode"/>
        <result column="delivery_template_id" property="deliveryTemplateId"/>
        <result column="delivery_amount" property="deliveryAmount"/>
        <result column="write_off_num" property="writeOffNum"/>
        <result column="write_off_multiple_count" property="writeOffMultipleCount"/>
        <result column="write_off_time" property="writeOffTime"/>
        <result column="write_off_start" property="writeOffStart"/>
        <result column="write_off_end" property="writeOffEnd"/>
        <collection property="brandLangList" ofType="com.tmerclub.cloud.common.product.vo.BrandLangVO">
            <id column="b_lang" property="lang"/>
            <result column="brand_name" property="name"/>
        </collection>
        <collection property="spuLangList" ofType="com.tmerclub.cloud.common.product.vo.SpuLangVO">
            <id column="lang" property="lang"/>
            <result column="spu_name" property="spuName"/>
            <result column="selling_point" property="sellingPoint"/>
        </collection>
        <collection property="spuDetailList" ofType="com.tmerclub.cloud.common.product.vo.SpuDetailVO">
            <id column="sd_lang" property="lang"/>
            <result column="detail" property="detail"/>
        </collection>
    </resultMap>

    <resultMap id="platformExcelMap" type="com.tmerclub.cloud.product.vo.PlatformSpuExcelVO">
        <result column="spu_id" property="spuId"/>
        <result column="spu_mold" property="spuMold"/>
        <result column="spu_type" property="spuType"/>
        <result column="is_refund" property="isRefund"/>
        <result column="virtual_remark" property="virtualRemark"/>
        <result column="category_id" property="categoryId"/>
        <result column="shop_category_id" property="shopCategoryId"/>
        <result column="status" property="status"/>
        <result column="delivery_mode" property="deliveryMode"/>
        <result column="delivery_template_id" property="deliveryTemplateId"/>
        <result column="water_sold_num" property="waterSoldNum"/>
        <result column="is_top" property="isTop"/>
        <result column="write_off_num" property="writeOffNum"/>
        <result column="write_off_multiple_count" property="writeOffMultipleCount"/>
        <result column="write_off_time" property="writeOffTime"/>
        <result column="write_off_start" property="writeOffStart"/>
        <result column="write_off_end" property="writeOffEnd"/>
        <collection property="brandLangList" ofType="com.tmerclub.cloud.common.product.vo.BrandLangVO">
            <id column="b_lang" property="lang"/>
            <result column="brand_name" property="name"/>
        </collection>
        <collection property="spuLangList" ofType="com.tmerclub.cloud.common.product.vo.SpuLangVO">
            <id column="lang" property="lang"/>
            <result column="spu_name" property="spuName"/>
            <result column="selling_point" property="sellingPoint"/>
        </collection>
    </resultMap>
    <resultMap id="esProductBO" type="com.tmerclub.cloud.common.product.bo.EsProductBO">
        <result column="spu_id" property="spuId"/>
        <result column="spu_name_zh" property="spuNameZh"/>
        <result column="spu_name_en" property="spuNameEn"/>
        <result column="selling_point_zh" property="sellingPointZh"/>
        <result column="selling_point_en" property="sellingPointEn"/>
        <result column="is_refund" property="isRefund"/>
        <result column="price_fee" property="priceFee"/>
        <result column="score_fee" property="scoreFee"/>
        <result column="market_price_fee" property="marketPriceFee"/>
        <result column="main_img_url" property="mainImgUrl"/>
        <result column="spu_type" property="spuType"/>
        <result column="spu_mold" property="spuMold"/>
        <result column="shop_id" property="shopId"/>
        <result column="spu_status" property="spuStatus"/>
        <result column="sale_num" property="saleNum"/>
        <result column="comment_num" property="commentNum"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="brand_id" property="brandId"/>
        <result column="seq" property="seq"/>
        <result column="activity_id" property="activityId"/>
        <result column="category_id" property="categoryId"/>
        <result column="shop_category_id" property="shopCategoryId"/>
        <result column="supplier_spu_type" property="supplierSpuType"/>
        <result column="supplier_id" property="supplierId"/>
        <result column="sku_choice_status" property="skuChoiceStatus"/>
        <result column="supplier_spu_id" property="supplierSpuId"/>
        <result column="spu_code" property="spuCode"/>
        <result column="supplier_delivery_type" property="supplierDeliveryType"/>
        <result column="virtual_remark" property="virtualRemark"/>
        <result column="pre_sale_type" property="preSaleType"/>
        <result column="delivery_mode" property="deliveryModeVO"/>
    </resultMap>
    <sql id="Vo_Column_List">
        s.`spu_id`,s.`shop_id`,s.`create_time`,s.`update_time`,s.`brand_id`,s.`shop_category_id`,s.`category_id`,s.`img_urls`,s.`main_img_url`,s.`price_fee`,s.`market_price_fee`,
        s.`status`,s.`has_sku_img`,s.`seq`,s.`is_top`,s.delivery_mode,s.delivery_template_id,s.is_refund,s.activity_id,s.spu_type,s.spu_mold,s.video,s.spu_code,s.`supplier_spu_type`,
        s.`supplier_spu_id`,s.`supplier_delivery_type`,s.`supplier_id`,s.`measure_unit`,s.`sku_choice_status`,s.`is_syn_update`,s.`is_update_msg`,s.`pre_sale_type`,s.`deposit_type`,
        s.`deposit_amount`,s.`deposit_expansion_amount`,s.`deposit_start_time`,s.`deposit_end_time`,s.`balance_start_time`,s.`balance_end_time`,s.`pre_sale_delivery_type`,s.`pre_sale_delivery_time`,s.`virtual_remark`,
        s.`write_off_num`,s.`write_off_multiple_count`,s.`write_off_time`,s.`write_off_start`,s.`write_off_end`,s.`delivery_amount`
    </sql>
    <select id="list" resultMap="spuDetailMap">
        select distinct
        <include refid="Vo_Column_List"/>
        ,sl.lang,sl.spu_name
        from spu s
        left join `spu_extension` se on s.`spu_id` = se.`spu_id`
        left join `spu_lang` sl on s.`spu_id` = sl.`spu_id`
        <if test="spu.partyCode != null and spu.partyCode != ''">
            join `sku` on s.`spu_id` = sku.`spu_id` and sku.party_code = #{spu.partyCode}
        </if>
        where s.status <![CDATA[<>]]> -1
        <if test="spu.spuId != null">
            and s.spu_id = #{spu.spuId}
        </if>
        <if test="spu.spuIds != null and spu.spuIds.size() > 0">
            and s.spu_id in
            <foreach collection="spu.spuIds" item="spuId" open="(" close=")" separator=",">
                #{spuId}
            </foreach>
        </if>
        <if test="spu.brandId != null">
            and s.brand_id = #{spu.brandId}
        </if>
        <if test="spu.categoryId != null">
            and s.category_id = #{spu.categoryId}
        </if>
        <if test="spu.name != null and spu.name != ''">
            and sl.spu_name like concat('%', #{spu.name}, '%')
        </if>
        <if test="spu.status != null">
            and s.status = #{spu.status}
        </if>
        <if test="spu.spuType != null">
            and s.spu_type = #{spu.spuType}
        </if>
        <if test="spu.spuStatus == 1">
            and s.status = 1
        </if>
        <if test="spu.spuStatus == 2">
            and s.status = 1
        </if>
        <if test="spu.spuStatus == 3">
            and s.status = 0
        </if>
        <if test="spu.maxPrice != null">
            and s.price_fee &lt;= #{spu.maxPrice}
        </if>
        <if test="spu.minPrice != null">
            and s.price_fee &gt;= #{spu.minPrice}
        </if>
        order by
        <trim prefixOverrides=",">
            <if test="spu.priceFeeSort == 0">
                s.price_fee desc
            </if>
            <if test="spu.priceFeeSort == 1">
                ,s.price_fee
            </if>
            <if test="spu.marketPriceFeeSort == 0">
                ,s.market_price_fee desc
            </if>
            <if test="spu.marketPriceFeeSort == 1">
                ,s.market_price_fee
            </if>
            <if test="spu.saleNumSort == 0">
                ,se.sale_num desc
            </if>
            <if test="spu.saleNumSort == 1">
                ,se.sale_num
            </if>
            <if test="spu.seqSort == 0">
                ,s.seq desc
            </if>
            <if test="spu.seqSort == 1">
                ,s.seq
            </if>
            <if test="spu.createTimeSort == 0">
                ,s.create_time desc
            </if>
            <if test="spu.createTimeSort == 1">
                ,s.create_time
            </if>
            <if test="spu.priceFeeSort == null and spu.marketPriceFeeSort == null and spu.saleNumSort == null
              and spu.seqSort == null and spu.createTimeSort == null">
              s.update_time desc, s.spu_id
            </if>
        </trim>
  </select>

  <select id="getBySpuId" resultMap="spuDetailMap">
    select
    <include refid="Vo_Column_List"/>
    ,
    sd.`detail`,sd.lang detail_lang,sl.lang,sl.spu_name,sl.selling_point, sl.spu_name as name, sl.spu_name, sl.selling_point
    FROM `spu` s
    LEFT JOIN spu_lang sl ON s.`spu_id` = sl.`spu_id`
    LEFT JOIN spu_detail sd ON s.`spu_id` = sd.`spu_id`
    WHERE s.`spu_id` = #{spuId}
  </select>


  <select id="getSpuInfoBySpuId" resultMap="spuDetailMap">
    select
    <include refid="Vo_Column_List"/>
    ,
    sd.`detail`,sd.lang detail_lang,sl.lang,sl.spu_name,sl.selling_point,sl.spu_name as name,sl.spu_name,sl.selling_point,sku.sku_id
    FROM `spu` s
    LEFT JOIN sku ON s.`spu_id` = sku.`spu_id`
    LEFT JOIN spu_lang sl ON s.`spu_id` = sl.`spu_id`
    LEFT JOIN spu_detail sd ON s.`spu_id` = sd.`spu_id`
    WHERE s.`spu_id` = #{spuId}
  </select>

    <insert id="save" useGeneratedKeys="true" keyProperty="spuId">
        insert into spu (`spu_id`, `brand_id`, `shop_id`, `category_id`, `shop_category_id`, `img_urls`, `main_img_url`, `video`,
                         `price_fee`, `market_price_fee`, `score_fee`, `status`, `delivery_mode`,
                         `delivery_template_id`, has_sku_img, `spu_type`,`spu_mold`,`is_refund`, `seq`, `spu_code`,
                         `supplier_spu_type`, `supplier_spu_id`, `supplier_delivery_type`, `supplier_id`,
                         `measure_unit`, `sku_choice_status`,`is_syn_update`,`pre_sale_type`,`deposit_type`,
                         `deposit_amount`,`deposit_expansion_amount`,`deposit_start_time`,`deposit_end_time`,
                         `balance_start_time`,`balance_end_time`,`pre_sale_delivery_type`,`pre_sale_delivery_time`,`virtual_remark`,
                         `write_off_num`,`write_off_multiple_count`,`write_off_time`,`write_off_start`,`write_off_end`, `delivery_amount`, `is_all_entity`)
         values (#{spu.spuId}, #{spu.brandId}, #{spu.shopId}, #{spu.categoryId}, #{spu.shopCategoryId}, #{spu.imgUrls},
                #{spu.mainImgUrl}, #{spu.video}, #{spu.priceFee}, #{spu.marketPriceFee}, #{spu.scoreFee}, #{spu.status},
                #{spu.deliveryMode}, #{spu.deliveryTemplateId}, #{spu.hasSkuImg}, #{spu.spuType}, #{spu.spuMold},
                #{spu.isRefund},#{spu.seq}, #{spu.spuCode}, #{spu.supplierSpuType}, #{spu.supplierSpuId}, #{spu.supplierDeliveryType},
                #{spu.supplierId}, #{spu.measureUnit}, #{spu.skuChoiceStatus},#{spu.isSynUpdate},#{spu.preSaleType},
                #{spu.depositType},#{spu.depositAmount},#{spu.depositExpansionAmount},#{spu.depositStartTime},#{spu.depositEndTime},
                #{spu.balanceStartTime},#{spu.balanceEndTime},#{spu.preSaleDeliveryType},#{spu.preSaleDeliveryTime},#{spu.virtualRemark},
                #{spu.writeOffNum},#{spu.writeOffMultipleCount},#{spu.writeOffTime},#{spu.writeOffStart},#{spu.writeOffEnd}, #{spu.deliveryAmount}, #{spu.isAllEntity});
    </insert>

    <update id="updateStatusBySpuId">
        update spu
        set status = -1
        where spu_id = #{spuId}
    </update>
    <update id="update">
        update spu
        set
        <if test="spu.categoryId != null">
            `category_id` = #{spu.categoryId},
        </if>
        <if test="spu.shopCategoryId != null">
            `shop_category_id` = #{spu.shopCategoryId},
        </if>
        <if test="spu.imgUrls != null">
            `img_urls` = #{spu.imgUrls},
        </if>
        <if test="spu.mainImgUrl != null">
            `main_img_url` = #{spu.mainImgUrl},
        </if>
        <if test="spu.video != null">
            `video` = #{spu.video},
        </if>
        <if test="spu.priceFee != null">
            `price_fee` = #{spu.priceFee},
        </if>
        <if test="spu.marketPriceFee != null">
            `market_price_fee` = #{spu.marketPriceFee},
        </if>
        <if test="spu.scoreFee != null">
            `score_fee` = #{spu.scoreFee},
        </if>
        <if test="spu.status != null">
            `status` = #{spu.status},
        </if>
        <if test="spu.deliveryMode != null">
            `delivery_mode` = #{spu.deliveryMode},
        </if>
        <if test="spu.deliveryTemplateId != null">
            `delivery_template_id` = #{spu.deliveryTemplateId},
        </if>
        <if test="spu.deliveryAmount != null">
            `delivery_amount` = #{spu.deliveryAmount},
        </if>
        <if test="spu.spuType != null">
            `spu_type` = #{spu.spuType},
        </if>
        <if test="spu.activityId != null">
            `activity_id` = #{spu.activityId},
        </if>
        <if test="spu.spuMold != null">
            `spu_mold` = #{spu.spuMold},
        </if>
        <if test="spu.isRefund != null">
            `is_refund` = #{spu.isRefund},
        </if>
        <if test="spu.isTop != null">
            `is_top` = #{spu.isTop},
        </if>
        <if test="spu.hasSkuImg != null">
            `has_sku_img` = #{spu.hasSkuImg},
        </if>
        <if test="spu.seq != null">
            `seq` = #{spu.seq},
        </if>
        <if test="spu.spuCode != null">
            `spu_code` = #{spu.spuCode},
        </if>
        <if test="spu.measureUnit != null">
            `measure_unit` = #{spu.measureUnit},
        </if>
        <if test="spu.supplierDeliveryType != null">
            `supplier_delivery_type` = #{spu.supplierDeliveryType},
        </if>
        <if test="spu.supplierSpuType != null">
            `supplier_spu_type` = #{spu.supplierSpuType},
        </if>
        <!--品牌是可以删除的, 0代表删除-->
        <if test="spu.brandId != null">
            `brand_id` = #{spu.brandId},
        </if>
        <if test="spu.preSaleType != null">
            pre_sale_type = #{spu.preSaleType},
        </if>
        <if test="spu.depositType != null">
            deposit_type = #{spu.depositType},
        </if>
        <if test="spu.depositAmount != null">
            deposit_amount = #{spu.depositAmount},
        </if>
        <if test="spu.depositExpansionAmount != null">
            deposit_expansion_amount = #{spu.depositExpansionAmount},
        </if>
        <if test="spu.depositStartTime != null">
            deposit_start_time = #{spu.depositStartTime},
        </if>
        <if test="spu.depositEndTime != null">
            deposit_end_time = #{spu.depositEndTime},
        </if>
        <if test="spu.balanceStartTime != null">
            balance_start_time = #{spu.balanceStartTime},
        </if>
        <if test="spu.balanceEndTime != null">
            balance_end_time = #{spu.balanceEndTime},
        </if>
        <if test="spu.preSaleDeliveryType != null">
            pre_sale_delivery_type = #{spu.preSaleDeliveryType},
        </if>
        <if test="spu.preSaleDeliveryTime != null">
            pre_sale_delivery_time = #{spu.preSaleDeliveryTime},
        </if>
        <if test="spu.virtualRemark != null">
            virtual_remark = #{spu.virtualRemark},
        </if>
        <if test="spu.writeOffNum != null">
            write_off_num = #{spu.writeOffNum},
        </if>
        <if test="spu.writeOffMultipleCount != null">
            write_off_multiple_count = #{spu.writeOffMultipleCount},
        </if>
        <if test="spu.writeOffTime != null">
            write_off_time = #{spu.writeOffTime},
        </if>
        <if test="spu.writeOffStart != null">
            write_off_start = #{spu.writeOffStart},
        </if>
        <if test="spu.writeOffEnd != null">
            write_off_end = #{spu.writeOffEnd},
        </if>
        <if test="spu.isAllEntity != null">
            is_all_entity = #{spu.isAllEntity},
        </if>
        `update_time` = NOW()
        where spu_id = #{spu.spuId}
    </update>

    <select id="loadEsProductBO" resultMap="esProductBO">
        SELECT s.spu_id,
               s.shop_id,
               s.price_fee,
               s.market_price_fee,
               s.img_urls,
               s.status              spu_status,
               s.score_fee,
               s.seq,
               s.is_top,
               s.spu_type,
               s.spu_mold,
               s.is_refund,
               s.create_time,
               s.update_time,
               s.category_id,
               s.main_img_url,
               s.category_id,
               s.shop_category_id,
               s.brand_id,
               s.shop_category_id AS shop_secondary_category_id,
               sl.spu_name_zh,
               sl.selling_point_zh,
               sl.spu_name_en,
               sl.selling_point_en,
               s.shop_category_id AS shop_secondary_category_id,
               s.activity_id,
               s.supplier_id,
               s.sku_choice_status,
               s.supplier_spu_id,
               s.spu_code,
               s.`supplier_spu_type`,
               s.`supplier_delivery_type`,
               s.`pre_sale_type`,
               s.`virtual_remark`,
               s.`delivery_mode`
        FROM spu s
        LEFT JOIN (SELECT zh.spu_id,
                       zh.spu_name      AS spu_name_zh,
                       zh.selling_point AS selling_point_zh,
                       en.spu_name      AS spu_name_en,
                       en.selling_point AS selling_point_en
            FROM spu_lang zh
            LEFT JOIN spu_lang en ON zh.spu_id = en.spu_id and en.lang = 2
            WHERE zh.lang = 1 AND zh.spu_id = #{spuId}
        ) sl ON s.spu_id = sl.spu_id
        WHERE s.spu_id = #{spuId}
    </select>

  <select id="getSpuIdsBySpuUpdateDTO" resultType="java.lang.Long">
    SELECT spu_id FROM spu
    where status = 1
    <if test="shopCategoryIds != null">
      AND shop_category_id IN
      <foreach collection="shopCategoryIds" item="shopCategoryId" open="(" close=")" separator=",">
        #{shopCategoryId}
      </foreach>
    </if>
    <if test="categoryIds != null">
      AND category_id IN
      <foreach collection="categoryIds" item="categoryId" open="(" close=")" separator=",">
        #{categoryId}
      </foreach>
    </if>
    <if test="brandId != null">
      AND brand_id = #{brandId}
    </if>
    <if test="shopId != null">
      AND shop_id = #{shopId}
    </if>
  </select>

  <update id="batchChangeSpuStatus">
    update spu set `status` = #{status}, `update_time` = NOW() where spu_id in
    <foreach collection="spuIds" item="spuId" open="(" close=")" separator=",">
      #{spuId}
    </foreach>
  </update>
  <update id="updateSpuUpdateTime">
    update spu set `update_time` = NOW()
    <where>
      <if test="categoryIds != null or shopIds != null">
        status = 1
      </if>
      <if test="categoryIds != null ">
        and category_id in
        <foreach collection="categoryIds" item="categoryId" open="(" close=")" separator=",">
          #{categoryId}
        </foreach>
      </if>
      <if test="spuIds != null">
        and spu_id in
        <foreach collection="spuIds" item="spuId" open="(" close=")" separator=",">
          #{spuId}
        </foreach>
      </if>
      <if test="shopIds != null">
        <if test="sysType == 1">
        and shop_id in
        </if>
        <if test="sysType == 3">
          and supplier_spu_type = 1 and supplier_id in
        </if>
        <foreach collection="shopIds" item="shopId" open="(" close=")" separator=",">
          #{shopId}
        </foreach>
      </if>
    </where>
  </update>
  <select id="listCanSeckillProd" resultMap="spuAndSkuMap">
    select spu.spu_id,spu.spu_name,spu.main_img_url,spu.status,spu.price_fee,
    sku.sku_id,sl.sku_name,sku.price_fee,sku.market_price_fee,sku.party_code,sku.model_id,sku.weight,sku.volume
    from (
        SELECT spu.*,pl.spu_name FROM spu spu
        LEFT JOIN spu_lang pl ON pl.spu_id = spu.spu_id AND pl.`lang` = #{lang}
        <where>
          spu.status = 1 and spu.spu_type = 0
          <if test="spuDTO.spuId != null and spuDTO.spuId != 0">
            AND spu.spu_id = #{spuDTO.spuId}
          </if>
          <if test="spuDTO.shopId != null">
            and spu.`shop_id` =#{spuDTO.shopId}
          </if>
          <if test="spuDTO.shopCategoryId != null">
            and spu.shop_category_id = #{spuDTO.shopCategoryId}
          </if>
          <if test="spuDTO.name != null and spuDTO.name != ''">
            and pl.spu_name like concat('%',#{spuDTO.name},'%')
          </if>
        </where>
        limit #{page.begin}, #{page.size}
    )as spu
    LEFT JOIN sku sku ON spu.spu_id = sku.spu_id AND sku.status != -1
    LEFT JOIN sku_lang sl ON sl.sku_id = sku.sku_id AND sl.`lang` = #{lang}
  </select>

  <select id="countByTransportId" resultType="int">
    SELECT ifnull(count(*),0) FROM spu
    <where>
      spu.status  <![CDATA[<>]]> -1 and delivery_template_id = #{transportId}
    </where>
  </select>

  <select id="getActivitySpuInfoByCategoryIdsAndShopId" resultType="com.tmerclub.cloud.common.product.vo.SpuVO">
    select spu_type,spu_id from spu
    where
    <if test="status != null">
      status = #{status}
    </if>
    <if test="status == null">
      `status` = 1
    </if>
    <if test="type == 1">
      and spu_id in
      <foreach collection="spuIds" item="spuId" open="(" close=")" separator=",">
        #{spuId}
      </foreach>
    </if>
    <if test="type == 2">
      <if test="shopId == 0">
        and category_id in
      </if>
      <if test="shopId != 0">
        and shop_category_id in
      </if>
      <foreach collection="categoryIds" item="categoryId" open="(" close=")" separator=",">
        #{categoryId}
      </foreach>
    </if>
    <if test="type == 3 and shopId != null">
      and shop_id = #{shopId}
    </if>
    <if test="type == 3 and shopIds != null">
        and shop_id in
        <foreach collection="shopIds" item="shopId" open="(" close=")" separator=",">
          #{shopId}
        </foreach>
    </if>
  </select>

  <update id="offlineSpuByShopId">
    update spu
    set status = 0
    where shop_id = #{shopId}
    and status = 1
  </update>

  <select id="excelSpuList" resultMap="spuExcelVO">
    SELECT
        spu.spu_id,
        spu.spu_mold,
        spu.spu_type,
        spu.is_refund,
        spu.virtual_remark,
        spu.main_img_url,
        spu.img_urls imgs,
        spu.video,
        sl.lang,
        sl.spu_name,
        sl.selling_point,
        spu.delivery_mode,
        spu.status,
        spu.delivery_template_id,
        spu.delivery_amount,
        spu.write_off_num,
        spu.write_off_multiple_count,
        spu.write_off_time,
        spu.write_off_start,
        spu.write_off_end,
        bl.lang as b_lang,
        bl.name AS brand_name,
        spu.category_id,
        spu.shop_category_id,
        sd.lang sd_lang,
        sd.detail
    FROM
        spu spu
        LEFT JOIN spu_lang sl ON spu.spu_id = sl.spu_id
        LEFT JOIN brand b ON spu.brand_id = b.brand_id
        LEFT JOIN brand_lang bl ON bl.brand_id = spu.brand_id
        LEFT JOIN spu_detail sd ON sd.spu_id = spu.spu_id
    WHERE spu.status != -1
    <if test="spuSearch.spuId != null">
      and spu.spu_id = #{spuSearch.spuId}
    </if>
    <if test="spuSearch.shopId != null">
      and spu.shop_id = #{spuSearch.shopId}
    </if>
    <if test="spuSearch.supplierId != null">
      and spu.supplier_id = #{spuSearch.supplierId}
    </if>
    <if test="spuSearch.supplierSpuType != null">
      and spu.supplier_spu_type = #{spuSearch.supplierSpuType}
    </if>
    <if test="spuSearch.spuMold != null">
      and spu.spu_mold = #{spuSearch.spuMold}
    </if>
    <if test="spuSearch.brandId != null">
      and spu.brand_id = #{spuSearch.brandId}
    </if>
    <if test="spuSearch.shopPrimaryCategoryId != null ">
      and spu.shop_category_id in (SELECT category_id FROM category WHERE parent_id =
      #{spuSearch.shopPrimaryCategoryId})
    </if>
    <if test="spuSearch.shopSecondaryCategoryId != null ">
      and spu.shop_category_id = #{spuSearch.shopSecondaryCategoryId}
    </if>
    <if test="spuSearch.primaryCategoryId != null ">
      and spu.category_id in (SELECT category_id FROM category WHERE parent_id IN (SELECT category_id FROM
      category WHERE parent_id =#{spuSearch.primaryCategoryId}) )
    </if>
    <if test="spuSearch.secondaryCategoryId != null ">
      and spu.category_id in (SELECT category_id FROM category WHERE parent_id =#{spuSearch.secondaryCategoryId})
    </if>
    <if test="spuSearch.categoryId != null">
      and spu.category_id = #{spuSearch.categoryId}
    </if>
    <if test="spuSearch.name != null and spuSearch.name != ''">
      and spu.spu_id in (select spu_id from spu_lang where spu_name like concat('%', #{spuSearch.name}, '%'))
    </if>
    <if test="spuSearch.status != null">
      and spu.status = #{spuSearch.status}
    </if>
    <if test="spuSearch.spuType != null">
      and spu.spu_type = #{spuSearch.spuType}
    </if>
    <if test="spuSearch.spuStatus == 3">
      and spu.status = 0
    </if>
    <if test="spuSearch.maxPrice != null">
      and spu.price_fee &lt;= #{spuSearch.maxPrice}
    </if>
    <if test="spuSearch.minPrice != null">
      and spu.price_fee &gt;= #{spuSearch.minPrice}
    </if>
    <if test="spuSearch.partyCode != null and spuSearch.partyCode != ''">
      and sku.party_code = #{spuSearch.partyCode}
    </if>
    <if test="spuSearch.dataType == 1">
      and spu.status = 1
    </if>
    <if test="spuSearch.dataType == 3">
      and spu.status in (0, 2, 3)
    </if>
    <if test="spuSearch.supplierDeliveryType != null">
      and spu.supplier_delivery_type = #{spuSearch.supplierDeliveryType}
    </if>
    <if test="spuSearch.spuOrigin == 1">
      and spu.supplier_spu_type in (1, 2)
    </if>
    <if test="spuSearch.spuOrigin == 0">
      and spu.supplier_spu_type = 0
    </if>
    <if test="spuSearch.spuIds != null and spuSearch.spuIds.size() > 0">
      AND spu.spu_id IN
        <foreach collection="spuSearch.spuIds" item="spuId" open="(" separator="," close=")">
            #{spuId}
        </foreach>
    </if>
  </select>

  <update id="changeToNormalSpu">
    update spu set spu_type = 0,activity_id = 0 where spu_id in
    <foreach collection="spuIds" item="spuId" separator="," open="(" close=")">
      #{spuId}
    </foreach>
  </update>
  <select id="countByCategoryAndShopId" resultType="java.lang.Integer">
    select count(*)
    from spu
    where category_id = #{categoryId}
    <if test="shopId != null">
      <if test="sysType == 1">
        and shop_id = #{shopId}
      </if>
      <if test="sysType == 3">
        and supplier_id = #{shopId} and supplier_spu_type = 1
      </if>
    </if>
    and status = 1
  </select>
  <select id="countByUserId" resultType="java.lang.Integer">
    select count(spu_id)
    from spu_collection
    where user_id = #{userId}
    group by user_id
  </select>

  <select id="spuShopIdsBySpuIds" resultType="java.lang.Long">
    SELECT DISTINCT shop_id FROM spu WHERE spu_id IN
    <foreach collection="spuIds" item="spuId" open="(" close=")" separator=",">
      #{spuId}
    </foreach>
  </select>
  <select id="listIdByCidAndShopId" resultType="java.lang.Long">
    SELECT DISTINCT spu_id FROM spu
    <where>
        status != -1
        and category_id IN
        <foreach collection="cidList" item="cid" open="(" close=")" separator=",">
            #{cid}
        </foreach>
        <if test="sysType != 3">
            AND shop_id = #{shopId}
        </if>
        <if test="sysType == 3">
            AND supplier_id = #{shopId}
        </if>
    </where>
  </select>


    <update id="updateBatch">
        <foreach collection="spuList" item="spu" separator=";">
            update spu
            <set>
                <if test="spu.brandId != null">
                    `brand_id` = #{spu.brandId},
                </if>
                <if test="spu.categoryId != null">
                    `category_id` = #{spu.categoryId},
                </if>
                <if test="spu.priceFee != null">
                    `price_fee` = #{spu.priceFee},
                </if>
                <if test="spu.marketPriceFee != null">
                    `market_price_fee` = #{spu.marketPriceFee},
                </if>
                <if test="spu.status != null">
                    `status` = #{spu.status},
                </if>
                <if test="spu.spuType != null">
                    `spu_type` = #{spu.spuType},
                </if>
                <if test="spu.activityId != null">
                    `activity_id` = #{spu.activityId},
                </if>
                <if test="spu.seq != null">
                    `seq` = #{spu.seq},
                </if>
                update_time=NOW()
            </set>
            where spu_id = #{spu.spuId}
        </foreach>
    </update>
    <update id="toTopBySpuId">
        update spu
        set is_top = if(is_top = 1, 0, 1)
        where spu_id = #{spuId}
          and (status = 1 or is_top = 1)
        limit 1
    </update>
    <update id="updateBatchFieldSetNull">
        <foreach collection="spuList" item="spu" separator=";">
            update spu
            <set>
                <if test="spu.brandId != null">
                    `brand_id` = #{spu.brandId},
                </if>
                <if test="spu.brandId == null">
                    `brand_id` = null,
                </if>
                <if test="spu.categoryId != null">
                    `category_id` = #{spu.categoryId},
                </if>
                <if test="spu.priceFee != null">
                    `price_fee` = #{spu.priceFee},
                </if>
                <if test="spu.marketPriceFee != null">
                    `market_price_fee` = #{spu.marketPriceFee},
                </if>
                <if test="spu.status != null">
                    `status` = #{spu.status},
                </if>
                <if test="spu.spuType != null">
                    `spu_type` = #{spu.spuType},
                </if>
                <if test="spu.activityId != null">
                    `activity_id` = #{spu.activityId},
                </if>
                <if test="spu.seq != null">
                    `seq` = #{spu.seq},
                </if>
            </set>
            where spu_id = #{spu.spuId}
        </foreach>
    </update>

    <select id="listSpuBySpuIds" resultMap="spuDetailMap">
        SELECT p.spu_id,pl.lang,p.main_img_url,
        pl.spu_name,p.price_fee,p.shop_id,p.`shop_category_id`,p.`category_id`,p.spu_type,p.spu_mold,p.main_img_url,p.`score_fee`,
        p.supplier_id,p.status,p.is_refund,p.is_syn_update, p.supplier_spu_id,p.shop_id,p.spu_code,p.`supplier_delivery_type`,
        p.`write_off_num`,p.pre_sale_type,p.`write_off_multiple_count`,p.`write_off_time`,p.`write_off_start`,p.`write_off_end`,p.delivery_template_id,p.delivery_amount
        FROM `spu` p
        LEFT JOIN spu_lang pl ON pl.spu_id = p.spu_id
        WHERE p.spu_id IN
        <foreach collection="spuIds" item="spuId" open="(" close=")" separator=",">
            #{spuId}
        </foreach>
    </select>

  <select id="listSpuDetailBySpuIds" resultMap="spuDetailMap">
    SELECT p.*
    FROM `spu` p
    WHERE p.spu_id IN
    <foreach collection="spuIds" item="spuId" open="(" close=")" separator=",">
      #{spuId}
    </foreach>
  </select>

  <select id="getAllSpuNum" resultType="java.lang.Long">
    select count(*)
    from spu
    where create_time &lt; #{time}
  </select>
  <select id="listAllSpu" resultMap="spuMap">
    select spu_id, update_time
    from spu
    where create_time &lt; #{time}
    order by create_time desc
  </select>
  <select id="listSpuIdByShopIdsAndStatus" resultType="java.lang.Long">
    select spu_id from spu
    <where>
        <if test="status != null">
            `status` = #{status}
        </if>
        <if test="sysType != null and sysType == 3">
            and shop_id = -1
            and supplier_id in
            <foreach collection="shopIds" item="shopId" open="(" close=")" separator=",">
                #{shopId}
            </foreach>
        </if>
        <if test="sysType != null and sysType != 3">
            and shop_id in
            <foreach collection="shopIds" item="shopId" open="(" close=")" separator=",">
                #{shopId}
            </foreach>
        </if>
        <if test="disStatus != null">
            and `status` != #{disStatus}
        </if>
    </where>
  </select>
  <select id="listSimple" resultType="com.tmerclub.cloud.api.product.bo.SpuSimpleBO">
    select s.spu_id, sl.spu_name, s.status, s.shop_id, s.main_img_url
    from spu s join spu_lang sl on s.spu_id = sl.spu_id and sl.lang = #{spuSimpleBO.lang}
    <where>
        s.status <![CDATA[ <> ]]> -1
      <if test="spuSimpleBO.spuName != null and spuSimpleBO.spuName != ''">
        AND sl.spu_name like concat('%', #{spuSimpleBO.spuName}, '%')
      </if>
      <if test="spuSimpleBO.status != null">
        AND s.status = #{spuSimpleBO.status}
      </if>
      <if test="spuSimpleBO.shopId != null">
        AND s.shop_id = #{spuSimpleBO.shopId}
      </if>
      <if test="spuSimpleBO.shopIds != null">
        AND s.shop_id in
        <foreach collection="spuSimpleBO.shopIds" item="shopId" open="(" close=")" separator=",">
          #{shopId}
        </foreach>
      </if>
      <if test="spuSimpleBO.spuIds != null">
        AND s.spu_id in
        <foreach collection="spuSimpleBO.spuIds" item="spuId" open="(" close=")" separator=",">
          #{spuId}
        </foreach>
      </if>
    </where>
    <if test="spuSimpleBO.seq == 1">
      ORDER BY spu_id ASC
    </if>
    <if test="spuSimpleBO.seq == 2">
      ORDER BY spu_id DESC
    </if>
  </select>

  <select id="listSpuNameBySpuIds" resultMap="spuDetailMap">
    select s.spu_id,sl.lang,sl.spu_name as spu_name
    from spu s
    join spu_lang sl on s.spu_id = sl.spu_id
    where s.spu_id in
    <foreach collection="spuIds" item="spuId" open="(" close=")" separator=",">
      #{spuId}
    </foreach>
  </select>
  <select id="getSpuIdsByShopIds" resultType="java.lang.Long">
    select spu_id from spu where shop_id in
    <foreach collection="shopIds" item="shopId" open="(" close=")" separator=",">
      #{shopId}
    </foreach>
  </select>

  <update id="removeBrandOfSpu">
    update spu
    set brand_id = null
    where brand_id = #{brandId}
  </update>
  <select id="listSpuStatusByIds" resultMap="spuDetailMap">
    select s.spu_id, s.status, s.supplier_spu_type, s.shop_id, s.supplier_id, sl.spu_name as name
    from spu s
    left join spu_lang sl on s.spu_id = sl.spu_id and sl.lang=1
    where s.spu_id in
    <foreach collection="spuIds" item="spuId" open="(" close=")" separator=",">
      #{spuId}
    </foreach>
  </select>

  <select id="getSpuCountBySupplierId" resultType="com.tmerclub.cloud.product.vo.SpuCountVO">
    SELECT COUNT(DISTINCT s.spu_id)                                            AS allCount,
    COUNT(DISTINCT CASE WHEN s.status = -1 THEN s.spu_id ELSE NULL END) AS deleted,
    COUNT(DISTINCT CASE WHEN s.status = 0 THEN s.spu_id ELSE NULL END)  AS removed,
    COUNT(DISTINCT CASE WHEN s.status = 1 THEN s.spu_id ELSE NULL END)  AS available,
    COUNT(DISTINCT CASE WHEN s.status = 2 THEN s.spu_id ELSE NULL END)  AS removedByPlatform,
    COUNT(DISTINCT CASE WHEN s.status = 3 THEN s.spu_id ELSE NULL END)  AS underReview
    FROM `spu` s
    WHERE s.supplier_id = #{supplierId}
    AND shop_id = -1
    AND supplier_spu_type = 1
  </select>

  <select id="isCommissionByShopIdAndSpuId" resultType="java.lang.Integer">
    SELECT count(0)
    FROM spu
    WHERE shop_id = #{shopId}
    AND supplier_spu_id = #{supplierSpuId}
    AND status != -1
  </select>

  <select id="getSupplierSupIdBySkuId" resultMap="spuMap">
    SELECT spu_id, shop_id, supplier_delivery_type
    FROM spu
    WHERE supplier_spu_id = (SELECT s.spu_id
    FROM sku s
    JOIN spu sp ON s.spu_id = sp.spu_id
    WHERE s.sku_id = #{skuId})
    AND `status` != -1
  </select>

  <select id="getSupplierSpuId" resultType="java.lang.Long">
    SELECT supplier_spu_id
    FROM spu
    WHERE shop_id = #{shopId}
    AND supplier_spu_type = 2
    AND status != -1
  </select>

  <select id="excelSupplierSpuList" resultMap="supplierSpuExcelVO">
    SELECT spu.spu_id,sl.lang,sl.spu_name,sl.selling_point,spu.delivery_mode,spu.status,spu.delivery_template_id,
    bl.lang as b_lang, bl.name AS
    brand_name,spu.category_id,spu.shop_category_id,spu.supplier_id,spu.supplier_delivery_type,spu.sku_choice_status,spu.measure_unit
    FROM spu spu
    LEFT JOIN spu_lang sl ON spu.spu_id = sl.spu_id
    LEFT JOIN brand b ON spu.brand_id = b.brand_id
    LEFT JOIN brand_lang bl ON bl.brand_id = spu.brand_id
    WHERE spu.status != -1
    and spu.supplier_spu_type = 2
    <if test="spuSearch.spuId != null">
      and spu.spu_id = #{spuSearch.spuId}
    </if>
    <if test="spuSearch.shopId != null">
      and spu.shop_id = #{spuSearch.shopId}
    </if>
    <if test="spuSearch.brandId != null">
      and spu.brand_id = #{spuSearch.brandId}
    </if>
    <if test="spuSearch.shopPrimaryCategoryId != null ">
      and spu.shop_category_id in (SELECT category_id FROM category WHERE parent_id =
      #{spuSearch.shopPrimaryCategoryId})
    </if>
    <if test="spuSearch.shopSecondaryCategoryId != null ">
      and spu.shop_category_id = #{spuSearch.shopSecondaryCategoryId}
    </if>
    <if test="spuSearch.primaryCategoryId != null ">
      and spu.category_id in (SELECT category_id FROM category WHERE parent_id IN (SELECT category_id FROM
      category WHERE parent_id =#{spuSearch.primaryCategoryId}) )
    </if>
    <if test="spuSearch.secondaryCategoryId != null ">
      and spu.category_id in (SELECT category_id FROM category WHERE parent_id =#{spuSearch.secondaryCategoryId})
    </if>
    <if test="spuSearch.categoryId != null">
      and spu.category_id = #{spuSearch.categoryId}
    </if>
    <if test="spuSearch.name != null and spuSearch.name != ''">
      and spu.spu_id in (select spu_id from spu_lang where spu_name like concat('%', #{spuSearch.name}, '%'))
    </if>
    <if test="spuSearch.status != null">
      and spu.status = #{spuSearch.status}
    </if>
    <if test="spuSearch.spuType != null">
      and spu.spu_type = #{spuSearch.spuType}
    </if>
    <if test="spuSearch.spuStatus == 3">
      and spu.status = 0
    </if>
    <if test="spuSearch.maxPrice != null">
      and spu.price_fee &lt;= #{spuSearch.maxPrice}
    </if>
    <if test="spuSearch.minPrice != null">
      and spu.price_fee &gt;= #{spuSearch.minPrice}
    </if>
    <if test="spuSearch.partyCode != null and spuSearch.partyCode != ''">
      and sku.party_code = #{spuSearch.partyCode}
    </if>
    <if test="spuSearch.dataType == 1">
      and spu.status = 1
    </if>
    <if test="spuSearch.dataType == 3">
      and spu.status in (0, 2, 3)
    </if>
    <if test="spuSearch.spuIds != null">
      and spu.spu_id IN
      <foreach collection="spuSearch.spuIds" item="spuId" open="(" separator="," close=")">
        #{spuId}
      </foreach>
    </if>
  </select>

  <select id="getSpuByShopIdAndSupplierSpuId" resultMap="spuDetailMap">
    SELECT
    <include refid="Vo_Column_List"/>
    FROM spu s WHERE s.shop_id = #{shopId} AND s.supplier_spu_id = #{supplierSpuID} AND s.status != -1
  </select>

  <select id="getSpuIdByShopIdAndSupplierSpuId" resultType="java.lang.Long">
    SELECT spu_id FROM spu WHERE shop_id = #{shopId} AND supplier_spu_id IN
    <foreach collection="supplierSpuId" item="spuId" open="(" separator="," close=")">
      #{spuId}
    </foreach>
  </select>

  <select id="supplierSpuList" resultMap="spuDetailMap">
    select distinct
    <include refid="Vo_Column_List"/>
    ,sl.lang,sl.spu_name
    from spu s
    left join `spu_lang` sl on s.`spu_id` = sl.`spu_id`
    <if test="spu.partyCode != null and spu.partyCode != ''">
      join `sku` on s.`spu_id` = sku.`spu_id` and sku.party_code = #{spu.partyCode}
    </if>
    where s.status <![CDATA[<>]]> -1
    <if test="spu.spuId != null">
      and s.spu_id = #{spu.spuId}
    </if>
    <if test="spu.spuIds != null and spu.spuIds.size() > 0">
      and s.spu_id in
      <foreach collection="spu.spuIds" item="spuId" open="(" close=")" separator=",">
        #{spuId}
      </foreach>
    </if>
    <if test="spu.brandId != null">
      and s.brand_id = #{spu.brandId}
    </if>
    <if test="spu.categoryId != null">
      and s.category_id = #{spu.categoryId}
    </if>
    <if test="spu.name != null and spu.name != ''">
      and sl.spu_name like concat('%', #{spu.name}, '%')
    </if>
    <if test="spu.status != null">
      and s.status = #{spu.status}
    </if>
    <if test="spu.spuType != null">
      and s.spu_type = #{spu.spuType}
    </if>
    <if test="spu.spuStatus == 1">
      and s.status = 1
    </if>
    <if test="spu.spuStatus == 3">
      and s.status = 0
    </if>
    <if test="spu.maxPrice != null">
      and s.price_fee &lt;= #{spu.maxPrice}
    </if>
    <if test="spu.minPrice != null">
      and s.price_fee &gt;= #{spu.minPrice}
    </if>
    order by
    <trim prefixOverrides=",">
      <if test="spu.priceFeeSort == 0">
        s.price_fee desc
      </if>
      <if test="spu.priceFeeSort == 1">
        ,s.price_fee
      </if>
      <if test="spu.marketPriceFeeSort == 0">
        ,s.market_price_fee desc
      </if>
      <if test="spu.marketPriceFeeSort == 1">
        ,s.market_price_fee
      </if>
      <if test="spu.seqSort == 0">
        ,s.seq desc
      </if>
      <if test="spu.seqSort == 1">
        ,s.seq
      </if>
      <if test="spu.createTimeSort == 0">
        ,s.create_time desc
      </if>
      <if test="spu.createTimeSort == 1">
        ,s.create_time
      </if>
      <if test="spu.priceFeeSort == null and spu.marketPriceFeeSort == null and spu.saleNumSort == null
          and spu.seqSort == null and spu.createTimeSort == null">
        s.update_time desc, s.spu_id
      </if>
    </trim>
  </select>

  <select id="listSpuByShopIdAndSupplierSpuId" resultMap="spuDetailMap">
    SELECT
    <include refid="Vo_Column_List"/>
    FROM spu s WHERE s.shop_id = #{shopId} AND s.status != -1 AND s.supplier_spu_id IN
    <foreach collection="supplierSpuIds" item="supplierSpuId" open="(" separator="," close=")">
      #{supplierSpuId}
    </foreach>
  </select>

  <update id="batchChangeSynUpdate">
    UPDATE spu SET is_syn_update = #{isSynUpdate} WHERE spu_id IN
    <foreach collection="spuIds" item="spuId" open="(" close=")" separator=",">
      #{spuId}
    </foreach>
  </update>

  <update id="changeSynUpdate">
    UPDATE spu
    SET is_syn_update = #{isSynUpdate}
    WHERE spu_id = #{spuId}
  </update>

  <select id="getSupIdListBySupplierId" resultType="java.lang.Long">
    SELECT spu_id
    FROM spu
    WHERE supplier_spu_id = #{supplierId}
    AND `status` != -1
  </select>

  <select id="batchSupIdListBySupplierId" resultType="java.lang.Long">
    SELECT spu_id FROM spu WHERE supplier_spu_id IN
    <foreach collection="supplierIds" item="supplierId" open="(" separator="," close=")">
      #{supplierId}
    </foreach>
    AND `status` != -1
  </select>

  <select id="batchSupListBySupplierId" resultType="com.tmerclub.cloud.common.product.vo.SpuVO">
    SELECT spu_id,supplier_spu_id,supplier_delivery_type FROM spu WHERE supplier_spu_id IN
    <foreach collection="supplierIds" item="supplierId" open="(" separator="," close=")">
      #{supplierId}
    </foreach>
    AND `status` != -1
  </select>

  <select id="getSpuIdsBySupplierIdAndSupplierSpuType" resultMap="spuDetailMap">
    SELECT spu_id,supplier_spu_id FROM spu
    where status = 1
    <if test="supplierId != null">
      AND supplier_id = #{supplierId}
    </if>
    <if test="createTime != null">
      AND create_time >= #{createTime}
    </if>
    AND supplier_spu_type = #{supplierSpuType}
  </select>

  <select id="listSpuBySupplierSpuId" resultMap="spuDetailMap">
    SELECT
    <include refid="Vo_Column_List"/>
    FROM spu s WHERE s.status != -1 AND s.supplier_spu_id = #{supplierSpuId} AND supplier_delivery_type = 1
  </select>
  <select id="getCountBySpuCode" resultType="java.lang.Integer">
    select ifnull(count(1),0)
    FROM `spu`
    <where>
      spu_code = #{spuCode} and `status` != -1
      <if test="spuId != null">
        and spu_id != #{spuId}
      </if>
      <if test="supplierSpuId != null and supplierSpuId != ''">
        and supplier_spu_id != #{supplierSpuId}
      </if>
      <if test="supplierSpuId == null">
        and supplier_spu_id is null
      </if>
    </where>
  </select>

  <select id="offlineSpuBySupplierId">
    update spu
    set status = 0
    where supplier_id = #{supplierId}
    and status = 1
    and (supplier_spu_type = 1 or supplier_spu_type = 2)
  </select>

  <select id="getCommissionSpuIdList" resultType="java.lang.Long">
    SELECT
    spu_id
    FROM spu s WHERE s.status != -1 AND s.supplier_spu_id = #{supplierSpuIds}
    <if test="deliveryType != null">
      AND supplier_delivery_type = #{deliveryType}
    </if>
  </select>

  <select id="getCommissionSpuListBySupplierDeliver" resultMap="spuDetailMap">
    SELECT
    <include refid="Vo_Column_List"/>, sl.spu_name as name
    FROM spu s
    left join spu_lang sl on s.spu_id = sl.spu_id and sl.lang=1
    WHERE s.status != -1 AND s.supplier_spu_id = #{supplierSpuId} AND s.supplier_delivery_type = 1
  </select>

  <select id="getSpuIsSynUpdate" resultMap="spuDetailMap">
    SELECT s.spu_id,IFNULL(s.is_syn_update,0) AS is_syn_update, s.supplier_spu_id
    FROM spu s
    left join spu_extension se on s.supplier_spu_id = se.spu_id
    WHERE s.spu_id IN
    <foreach collection="spuIds" item="spuId" open="(" separator="," close=")">
      #{spuId}
    </foreach>
  </select>

  <update id="batchUpdateCommissionSpuDeliverType">
    UPDATE spu SET supplier_delivery_type = #{deliverType} WHERE spu_id IN
    <foreach collection="spuIds" item="spuId" open="(" separator="," close=")">
      #{spuId}
    </foreach>
  </update>

  <select id="getCommissionSpuList" resultMap="spuDetailMap">
    SELECT
    <include refid="Vo_Column_List"/>
    FROM spu s WHERE s.status != -1 AND s.supplier_spu_id = #{supplierSpuId}
  </select>

  <select id="getCommissionSpuListByShopId" resultType="java.lang.Long">
    SELECT s.spu_id
    FROM spu s
    WHERE s.status != -1
    AND s.shop_id = #{shopId}
    and s.supplier_spu_type = 2
  </select>

  <select id="listMerchantSpuIdsBySupplierId" resultType="com.tmerclub.cloud.common.product.vo.SpuVO">
    SELECT s1.spu_id,s1.supplier_spu_id
    FROM spu s1 JOIN spu s2 ON s1.supplier_spu_id = s2.spu_id
    WHERE s1.supplier_id = #{supplierId} AND s1.supplier_spu_type = 2 AND s1.supplier_delivery_type = 1 AND s2.status != -1
  </select>
    <select id="getBySpuIds" resultType="com.tmerclub.cloud.common.product.vo.SpuVO">
        select
        <include refid="Vo_Column_List"/>
        ,
        sd.`detail`,sd.lang detail_lang,sl.lang, sl.spu_name as name,sl.selling_point
        FROM `spu` s
        LEFT JOIN spu_lang sl ON s.`spu_id` = sl.`spu_id`
        LEFT JOIN spu_detail sd ON s.`spu_id` = sd.`spu_id`
        WHERE s.`spu_id`in
        <foreach collection="spuIds" item="spuId" open="(" close=")" separator=",">
            #{spuId}
        </foreach>
    </select>

    <update id="updateSpuByBrandIdAndShopId">
    update spu
    set brand_id  = null
    <where>
      <if test="sysType == 3">
          and supplier_id = #{shopId} and supplier_spu_type = 1
      </if>
        <if test="sysType != 3">
            and shop_id = #{shopId}
        </if>
    <if test="brandIdList != null and brandIdList.size() > 0">
      and brand_id in
      <foreach collection="brandIdList" item="brandId" open="(" close=")" separator=",">
        #{brandId}
      </foreach>
    </if>
    </where>
  </update>

  <select id="listSpuIdsByExcludeStatus" resultType="java.lang.Long">
      select spu_id from spu where status != #{status}
  </select>
    <select id="listPlatformSpuExcel" resultMap="platformExcelMap">
        SELECT spu.spu_id,spu.spu_mold,spu.spu_type,spu.is_refund,spu.virtual_remark,
               sl.lang,sl.spu_name,sl.selling_point,spu.delivery_mode,spu.status,spu.delivery_template_id,
               spu.write_off_num,spu.write_off_multiple_count,spu.write_off_time,spu.write_off_start,spu.write_off_end,
               bl.lang as b_lang,bl.name AS brand_name,spu.category_id,spu.shop_category_id,se.water_sold_num, spu.is_top
        FROM spu spu
                 LEFT JOIN spu_extension se ON se.spu_id = spu.spu_id
                 LEFT JOIN spu_lang sl ON spu.spu_id = sl.spu_id
                 LEFT JOIN brand b ON spu.brand_id = b.brand_id
                 LEFT JOIN brand_lang bl ON bl.brand_id = spu.brand_id
        WHERE spu.spu_id in
        <foreach collection="spuIds" open="(" close=")" item="spuId" separator=",">
            #{spuId}
        </foreach>
    </select>
  <select id="getSupplierSpuBySupplierId" resultType="com.tmerclub.cloud.common.product.vo.SpuVO">
    select <include refid="Vo_Column_List"/> from spu s
    where s.supplier_id = #{supplierId}
    <if test="status != null">
      and s.status = #{status}
    </if>
  </select>
    <select id="listSimples" resultType="com.tmerclub.cloud.api.product.bo.SpuSimpleBO">
        select s.spu_id, sl.spu_name, s.status, s.shop_id, s.main_img_url
        from spu s join spu_lang sl on s.spu_id = sl.spu_id and sl.lang = #{spuSimpleBO.lang}
        <where>
            s.status >= -1
            <if test="spuSimpleBO.spuName != null and spuSimpleBO.spuName != ''">
                AND sl.spu_name like concat('%', #{spuSimpleBO.spuName}, '%')
            </if>
            <if test="spuSimpleBO.shopId != null">
                AND s.shop_id = #{spuSimpleBO.shopId}
            </if>
        </where>
        <if test="spuSimpleBO.seq == 1">
            ORDER BY spu_id ASC
        </if>
        <if test="spuSimpleBO.seq == 2">
            ORDER BY spu_id DESC
        </if>
    </select>

    <select id="pageSpuSku" resultMap="spuAndSkuMap">
        SELECT
            s.spu_id,
            s.main_img_url,
            s.img_urls,
            s.status,
            sl.spu_name,
            sku.sku_id,
            sku.party_code,
            sku.price_fee,
            skl.sku_name
        FROM
            spu s
            LEFT JOIN `spu_lang` sl on s.`spu_id` = sl.`spu_id`
            AND sl.lang = #{lang}
            LEFT JOIN `sku` on s.`spu_id` = sku.`spu_id`
            LEFT JOIN `sku_lang` skl ON skl.sku_id = sku.sku_id
            AND skl.lang = #{lang}
        WHERE
            s.shop_id = #{spuDTO.shopId} and s.status > -1 and sku.status > -1
            <if test="spuDTO.status != null">
                AND s.status = #{spuDTO.status}
            </if>
            <if test="spuDTO.name != null and spuDTO.name != ''">
                AND sl.spu_name LIKE CONCAT( '%', #{spuDTO.name}, '%' )
            </if>
            <if test="spuDTO.partyCode != null and spuDTO.partyCode != ''">
                AND sku.party_code LIKE CONCAT( '%', #{spuDTO.partyCode}, '%' )
            </if>
        ORDER BY
            s.create_time DESC
    </select>
    <select id="stockWarningCount" resultType="java.lang.Long">
        select count(DISTINCT(sp.spu_id))
        from spu sp
        LEFT JOIN sku sk on sp.spu_id = sk.spu_id
        WHERE sp.supplier_id = #{supplierId}
        and sk.`status`!=-1 and sp.status!=-1 and sp.shop_id = -1
        and sk.stock_warning_status = 1
    </select>

    <select id="getVirtualSpuList" resultMap="spuDetailMap">
        select
        <include refid="Vo_Column_List"/>
        from spu s
        where s.spu_mold = 1 and s.spu_type = 0 and s.write_off_time = 0 and s.write_off_end &lt; NOW() and s.status = 1
    </select>

    <update id="updateSpuByShopId">
        update spu set `update_time` = NOW() where
        <if test="sysType == 1">
            shop_id = #{shopId}
        </if>
        <if test="sysType == 3">
            supplier_id = #{shopId}
        </if>
    </update>

    <select id="listShopSpuBySupplierSpuIds" resultMap="spuDetailMap">
        SELECT
        <include refid="Vo_Column_List"/>,sl.lang,sl.spu_name
        FROM spu s
        LEFT JOIN spu_lang sl on s.spu_id = sl.spu_id
        WHERE s.shop_id = #{shopId} AND s.status != -1 AND s.supplier_spu_id IN
        <foreach collection="supplierSpuIds" open="(" close=")" item="supplierSpuId" separator=",">
            #{supplierSpuId}
        </foreach>
    </select>

    <select id="listPointSpu" resultMap="spuDetailMap">
        select
        <include refid="Vo_Column_List"/>
        ,sl.lang, sl.spu_name,sl.selling_point, sl.spu_name as name
        FROM `spu` s
        LEFT JOIN spu_lang sl ON s.`spu_id` = sl.`spu_id`
        LEFT JOIN stock_point_sku sps ON s.spu_id = sps.spu_id
        <if test="spuDTO.partyCode != null and spuDTO.partyCode != ''">
            LEFT JOIN `sku` sku ON s.`spu_id` = sku.`spu_id`
        </if>
        WHERE
        <if test="spuDTO.status == null">
            s.status != -1
        </if>
        <if test="spuDTO.status != null">
            s.status = #{spuDTO.status}
        </if>
        <if test="spuDTO.shopId != null">
            and s.shop_id = #{spuDTO.shopId}
        </if>
        <if test="spuDTO.supplierId != null" >
            and s.supplier_id = #{spuDTO.supplierId}
            AND s.supplier_spu_type = 1
        </if>
        <if test="spuDTO.spuName != null and  spuDTO.spuName != ''" >
            and sl.spu_name like CONCAT('%',#{spuDTO.spuName},'%')
        </if>
        <if test="spuDTO.partyCode != null and spuDTO.partyCode != ''">
            and sku.party_code like CONCAT( '%', #{spuDTO.partyCode}, '%' )
        </if>
        <if test="spuDTO.categoryId != null" >
            and s.shop_category_id = #{spuDTO.categoryId}
        </if>
        <if test="spuDTO.stockPointId != null">
            and sps.`stock_point_id` = #{spuDTO.stockPointId}
        </if>
        <if test="spuDTO.stockPointIds != null and spuDTO.stockPointIds.size() > 0">
            AND sps.`stock_point_id` IN
            <foreach collection="spuDTO.stockPointIds" item="stockPointId" open="(" separator="," close=")">
                #{stockPointId}
            </foreach>
        </if>
        <if test="spuDTO.notInSkuIds != null and spuDTO.notInSkuIds.size() > 0">
            AND sps.sku_id NOT IN
            <foreach collection="spuDTO.notInSkuIds" item="skuId" open="(" separator="," close=")">
                #{skuId}
            </foreach>
        </if>
        <if test="spuDTO.categoryIds != null and spuDTO.categoryIds.size() > 0">
            AND s.category_id IN
            <foreach collection="spuDTO.categoryIds" item="categoryId" open="(" separator="," close=")">
                #{categoryId}
            </foreach>
        </if>
        <if test="spuDTO.stockPointType != 1 or spuDTO.type == 1">
            and s.spu_mold != 1
        </if>
        <if test="spuDTO.spuMold != null">
            AND s.spu_mold = #{spuDTO.spuMold}
        </if>
        group by s.spu_id
    </select>

    <select id="countNormalSpuBySpuIds" resultType="int">
        SELECT ifnull(count(*),0) FROM spu
        WHERE
        spu_id IN
        <foreach collection="spuIds" item="spuId" open="(" separator="," close=")">
            #{spuId}
        </foreach>
        and ( status <![CDATA[ <> ]]> 1 or pre_sale_type <![CDATA[ <> ]]> -1 OR (spu.shop_id != 0 AND supplier_delivery_type = 1))
    </select>

    <select id="listStockPointSpuAndSku" resultMap="spuDetailMap">
        SELECT
            <include refid="Vo_Column_List"/>,
            sl.lang,
            sl.spu_name,
            sl.selling_point,
            sl.spu_name,
            sps.stock_point_id,
            sps.sku_id,
            sps.supplier_sku_id
        FROM
            `spu` s
            LEFT JOIN spu_lang sl ON s.`spu_id` = sl.`spu_id`
            LEFT JOIN stock_point_sku sps ON s.spu_id = sps.spu_id
        WHERE
            s.spu_id IN
                <foreach collection="spuDTO.spuIds" item="spuId" open="(" separator="," close=")">
                    #{spuId}
                </foreach>
            AND sps.stock_point_id IN
                <foreach collection="spuDTO.stockPointIds" item="stockPointId" open="(" separator="," close=")">
                    #{stockPointId}
                </foreach>
        ORDER BY
            s.create_time DESC
    </select>

    <select id="listSpuCodes" resultType="java.lang.String">
        SELECT
            spu_code
        FROM
            spu
        <where>
            <if test="sysType == 1">
                shop_id = #{tenantId}
            </if>
            <if test="sysType == 2">
                AND shop_id = 0
            </if>
            <if test="sysType == 3">
                supplier_id = #{tenantId}
                AND shop_id = -1
            </if>
            <if test="spuId != null">
                AND spu_id != #{spuId}
            </if>
        </where>
    </select>


</mapper>
