<?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.yami.shop.dao.ProdCommMapper">
    <resultMap id="BaseResultMap" type="com.yami.shop.bean.model.ProdComm">
        <id column="prod_comm_id" jdbcType="BIGINT" property="prodCommId"/>
        <result property="prodId" column="prod_id"/>
        <result property="orderNumber" column="order_number"/>
        <result property="userId" column="user_id"/>
        <result property="content" column="content"/>
        <result property="replyContent" column="reply_content"/>
        <result property="recTime" column="rec_time"/>
        <result property="replyTime" column="reply_time"/>
        <result property="replySts" column="reply_sts"/>
        <result property="postip" column="postip"/>
        <result property="score" column="score"/>
        <result property="usefulCounts" column="useful_counts"/>
        <result property="pics" column="pics"/>
        <result property="isAnonymous" column="is_anonymous"/>
        <result property="status" column="status"/>
        <result property="evaluate" column="evaluate"/>

        <result property="nickName" column="nick_name"/>
        <result property="avatar" column="avatar"/>
    </resultMap>


    <select id="getProdCommDataByProdId" resultType="com.yami.shop.bean.app.dto.ProdCommDataDto">
        SELECT count(1)                                               AS number,
               count(CASE WHEN evaluate = 0 THEN 1 ELSE null END)     AS praise_number,
               count(CASE WHEN evaluate = 1 THEN 1 ELSE null END)     AS secondary_number,
               count(CASE WHEN evaluate = 2 THEN 1 ELSE null END)     AS negative_number,
               count(CASE WHEN score = 1 THEN 1 ELSE null END)        AS scoreNumber1,
               count(CASE WHEN score = 2 THEN 1 ELSE null END)        AS scoreNumber2,
               count(CASE WHEN score = 3 THEN 1 ELSE null END)        AS scoreNumber3,
               count(CASE WHEN score = 4 THEN 1 ELSE null END)        AS scoreNumber4,
               count(CASE WHEN score = 5 THEN 1 ELSE null END)        AS scoreNumber5,
               count(CASE WHEN pics is not null THEN 1 ELSE null END) AS pic_number
        FROM tz_prod_comm
        WHERE prod_id = #{prodId}
          and (status = 1)
    </select>


    <sql id="ProdCommDto_SQL">
        pc
        .
        prod_comm_id
        ,
        pc.reply_content,
        pc.rec_time,
        pc.score,
        pc.is_anonymous,
        pc.pics,
        pc.user_id,
        pc.reply_sts,
        pc.evaluate,
        pc.content,
        u.nick_name,
        u.pic
    </sql>

    <resultMap id="ProdCommDto" type="com.yami.shop.bean.app.dto.ProdCommDto">
        <id column="prod_comm_id" jdbcType="BIGINT" property="prodCommId"/>
        <result column="reply_content" jdbcType="VARCHAR" property="replyContent"/>
        <result column="rec_time" jdbcType="TIMESTAMP" property="recTime"/>
        <result column="reply_time" jdbcType="TIMESTAMP" property="replyTime"/>
        <result column="score" jdbcType="TINYINT" property="score"/>
        <result column="is_anonymous" jdbcType="INTEGER" property="isAnonymous"/>
        <result column="pics" jdbcType="VARCHAR" property="pics"/>
        <result column="nick_name" jdbcType="VARCHAR" property="nickName"/>
        <result column="pic" jdbcType="VARCHAR" property="pic"/>
        <result column="reply_sts" jdbcType="VARCHAR" property="replySts"/>
        <result property="evaluate" jdbcType="TINYINT" column="evaluate"/>
        <result property="content" jdbcType="VARCHAR" column="content"/>
    </resultMap>

    <select id="getProdCommDtoPageByProdId" resultMap="ProdCommDto">
        select
        <include refid="ProdCommDto_SQL"/>
        from
        tz_prod_comm pc
        left join tz_user u on u.user_id=pc.user_id
        where pc.prod_id = #{prodId} and (pc.status = 1)
        <if test="evaluate != -1 and evaluate != 3">
            and pc.evaluate = #{evaluate}
        </if>
        <if test="evaluate == 3">
            and pc.pics is not null
        </if>
    </select>

    <select id="getProdCommDtoPageByUserId" resultMap="ProdCommDto">
        select
        <include refid="ProdCommDto_SQL"/>
        from
        tz_prod_comm pc
        left join tz_user u on u.user_id=pc.user_id
        where pc.user_id = #{userId} and pc.status = 1
    </select>

    <!--评论管理-->
    <resultMap id="ProdComm_Prod_User" type="com.yami.shop.bean.model.ProdComm">
        <id column="prod_comm_id" jdbcType="BIGINT" property="prodCommId"/>
        <result property="prodId" column="prod_id"/>
        <result property="orderNumber" column="order_number"/>
        <result property="userId" column="user_id"/>
        <result property="content" column="content"/>
        <result property="replyContent" column="reply_content"/>
        <result property="recTime" column="rec_time"/>
        <result property="replyTime" column="reply_time"/>
        <result property="replySts" column="reply_sts"/>
        <result property="postip" column="postip"/>
        <result property="score" column="score"/>
        <result property="usefulCounts" column="useful_counts"/>
        <result property="pics" column="pics"/>
        <result property="isAnonymous" column="is_anonymous"/>
        <result property="status" column="status"/>
        <result property="prodName" column="prod_name"/>
        <result property="evaluate" column="evaluate"/>
        <association property="user" javaType="com.yami.shop.bean.vo.UserVO">
            <id column="user_id" jdbcType="VARCHAR" property="userId"/>
            <result column="nick_name" jdbcType="VARCHAR" property="nickName"/>
            <result column="user_mobile" jdbcType="VARCHAR" property="userMobile"/>
        </association>
    </resultMap>
    <select id="getProdCommPage" resultMap="ProdComm_Prod_User">
        select
        pc.prod_comm_id,
        pc.score,
        pc.is_anonymous,
        pc.status,
        pc.rec_time,
        pc.reply_time,
        pc.evaluate,
        u.nick_name,
        u.user_mobile,
        p.prod_name
        from
        tz_prod_comm pc
        left join tz_prod p on p.prod_id=pc.prod_id
        left join tz_user u on pc.user_id=u.user_id
        <where>
            <if test="prodComm.status!=null">
                and pc.status=#{prodComm.status}
            </if>
            <if test="prodComm.status!=null">
                and pc.status=#{prodComm.status}
            </if>
            <if test="shopId!=null">
                and p.shop_id=#{shopId}
            </if>
            <if test="prodComm.prodName!=null and prodComm.prodName!= ''">
                and p.prod_name like concat('%',#{prodComm.prodName},'%')
            </if>
        </where>
        order by pc.prod_comm_id desc
    </select>


    <select id="commList" resultMap="BaseResultMap">
        SELECT b.nick_name,b.pic avatar,a.*
        FROM tz_prod_comm a
        LEFT JOIN tz_user b on a.user_id=b.user_id
        WHERE a.order_number in (SELECT a.order_number FROM tz_order_item a
        LEFT JOIN tz_order b on a.order_number=b.order_number
        WHERE a.prod_id = #{prodId} and a.shop_id = #{shopId} and b.channel_id=#{channelId}) and a.`status`=1
        <if test="commStatus != null">
        <choose>
            <when test="commStatus == 3">
                and !ISNULL(a.pics)
            </when>
            <otherwise>
                and a.evaluate = #{commStatus}
            </otherwise>
        </choose>
        </if>
        ORDER BY a.rec_time desc
    </select>

    <select id="commentCount" resultType="integer">
        SELECT COUNT(1)
        FROM tz_prod_comm
        WHERE order_number in (SELECT a.order_number FROM tz_order_item a
        LEFT JOIN tz_order b on a.order_number=b.order_number
        WHERE a.prod_id = #{prodId} and a.shop_id = #{shopId} and b.channel_id=#{channelId})
        and `status` = 1
        <if test="evaluate!=null">
            and evaluate = #{evaluate}
        </if>
    </select>

    <select id="picCount" resultType="integer">
        SELECT COUNT(1)
        FROM tz_prod_comm
        WHERE order_number in (SELECT a.order_number
                               FROM tz_order_item a
                                        LEFT JOIN tz_order b on a.order_number = b.order_number
                               WHERE a.prod_id = #{prodId}
                                 and a.shop_id = #{shopId}
                                 and b.channel_id = #{channelId})
          and `status` = 1
          and TRIM(pics) > ''
    </select>

    <select id="goodRate" resultType="string">
        SELECT IFNULL(CONCAT(CAST(SUM(CASE WHEN pc.evaluate = 0 AND pc.`status` = 1 THEN 1 ELSE 0 END) * 100.0 /
                                  COUNT(*) AS DECIMAL(5, 2)), '%'), '100%') AS rate
        FROM tz_prod_comm pc
        WHERE pc.order_number in (SELECT a.order_number
                                  FROM tz_order_item a
                                           LEFT JOIN tz_order b on a.order_number = b.order_number
                                  WHERE a.prod_id = #{prodId}
                                    and a.shop_id = #{shopId}
                                    and b.channel_id = #{channelId})
    </select>

    <select id="myCommList" resultMap="BaseResultMap">
        SELECT b.nick_name, b.pic avatar, a.*
        FROM tz_prod_comm a
                 LEFT JOIN tz_user b on a.user_id = b.user_id
        WHERE a.user_id = #{userId}
        ORDER BY a.rec_time desc, a.prod_comm_id desc
    </select>

    <select id="getShopName" resultType="string">
        SELECT shop_name
        FROM tz_shop_detail
        WHERE shop_id = (SELECT shop_id FROM tz_order WHERE order_number = #{orderNumber})
    </select>

    <select id="backendCommList" resultMap="BaseResultMap">
        SELECT b.nick_name,b.pic avatar,a.*
        FROM tz_prod_comm a
        LEFT JOIN tz_user b on a.user_id=b.user_id
        LEFT JOIN tz_order c on a.order_number =c.order_number
        <where>
            <if test="po.score != null">
                and a.score=#{po.score}
            </if>
            <if test="po.score != null">
                and a.status=#{po.status}
            </if>
            <if test="po.replySts != null">
                and a.reply_sts=#{po.replySts}
            </if>
            <if test="po.shopId != null">
                and c.shop_id=#{po.shopId}
            </if>
            <if test="po.startTime != null and po.startTime != ''">
                AND a.rec_time &gt;= str_to_date(#{po.startTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="po.endTime != null and po.endTime != ''">
                AND a.rec_time &lt;= str_to_date(#{po.endTime},'%Y-%m-%d %H:%i:%s')
            </if>
        </where>
        ORDER BY a.rec_time desc,a.prod_comm_id desc
    </select>

</mapper>
