<?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.ruoyi.frequency.rechargeorder.mapper.RechargeOrderMapper">

    <resultMap type="com.ruoyi.frequency.rechargeorder.entity.RechargeOrder" id="RechargeOrderResult">
        <result property="id"    column="id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="customerId"    column="customer_id"    />
        <result property="price"    column="price"    />
        <result property="orderNo"    column="order_no"    />
        <result property="payType"    column="pay_type"    />
        <result property="payStatus"    column="pay_status"    />
        <result property="aliPayOrder"    column="ali_pay_order"    />
        <result property="payPrice"    column="pay_price"    />
    </resultMap>

    <sql id="selectRechargeOrderVo">
        SELECT
            t.id,
            t.create_by,
            t.create_time,
            t.update_by,
            t.update_time,
            t.del_flag,
            t.customer_id,
            t.price,
            t.order_no,
            t.pay_type,
            t.pay_status,
            t.ali_pay_order,
            t.pay_price
        FROM
            t_recharge_order AS t
                LEFT JOIN t_customer AS c ON t.customer_id = c.id
    </sql>

    <select id="selectRechargeOrderList" parameterType="com.ruoyi.frequency.rechargeorder.entity.RechargeOrder" resultMap="RechargeOrderResult">
        <include refid="selectRechargeOrderVo"/>
        <where>  and t.del_flag = 1 and t.pay_status = 2
            <if test="customerId != null "> and t.customer_id = #{customerId}</if>
            <if test="price != null "> and t.price = #{price}</if>
            <if test="orderNo != null  and orderNo != ''"> and t.order_no like concat('%', #{orderNo}, '%')</if>
            <if test="payType != null "> and t.pay_type = #{payType}</if>
            <if test="payStatus != null "> and t.pay_status = #{payStatus}</if>
            <if test="aliPayOrder != null  and aliPayOrder != ''"> and t.ali_pay_order = #{aliPayOrder}</if>
            <if test="payPrice != null "> and t.pay_price = #{payPrice}</if>
            <if test="userInfo != null and userInfo.nickname != null  and userInfo.nickname != '' ">
                and c.nickname like concat('%', #{userInfo.nickname}, '%')
            </if>
            <if test="userInfo != null and  userInfo.mobile != null and  userInfo.mobile != '' ">
                and c.mobile like concat('%', #{userInfo.mobile}, '%')
            </if>
            <if test="beginTime != null ">
                and  t.create_time >= #{beginTime}

            </if>
            <if test="endTime != null ">
                and  t.create_time &lt; #{endTime}
            </if>
        </where>
        order by t.create_time desc
    </select>

    <select id="statistics" parameterType="com.ruoyi.frequency.rechargeorder.entity.RechargeOrder" resultType="com.ruoyi.frequency.rechargeorder.model.result.RechargeOrderCount">
        SELECT
        count(distinct t.customer_id) number,
        ifnull(sum(t.pay_price),0) pay_price,
        ifnull(sum(t.price),0) price
        FROM
        t_recharge_order AS t
        where
        t.id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectRechargeOrderById" parameterType="Long" resultMap="RechargeOrderResult">
        <include refid="selectRechargeOrderVo"/>
        where t.id = #{id}
    </select>

    <insert id="insertRechargeOrder" parameterType="com.ruoyi.frequency.rechargeorder.entity.RechargeOrder">
        insert into t_recharge_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="customerId != null">customer_id,</if>
            <if test="price != null">price,</if>
            <if test="orderNo != null">order_no,</if>
            <if test="payType != null">pay_type,</if>
            <if test="payStatus != null">pay_status,</if>
            <if test="aliPayOrder != null">ali_pay_order,</if>
            <if test="payPrice != null">pay_price,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="customerId != null">#{customerId},</if>
            <if test="price != null">#{price},</if>
            <if test="orderNo != null">#{orderNo},</if>
            <if test="payType != null">#{payType},</if>
            <if test="payStatus != null">#{payStatus},</if>
            <if test="aliPayOrder != null">#{aliPayOrder},</if>
            <if test="payPrice != null">#{payPrice},</if>
        </trim>
    </insert>

    <update id="updateRechargeOrder" parameterType="com.ruoyi.frequency.rechargeorder.entity.RechargeOrder">
        update t_recharge_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="customerId != null">customer_id = #{customerId},</if>
            <if test="price != null">price = #{price},</if>
            <if test="orderNo != null">order_no = #{orderNo},</if>
            <if test="payType != null">pay_type = #{payType},</if>
            <if test="payStatus != null">pay_status = #{payStatus},</if>
            <if test="aliPayOrder != null">ali_pay_order = #{aliPayOrder},</if>
            <if test="payPrice != null">pay_price = #{payPrice},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteRechargeOrderById" parameterType="Long">
        delete from t_recharge_order where id = #{id}
    </delete>

    <delete id="deleteRechargeOrderByIds" parameterType="String">
        delete from t_recharge_order where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
