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

    <resultMap id="BaseResultMap" type="com.pcloud.book.weixinclock.entity.WeixinClockMember">
        <id column="weixin_clock_member_id" property="weixinClockMemberId" jdbcType="BIGINT"/>
        <result column="weixin_clock_id" property="weixinClockId" jdbcType="BIGINT"/>
        <result column="wx_user_id" property="wxUserId" jdbcType="VARCHAR"/>
        <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT"/>
        <result column="book_group_classify_id" property="bookGroupClassifyId" jdbcType="BIGINT"/>
        <result column="wx_group_id" property="wxGroupId" jdbcType="VARCHAR"/>
        <result column="nickname" property="nickname" jdbcType="VARCHAR"/>
        <result column="head_pic" property="headPic" jdbcType="VARCHAR"/>
        <result column="all_clock_day" property="allClockDay" jdbcType="INTEGER"/>
        <result column="score" property="score" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        weixin_clock_member_id, weixin_clock_id, wx_user_id, book_group_classify_id, wx_group_id, nickname, head_pic, all_clock_day, score,
        create_time
    </sql>

    <insert id="insert" parameterType="weixinClockMember" useGeneratedKeys="true" keyProperty="weixinClockMemberId" flushCache="true">
        INSERT INTO weixin_clock_member (
        weixin_clock_id, wx_user_id, book_group_classify_id, wx_group_id, nickname, head_pic, all_clock_day, score,
        create_time, update_time,wechat_user_id
        )
        VALUES (
        #{weixinClockId, jdbcType=BIGINT}, #{wxUserId, jdbcType=VARCHAR}, #{bookGroupClassifyId, jdbcType=BIGINT}, #{wxGroupId, jdbcType=VARCHAR},
        #{nickname, jdbcType=VARCHAR}, #{headPic, jdbcType=VARCHAR}, #{allClockDay, jdbcType=INTEGER}, #{score}, NOW(), NOW(),#{wechatUserId, jdbcType=BIGINT}
        )
        ON DUPLICATE KEY
        UPDATE
        update_time = NOW()
    </insert>

    <select id="listWeixinClockRank" parameterType="map" resultType="com.pcloud.book.weixinclock.dto.RankDto">
        SELECT
	        w.wx_user_id wxUserId, w.all_clock_day clockNum, w.score score, w.nickname nickname, w.head_pic headPic, (@i :=@i + 1) rankNum
        FROM
	        weixin_clock_member w,
	        (SELECT @i := 0) i
        WHERE
	        w.wx_group_id = #{wxGroupId, jdbcType=VARCHAR}
        AND (w.all_clock_day <![CDATA[ >]]> 0  or w.wx_user_id = #{wxUserId, jdbcType=VARCHAR})
        ORDER BY w.score desc, w.update_time asc
    </select>

    <select id="getByWxUserId" parameterType="map" resultMap="BaseResultMap">
        select
            weixin_clock_member_id, weixin_clock_id, wx_user_id, book_group_classify_id, wx_group_id, nickname, head_pic, all_clock_day, score, create_time, wechat_user_id
        from
            weixin_clock_member
        where
            wx_user_id = #{userWxId}
        and wx_group_id = #{wechatGroupId}
        LIMIT 1
    </select>

    <update id="updateClockDays" parameterType="map" flushCache="true">
		update weixin_clock_member
		set
			all_clock_day = all_clock_day + 1,
		    score = score + 1,
			wechat_user_id = #{wechatUserId, jdbcType=BIGINT},
			update_time = NOW()
		where
            wx_user_id = #{userWxId}
        and wx_group_id = #{wechatGroupId}
	</update>

    <select id="getMyNewOrder" parameterType="map" resultType="java.lang.Integer">
        select cm.rowNo from (
            select wx_user_id, (@rowNum:=@rowNum+1) AS rowNo from (
                select wx_user_id, @rowNum:=0 from weixin_clock_member
                where wx_group_id = #{wechatGroupId}
                AND (all_clock_day <![CDATA[ >]]> 0  or wx_user_id = #{userWxId, jdbcType=VARCHAR})
                order by score desc, update_time asc
            ) a
        ) cm where cm.wx_user_id = #{userWxId}
        LIMIT 1
    </select>

    <select id="getClockWechatUserRank" parameterType="map" resultType="com.pcloud.book.weixinclock.dto.RankDto">
        select cm.wx_user_id wxUserId, cm.rankNum rankNum, cm.clockNum clockNum, cm.nickname nickname, cm.headPic headPic, cm.score score from (
            select wx_user_id, (@rowNum:=@rowNum+1) AS rankNum, all_clock_day clockNum, nickname nickname, score, head_pic headPic, wechat_user_id from (
                select wx_user_id, all_clock_day, nickname, score, head_pic, wechat_user_id, @rowNum:=0 from weixin_clock_member
                where wx_group_id = #{wechatGroupId}
                AND (all_clock_day <![CDATA[ >]]> 0  or wechat_user_id = #{wechatUserId, jdbcType=VARCHAR})
                order by score desc, update_time asc
            ) a
        ) cm where cm.wechat_user_id = #{wechatUserId}
        LIMIT 1
    </select>

    <update id="updatMemberWechatUserId" parameterType="map">
      update weixin_clock_member
      set wechat_user_id = #{wechatUserId,jdbcType=BIGINT},
          update_time = NOW()
      where wx_user_id = #{wxUserId,jdbcType=VARCHAR}
    </update>

    <select id="getClockWechatUserRankByWxUserId" parameterType="map" resultType="com.pcloud.book.weixinclock.dto.RankDto">
        select cm.wx_user_id wxUserId, cm.rankNum rankNum, cm.clockNum clockNum, cm.score score, cm.nickname nickname, cm.headPic headPic from (
              select wx_user_id, (@rowNum:=@rowNum+1) AS rankNum, all_clock_day clockNum, score, nickname nickname, head_pic headPic, wechat_user_id from (
                    select wx_user_id, all_clock_day, score, nickname, head_pic, wechat_user_id, @rowNum:=0 from weixin_clock_member
                    where wx_group_id = #{wechatGroupId}
                        AND (all_clock_day <![CDATA[ >]]> 0  or wx_user_id = #{wxUserId, jdbcType=VARCHAR})
                    order by score desc, update_time asc
                    ) a
              ) cm where cm.wx_user_id = #{wxUserId}
        LIMIT 1
    </select>

    <select id="updateScore" parameterType="map">
        update weixin_clock_member set
        <choose>
            <when test="isCancel == 1">
                score = score + 1
            </when>
            <otherwise>
                score = score - 1
            </otherwise>
        </choose>
        where weixin_clock_member_id = #{weixinClockMemberId}
    </select>

    <!--旧数据处理：初始化分数-->
    <update id="initScore">
        update weixin_clock_member set score = all_clock_day
    </update>

    <!--旧数据处理-->
    <update id="addPraiseNum" parameterType="map">
        update weixin_clock_member set score = score + #{praiseNum} where wx_user_id = #{wxUserId} and wx_group_id = #{wxGroupId}
    </update>
</mapper>