<?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.WeixinClockTopicDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.weixinclock.entity.WeixinClockTopic">
        <id column="weixin_clock_topic_id" property="weixinClockTopicId" jdbcType="BIGINT"/>
        <result column="weixin_clock_id" property="weixinClockId" jdbcType="BIGINT"/>
        <result column="weixin_clock_member_id" property="weixinClockMemberId" jdbcType="BIGINT"/>
        <result column="wx_user_id" property="wxUserId" jdbcType="VARCHAR"/>
        <result column="book_group_classify_id" property="bookGroupClassifyId" jdbcType="BIGINT"/>
        <result column="wx_group_id" property="wxGroupId" jdbcType="VARCHAR"/>
        <result column="created_day" property="createdDay" jdbcType="DATE"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        weixin_clock_topic_id, weixin_clock_id, weixin_clock_member_id, wx_user_id, book_group_classify_id, wx_group_id, created_day, create_time
    </sql>

    <insert id="insert" parameterType="weixinClockTopic" useGeneratedKeys="true" keyProperty="weixinClockTopicId" flushCache="true">
        INSERT INTO weixin_clock_topic (
        weixin_clock_id, weixin_clock_member_id, wx_user_id, book_group_classify_id, wx_group_id, created_day, create_time
        )
        VALUES (
        #{weixinClockId, jdbcType=BIGINT}, #{weixinClockMemberId, jdbcType=BIGINT}, #{wxUserId, jdbcType=VARCHAR}, #{bookGroupClassifyId, jdbcType=BIGINT},
        #{wxGroupId, jdbcType=VARCHAR}, #{createdDay, jdbcType=DATE}, NOW()
        )
        ON DUPLICATE KEY
        UPDATE
        weixin_clock_member_id = weixin_clock_member_id
    </insert>

    <!-- 获取用户今天打卡情况 -->
    <select id="getTopicByIds4Today" resultMap="BaseResultMap" parameterType="map">
        select
        <include refid="Base_Column_List"/>
        from weixin_clock_topic
        where
        wx_group_id =  #{wxGroupId, jdbcType=VARCHAR}
        and wx_user_id = #{wxUserId, jdbcType=VARCHAR}
        AND CREATED_DAY = #{date}
        LIMIT 1
    </select>

    <!-- 获得该用户的打卡天数 -->
    <select id="getClockCountByUser" parameterType="map" resultType="long">
        SELECT
        count(DISTINCT CREATED_DAY)
        FROM
        weixin_clock_topic
        WHERE
        wx_group_id =  #{wxGroupId, jdbcType=VARCHAR}
        AND wx_user_id = #{wxUserId, jdbcType=VARCHAR}
        <if test="createdDay != null">
            AND CREATED_DAY = #{createdDay,jdbcType=VARCHAR}
        </if>
    </select>

</mapper>