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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.m2pool.lease.entity.LeaseOrderItem">
        <id column="id" property="id" />
        <result column="order_id" property="orderId" />
        <result column="product_id" property="productId" />
        <result column="product_machine_id" property="productMachineId" />
        <result column="name" property="name" />
        <result column="unit" property="unit" />
        <result column="image" property="image" />
        <result column="quantity" property="quantity" />
        <result column="price" property="price" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="del" property="del" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, order_id, product_id,product_machine_id,`name`, unit, image, quantity, price, create_time, update_time, del
    </sql>
    <select id="getMachineByOrderStatusIsPayInData" resultType="java.lang.Long">
        select product_machine_id AS productMachineId from lease_order_item
        where
        <foreach collection="list" item="item" separator="OR">
            (`order_id` = #{item.orderId} and `shop_id` = #{item.shopId})
        </foreach>
    </select>
    <select id="getOrderIds" resultType="java.lang.Long">
        select DISTINCT order_id  FROM lease_order_item
        WHERE
        id IN
        <foreach item="id" collection="orderItemIds" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
    <select id="getOrderInfoStatus" resultType="com.m2pool.lease.dto.OrderStatusDto">
        SELECT
        order_id as orderId,
        CASE
        WHEN SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) > 0 THEN 7
        ELSE 8
        END as orderInfoStatus
        FROM
        `lease_order_item`
        WHERE
        order_id IN
        <foreach item="id" collection="orderIds" open="(" separator="," close=")">
            #{id}
        </foreach>
        GROUP BY
        order_id;
    </select>
    <select id="selectOrderInfoIds" resultType="java.lang.Long">
        SELECT
        DISTINCT order_id
        FROM
        `lease_order_item`
        WHERE
        id IN
        <foreach item="id" collection="itemIds" open="(" separator="," close=")">
            #{id}
        </foreach>
        GROUP BY
        order_id;
    </select>
    <select id="getNeedUpdateOrderItem" resultType="com.m2pool.lease.entity.LeaseOrderItem">
        SELECT
        id,
        already_pay_real_amount AS alreadyPayRealAmount,
        settle_pay_real_amount AS settlePayRealAmount,
        del
        FROM
        `lease_order_item`
        WHERE
        order_id = #{orderId}
    </select>
    <select id="getOrderItemByOrderIds" resultType="com.m2pool.lease.entity.LeaseOrderItem">
        SELECT
            oi.user,
            oi.miner,
            oi.coin,
            oi.from_address AS fromAddress,
            oi.from_chain AS fromChain,
            oi.from_symbol AS fromSymbol,
            oi.address AS address,
            oi.chain AS chain,
            oi.symbol AS symbol,
            COALESCE(prm.status,0)  AS status
        FROM
        `lease_order_item` oi
        LEFT JOIN lease_pay_record_message prm ON
            oi.from_address = prm.from_address AND oi.from_chain = prm.from_chain AND oi.from_symbol = prm.from_symbol
            AND oi.address = prm.to_address AND oi.chain = prm.to_chain AND oi.symbol = prm.to_symbol AND DATE(prm.create_time) = CURRENT_DATE
        WHERE
        oi.order_id IN
        <foreach item="id" collection="orderIds" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
    <select id="getOneDayApartOrder" resultType="com.m2pool.lease.dto.v2.OrderTimeInfoDto">
        SELECT
            id,
            machine_id AS machineId,
            order_id AS orderId,
            order_item_id as orderItemId,
            lease_time AS leaseTime,
            create_time AS startTime,
            price,
            numbers
        FROM
            lease_order_item
        WHERE
            STATUS = 1
    </select>


</mapper>
