<?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.fs.live.mapper.LiveGoodsMapper">

    <resultMap type="LiveGoods" id="LiveGoodsResult">
        <result property="goodsId"    column="goods_id"    />
        <result property="liveId"    column="live_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="storeId"    column="store_id"    />
        <result property="productId"    column="product_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="status"    column="status"    />
        <result property="stock"    column="stock"    />
        <result property="sort"    column="sort"    />
        <result property="isShow"    column="is_show"    />
        <result property="sales"    column="sales"    />
    </resultMap>

    <sql id="selectLiveGoodsVo">
        select goods_id, live_id, company_id, company_user_id,sales, store_id, product_id, create_time, create_by, update_by, update_time, remark, status, stock, sort,case when is_show = 1 then true else false end as is_show from live_goods
    </sql>

    <select id="selectLiveGoodsList" parameterType="LiveGoods" resultMap="LiveGoodsResult">
        <include refid="selectLiveGoodsVo"/>
        <where>
            <if test="liveId != null "> and live_id = #{liveId}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
            <if test="storeId != null "> and store_id = #{storeId}</if>
            <if test="productId != null "> and product_id = #{productId}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="stock != null "> and stock = #{stock}</if>
            <if test="sort != null "> and sort = #{sort}</if>
            <if test="isShow != null "> and is_show = #{isShow}</if>
            <if test="sales != null "> and sales = #{sales}</if>
        </where>
    </select>

    <select id="selectLiveGoodsByGoodsId" parameterType="Long" resultMap="LiveGoodsResult">
        <include refid="selectLiveGoodsVo"/>
        where goods_id = #{goodsId}
    </select>

    <insert id="insertLiveGoods" parameterType="LiveGoods" useGeneratedKeys="true" keyProperty="goodsId">
        insert into live_goods
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="liveId != null">live_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="storeId != null">store_id,</if>
            <if test="productId != null">product_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="status != null">status,</if>
            <if test="stock != null">stock,</if>
            <if test="sort != null">sort,</if>
            <if test="isShow != null">is_show,</if>
            <if test="sales != null">sales,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="liveId != null">#{liveId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="storeId != null">#{storeId},</if>
            <if test="productId != null">#{productId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="status != null">#{status},</if>
            <if test="stock != null">#{stock},</if>
            <if test="sort != null">#{sort},</if>
            <if test="isShow != null">#{isShow},</if>
            <if test="sales != null">#{sales},</if>
         </trim>
    </insert>

    <update id="updateLiveGoods" parameterType="LiveGoods">
        update live_goods
        <trim prefix="SET" suffixOverrides=",">
            <if test="liveId != null">live_id = #{liveId},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="storeId != null">store_id = #{storeId},</if>
            <if test="productId != null">product_id = #{productId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="status != null">status = #{status},</if>
            <if test="stock != null">stock = #{stock},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="isShow != null">is_show = #{isShow},</if>
            <if test="sales != null">sales = #{sales},</if>
        </trim>
        where goods_id = #{goodsId}
    </update>

    <delete id="deleteLiveGoodsByGoodsId" parameterType="Long">
        delete from live_goods where goods_id = #{goodsId}
    </delete>

    <delete id="deleteLiveGoodsByGoodsIds" parameterType="String">
        delete from live_goods where goods_id in
        <foreach item="goodsId" collection="array" open="(" separator="," close=")">
            #{goodsId}
        </foreach>
    </delete>

    <select id="selectStoreIdByLiveId" parameterType="Long" resultType="Long">
        select distinct store_id from live_goods where live_id = #{liveId}
    </select>
    <insert id="insertLiveGoodsList">
        INSERT INTO live_goods (
        live_id, company_id, company_user_id,
        product_id, create_time, create_by, update_by,
        update_time, remark, status, stock, sort, is_show,sales
        ) VALUES
        <foreach collection="liveGoodsList" item="item" separator=",">
            (
            #{item.liveId},
            <choose>
                <when test="item.companyId != null">
                    #{item.companyId},
                </when>
                <otherwise>
                    null,
                </otherwise>
            </choose>
            <choose>
                <when test="item.companyUserId != null">
                    #{item.companyUserId},
                </when>
                <otherwise>
                    null,
                </otherwise>
            </choose>
            #{item.productId},
            #{item.createTime},
            #{item.createBy},
            #{item.updateBy},
            #{item.updateTime},
            #{item.remark},
            #{item.status},
            #{item.stock},
            #{item.sort},
            #{item.isShow},
            #{item.sales}
            )
        </foreach>
    </insert>

    <select id="selectProductIdsByLiveId" resultType="string">
        SELECT product_id
        FROM live_goods
        WHERE live_id = #{liveId}
    </select>

    <select id="selectProductListByLiveId" parameterType="LiveGoods" resultType="com.fs.live.vo.LiveGoodsVo">

        select lg.goods_id,sp.image as img_url,sp.product_name,sp.price,sp.stock,lg.sales,lg.status,sp.product_id,sp.ot_price,case when lg.is_show = 1 then true else false end as is_show
        <if test="companyUserId != null "> ,if(uf.favorite_id is not null, true, false) is_favorite </if>

        from live_goods lg
        left join fs_store_product sp
        ON  lg.product_id = sp.product_id
        <if test="companyUserId != null "> left join live_user_favorite uf on uf.product_id = lg.goods_id and uf.user_id = #{companyUserId} </if>


        <where>
            lg.live_id = #{liveId} and lg.status=1 and sp.is_show = 1
            <if test="companyId != null "> and company_id = #{companyId}</if>

            <if test="productId != null "> and product_id = #{productId}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="stock != null "> and stock = #{stock}</if>
            <if test="sort != null "> and sort = #{sort}</if>
            <if test="isShow != null "> and is_show = #{isShow}</if>
            <if test="sales != null "> and sales = #{sales}</if>
            <if test="keywords != null and keywords != ''">
                and sp.product_name like concat('%',#{keywords},'%')
            </if>
        </where>
    </select>

    <select id="selectProductListByLiveIdAll" parameterType="LiveGoods" resultType="com.fs.live.vo.LiveGoodsVo">

        select lg.goods_id,sp.image as img_url,sp.product_name,sp.price,lg.stock,lg.sales,lg.status,sp.product_id,sp.ot_price,case when lg.is_show = 1 then true else false end as is_show,sp.is_show as fs_status
        from live_goods lg
        left join fs_store_product sp
        ON  lg.product_id = sp.product_id

        <where>
            live_id = #{liveId}
            <if test="productId != null "> and product_id = #{productId}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="stock != null "> and stock = #{stock}</if>
            <if test="sort != null "> and sort = #{sort}</if>
            <if test="isShow != null "> and is_show = #{isShow}</if>
            <if test="keywords != null and keywords != ''">
                and sp.product_name like concat('%',#{keywords},'%')
            </if>
        </where>
    </select>

    <select id="selectLiveGoodsVoByGoodsId"  resultType="com.fs.live.vo.LiveGoodsVo">

        select lg.goods_id,sp.image as img_url,sp.product_name,sp.price,lg.stock,lg.sales,lg.status,sp.product_id,sp.ot_price, case when lg.is_show = 1 then true else false end as is_show,sp.is_show as fs_status
        from live_goods lg
        left join fs_store_product sp
        ON  lg.product_id = sp.product_id

        <where>
            goods_id = #{goodsId}
        </where>
    </select>

    <select id="showGoods"  resultType="com.fs.live.vo.LiveGoodsVo">

        select lg.goods_id,sp.image as img_url,sp.product_name,sp.price,lg.stock,lg.sales,lg.status,sp.product_id,sp.ot_price, case when lg.is_show = 1 then true else false end as is_show,sp.is_show as fs_status
        from live_goods lg
        left join fs_store_product sp
        ON  lg.product_id = sp.product_id
        <where>
            lg.live_id = #{liveId} and lg.is_show = 1
        </where>
        limit 1
    </select>

    <update id="updateBatchList" parameterType="com.fs.live.vo.LiveGoodsListVo">
        update live_goods set status = #{listVo.status} where goods_id in
        <foreach item="goodsId" collection="listVo.goodsIds" open="(" separator="," close=")">
            #{goodsId}
        </foreach>
    </update>

    <delete id="deleteBatchList" parameterType="com.fs.live.vo.LiveGoodsListVo">
        delete from live_goods
        WHERE goods_id IN
        <foreach item="goodsId" collection="listVo.goodsIds" open="(" separator="," close=")">
            #{goodsId}
        </foreach>
    </delete>

    <select id="selectProductListByOrder" parameterType="com.fs.live.domain.LiveOrder" resultType="com.fs.live.vo.LiveGoodsVo">

        select sp.image as img_url,sp.product_name,sp.price,sp.stock,sp.sales,sp.product_id,sp.ot_price, sp.is_show as fs_status,pav.bar_code
        from fs_store_product sp
        left join fs_store_product_attr_value pav
        ON  sp.product_id = pav.product_id
        <where>
            sp.product_id = #{productId}
        </where>
    </select>

    <select id="selectLiveGoodsListByStoreId" resultType="com.fs.live.vo.LiveGoodsVo">
        select
            g.*,
            if(uf.favorite_id is not null, true, false) isFavorite,
            p.image as img_url, p.product_name, p.price, g.sales, p.ot_price,p.is_show as fs_status
        from live_goods g
        left join fs_store_product p on p.product_id = g.product_id and p.is_show = 1
        left join live_user_favorite uf on uf.product_id = g.goods_id and uf.user_id = #{params.userId}
        where  g.status = 1
        <if test="params.productName != null "> and p.product_name like concat('%',#{params.productName},'%')</if>
        <if test="params.liveId != null "> and g.live_id = #{params.liveId}</if>
    </select>

    <update id="handleShelfOrUnAdmin" parameterType="com.fs.live.vo.LiveGoodsListVo">
        update live_goods set status = #{listVo.status}  where goods_id in
        <foreach item="goodsId" collection="listVo.goodsIds" open="(" separator="," close=")">
            #{goodsId}
        </foreach>
    </update>

    <update id="handleIsShowChange" parameterType="com.fs.live.vo.LiveGoodsListVo">
        update live_goods set is_show = #{listVo.isShow} and status = 1 where goods_id in
        <foreach item="goodsId" collection="listVo.goodsIds" open="(" separator="," close=")">
            #{goodsId}
        </foreach>
    </update>

    <delete id="handleDeleteSelectedAdmin" parameterType="com.fs.live.vo.LiveGoodsListVo">
        delete from live_goods
        WHERE goods_id IN
        <foreach item="goodsId" collection="listVo.goodsIds" open="(" separator="," close=")">
            #{goodsId}
        </foreach>
    </delete>

</mapper>
