<?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="create_user" property="createUser" 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="BIGINT"/>
        <result column="has_used" property="hasUsed" jdbcType="BOOLEAN"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, cipher, classify_id, create_time, create_user, update_time, wx_id, has_used
    </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,
            create_time,
            create_user,
            has_used
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{cipher,jdbcType=BIGINT},
            #{classifyId,jdbcType=BIGINT},
            now(),
            #{createUser,jdbcType=BIGINT},
            0
        </trim>
    </insert>

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

    <!--根据暗号获取-->
    <select id="getByCipher" parameterType="String" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from join_group_cipher
        where 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>
</mapper>