<?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.jarvis.mapper.CommentMapper">

    <resultMap id="CommentResult" type="Comment">
        <result property="id" column="id"/>
        <result property="productId" column="product_id"/>
        <result property="userName" column="user_name"/>
        <result property="commentText" column="comment_text"/>
        <result property="commentId" column="comment_id"/>
        <result property="pictureUrls" column="picture_urls"/>
        <result property="createdAt" column="created_at"/>
        <result property="commentDate" column="comment_date"/>
        <result property="isUse" column="is_use"/>
    </resultMap>

    <sql id="selectCommentBase">
        select id, product_id, user_name, comment_text, comment_id, picture_urls, 
               created_at, comment_date, is_use
        from comments
    </sql>

    <select id="selectCommentList" parameterType="Comment" resultMap="CommentResult">
        <include refid="selectCommentBase"/>
        <where>
            <if test="productId != null and productId != ''"> and product_id = #{productId}</if>
            <if test="userName != null and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
            <if test="commentText != null and commentText != ''"> and comment_text like concat('%', #{commentText}, '%')</if>
            <if test="isUse != null"> and is_use = #{isUse}</if>
            <if test="params.beginTime != null and params.beginTime != ''">
                and created_at &gt;= #{params.beginTime}
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                and created_at &lt;= #{params.endTime}
            </if>
        </where>
        order by created_at desc
    </select>

    <select id="selectCommentById" parameterType="Long" resultMap="CommentResult">
        <include refid="selectCommentBase"/>
        where id = #{id}
    </select>

    <select id="selectCommentStatisticsByProductId" parameterType="String" resultType="java.util.Map">
        select 
            count(*) as totalCount,
            sum(case when is_use = 0 then 1 else 0 end) as availableCount,
            sum(case when is_use = 1 then 1 else 0 end) as usedCount,
            (select max(created_at) from comments where product_id = #{productId}) as lastCommentUpdateTime
        from comments
        where product_id = #{productId}
    </select>

    <update id="updateCommentIsUse" parameterType="Comment">
        update comments
        <set>
            <if test="isUse != null">is_use = #{isUse},</if>
        </set>
        where id = #{id}
    </update>

    <update id="resetCommentIsUseByProductId" parameterType="String">
        update comments
        set is_use = 0
        where product_id = #{productId}
    </update>

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

</mapper>

