<?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="org.springblade.sing.active.mapper.ActiveHelpRecordMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="activeHelpRecordResultMap" type="org.springblade.sing.active.vo.ActiveHelpRecordVO">
        <result column="id" property="id"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="user_id" property="userId"/>
        <result column="active_id" property="activeId"/>
        <result column="vote_count" property="voteCount"/>
        <result column="helpGoodsName" property="helpGoodsName"/>
        <result column="userName" property="userName"/>
        <result column="productTitle" property="productTitle"/>
        <result column="ip" property="ip"/>
        <result column="active_name" property="activeName"/>
    </resultMap>

    <resultMap id="productVoteResultMap" type="org.springblade.sing.active.entity.ProductVote">
        <result column="product_name" property="productName"/>
        <result column="ip" property="ip"/>
        <result column="product_no" property="productNo"/>
        <result column="t_count" property="count"/>
        <result column="t_status" property="status"/>
    </resultMap>


    <select id="selectActiveHelpRecordPage" resultMap="activeHelpRecordResultMap">
        select hr.*,g.name helpGoodsName,u.nick_name userName,pr.title productTitle,sar.title active_name from sing_active_help_record hr
        inner JOIN sing_help_goods g on hr.help_goods_id = g.id
        inner JOIN sing_login_user u on hr.user_id = u.id
        inner JOIN sing_active_product_record pr on hr.product_id = pr.id
        inner JOIN sing_active_record sar on hr.active_id = sar.id
        where hr.is_deleted = 0
        <if test="activeHelpRecord.helpGoodsName != null">
            and g.name like concat('%',#{activeHelpRecord.helpGoodsName},'%')
        </if>
        <if test="activeHelpRecord.productTitle != null">
            and pr.title like concat('%',#{activeHelpRecord.productTitle},'%')
        </if>
        <if test="activeHelpRecord.userName != null">
            and u.nick_name like concat('%',#{activeHelpRecord.userName},'%')
        </if>
        <if test="activeHelpRecord.ip != null">
            and hr.ip = #{activeHelpRecord.ip}
        </if>
        <if test="activeHelpRecord.status != null">
            and hr.status = #{activeHelpRecord.status}
        </if>
        <if test="activeHelpRecord.activeId != null">
            and hr.active_id = #{activeHelpRecord.activeId}
        </if>
        order by hr.create_time DESC
    </select>


    <select id="selectVote" resultMap="productVoteResultMap">
        SELECT p.product_no product_no,p.title product_name,h.ip,count(0) t_count,h.status t_status FROM sing_active_help_record h
        left join sing_active_product_record p on h.product_id = p.id
        WHERE product_id = #{productId} and h.is_deleted = 0
        group by h.ip,h.status order by t_count desc
    </select>

</mapper>
