<?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.ss.businessRecord.mapper.SmBusinessRecordMapper">

    <resultMap type="SmBusinessRecordVo" id="SmBusinessRecordResult" autoMapping="true">
        <result property="totalDevice" column="total_device" typeHandler="com.ruoyi.system.mapper.typehandler.NonNullIntegerTyperHandler"/>
        <result property="totalModel" column="total_model" typeHandler="com.ruoyi.system.mapper.typehandler.NonNullIntegerTyperHandler"/>
        <result property="totalOnline" column="total_online" typeHandler="com.ruoyi.system.mapper.typehandler.NonNullIntegerTyperHandler"/>
        <result property="totalUser" column="total_user" typeHandler="com.ruoyi.system.mapper.typehandler.NonNullIntegerTyperHandler"/>
        <result property="totalRecharge" column="total_recharge" typeHandler="com.ruoyi.system.mapper.typehandler.NonNullDecimalTypeHandler"/>
        <result property="rechargeCount" column="recharge_count" typeHandler="com.ruoyi.system.mapper.typehandler.NonNullIntegerTyperHandler"/>
        <result property="totalMonth" column="total_month" typeHandler="com.ruoyi.system.mapper.typehandler.NonNullDecimalTypeHandler"/>
        <result property="userBalance" column="user_balance" typeHandler="com.ruoyi.system.mapper.typehandler.NonNullDecimalTypeHandler"/>
        <result property="todayRechargeCount"    column="today_recharge_count"  typeHandler="com.ruoyi.system.mapper.typehandler.NonNullIntegerTyperHandler"  />
        <result property="todayRechargeAmount"    column="today_recharge_amount"   typeHandler="com.ruoyi.system.mapper.typehandler.NonNullDecimalTypeHandler" />
        <result property="deviceCount"    column="device_count"    typeHandler="com.ruoyi.system.mapper.typehandler.NonNullIntegerTyperHandler" />
    </resultMap>

    <sql id="selectSmBusinessRecordVo">
        select
            sbr.id,
            sbr.total_device,
            sbr.total_model,
            sbr.total_online,
            sbr.total_user,
            sbr.create_time,
            sbr.total_recharge,
            sbr.recharge_count,
            sbr.total_month,
            sbr.user_balance,
            sbr.today_recharge_count,
            sbr.today_recharge_amount,
            device_count
        from sm_business_record sbr
    </sql>

    <sql id="searchCondition">
        <if test="totalDevice != null "> and sbr.total_device = #{totalDevice}</if>
        <if test="totalModel != null "> and sbr.total_model = #{totalModel}</if>
        <if test="totalOnline != null "> and sbr.total_online = #{totalOnline}</if>
        <if test="totalUser != null "> and sbr.total_user = #{totalUser}</if>
        <if test="createDate != null "> and sbr.date(create_time) = date(#{createDate})</if>
    </sql>

    <select id="selectSmBusinessRecordList" parameterType="SmBusinessRecord" resultMap="SmBusinessRecordResult">
        <include refid="selectSmBusinessRecordVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectSmBusinessRecordById" parameterType="Long" resultMap="SmBusinessRecordResult">
        <include refid="selectSmBusinessRecordVo"/>
        where sbr.id = #{id}
    </select>

    <select id="selectLatest" resultMap="SmBusinessRecordResult">
        select sbr.*
        from sm_business_record sbr
        <where>
            <include refid="searchCondition"/>
        </where>
        order by create_time desc
        limit 1
    </select>

    <select id="selectDailyLatest" resultMap="SmBusinessRecordResult">
        SELECT
            date( create_time ) create_date,
            sbr.*
        FROM
            sm_business_record sbr
        WHERE
            sbr.id IN (
                SELECT max( id )
                FROM sm_business_record
                <where>
                    <if test="startDate != null">
                        and date(create_time) >= date(#{startDate})
                    </if>
                    <if test="endDate != null">
                        and date(#{endDate}) >= date(create_time)
                    </if>
                </where>
                GROUP BY date( create_time )
            )
            <include refid="searchCondition"/>
        GROUP BY create_date
        ORDER BY create_date ASC
    </select>

    <insert id="insertSmBusinessRecord" parameterType="SmBusinessRecord" useGeneratedKeys="true" keyProperty="id">
        insert into sm_business_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="totalDevice != null">total_device,</if>
            <if test="totalModel != null">total_model,</if>
            <if test="totalOnline != null">total_online,</if>
            <if test="totalUser != null">total_user,</if>
            <if test="createTime != null">create_time,</if>
            <if test="totalRecharge != null">total_recharge,</if>
            <if test="rechargeCount != null">recharge_count,</if>
            <if test="totalMonth != null">total_month,</if>
            <if test="userBalance != null">user_balance,</if>
            <if test="todayRechargeCount != null">today_recharge_count,</if>
            <if test="todayRechargeAmount != null">today_recharge_amount,</if>
            <if test="deviceCount != null">device_count,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="totalDevice != null">#{totalDevice},</if>
            <if test="totalModel != null">#{totalModel},</if>
            <if test="totalOnline != null">#{totalOnline},</if>
            <if test="totalUser != null">#{totalUser},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="totalRecharge != null">#{totalRecharge},</if>
            <if test="rechargeCount != null">#{rechargeCount},</if>
            <if test="totalMonth != null">#{totalMonth},</if>
            <if test="userBalance != null">#{userBalance},</if>
            <if test="todayRechargeCount != null">#{todayRechargeCount},</if>
            <if test="todayRechargeAmount != null">#{todayRechargeAmount},</if>
            <if test="deviceCount != null">#{deviceCount},</if>
         </trim>
    </insert>

    <update id="updateSmBusinessRecord" parameterType="SmBusinessRecord">
        update sm_business_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="totalDevice != null">total_device = #{totalDevice},</if>
            <if test="totalModel != null">total_model = #{totalModel},</if>
            <if test="totalOnline != null">total_online = #{totalOnline},</if>
            <if test="totalUser != null">total_user = #{totalUser},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="totalRecharge != null">total_recharge = #{totalRecharge},</if>
            <if test="rechargeCount != null">recharge_count = #{rechargeCount},</if>
            <if test="totalMonth != null">total_month = #{totalMonth},</if>
            <if test="userBalance != null">user_balance = #{userBalance},</if>
            <if test="todayRechargeCount != null">today_recharge_count = #{todayRechargeCount},</if>
            <if test="todayRechargeAmount != null">today_recharge_amount = #{todayRechargeAmount},</if>
            <if test="deviceCount != null">device_count = #{deviceCount},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSmBusinessRecordById" parameterType="Long">
        delete from sm_business_record where id = #{id}
    </delete>

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