<?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.web.modules.order.mapper.BizGoodsReviewMapper">

    <resultMap id="BizGoodsReviewResult" type="com.ruoyi.web.modules.order.domain.BizGoodsReview">
        <id property="reviewId" column="review_id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="orderId" column="order_id"/>
        <result property="orderItemId" column="order_item_id"/>
        <result property="memberId" column="member_id"/>
        <result property="shopId" column="shop_id"/>
        <result property="memberNickName" column="member_nick_name"/>
        <result property="memberAvatar" column="member_avatar"/>
        <result property="content" column="content"/>
        <result property="score" column="score"/>
        <result property="reviewPics" column="review_pics"/>
        <result property="goodsMainPic" column="goods_main_pic"/>
        <result property="goodsSpec" column="goods_spec"/>
        <result property="replyContent" column="reply_content"/>
        <result property="replyBy" column="reply_by"/>
        <result property="replyTime" column="reply_time"/>
        <result property="showFlag" column="show_flag"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="sellerListWhere">
        where r.shop_id = #{shopId}
          and r.del_flag = '0'
        <if test="query != null and query.replyStatus != null and query.replyStatus == 'UNREPLIED'">
            and (r.reply_content is null or r.reply_content = '')
        </if>
        <if test="query != null and query.replyStatus != null and query.replyStatus == 'REPLIED'">
            and r.reply_content is not null and r.reply_content &lt;&gt; ''
        </if>
        <if test="query != null and query.score != null">
            and r.score = #{query.score}
        </if>
        <if test="query != null and query.memberNickName != null and query.memberNickName != ''">
            and r.member_nick_name like concat('%', #{query.memberNickName}, '%')
        </if>
        <if test="query != null and query.goodsName != null and query.goodsName != ''">
            and oi.goods_name like concat('%', #{query.goodsName}, '%')
        </if>
        <if test="query != null and query.beginTime != null and query.beginTime != ''">
            and r.create_time &gt;= #{query.beginTime}
        </if>
        <if test="query != null and query.endTime != null and query.endTime != ''">
            and r.create_time &lt;= #{query.endTime}
        </if>
    </sql>

    <insert id="insert" useGeneratedKeys="true" keyProperty="reviewId">
        insert into biz_goods_review (goods_id, order_id, order_item_id, member_id, shop_id,
                                      member_nick_name, member_avatar, content, score, review_pics,
                                      goods_main_pic, goods_spec, show_flag, del_flag, create_time, update_time)
        values (#{goodsId}, #{orderId}, #{orderItemId}, #{memberId}, #{shopId},
                #{memberNickName}, #{memberAvatar}, #{content}, #{score}, #{reviewPics},
                #{goodsMainPic}, #{goodsSpec}, #{showFlag}, #{delFlag}, #{createTime}, #{updateTime})
    </insert>

    <select id="selectByOrderId" resultMap="BizGoodsReviewResult">
        select * from biz_goods_review
        where order_id = #{orderId} and del_flag = '0'
        limit 1
    </select>

    <select id="selectByOrderItemId" resultMap="BizGoodsReviewResult">
        select * from biz_goods_review
        where order_item_id = #{orderItemId} and del_flag = '0'
        limit 1
    </select>

    <select id="selectByOrderIds" resultMap="BizGoodsReviewResult">
        select * from biz_goods_review
        where del_flag = '0' and order_id in
        <foreach collection="orderIds" item="id" open="(" separator="," close=")">#{id}</foreach>
    </select>

    <select id="selectMemberList" resultMap="BizGoodsReviewResult">
        select * from biz_goods_review
        where member_id = #{memberId} and del_flag = '0'
        order by create_time desc
    </select>

    <select id="selectByGoodsId" resultMap="BizGoodsReviewResult">
        select * from biz_goods_review
        where goods_id = #{goodsId}
          and show_flag = '1'
          and del_flag = '0'
        order by create_time desc, review_id desc
    </select>

    <select id="selectById" resultMap="BizGoodsReviewResult">
        select * from biz_goods_review
        where review_id = #{reviewId}
        limit 1
    </select>

    <select id="selectSellerList" resultType="com.ruoyi.web.modules.order.vo.SellerReviewListRowVO">
        select r.review_id as reviewId,
               r.member_nick_name as memberNickName,
               r.goods_id as goodsId,
               oi.goods_name as goodsName,
               r.goods_main_pic as goodsMainPic,
               r.goods_spec as goodsSpec,
               r.content as content,
               r.review_pics as reviewPicsRaw,
               r.score as score,
               r.create_time as createTime,
               r.reply_content as replyContent
        from biz_goods_review r
        left join biz_order_item oi on r.order_item_id = oi.item_id
        <include refid="sellerListWhere"/>
        order by r.create_time desc, r.review_id desc
    </select>

    <select id="selectSellerDetail" resultType="com.ruoyi.web.modules.order.vo.SellerReviewDetailVO">
        select r.review_id as reviewId,
               r.score as score,
               r.content as content,
               r.review_pics as reviewPicsRaw,
               r.create_time as createTime,
               r.goods_id as goodsId,
               oi.goods_name as goodsName,
               r.goods_main_pic as goodsMainPic,
               r.goods_spec as goodsSpec,
               r.member_nick_name as memberNickName,
               r.member_avatar as memberAvatar,
               r.order_id as orderId,
               o.order_no as orderNo,
               r.reply_content as replyContent,
               r.reply_by as replyBy,
               r.reply_time as replyTime
        from biz_goods_review r
        left join biz_order_item oi on r.order_item_id = oi.item_id
        left join biz_order o on r.order_id = o.order_id
        where r.review_id = #{reviewId}
          and r.shop_id = #{shopId}
          and r.del_flag = '0'
        limit 1
    </select>

    <update id="updateReply" parameterType="com.ruoyi.web.modules.order.domain.BizGoodsReview">
        update biz_goods_review
        set reply_content = #{replyContent},
            reply_by = #{replyBy},
            reply_time = #{replyTime},
            update_time = #{updateTime}
        where review_id = #{reviewId}
    </update>

    <update id="updateDelFlag">
        update biz_goods_review
        set del_flag = '2',
            update_time = now()
        where review_id = #{reviewId}
          and shop_id = #{shopId}
          and del_flag = '0'
    </update>

</mapper>
