<?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.MallOrderCommentMapper">
    
    <resultMap type="MallOrderComment" id="MallOrderCommentResult">
        <result property="id"    column="id"    />
        <result property="goodsId"    column="goods_id"    />
        <result property="productId"    column="product_id"    />
        <result property="userId"    column="user_id"    />
        <result property="orderId"    column="order_id"    />
        <result property="grade"    column="grade"    />
        <result property="context"    column="context"    />
        <result property="status"    column="status"    />
        <result property="createAt"    column="create_at"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectMallOrderCommentVo">
        select id, goods_id, product_id, user_id, order_id, grade, context, status, create_at, del_flag from mall_order_comment
    </sql>

    <select id="selectMallOrderCommentList" parameterType="MallOrderComment" resultType="map">
        select moc.id, moc.goods_id goodsId, moc.product_id productId, moc.user_id userId, moc.order_id orderId, moc.grade, moc.context, moc.create_at createAt
        ,mgg.goods_name goodsName, mgg.goods_img goodsImg
        from mall_order_comment moc
        left join mall_goods_goods mgg on moc.goods_id = mgg.id
        <where>
        <if test="orderId != null  and orderId != ''"> and moc.order_id = #{orderId}</if>
        <if test="goodsId != null  and goodsId != ''"> and moc.goods_id = #{goodsId}</if>
        </where>
    </select>
    
    <select id="selectMallOrderCommentById" parameterType="Long" resultMap="MallOrderCommentResult">
        <include refid="selectMallOrderCommentVo"/>
        where id = #{id}
    </select>
    <select id="selectCountByGoodsId" parameterType="Long" resultType="Integer">
        select count(1) from mall_order_comment where goods_id = #{goodsId}
    </select>
    <select id="selectTwoRecordByGoodsId" parameterType="Long" resultType="map">
        select moc.id, moc.context, moc.create_at, mmu.name, mmu.avatar
        from mall_order_comment moc
        left join mall_goods_goods mgg on moc.goods_id = mgg.id
        left join mall_member_user mmu on moc.user_id = mmu.id
        where moc.goods_id = #{goodsId}
        limit 2
    </select>

    <select id="selectCommentListByGoodsId" parameterType="Long" resultType="map">
        select moc.id, moc.context, moc.create_at, mmu.name, mmu.avatar
        from mall_order_comment moc
        left join mall_goods_goods mgg on moc.goods_id = mgg.id
        left join mall_member_user mmu on moc.user_id = mmu.id
        where moc.goods_id = #{goodsId}
        and mgg.del_flag = '0'
    </select>

    <insert id="insertMallOrderComment" parameterType="MallOrderComment" useGeneratedKeys="true" keyProperty="id">
        insert into mall_order_comment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="goodsId != null">goods_id,</if>
            <if test="productId != null">product_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="orderId != null">order_id,</if>
            <if test="grade != null">grade,</if>
            <if test="context != null">context,</if>
            <if test="status != null">status,</if>
            <if test="createAt != null">create_at,</if>
            <if test="delFlag != null">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="goodsId != null">#{goodsId},</if>
            <if test="productId != null">#{productId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="grade != null">#{grade},</if>
            <if test="context != null">#{context},</if>
            <if test="status != null">#{status},</if>
            <if test="createAt != null">#{createAt},</if>
            <if test="delFlag != null">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateMallOrderComment" parameterType="MallOrderComment">
        update mall_order_comment
        <trim prefix="SET" suffixOverrides=",">
            <if test="goodsId != null">goods_id = #{goodsId},</if>
            <if test="productId != null">product_id = #{productId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="grade != null">grade = #{grade},</if>
            <if test="context != null">context = #{context},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createAt != null">create_at = #{createAt},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMallOrderCommentById" parameterType="Long">
        delete from mall_order_comment where id = #{id}
    </delete>

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