<?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.LeaseUserOwnedProductMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.m2pool.lease.entity.LeaseUserOwnedProduct">
        <id column="id" property="id" />
        <result column="address" property="address" />
        <result column="product_machine_id" property="productMachineId" />
        <result column="order_item_id" property="orderItemId" />
        <result column="start_time" property="startTime" />
        <result column="end_time" property="endTime" />
        <result column="purchased_computing_power" property="purchasedComputingPower" />
        <result column="status" property="status" />
        <result column="type" property="type" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="del" property="del" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, address, product_machine_id, order_item_id, start_time, end_time, purchased_computing_power, status, type, create_time, update_time, del
    </sql>

    <select id="getProductMachineIds" resultType="java.lang.Long">
        select product_machine_id from lease_user_owned_product
                                  where start_time <![CDATA[<=]]> #{beginOfDay} and end_time > #{beginOfDay}
    </select>
    <select id="getHourIncomeList" resultType="com.m2pool.lease.dto.HourIncomeDto">
        select  `user`,miner,income, `usdt_income` from ${coin}_hour_income
                                  where create_time >= #{start} and create_time <![CDATA[<=]]> #{end}
        AND (
        <foreach collection="list" item="item" separator="OR">
            (`user` = #{item.user} and `miner` = #{item.miner})
        </foreach>
        )
    </select>
    <select id="getUserMinerPower" resultType="com.m2pool.lease.dto.UserMinerPowerDto">

<!--        SELECT-->
<!--        sub.`user`,-->
<!--        sub.`miner`,-->
<!--        sub.startTime,-->
<!--        sub.power,-->
<!--        sub.coin-->
<!--        FROM (-->
<!--        SELECT-->
<!--        `user`,-->
<!--        `miner`,-->
<!--        `date` as startTime,-->
<!--        mhs as power,-->
<!--        #{coin} as coin,-->
<!--        ROW_NUMBER() OVER (PARTITION BY `user`, `miner` ORDER BY `date` DESC) as rn-->
<!--        FROM ${coin}_mhs30m-->
<!--        WHERE-->
<!--        <foreach collection="list" item="pair" separator="OR">-->
<!--            (`user` = #{pair.user} AND miner = #{pair.miner})-->
<!--        </foreach>-->
<!--        ) sub-->
<!--        WHERE sub.rn = 1;  TODO 开发环境修改-->


        SELECT
        sub.`user`,
        sub.`miner`,
        sub.startTime,
        sub.power,
        sub.coin
        FROM (
        SELECT
        t.`user`,
        t.`miner`,
        t.`date` as startTime,
        t.mhs as power,
        #{coin} as coin,
        @row_num := IF(@current_user = t.`user` AND @current_miner = t.`miner`, @row_num + 1, 1) as rn,
        @current_user := t.`user`,
        @current_miner := t.`miner`
        FROM (
        SELECT
        `user`,
        `miner`,
        `date`,
        mhs
        FROM ${coin}_mhs30m
        WHERE
        <foreach collection="list" item="pair" separator="OR">
            (`user` = #{pair.user} AND miner = #{pair.miner})
        </foreach>
        ORDER BY `user`, `miner`, `date` DESC
        ) t
        JOIN (SELECT @row_num := 0, @current_user := NULL, @current_miner := NULL) vars
        ) sub
        WHERE sub.rn = 1;
    </select>
    <update id="updateBatchIncome">
        INSERT INTO lease_user_owned_product (id, settle_income, settle_usdt_income,start_time, end_time)
        VALUES
        <foreach collection="list" item="item" separator="," open="(" close=")">
            #{item.id}, #{item.settleIncome}, #{item.settleUsdtIncome}, #{item.startTime}, #{item.endTime}
        </foreach>
        ON DUPLICATE KEY UPDATE
        settle_income = VALUES(settle_income),
        settle_usdt_income = VALUES(settle_usdt_income),
        start_time = VALUES(start_time),
        end_time = VALUES(end_time)
    </update>
</mapper>
