<?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.pcloud.book.weixinclock.dao.impl.WeixinClockPraiseDaoImpl" >
    <resultMap id="BaseResultMap" type="com.pcloud.book.weixinclock.entity.WeixinClockPraise">
        <id column="id" property="id" jdbcType="BIGINT" />
        <result column="weixin_clock_id" property="weixinClockId" jdbcType="BIGINT" />
        <result column="wx_user_id" property="wxUserId" jdbcType="VARCHAR" />
        <result column="wx_group_id" property="wxGroupId" jdbcType="VARCHAR" />
        <result column="praise_user_id" property="praiseUserId" jdbcType="BIGINT" />
        <result column="is_cancel" property="isCancel" jdbcType="INTEGER" />
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
    </resultMap>

    <sql id="Base_Column_List" >
        id, weixin_clock_id, wx_user_id, wx_group_id, praise_user_id, is_cancel, create_time, update_time
    </sql>

    <insert id="insert" parameterType="com.pcloud.book.weixinclock.entity.WeixinClockPraise" useGeneratedKeys="true" keyProperty="id">
        insert into weixin_clock_praise (weixin_clock_id, wx_user_id, wx_group_id, praise_user_id, is_cancel, create_time, update_time)
        values (#{weixinClockId}, #{wxUserId}, #{wxGroupId}, #{praiseUserId}, #{isCancel}, now(), now())
    </insert>

    <update id="update" parameterType="com.pcloud.book.weixinclock.entity.WeixinClockPraise">
        update weixin_clock_praise
        <set>
            <if test="weixinClockId != null">
                weixin_clock_id = #{weixinClockId},
            </if>
            <if test="wxUserId != null">
                wx_user_id = #{wxUserId},
            </if>
            <if test="wxGroupId">
                wx_group_id = #{wxGroupId},
            </if>
            <if test="praiseUserId != null">
                praise_user_id = #{praiseUserId},
            </if>
            <if test="isCancel != null">
                is_cancel = #{isCancel},
            </if>
            update_time = now(),
        </set>
        where id = #{id}
    </update>

    <select id="getByMemberAndUser" parameterType="com.pcloud.book.weixinclock.dto.GetByMemberAndUserDto" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from weixin_clock_praise
        where wx_group_id = #{wxGroupId} and wx_user_id = #{wxUserId}
          and praise_user_id = #{wechatUserId} and create_time <![CDATA[>]]> #{createTime}
        limit 1
    </select>

    <select id="getMemberPraise" parameterType="com.pcloud.book.weixinclock.dto.ListWeixinClockRankDto" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from weixin_clock_praise
        where wx_group_id = #{wxGroupId}
        and wx_user_id in
        <foreach collection="wxUserIdList" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        and is_cancel = 1
    </select>

    <select id="getPraiseNum" parameterType="map" resultType="integer">
        select count(1) from weixin_clock_praise
        where wx_user_id = #{wxUserId} and wx_group_id = #{wechatGroupId} and is_cancel = 1
    </select>

    <select id="listByWxUserId" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from weixin_clock_praise
        where wx_user_id = #{wxUserId} and wx_group_id = #{wechatGroupId} and is_cancel = 1
    </select>

    <select id="listAll" resultType="com.pcloud.book.weixinclock.dto.RankDto">
        select wx_group_id wxGroupId, wx_user_id wxUserId, count(1) praiseNum from weixin_clock_praise
        where is_cancel = 1 group by wxGroupId, wxUserId having praiseNum > 0
    </select>
</mapper>