<?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.huashe.park.core.mapper.PileHoleIndexRealtimeMapper">

    <resultMap type="PileHoleIndexRealtime" id="PileHoleIndexRealtimeResult">
        <result property="id" column="id"/>
        <result property="holeId" column="hole_id"/>
        <result property="holeByteKey" column="hole_byte_key"/>
        <result property="machineId" column="machine_id"/>
        <result property="machineByteKey" column="machine_byte_key"/>
        <result property="depth" column="depth"/>
        <result property="current" column="current"/>
        <result property="verticalDeviation" column="vertical_deviation"/>
        <result property="sprayVolume" column="spray_volume"/>
        <result property="sendSprayVolume" column="send_spray_volume"/>
        <result property="sprayPressure" column="spray_pressure"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="status" column="status"/>
        <result property="tiltAngle" column="tilt_angle"/>
        <result property="forwardTiltAngle" column="forward_tilt_angle"/>
        <result property="realX" column="real_x"/>
        <result property="realY" column="real_y"/>
        <result property="realLat" column="real_lat"/>
        <result property="realLng" column="real_lng"/>
        <result property="consUnitId" column="cons_unit_id"/>
        <result property="holeNum" column="hole_num"/>
        <result property="updateTime" column="update_time"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateBy" column="update_by"/>
        <result property="tenantId" column="tenant_id"/>
    </resultMap>

    <sql id="selectPileHoleIndexRealtimeVo">
        select id,
               hole_id,
               hole_num,
               hole_byte_key,
               machine_id,
               machine_byte_key,
               depth,
               `current`,
               vertical_deviation,
               spray_volume,
               send_spray_volume,
               spray_pressure,
               start_time,
               end_time,
               status,
               tilt_angle,
               forward_tilt_angle,
               real_x,
               real_y,
               real_lat,
               real_lng,
               end_press,
               depth_press,
               update_time,
               create_time,
               create_by,
               update_by,
               cons_unit_id,
               tenant_id
        from cons_pile_hole_index_realtime
    </sql>

    <select id="selectPileHoleIndexRealtimeList" parameterType="PileHoleIndexRealtime"
            resultMap="PileHoleIndexRealtimeResult">
        <include refid="selectPileHoleIndexRealtimeVo"/>
        <where>
            <if test="holeId != null ">and hole_id = #{holeId}</if>
            <if test="holeByteKey != null  and holeByteKey != ''">and hole_byte_key = #{holeByteKey}</if>
            <if test="machineId != null ">and machine_id = #{machineId}</if>
            <if test="machineByteKey != null  and machineByteKey != ''">and machine_byte_key = #{machineByteKey}</if>
            <if test="depth != null ">and depth = #{depth}</if>
            <if test="current != null ">and current = #{current}</if>
            <if test="verticalDeviation != null ">and vertical_deviation = #{verticalDeviation}</if>
            <if test="sprayVolume != null ">and spray_volume = #{sprayVolume}</if>
            <if test="sprayPressure != null ">and spray_pressure = #{sprayPressure}</if>
            <if test="startTime != null ">and start_time = #{startTime}</if>
            <if test="endTime != null ">and end_time = #{endTime}</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
            <if test="tiltAngle != null ">and tilt_angle = #{tiltAngle}</if>
            <if test="forwardTiltAngle != null ">and forward_tilt_angle = #{forwardTiltAngle}</if>
        </where>
    </select>

    <select id="selectPileHoleIndexRealtimeById" parameterType="Long" resultType="PileHoleIndexRealtime">
        <include refid="selectPileHoleIndexRealtimeVo"/>
        where id = #{id}
    </select>

    <insert id="insertPileHoleIndexRealtime" parameterType="PileHoleIndexRealtime" useGeneratedKeys="true"
            keyProperty="id">
        insert into cons_pile_hole_index_realtime
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="holeId != null">hole_id,</if>
            <if test="holeByteKey != null and holeByteKey != ''">hole_byte_key,</if>
            <if test="machineId != null">machine_id,</if>
            <if test="machineByteKey != null and machineByteKey != ''">machine_byte_key,</if>
            <if test="depth != null">depth,</if>
            <if test="current != null">current,</if>
            <if test="verticalDeviation != null">vertical_deviation,</if>
            <if test="sprayVolume != null">spray_volume,</if>
            <if test="sendSprayVolume != null">send_spray_volume,</if>
            <if test="sprayPressure != null">spray_pressure,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="status != null">status,</if>
            <if test="tiltAngle != null ">tilt_angle,</if>
            <if test="forwardTiltAngle != null ">forward_tilt_angle,</if>
            <if test="realX != null">real_x,</if>
            <if test="realY != null">real_y,</if>
            <if test="realLat != null">real_lat,</if>
            <if test="realLng != null">real_lng,</if>
            <if test="consUnitId != null">cons_unit_id,</if>
            <if test="holeNum != null">hole_num,</if>
            <if test="endPress != null">end_press,</if>
            <if test="depthPress != null">depth_press,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="tenantId != null">tenant_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="holeId != null">#{holeId},</if>
            <if test="holeByteKey != null and holeByteKey != ''">#{holeByteKey},</if>
            <if test="machineId != null">#{machineId},</if>
            <if test="machineByteKey != null and machineByteKey != ''">#{machineByteKey},</if>
            <if test="depth != null">#{depth},</if>
            <if test="current != null">#{current},</if>
            <if test="verticalDeviation != null">#{verticalDeviation},</if>
            <if test="sprayVolume != null">#{sprayVolume},</if>
            <if test="sendSprayVolume != null">#{sendSprayVolume},</if>
            <if test="sprayPressure != null">#{sprayPressure},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="status != null">#{status},</if>
            <if test="tiltAngle != null ">#{tiltAngle},</if>
            <if test="forwardTiltAngle != null ">#{forwardTiltAngle},</if>
            <if test="realX != null">#{realX},</if>
            <if test="realY != null">#{realY},</if>
            <if test="realLat != null">#{realLat},</if>
            <if test="realLng != null">#{realLng},</if>
            <if test="consUnitId != null">#{consUnitId},</if>
            <if test="holeNum != null">#{holeNum},</if>
            <if test="endPress != null">#{endPress},</if>
            <if test="depthPress != null">#{depthPress},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="tenantId != null">#{tenantId},</if>
        </trim>
    </insert>

    <update id="updatePileHoleIndexRealtime" parameterType="PileHoleIndexRealtime">
        update cons_pile_hole_index_realtime
        <trim prefix="SET" suffixOverrides=",">
            <if test="holeId != null">hole_id = #{holeId},</if>
            <if test="holeByteKey != null and holeByteKey != ''">hole_byte_key = #{holeByteKey},</if>
            <if test="machineId != null">machine_id = #{machineId},</if>
            <if test="machineByteKey != null and machineByteKey != ''">machine_byte_key = #{machineByteKey},</if>
            <if test="depth != null">depth = #{depth},</if>
            <if test="current != null">current = #{current},</if>
            <if test="verticalDeviation != null">vertical_deviation = #{verticalDeviation},</if>
            <if test="sprayVolume != null">spray_volume = #{sprayVolume},</if>
            <if test="sendSprayVolume != null">send_spray_volume = #{sendSprayVolume},</if>
            <if test="sprayPressure != null">spray_pressure = #{sprayPressure},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="tiltAngle != null ">tilt_angle = #{tiltAngle},</if>
            <if test="forwardTiltAngle != null ">forward_tilt_angle = #{forwardTiltAngle},</if>
            <if test="realX != null">real_x = #{realX},</if>
            <if test="realY != null">real_y = #{realY},</if>
            <if test="realLat != null">real_lat = #{realLat},</if>
            <if test="realLng != null">real_lng = #{realLng},</if>
            <if test="consUnitId != null">cons_unit_id = #{consUnitId},</if>
            <if test="holeNum != null">hole_num = #{holeNum},</if>
            <if test="endPress != null">end_press = #{endPress},</if>
            <if test="depthPress != null">depth_press = #{depthPress},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="tenantId != null">tenant_id = #{tenantId},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updatePileHoleIndexRealtimeByHoleIdAndMachineId" parameterType="PileHoleIndexRealtime">
        update cons_pile_hole_index_realtime
        <trim prefix="SET" suffixOverrides=",">
            <if test="depth != null">depth = #{depth},</if>
            <if test="current != null">current = #{current},</if>
            <if test="verticalDeviation != null">vertical_deviation = #{verticalDeviation},</if>
            <if test="sprayVolume != null">spray_volume = #{sprayVolume},</if>
            <if test="sendSprayVolume != null">send_spray_volume = #{sendSprayVolume},</if>
            <if test="sprayPressure != null">spray_pressure = #{sprayPressure},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="tiltAngle != null ">tilt_angle = #{tiltAngle},</if>
            <if test="forwardTiltAngle != null ">forward_tilt_angle = #{forwardTiltAngle},</if>
            <if test="realX != null">real_x = #{realX},</if>
            <if test="realY != null">real_y = #{realY},</if>
            <if test="realLat != null">real_lat = #{realLat},</if>
            <if test="realLng != null">real_lng = #{realLng},</if>
            <if test="consUnitId != null">cons_unit_id = #{consUnitId},</if>
            <if test="holeNum != null">hole_num = #{holeNum},</if>
            <if test="endPress != null">end_press = #{endPress},</if>
            <if test="depthPress != null">depth_press = #{depthPress},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="tenantId != null">tenant_id = #{tenantId},</if>
        </trim>
        where hole_id = #{holeId} and machine_id = #{machineId}
    </update>

    <delete id="deletePileHoleIndexRealtimeById" parameterType="Long">
        delete
        from cons_pile_hole_index_realtime
        where id = #{id}
    </delete>

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


    <select id="selectMachineConstingPileHole" resultType="Integer">
        select count(1)
        from cons_pile_hole_index_realtime
        where machine_id = #{machineId}
          and update_time > #{startTime}
    </select>

    <select id="selectMachineLatestIndex" resultMap="PileHoleIndexRealtimeResult">
        SELECT *
        FROM cons_pile_hole_index_realtime
        WHERE id = (SELECT MAX(id) FROM cons_pile_hole_index_realtime where machine_id = #{machineId}) limit 1
    </select>

</mapper>