<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.pcloud.book.group.dao.impl.JoinGroupCipherDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.group.entity.JoinGroupCipher">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="cipher" property="cipher" jdbcType="VARCHAR"/>
        <result column="classify_id" property="classifyId" jdbcType="BIGINT"/>
        <result column="qrcode_id" property="qrcodeId" jdbcType="BIGINT"/>
        <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="wx_id" property="wxId" jdbcType="VARCHAR"/>
        <result column="has_used" property="hasUsed" jdbcType="BOOLEAN"/>
        <result column="alt_id" property="altId" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, cipher, classify_id, qrcode_id, create_time, wechat_user_id, update_time, wx_id, has_used, alt_id
    </sql>

    <insert id="insert" parameterType="com.pcloud.book.group.entity.JoinGroupCipher" useGeneratedKeys="true"
            keyProperty="id">
        insert into join_group_cipher
        <trim prefix="(" suffix=")" suffixOverrides=",">
            cipher,
            classify_id,
            qrcode_id,
            create_time,
            wechat_user_id,
            has_used,
            alt_id
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{cipher,jdbcType=VARCHAR},
            #{classifyId,jdbcType=BIGINT},
            #{qrcodeId,jdbcType=BIGINT},
            now(),
            #{wechatUserId,jdbcType=BIGINT},
            0,
            #{altId,jdbcType=VARCHAR}
        </trim>
    </insert>

    <!--获取基本信息-->
    <select id="getByWechatUserId" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from join_group_cipher
        where wechat_user_id=#{wechatUserId} and
        classify_id=#{classifyId}
    </select>

    <!--根据暗号获取-->
    <select id="getByCipher" parameterType="String" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from join_group_cipher
        where cipher=#{cipher} limit 1
    </select>

    <!--根据暗号获取包含bookGroupId-->
    <select id="getDTOByCipher" parameterType="String" resultType="com.pcloud.book.group.dto.JoinGroupCipherDTO">
        select
            a.id, cipher, a.classify_id as classifyId, a.qrcode_id qrcodeId, a.wechat_user_id as wechatUserId, a.wx_id as wxId,
            a.has_used as hasUsed, b.book_group_id as bookGroupId
        from join_group_cipher a left join book_group_classify b on a.classify_id = b.id
        where a.cipher=#{cipher} limit 1
    </select>

    <!--更新暗号状态为已使用-->
    <update id="updateCipherStateToUsed" parameterType="map">
        update join_group_cipher set
        has_used=1,
        wx_id=#{wxId},
        update_time=now()
        where cipher=#{cipher}
    </update>

    <!--根据微信id和机器人id获取分类集合-->
    <select id="getClassifyIdsByWxIdAndAltId" parameterType="map" resultType="com.pcloud.book.group.dto.JoinGroupCipherDTO">
        select a.classify_id as classifyId, a.qrcode_id qrcodeId, b.book_group_id as bookGroupId from join_group_cipher a
        left join book_group_classify b on a.classify_id = b.id where
        a.has_used=1
        and a.alt_id=#{altId}
        and a.wx_id=#{wxId}
    </select>

    <!--更新二维码-->
    <update id="updateQrcodeId" parameterType="map">
        update join_group_cipher set
        qrcode_id=#{qrcodeId}
        where cipher=#{cipher}
        and wx_id=#{wxId}
    </update>
</mapper>