<?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.yiqi.app.mapper.AppGoodsCollectMapper">

    <resultMap type="AppGoodsCollect" id="AppGoodsCollectResult">
        <result property="id" column="id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="appUserId" column="app_user_id"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <resultMap type="com.yiqi.core.domain.vo.weapp.WeAppGoodsInfoQueryVO" id="WeAppGoodsResult">
        <result property="id" column="id"/>
        <result property="goodsName" column="goods_name"/>
        <result property="goodsType" column="goods_type"/>
        <result property="goodsCategoryId" column="goods_category_id"/>
        <result property="goodsSynopsis" column="goods_synopsis"/>
        <result property="brandId" column="brand_id"/>
        <result property="goodsImg" column="goods_img"/>
        <result property="goodsImgs" column="goods_imgs"/>
        <result property="goodsCode" column="goods_code"/>
        <result property="isBalancePay" column="is_balance_pay"/>
        <result property="isCashPay" column="is_cash_pay"/>
        <result property="cashMoney" column="cash_money"/>
        <result property="isSell" column="is_sell"/>
        <result property="sellNum" column="sell_num"/>
        <result property="goodComment" column="good_comment"/>
        <result property="minSalePrice" column="min_sale_price"/>
        <result property="maxSalePrice" column="max_sale_price"/>
    </resultMap>

    <sql id="selectAppGoodsCollectVo">
        select id, goods_id, app_user_id, create_time
        from app_goods_collect
    </sql>

    <select id="selectAppGoodsCollectList" resultMap="WeAppGoodsResult">
        select g.* from app_goods_collect as gc left join (select g.id,
        g.goods_name,
        g.goods_type,
        g.goods_category_id,
        g.brand_id,
        g.goods_img,
        g.goods_imgs,
        g.goods_code,
        g.is_balance_pay,
        g.is_cash_pay,
        g.cash_money,
        g.is_sell,
        g.sell_num,
        g.good_comment,
        g.create_time,
        gss.status as status,
        g.del_flag,
        min(gss.sale_price) as min_sale_price,
        max(gss.sale_price) as max_sale_price
        from goods_sku as sku
        left join goods_sku_store as gss on gss.source_type = '00' and gss.goods_sku_id = sku.id
        left join goods_info as g on g.id = sku.goods_id
        where gss.status='0' and g.del_flag='0' group by sku.goods_id) as g on g.id=gc.goods_id
        <where>
            g.status='0' and g.del_flag='0'
            <if test="appUserId != null ">
                and gc.app_user_id = #{appUserId}
            </if>
        </where>
        limit #{pageVO.startRow},#{pageVO.pageSize}
    </select>

    <select id="selectAppGoodsCollectById" parameterType="Long"
            resultMap="AppGoodsCollectResult">
        <include refid="selectAppGoodsCollectVo"/>
        where id = #{id}
    </select>

    <insert id="insertAppGoodsCollect" parameterType="AppGoodsCollect" useGeneratedKeys="true"
            keyProperty="id">
        insert into app_goods_collect
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="goodsId != null">goods_id,
            </if>
            <if test="appUserId != null">app_user_id,
            </if>
            <if test="createTime != null">create_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="goodsId != null">#{goodsId},
            </if>
            <if test="appUserId != null">#{appUserId},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
        </trim>
    </insert>

    <update id="updateAppGoodsCollect" parameterType="AppGoodsCollect">
        update app_goods_collect
        <trim prefix="SET" suffixOverrides=",">
            <if test="goodsId != null">goods_id =
                #{goodsId},
            </if>
            <if test="appUserId != null">app_user_id =
                #{appUserId},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAppGoodsCollectById" parameterType="Long">
            delete from app_goods_collect where id = #{id}
        </delete>

    <delete id="deleteAppGoodsCollectByIds">
        delete from app_goods_collect where goods_id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
        and app_user_id=#{appUserId}
    </delete>



</mapper>