<?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.device.mapper.RecordUseMapper">
    
    <resultMap type="RecordUse" id="RecordUseResult">
        <result property="objId"    column="obj_id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="deviceCode"    column="device_code"    />
        <result property="deviceName"    column="device_name"    />
        <result property="deviceType"    column="device_type"    />
        <result property="openTime"    column="open_time"    />
        <result property="closeTime"    column="close_time"    />
        <result property="useTime"    column="use_time"    />
        <result property="defaultTime"    column="default_time"    />
        <result property="chargePrice"    column="charge_price"    />
        <result property="overtimeState"    column="overtime_state"    />
        <result property="useState"    column="use_state"    />
        <result property="useUser"    column="use_user"    />
        <result property="useUnit"    column="use_unit"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="queryYear"    column="query_year"    />
        <result property="queryMonth"    column="query_month"    />
        <result property="deptId"    column="dept_id"    />
        <result property="userId"    column="user_id"    />
    </resultMap>

    <sql id="selectRecordUseVo">
        select obj_id, device_id, device_code, device_name, device_type, open_time, close_time, use_time, default_time, charge_price, overtime_state, use_state, use_user, use_unit, create_by, create_time, query_year, query_month, dept_id, user_id from device_record_use
    </sql>

    <select id="selectRecordUseList" parameterType="RecordUse" resultMap="RecordUseResult">
        <include refid="selectRecordUseVo"/>
        <where>  
            <if test="deviceName != null  and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</if>
            <if test="deviceType != null  and deviceType != ''"> and device_type = #{deviceType}</if>
            <if test="params.beginOpenTime != null and params.beginOpenTime != '' and params.endOpenTime != null and params.endOpenTime != ''"> and open_time between #{params.beginOpenTime} and #{params.endOpenTime}</if>
            <if test="overtimeState != null  and overtimeState != ''"> and overtime_state = #{overtimeState}</if>
            <if test="useState != null  and useState != ''"> and use_state = #{useState}</if>
            <if test="useUnit != null  and useUnit != ''"> and use_unit = #{useUnit}</if>
            <if test="queryYear != null  and queryYear != ''"> and query_year = #{queryYear}</if>
            <if test="queryMonth != null  and queryMonth != ''"> and query_month = #{queryMonth}</if>
        </where>
    </select>
    
    <select id="selectRecordUseByObjId" parameterType="Long" resultMap="RecordUseResult">
        <include refid="selectRecordUseVo"/>
        where obj_id = #{objId}
    </select>

    <insert id="insertRecordUse" parameterType="RecordUse" useGeneratedKeys="true" keyProperty="objId">
        insert into device_record_use
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">device_id,</if>
            <if test="deviceCode != null">device_code,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="deviceType != null">device_type,</if>
            <if test="openTime != null">open_time,</if>
            <if test="closeTime != null">close_time,</if>
            <if test="useTime != null">use_time,</if>
            <if test="defaultTime != null">default_time,</if>
            <if test="chargePrice != null">charge_price,</if>
            <if test="overtimeState != null">overtime_state,</if>
            <if test="useState != null">use_state,</if>
            <if test="useUser != null">use_user,</if>
            <if test="useUnit != null">use_unit,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="queryYear != null">query_year,</if>
            <if test="queryMonth != null">query_month,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="userId != null">user_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">#{deviceId},</if>
            <if test="deviceCode != null">#{deviceCode},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="deviceType != null">#{deviceType},</if>
            <if test="openTime != null">#{openTime},</if>
            <if test="closeTime != null">#{closeTime},</if>
            <if test="useTime != null">#{useTime},</if>
            <if test="defaultTime != null">#{defaultTime},</if>
            <if test="chargePrice != null">#{chargePrice},</if>
            <if test="overtimeState != null">#{overtimeState},</if>
            <if test="useState != null">#{useState},</if>
            <if test="useUser != null">#{useUser},</if>
            <if test="useUnit != null">#{useUnit},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="queryYear != null">#{queryYear},</if>
            <if test="queryMonth != null">#{queryMonth},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="userId != null">#{userId},</if>
         </trim>
    </insert>

    <update id="updateRecordUse" parameterType="RecordUse">
        update device_record_use
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="deviceCode != null">device_code = #{deviceCode},</if>
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="deviceType != null">device_type = #{deviceType},</if>
            <if test="openTime != null">open_time = #{openTime},</if>
            <if test="closeTime != null">close_time = #{closeTime},</if>
            <if test="useTime != null">use_time = #{useTime},</if>
            <if test="defaultTime != null">default_time = #{defaultTime},</if>
            <if test="chargePrice != null">charge_price = #{chargePrice},</if>
            <if test="overtimeState != null">overtime_state = #{overtimeState},</if>
            <if test="useState != null">use_state = #{useState},</if>
            <if test="useUser != null">use_user = #{useUser},</if>
            <if test="useUnit != null">use_unit = #{useUnit},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="queryYear != null">query_year = #{queryYear},</if>
            <if test="queryMonth != null">query_month = #{queryMonth},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="userId != null">user_id = #{userId},</if>
        </trim>
        where obj_id = #{objId}
    </update>

    <delete id="deleteRecordUseByObjId" parameterType="Long">
        delete from device_record_use where obj_id = #{objId}
    </delete>

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

</mapper>