<?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.m2pool.lease.mapper.LeaseOrderMiningMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.m2pool.lease.entity.LeaseOrderMining">
        <id column="id" property="id" />
        <result column="user_id" property="userId" />
        <result column="order_id" property="orderId" />
        <result column="coin" property="coin" />
        <result column="algorithm" property="algorithm" />
        <result column="pool" property="pool" />
        <result column="pool_url" property="poolUrl" />
        <result column="pool_user" property="poolUser" />
        <result column="worker_id" property="workerId" />
        <result column="wallet_address" property="walletAddress" />
        <result column="watch_url" property="watchUrl" />
        <result column="status" property="status" />
        <result column="start_time" property="startTime" />
        <result column="end_time" property="endTime" />
        <result column="del" property="del" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,user_id, order_id, coin, algorithm, pool, pool_url, pool_user, worker_id, wallet_address, watch_url, status, start_time, end_time, del
    </sql>
    <insert id="insertBatch">

        INSERT INTO lease_order_mining (
            order_id,
            machine_id,
            order_item_id,
            user_id,
            coin,
            algorithm,
            pool,
            pool_url,
            pool_user,
            worker_id,
            wallet_address,
            start_time,
            end_time
        )
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
             #{item.orderId},
             #{item.machineId},
              #{item.orderItemId},
             #{item.userId},
             #{item.coin},
             #{item.algorithm},
             #{item.pool},
             #{item.poolUrl},
             #{item.poolUser},
             #{item.workerId},
             #{item.walletAddress},
            #{item.startTime},
             #{item.endTime})
        </foreach>
    </insert>
    <update id="updateOrderMining">
        UPDATE lease_order_mining
        SET watch_url = #{clientConfigurationMining.watchUrl}
        WHERE user_id = #{userEmail} AND pool = #{clientConfigurationMining.pool}
    </update>

    <select id="getPurchasedItems" resultType="com.m2pool.lease.dto.v2.PurchasedMachineListDto">
        select
            id,
            coin,
            algorithm,
            pool,
            pool_url as poolUrl,
            pool_user as poolUser,
            wallet_address as walletAddress,
            watch_url as watchUrl,
            end_time
        FROM lease_order_mining
        WHERE user_id = #{userEmail}
        GROUP BY wallet_address,pool_user,pool,pool_url,coin,`algorithm`
        ORDER BY `status` DESC,end_time DESC
    </select>
    <select id="getPurchasedInfo" resultType="com.m2pool.lease.dto.v2.MiningConfigInfoDto">
        select
            id ,
            coin,
            algorithm,
            pool,
            pool_url as poolUrl,
            pool_user as poolUser,
            wallet_address as walletAddress,
            watch_url as watchUrl
        FROM lease_order_mining
        WHERE id = #{id}
    </select>
    <select id="getRecentlyHashrate" resultType="com.m2pool.lease.dto.v2.MiningHashrateInfoDto">
        SELECT
        t.datetime AS recordTime,
        t.hashrate AS power,
        t.miner
        FROM (
            SELECT
                datetime,
                hashrate,
                miner,
                ROW_NUMBER() OVER(PARTITION BY miner ORDER BY datetime DESC) as rn
            FROM `${pool}`
                WHERE wallet = #{wallet}
                AND coin = #{coin}
                AND miner IN (
                    <foreach collection="list" item="item" separator=",">
                        #{item.workerId}
                    </foreach>
                )
        ) t
        WHERE t.rn = 1
    </select>
    <select id="getRecently24HourHashrate" resultType="com.m2pool.lease.dto.v2.RealHashrateInfoDto">
        SELECT
            hashrate AS power,
            pool_name AS pool,
            wallet AS walletAddress,
            miner AS miner,
            algorithm,
            coin
        FROM
            `${tableName}`
        WHERE
            datetime >= DATE_SUB(#{now}, INTERVAL 24 HOUR) AND (wallet, coin, miner, algorithm) IN (
        <foreach collection="list" item="item" separator=",">
            (#{item.walletAddress}, #{item.coin}, #{item.workerId}, #{item.algorithm})
        </foreach>
        )
    </select>
    <select id="getMiningInfoByOrderId" resultType="com.m2pool.lease.dto.v2.PurchasedMachineListDto">
        select
            id ,
            order_item_id as orderItemId,
            coin,
            algorithm,
            pool,
            pool_url as poolUrl,
            pool_user as poolUser,
            worker_id as workerId,
            wallet_address as walletAddress,
            watch_url as watchUrl,
            status,
            start_time as startTime,
            end_time as endTime
        FROM lease_order_mining
        WHERE status = 1 and order_item_id in
        <foreach item="item" collection="orderIds" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>
    <select id="checkMiningMinersExist" resultType="com.m2pool.lease.dto.v2.OrderMiningInfoDto">
        select
            worker_id as workerId,
            wallet_address as walletAddress
        FROM lease_order_mining
        WHERE status = 1 AND (
        <foreach collection="list" item="item" separator="OR">
            (`wallet_address` = #{item.walletAddress} AND worker_id = #{item.workerId} )
        </foreach>
            )
    </select>
    <select id="getPurchasedMachineInfo" resultType="com.m2pool.lease.dto.v2.PurchasedMachineDto">
        select
            id ,
            worker_id as workerId,
            status,
            start_time as startTime,
            end_time as endTime
        FROM lease_order_mining
        WHERE
            wallet_address = #{info.walletAddress} AND
            pool_user = #{info.poolUser} AND
            pool = #{info.pool} AND
            pool_url = #{info.poolUrl} AND
            coin = #{info.coin} AND
            `algorithm` = #{info.algorithm}
    </select>


</mapper>
