<?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.szxgl.nft.mapper.NftPhotoMapper">

    <select id="getRandomHash" resultType="java.lang.Long">
        SELECT
            np.id
        FROM
            user_nft_photo up
        RIGHT JOIN nft_photo np ON up.nft_photo_id = np.id
        WHERE
            ( LENGTH( up.phone ) = 0 OR up.phone IS NULL )
            AND np.STATUS = #{flag}
        ORDER BY
            rand()
        LIMIT 1
    </select>

    <insert id="addNftPhoto">
        insert into user_nft_photo
        (
         phone,
         nft_photo_id,
         create_time
        )
        values
        (
        #{seed},
        #{id},
        now()
        )
    </insert>

    <select id="getPhone" resultType="com.szxgl.nft.entity.UserDO">
        select * from user where openid = #{openid}
    </select>

    <select id="getNftPhotoUrl" resultType="java.util.Map">
        SELECT
            np.`name` as url,
            np.painting_hash,
            np.status,
            u.nickname,
            u.wallet_addr,
            u.phone
        FROM
            nft_photo np
        INNER JOIN user_nft_photo up ON np.id = up.nft_photo_id
        RIGHT JOIN `user` u ON up.phone = u.phone
        where u.openid = #{openid}
    </select>

    <select id="getPhoneAndHash" resultType="java.util.Map">
        SELECT
            u.phone,
            np.painting_hash
        FROM
            nft_photo np
                INNER JOIN user_nft_photo unp ON np.id = unp.nft_photo_id
                INNER JOIN USER u ON u.phone = unp.phone
        where u.openid = #{openid}
    </select>

    <select id="getNftCount" resultType="java.lang.Integer">
        SELECT
            count( np.id )
        FROM
            nft_photo np
        LEFT JOIN user_nft_photo unp ON np.id = unp.nft_photo_id
        WHERE
            unp.id IS NULL AND np.`status` != 1
    </select>

    <select id="getStatus" resultType="java.lang.String">
        SELECT
            np.status
        FROM
            nft_photo np
        INNER JOIN user_nft_photo up ON np.id = up.nft_photo_id
        INNER JOIN `user` u ON up.phone = u.phone
        where u.openid = #{openid}
    </select>

</mapper>