<?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.AppGoodsMarketMapper">

    <resultMap type="AppGoodsMarket" id="AppGoodsMarketResult">
        <result property="id" column="id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="goodsSkuId" column="goods_sku_id"/>
        <result property="isSelect" column="is_select"/>
        <result property="appUserId" column="app_user_id"/>
        <result property="buyNum" column="buy_num"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <resultMap type="com.yiqi.app.domain.vo.weapp.WeAppGoodsMarketVO" id="AppGoodsMarketVOResult">
        <result property="id" column="id"/>
        <result property="isSelect" column="is_select"/>
        <result property="buyNum" column="buy_num"/>
        <result property="createTime" column="create_time"/>

        <association property="goodsInfoVO" columnPrefix="goods_" javaType="com.yiqi.core.domain.vo.GoodsInfoQueryVO">
            <result property="id" column="id"/>
            <result property="goodsName" column="name"/>
            <result property="goodsType" column="type"/>
            <result property="goodsImg" column="img"/>
            <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="status" column="status"/>
            <result property="delFlag" column="del_flag"/>
            <result property="sellNum" column="sell_num"/>
        </association>

        <association property="goodsSkuVO" columnPrefix="goods_sku_" javaType="com.yiqi.core.domain.vo.weapp.WeAppGoodsSkuVO">
            <result property="skuId" column="id"/>
            <result property="salePrice" column="sale_price"/>
            <result property="stock" column="stock"/>
        </association>
    </resultMap>

    <sql id="selectAppGoodsMarketVo">
        select id, goods_id, goods_sku_id, is_select, app_user_id, buy_num, create_time
        from app_goods_market
    </sql>

    <select id="selectAppGoodsMarketList" parameterType="AppGoodsMarket" resultMap="AppGoodsMarketVOResult">
        select gm.id, gm.is_select, gm.buy_num, gm.create_time,g.id as goods_id
        ,g.goods_name,g.goods_type,g.goods_img,g.is_balance_pay as goods_is_balance_pay
        ,g.is_cash_pay as goods_is_cash_pay,g.cash_money as goods_cash_money,g.is_sell as goods_is_sell
        ,g.del_flag as goods_del_flag,g.sell_num as sellNum,sku.id as goods_sku_id
        ,gss.sale_price as goods_sku_sale_price,gss.stock as goods_sku_stock
        from app_goods_market as gm
        left join goods_info as g on g.id = gm.goods_id
        left join goods_sku as sku on sku.id = gm.goods_sku_id
        left join goods_sku_store as gss on gss.source_type = '00' and gss.goods_sku_id = sku.id
        <where>
            <if test="goodsId != null ">
                and goods_id = #{goodsId}
            </if>
            <if test="goodsSkuId != null ">
                and goods_sku_id = #{goodsSkuId}
            </if>
            <if test="isSelect != null  and isSelect != ''">
                and is_select = #{isSelect}
            </if>
            <if test="appUserId != null ">
                and app_user_id = #{appUserId}
            </if>
        </where>
        order by gm.create_time desc
    </select>

    <select id="selectAppGoodsMarketById" parameterType="Long"
            resultMap="AppGoodsMarketResult">
        <include refid="selectAppGoodsMarketVo"/>
        where id = #{id}
    </select>

    <insert id="insertAppGoodsMarket" parameterType="AppGoodsMarket" useGeneratedKeys="true"
            keyProperty="id">
        insert into app_goods_market
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="goodsId != null">goods_id,
            </if>
            <if test="goodsSkuId != null">goods_sku_id,
            </if>
            <if test="isSelect != null">is_select,
            </if>
            <if test="appUserId != null">app_user_id,
            </if>
            <if test="buyNum != null">buy_num,
            </if>
            <if test="createTime != null">create_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="goodsId != null">#{goodsId},
            </if>
            <if test="goodsSkuId != null">#{goodsSkuId},
            </if>
            <if test="isSelect != null">#{isSelect},
            </if>
            <if test="appUserId != null">#{appUserId},
            </if>
            <if test="buyNum != null">#{buyNum},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
        </trim>
    </insert>

    <update id="updateAppGoodsMarket" parameterType="AppGoodsMarket">
        update app_goods_market
        <trim prefix="SET" suffixOverrides=",">
            <if test="goodsId != null">goods_id =
                #{goodsId},
            </if>
            <if test="goodsSkuId != null">goods_sku_id =
                #{goodsSkuId},
            </if>
            <if test="isSelect != null">is_select =
                #{isSelect},
            </if>
            <if test="appUserId != null">app_user_id =
                #{appUserId},
            </if>
            <if test="buyNum != null">buy_num =
                #{buyNum},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAppGoodsMarketById" parameterType="Long">
            delete from app_goods_market where id = #{id}
        </delete>

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



</mapper>