<?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="book_group_id" property="bookGroupId" jdbcType="BIGINT"/>
        <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"/>
        <result column="type" property="type" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, cipher, book_group_id, classify_id, qrcode_id, create_time, wechat_user_id, update_time, wx_id, has_used, alt_id, type
    </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,
            book_group_id,
            classify_id,
            qrcode_id,
            create_time,
            wechat_user_id,
            has_used,
            alt_id,
            type
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{cipher,jdbcType=VARCHAR},
            #{bookGroupId,jdbcType=BIGINT},
            #{classifyId,jdbcType=BIGINT},
            #{qrcodeId,jdbcType=BIGINT},
            now(),
            #{wechatUserId,jdbcType=BIGINT},
            0,
            #{altId,jdbcType=VARCHAR},
            #{type,jdbcType=INTEGER}
        </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>

    <!--获取用户支付金额-->
    <select id="getPayPrice" parameterType="map" resultType="decimal">
        SELECT
        price
        FROM
        join_group_cipher c
        LEFT JOIN book_classify_buy_record r ON c.classify_id = r.classify_id
        AND c.wechat_user_id = r.wechat_user_id
        WHERE
        c.qrcode_id = #{qrcodeId}
        AND c.wx_id = #{wxId}
    </select>

    <!--根据条件获取-->
    <select id="getByCondition" parameterType="map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from join_group_cipher
        where wechat_user_id=#{wechatUserId} and
        book_group_id=#{bookGroupId} and
        type=#{type}
    </select>

    <!--根据用户微信id、机器人id、类型获取暗号集合-->
    <select id="getListByAltIdAndWxIdAndType" parameterType="map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from join_group_cipher
        where wx_id=#{userWxId} and
        alt_id=#{robotId} and
        type=#{type}
    </select>

    <!--根据用户id和群id获取基本信息-->
    <select id="getByWechatUserIdAndQrcodeId" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from join_group_cipher
        where wechat_user_id=#{wechatUserId} and
        qrcode_id=#{qrcodeId} and
        type=#{type} limit 1
    </select>

    <!--更新暗号-->
    <update id="updateCipher" parameterType="map" >
        update join_group_cipher set
        cipher=#{cipher}
        where id=#{id}
    </update>

</mapper>