<?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="io.nebulas.explorer.mapper.NebAddressMapper">
    <resultMap id="BaseResultMap" type="io.nebulas.explorer.domain.NebAddress">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="hash" property="hash" jdbcType="VARCHAR"/>
        <result column="nonce" property="nonce" jdbcType="VARCHAR"/>
        <result column="type" property="type" jdbcType="TINYINT"/>
        <result column="alias" property="alias" jdbcType="VARCHAR"/>
        <result column="current_balance" property="currentBalance" jdbcType="DECIMAL"/>
        <result column="created_at" property="createdAt" jdbcType="TIMESTAMP"/>
        <result column="updated_at" property="updatedAt" jdbcType="TIMESTAMP"/>
    </resultMap>
    <sql id="Base_Column_List">
        `id`, `hash`,`nonce`, `type`, `alias`,`current_balance`, `created_at`, `updated_at`
    </sql>

    <!--<insert id="add" parameterType="map" useGeneratedKeys="false">-->
        <!--INSERT ignore INTO `neb_address` (`hash`, `type`) VALUES (#{hash}, #{type})-->
    <!--</insert>-->

    <insert id="addAddress" parameterType="map" useGeneratedKeys="false">
        INSERT ignore INTO `neb_address` (`hash`,`nonce`, `type`, `current_balance`) VALUES (#{hash}, #{nonce}, #{type}, #{balance})
    </insert>

    <update id="update" parameterType="map">
        UPDATE `neb_address`
        SET `current_balance` = #{balance,jdbcType=DECIMAL}, `nonce` = #{nonce,jdbcType=VARCHAR}
        WHERE `hash` = #{hash,jdbcType=VARCHAR}
    </update>

    <select id="countTotalAddressCnt" resultType="java.lang.Long">
        SELECT count(1)
        FROM `neb_address`
    </select>

    <select id="countAccountGroupByType" resultType="io.nebulas.explorer.model.vo.AddrTypeVo">
        select `type` as type,count(*) as amount
        from `neb_address`
        group by `type`
    </select>

    <select id="getByHash" resultMap="BaseResultMap" parameterType="java.lang.String">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `neb_address`
        WHERE `hash` = #{hash,jdbcType=VARCHAR}
    </select>

    <select id="findAddressOrderByBalance" parameterType="map" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `neb_address`
        ORDER BY `current_balance` DESC
        LIMIT ${offset},${limit}
    </select>

    <select id="findAddressMapByAddressHash" resultType="map" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `neb_address`
        WHERE `hash` in
        <foreach collection="list" item="item" open="(" close=")" separator=",">#{item,jdbcType=VARCHAR}</foreach>
    </select>


</mapper>