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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.m2pool.lease.entity.LeaseProduct">
        <id column="id" property="id" />
        <result column="name" property="name" />
        <result column="image" property="image" />
        <result column="type" property="type" />
        <result column="state" property="state" />
        <result column="description" property="description" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="del" property="del" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, `name`, image,`state`,unit, `type`, description, create_time, update_time, del
    </sql>
    <select id="getCoinNetPower" resultType="java.math.BigDecimal">
        select
            AVG(`value`)
        from
            ${coin}_net_power
        where
            `date` >= DATE_SUB(NOW(), INTERVAL 3 DAY)
    </select>
    <select id="getNetBlocks" resultType="java.math.BigDecimal">
        SELECT MAX(height) - MIN(height) FROM ${coin}_net_block WHERE `date` >= DATE_SUB(now(),INTERVAL 3 DAY)
    </select>
    <select id="getProductList" resultType="com.m2pool.lease.dto.ProductDto">
        SELECT
        id,
        name,
        image,
        type,
        state,
        CASE
        WHEN max_price = min_price THEN CAST(max_price AS CHAR)
        ELSE CONCAT(CAST(min_price AS CHAR), '-', CAST(max_price AS CHAR))
        END AS priceRange ,
        description,
        algorithm,
        shop_id AS shopId,
        coin_full_name AS coinFullName,
        coin
        FROM
        lease_product
        <where>
            del = false
            <choose>
                <when test="coin != null and coin != ''">
                    <trim prefix="AND (" suffix=")" >
                        <trim prefix="(" suffix=")" prefixOverrides="OR">
                            <if test="coin != null and coin != ''">
                                coin LIKE CONCAT('%', #{coin}, '%')
                            </if>
                            <if test="coin != null and coin != ''">
                                OR coin_full_name LIKE CONCAT('%', #{coin}, '%')
                            </if>
                        </trim>
                        <if test="algorithm != null and algorithm != ''">
                            AND algorithm LIKE CONCAT('%', #{algorithm}, '%')
                        </if>
                    </trim>
                </when>
                <otherwise>
                    <if test="algorithm != null and algorithm != ''">
                        AND algorithm LIKE CONCAT('%', #{algorithm}, '%')
                    </if>
                </otherwise>
            </choose>
        </where>
    </select>
    <select id="getProductListForShopAndUserCenter" resultType="com.m2pool.lease.dto.ProductDto">
        SELECT
            lp.id,
            lp.shop_id AS shopId,
            lp.name,
            lp.image,
            lp.type,
            lp.state,
            lp.description,
            lp.algorithm,
            lp.shop_id AS shopId,
            lp.coin_full_name AS coinFullName,
            lp.coin,
            lp.sale_number AS saleNumber,
            lp.total_machine_number AS totalMachineNumber,
            ls.name AS shopName
        FROM lease_product lp RIGHT JOIN lease_shop ls ON lp.shop_id = ls.id and ls.state = 1
        WHERE lp.del = false
        <choose>
            <when test="shopId != 0">
                AND lp.shop_id = #{shopId}
            </when>
            <otherwise>
                AND lp.state = 0 AND lp.total_machine_number != 0
            </otherwise>
        </choose>
         <choose>
             <when test="coin != null and coin != ''">
                 AND (
                 (
                 lp.coin LIKE CONCAT('%', #{coin}, '%')
                 OR lp.coin_full_name LIKE CONCAT('%', #{coin}, '%')
                 )
                 <if test="algorithm != null and algorithm != ''">
                     AND lp.algorithm LIKE CONCAT('%', #{algorithm}, '%')
                 </if>
                 )
             </when>
              <otherwise>
                  <if test="algorithm != null and algorithm != ''">
                      AND lp.algorithm LIKE CONCAT('%', #{algorithm}, '%')
                  </if>
              </otherwise>
         </choose>
        order by lp.sale_number desc,lp.create_time desc
    </select>
    <select id="getSupportPayType" resultType="com.m2pool.lease.dto.PayTypeDto">
        select pay_coin as coin,chain from lease_shop_config where shop_id = #{shopId} and del = 0;
    </select>
    <select id="getProductListForShopWalletConfig" resultType="com.m2pool.lease.dto.ProductForWalletConfigDto">
        SELECT
            id as productId,
            name,
            image,
            state,
            description,
            algorithm,
            coin,
            total_machine_number AS totalMachineNumber
        FROM
        lease_product
        WHERE del = false and shop_id = #{shopId}
    </select>

</mapper>
