<?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.ruoyi.mall.mapper.MallAppGoodsMapper">
    
    <resultMap type="MallAppGoods" id="MallAppGoodsResult">
        <result property="id"    column="id"    />
        <result property="goodsName"    column="goods_name"    />
        <result property="articleName"    column="article_name"    />
        <result property="num"    column="num"    />
        <result property="unit"    column="unit"    />
        <result property="specification"    column="specification"    />
        <result property="unitPrice"    column="unit_price"    />
        <result property="commission"    column="commission"    />
    </resultMap>

    <sql id="selectMallAppGoodsVo">
        select id, goods_name, article_name, num, unit, specification, unit_price, commission from mall_app_goods
    </sql>

    <select id="selectMallAppGoodsList" parameterType="MallAppGoods" resultMap="MallAppGoodsResult">
        <include refid="selectMallAppGoodsVo"/>
        <where>  
            <if test="goodsName != null  and goodsName != ''"> and goods_name like concat('%', #{goodsName}, '%')</if>
            <if test="articleName != null  and articleName != ''"> and article_name like concat('%', #{articleName}, '%')</if>
            <if test="num != null "> and num = #{num}</if>
            <if test="unit != null  and unit != ''"> and unit = #{unit}</if>
            <if test="specification != null  and specification != ''"> and specification = #{specification}</if>
            <if test="unitPrice != null "> and unit_price = #{unitPrice}</if>
            <if test="commission != null "> and commission = #{commission}</if>
        </where>
    </select>
    
    <select id="selectMallAppGoodsById" parameterType="Long" resultMap="MallAppGoodsResult">
        <include refid="selectMallAppGoodsVo"/>
        where id = #{id}
    </select>

    <insert id="insertMallAppGoods" parameterType="MallAppGoods">
        insert into mall_app_goods
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="goodsName != null">goods_name,</if>
            <if test="articleName != null">article_name,</if>
            <if test="num != null">num,</if>
            <if test="unit != null">unit,</if>
            <if test="specification != null">specification,</if>
            <if test="unitPrice != null">unit_price,</if>
            <if test="commission != null">commission,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="goodsName != null">#{goodsName},</if>
            <if test="articleName != null">#{articleName},</if>
            <if test="num != null">#{num},</if>
            <if test="unit != null">#{unit},</if>
            <if test="specification != null">#{specification},</if>
            <if test="unitPrice != null">#{unitPrice},</if>
            <if test="commission != null">#{commission},</if>
         </trim>
    </insert>

    <update id="updateMallAppGoods" parameterType="MallAppGoods">
        update mall_app_goods
        <trim prefix="SET" suffixOverrides=",">
            <if test="goodsName != null">goods_name = #{goodsName},</if>
            <if test="articleName != null">article_name = #{articleName},</if>
            <if test="num != null">num = #{num},</if>
            <if test="unit != null">unit = #{unit},</if>
            <if test="specification != null">specification = #{specification},</if>
            <if test="unitPrice != null">unit_price = #{unitPrice},</if>
            <if test="commission != null">commission = #{commission},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMallAppGoodsById" parameterType="Long">
        delete from mall_app_goods where id = #{id}
    </delete>

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

    <select id="selectMallAppGoodsListByname" parameterType="java.lang.String"  resultType="com.ruoyi.mall.domain.MallAppGoods">
        SELECT
        mgg.id as id,
        mgg.goods_name as goodsName,
        mgg.spec as unit,
        mgp.price as unitPrice,
        mgg.commission as commission,
        mgp.product_name as specification,
        mgp.num as num
        FROM
        mall_goods_goods mgg
        LEFT JOIN mall_goods_product mgp ON mgg.id = mgp.goods_id
        <where>
            mgp.num = 1
            <if test="name != null  and name != ''">
                and mgg.goods_name like concat('%', #{name}, '%')
            </if>
            and mgp.is_default = '1'
            and mgg.is_web = '1'
            and mgg.del_flag = '0'
            and mgg.status = '0'
            and mgg.check_status = '1'
            and mgg.show_status = 0
        </where>
        order by mgg.create_at desc
    </select>
</mapper>