<?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.bst.model.mapper.ModelMapper">

    <resultMap type="ModelVO" id="ModelResult" autoMapping="true"/>

    <sql id="selectModelVo">
        select
            bm.id,
            bm.user_id,
            bm.name,
            bm.full_voltage,
            bm.low_voltage,
            bm.full_endurance,
            bm.create_time,
            bm.remark,
            bm.deleted,
            bm.low_battery_reminder_switch,
            bm.low_battery_reminder,
            su.nick_name as user_name
        from <include refid="searchTables"/>
    </sql>

    <sql id="searchTables">
        bst_model bm
            left join sys_user su on bm.user_id = su.user_id
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and bm.id = #{query.id}</if>
        <if test="query.userId != null "> and bm.user_id = #{query.userId}</if>
        <if test="query.name != null  and query.name != ''"> and bm.name like concat('%', #{query.name}, '%')</if>
        <if test="query.deleted != null "> and bm.deleted = #{query.deleted}</if>
        <if test="query.deleted == null "> and bm.deleted = false</if>
        <if test="query.lowBatteryReminderSwitch != null "> and bm.low_battery_reminder_switch = #{query.lowBatteryReminderSwitch}</if>
        <if test="query.userName != null and query.userName != ''"> and su.nick_name like concat('%', #{query.userName}, '%')</if>
        <if test="query.ids != null">
            and bm.id in
            <foreach item="item" collection="query.ids" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="query.keyword != null  and query.keyword != ''">
            and (
                bm.name like concat('%', #{query.keyword}, '%')
            )
        </if>
        ${@com.ruoyi.framework.util.DataScopeUtil@dataScope(
            null,
            "bm.user_id",
            null,
            null,
            query.scope
        )}
        ${query.params.dataScope}
    </sql>

    <select id="selectModelList" parameterType="ModelQuery" resultMap="ModelResult">
        <include refid="selectModelVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectModelById" parameterType="Long" resultMap="ModelResult">
        <include refid="selectModelVo"/>
        where bm.id = #{id}
    </select>

    <insert id="insertModel" parameterType="Model" useGeneratedKeys="true" keyProperty="id">
        insert into bst_model
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaId != null">area_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="fullVoltage != null">full_voltage,</if>
            <if test="lowVoltage != null">low_voltage,</if>
            <if test="fullEndurance != null">full_endurance,</if>
            <if test="createTime != null">create_time,</if>
            <if test="remark != null">remark,</if>
            <if test="lowBatteryReminderSwitch != null">low_battery_reminder_switch,</if>
            <if test="lowBatteryReminder != null">low_battery_reminder,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaId != null">#{areaId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="fullVoltage != null">#{fullVoltage},</if>
            <if test="lowVoltage != null">#{lowVoltage},</if>
            <if test="fullEndurance != null">#{fullEndurance},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="lowBatteryReminderSwitch != null">#{lowBatteryReminderSwitch},</if>
            <if test="lowBatteryReminder != null">#{lowBatteryReminder},</if>
         </trim>
    </insert>

    <update id="updateModel" parameterType="Model">
        update bst_model
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.name != null and data.name != ''">name = #{data.name},</if>
        <if test="data.fullVoltage != null">full_voltage = #{data.fullVoltage},</if>
        <if test="data.lowVoltage != null">low_voltage = #{data.lowVoltage},</if>
        <if test="data.fullEndurance != null">full_endurance = #{data.fullEndurance},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.remark != null">remark = #{data.remark},</if>
        <if test="data.lowBatteryReminderSwitch != null">low_battery_reminder_switch = #{data.lowBatteryReminderSwitch},</if>
        <if test="data.lowBatteryReminder != null">low_battery_reminder = #{data.lowBatteryReminder},</if>
    </sql>

    <delete id="deleteModelById" parameterType="Long">
        delete from bst_model where id = #{id}
    </delete>

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

    <!-- logicDel -->

    <update id="logicDel">
        update bst_model
        set deleted = true
        where id in
            <foreach item="id" collection="ids" open="(" separator="," close=")">
                #{id}
            </foreach>
            and deleted = false
    </update>

    <!-- selectIdList -->

    <select id="selectIdList" resultType="Long">
        select bm.id
        from <include refid="searchTables"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <!-- selectCount -->

    <select id="selectCount" resultType="Integer">
        select count(bm.id)
        from <include refid="searchTables"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

</mapper>
