<?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.diaoyun.zion.chinafrica.dao.TbCfItemCommentDao">

    <resultMap type="com.diaoyun.zion.chinafrica.entity.TbCfItemCommentEntity" id="tbCfItemCommentMap">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="orderId" column="order_id"/>
        <result property="itemId" column="item_id"/>
        <result property="type" column="type"/>
        <result property="urls" column="urls"/>
        <result property="itemScore" column="item_score"/>
        <result property="serviceScore" column="service_score"/>
        <result property="logisticsScore" column="logistics_score"/>
        <result property="priceScore" column="price_score"/>
        <result property="itemReview" column="item_review"/>
        <result property="likeNum" column="like_num"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <select id="queryObject" resultType="com.diaoyun.zion.chinafrica.entity.TbCfItemCommentEntity">
		select
			`id`,
			`user_id`,
			`order_id`,
			`item_id`,
			`type`,
			`urls`,
			`item_score`,
			`service_score`,
			`logistics_score`,
			`price_score`,
			`item_review`,
			`like_num`,
			`del_flag`,
			`create_time`,
			`update_time`
		from tb_cf_item_comment
		where id = #{id}
	</select>
    <select id="querycomments" resultType="com.diaoyun.zion.chinafrica.entity.TbCfItemCommentEntity">
        SELECT
        u.nick username,
        c.*
        FROM
        tb_cf_item_comment c
        LEFT JOIN tb_cf_user_info u ON c.user_id = u.user_id
        WHERE
        item_id =#{itemId}
        AND c.del_flag = 0
        ORDER BY c.create_time DESC
    </select>
    <select id="queryLastComment" resultType="com.diaoyun.zion.chinafrica.entity.TbCfItemCommentEntity">
		SELECT
			c.*,
			u.nick username,
			m.count count
		FROM
			tb_cf_item_comment c
			LEFT JOIN tb_cf_user_info u ON c.user_id = u.user_id
			LEFT JOIN ( SELECT item_id, count(*) count FROM tb_cf_item_comment GROUP BY item_id ) m ON c.item_id = m.item_id
		WHERE
			c.item_id = #{itemId}
			AND c.del_flag = 0
			AND c.del_flag = 0
		ORDER BY
			c.create_time DESC
			LIMIT 1
	</select>
    <select id="queryByUser" resultType="com.diaoyun.zion.chinafrica.entity.TbCfItemCommentEntity">
        select
        `id`,
        `user_id`,
        `order_id`,
        `item_id`,
        `type`,
		`urls`,
        `item_score`,
        `service_score`,
        `logistics_score`,
        `price_score`,
        `item_review`,
        `like_num`,
        `del_flag`,
        `create_time`,
        `update_time`
        from tb_cf_item_comment
        WHERE item_id=#{itemId} and user_id=#{userId}
    </select>
    <select id="queryScore" resultType="com.diaoyun.zion.chinafrica.entity.TbCfItemCommentEntity">
        select
        `id`,
        `user_id`,
        `order_id`,
        `item_id`,
        `type`,
		`urls`,
        `item_score`,
        `service_score`,
        `logistics_score`,
        `price_score`,
        `item_review`,
        `like_num`,
        `del_flag`,
        `create_time`,
        `update_time`
        from tb_cf_item_comment
        WHERE item_id=#{itemId}
    </select>
    <select id="queryList" resultType="com.diaoyun.zion.chinafrica.entity.TbCfItemCommentEntity">
        select
        `id`,
        `user_id`,
        `order_id`,
        `item_id`,
        `item_score`,
        `type`,
        `urls`,
        `service_score`,
        `logistics_score`,
        `price_score`,
        `item_review`,
        `like_num`,
        `del_flag`,
        `create_time`,
        `update_time`
        from tb_cf_item_comment
        WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>


    <select id="queryTotal" resultType="int">
        select count(*) from tb_cf_item_comment
        WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
    </select>

    <insert id="save" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfItemCommentEntity">
		insert into tb_cf_item_comment(
			`id`,
			`user_id`,
			`order_id`,
			`item_id`,
			`type`,
			`urls`,
			`item_score`,
			`service_score`,
			`logistics_score`,
			`price_score`,
			`item_review`,
			`like_num`,
			`del_flag`,
			`create_time`,
			`update_time`)
		values(
			#{id},
			#{userId},
			#{orderId},
			#{itemId},
			#{type},
			#{urls},
			#{itemScore},
			#{serviceScore},
			#{logisticsScore},
			#{priceScore},
			#{itemReview},
			#{likeNum},
			#{delFlag},
			#{createTime},
			#{updateTime})
	</insert>

    <update id="update" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfItemCommentEntity">
        update tb_cf_item_comment
        <set>
            <if test="userId != null">`user_id` = #{userId},</if>
            <if test="orderId != null">`order_id` = #{orderId},</if>
            <if test="itemId != null">`item_id` = #{itemId},</if>
            <if test="type != null">`type` = #{type},</if>
            <if test="urls != null">`urls` = #{urls},</if>
            <if test="itemScore != null">`item_score` = #{itemScore},</if>
            <if test="serviceScore != null">`service_score` = #{serviceScore},</if>
            <if test="logisticsScore != null">`logistics_score` = #{logisticsScore},</if>
            <if test="priceScore != null">`price_score` = #{priceScore},</if>
            <if test="itemReview != null">`item_review` = #{itemReview},</if>
            <if test="likeNum != null">`like_num` = #{likeNum},</if>
            <if test="delFlag != null">`del_flag` = #{delFlag},</if>
            <if test="createTime != null">`create_time` = #{createTime},</if>
            <if test="updateTime != null">`update_time` = #{updateTime}</if>
        </set>
        where id = #{id}
    </update>

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

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

</mapper>