<?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.platform.dao.CommentDao">

    <resultMap type="com.platform.entity.CommentEntity" id="commentMap">
        <result property="id" column="id"/>
        <result property="typeId" column="type_id"/>
        <result property="valueId" column="value_id"/>
        <result property="content" column="content"/>
        <result property="addTime" column="add_time"/>
        <result property="status" column="status"/>
        <result property="userId" column="user_id"/>
    </resultMap>

    <select id="queryObject" resultType="com.platform.entity.CommentEntity">
		select
			`id`,
			`type_id`,
			`value_id`,
			`content`,
			`add_time`,
			`status`,
			`user_id`
		from nideshop_comment
		where id = #{id}
	</select>

    <select id="queryList" resultType="com.platform.entity.CommentEntity">
        select
        nideshop_comment.id,
        nideshop_comment.type_id,
        nideshop_comment.value_id,
        nideshop_comment.content,
        nideshop_comment.add_time,
        nideshop_comment.status,
        nideshop_comment.user_id,
        nideshop_user.username user_name,
        nideshop_goods.name value_name
        from nideshop_comment LEFT JOIN nideshop_user ON nideshop_comment.user_id = nideshop_user.id
        LEFT JOIN nideshop_goods ON nideshop_comment.value_id = nideshop_goods.id
        WHERE 1=1
        <if test="userName != null and userName.trim() != ''">
            AND nideshop_user.username LIKE concat('%',#{userName},'%')
        </if>
        <if test="valueName != null and valueName.trim() != ''">
            AND nideshop_goods.name LIKE concat('%',#{valueName},'%')
        </if>
        <if test="status != null">
            AND nideshop_comment.status = #{status}
        </if>
        <if test="picUrl == 'true'">
            AND EXISTS(SELECT 1 FROM nideshop_comment_picture WHERE nideshop_comment.id = nideshop_comment_picture.comment_id)
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by nideshop_comment.id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
        select count(*) from nideshop_comment LEFT JOIN nideshop_user ON nideshop_comment.user_id = nideshop_user.id
        LEFT JOIN nideshop_goods ON nideshop_comment.value_id = nideshop_goods.id
        WHERE 1=1
        <if test="userName != null and userName.trim() != ''">
            AND nideshop_user.username LIKE concat('%',#{userName},'%')
        </if>
        <if test="valueName != null and valueName.trim() != ''">
            AND nideshop_goods.name LIKE concat('%',#{valueName},'%')
        </if>
        <if test="status != null">
            AND nideshop_comment.status = #{status}
        </if>
        <if test="picUrl == 'true'">
            AND EXISTS(SELECT 1 FROM nideshop_comment_picture WHERE nideshop_comment.id = nideshop_comment_picture.comment_id)
        </if>
    </select>

    <insert id="save" parameterType="com.platform.entity.CommentEntity" useGeneratedKeys="true" keyProperty="id">
		insert into nideshop_comment(
			`type_id`,
			`value_id`,
			`content`,
			`add_time`,
			`status`,
			`user_id`)
		values(
			#{typeId},
			#{valueId},
			#{content},
			#{addTime},
			#{status},
			#{userId})
	</insert>

    <update id="update" parameterType="com.platform.entity.CommentEntity">
        update nideshop_comment
        <set>
            <if test="typeId != null">`type_id` = #{typeId},</if>
            <if test="valueId != null">`value_id` = #{valueId},</if>
            <if test="content != null">`content` = #{content},</if>
            <if test="addTime != null">`add_time` = #{addTime},</if>
            <if test="status != null">`status` = #{status},</if>
            <if test="userId != null">`user_id` = #{userId}</if>
        </set>
        where id = #{id}
    </update>

    <delete id="delete">
		delete from nideshop_comment where id = #{value}
	</delete>

    <delete id="deleteBatch">
        delete from nideshop_comment where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>