<?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.ConsPileHoleInfoMapper">

    <resultMap type="ConsPileHoleInfo" id="ConsPileHoleInfoResult">
        <result property="id" column="id"/>
        <result property="byteId" column="byte_id"/>
        <result property="holeKey" column="hole_key"/>
        <result property="holeNum" column="hole_num"/>
        <result property="deltaX" column="delta_x"/>
        <result property="deltaY" column="delta_y"/>
        <result property="lng" column="lng"/>
        <result property="lat" column="lat"/>
        <result property="consStatus" column="cons_status"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="desDept" column="des_dept"/>
        <result property="diameter" column="diameter"/>
        <result property="consUnitId" column="cons_unit_id"/>
        <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="selectConsPileHoleInfoVo">
        select id,
               byte_id,
               hole_key,
               hole_num,
               delta_x,
               delta_y,
               lng,
               lat,
               cons_status,
               start_time,
               end_time,
               des_dept,
               diameter,
               cons_unit_id,
               update_time,
               create_time,
               create_by,
               update_by,
               tenant_id
        from cons_pile_hole_info
    </sql>

    <select id="selectConsPileHoleInfoList" parameterType="ConsPileHoleInfo" resultMap="ConsPileHoleInfoResult">
        <include refid="selectConsPileHoleInfoVo"/>
        <where>
            <if test="holeNum != null  and holeNum != ''">and hole_num = #{holeNum}</if>
            <if test="consStatus != null  and consStatus != ''">and cons_status = #{consStatus}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectConsPileHoleRealtimeIndex" parameterType="ConsPileHoleInfo"
            resultType="com.huashe.park.domain.dto.cons.PileRealtimeIndex">
        select cphi.*,
        cphir.depth,
        cphir.current,
        cphir.vertical_deviation,
        cphir.spray_volume,
        cphir.send_spray_volume,
        cphir.spray_pressure,
        cphir.start_time index_start_time,
        cphir.end_time index_end_time,
        cphir.status,
        cphir.tilt_angle,
        cphir.forward_tilt_angle,
        cphir.machine_id,
        cphir.real_x,
        cphir.real_y,
        cphir.real_lat,
        cphir.real_lng
        from cons_pile_hole_info cphi
        left join cons_pile_hole_index_realtime cphir
        on
        cphi.id = cphir.hole_id
        <where>
            <if test="holeNum != null  and holeNum != ''">and cphi.hole_num = #{holeNum}</if>
            <if test="holeKey != null  and holeKey != ''">and cphi.hole_key = #{holeKey}</if>
            <if test="consUnitId != null  and consUnitId != ''">and cons_unit_id = #{consUnitId}</if>
            <if test="consStatus != null  and consStatus != ''">and cons_status != #{consStatus}</if>
            <if test="id !=null">and cphi.id = #{id}</if>
        </where>
    </select>

    <select id="selectNonConsPileHoleInfo" parameterType="ConsPileHoleInfo" resultMap="ConsPileHoleInfoResult">
        <include refid="selectConsPileHoleInfoVo"/>
        <where>
            <if test="consUnitId != null  and consUnitId != ''">and cons_unit_id = #{consUnitId}</if>
            <if test="consStatus != null  and consStatus != ''">and cons_status != #{consStatus}</if>
        </where>
    </select>

    <select id="selectConsPileHoleInfoById" parameterType="Long" resultMap="ConsPileHoleInfoResult">
        <include refid="selectConsPileHoleInfoVo"/>
        where id = #{id}
    </select>

    <select id="selectConsPileHoleInfoByByteId" parameterType="String" resultMap="ConsPileHoleInfoResult">
        <include refid="selectConsPileHoleInfoVo"/>
        where byte_id = #{byteId}
    </select>

    <insert id="insertConsPileHoleInfo" parameterType="ConsPileHoleInfo" useGeneratedKeys="true" keyProperty="id">
        insert into cons_pile_hole_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="byteId != null and byteId != ''">byte_id,</if>
            <if test="holeKey != null and holeKey != ''">hole_key,</if>
            <if test="holeNum != null and holeNum != ''">hole_num,</if>
            <if test="deltaX != null">delta_x,</if>
            <if test="deltaY != null">delta_y,</if>
            <if test="lng != null">lng,</if>
            <if test="lat != null">lat,</if>
            <if test="consStatus != null">cons_status,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="desDept != null">des_dept,</if>
            <if test="diameter != null">diameter,</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="byteId != null and byteId != ''">#{byteId},</if>
            <if test="holeKey != null and holeKey != ''">#{holeKey},</if>
            <if test="holeNum != null and holeNum != ''">#{holeNum},</if>
            <if test="deltaX != null">#{deltaX},</if>
            <if test="deltaY != null">#{deltaY},</if>
            <if test="lng != null">#{lng},</if>
            <if test="lat != null">#{lat},</if>
            <if test="consStatus != null">#{consStatus},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="desDept != null">#{desDept},</if>
            <if test="diameter != null">#{diameter},</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>
    <insert id="batchInsertConsPileHoleInfo" parameterType="List" useGeneratedKeys="true" keyProperty="id">
        insert into cons_pile_hole_info
        (
        byte_id,
        hole_key,
        hole_num,
        delta_x,
        delta_y,
        lng,
        lat,
        cons_status,
        start_time,
        end_time,
        des_dept,
        diameter,
        cons_unit_id,
        update_time,
        create_by,
        create_time,
        tenant_id
        )
        values
        <foreach collection="list" item="item" separator=",">
            (
            #{item.byteId},
            #{item.holeKey},
            #{item.holeNum},
            #{item.deltaX},
            #{item.deltaY},
            #{item.lng},
            #{item.lat},
            #{item.consStatus},
            #{item.startTime},
            #{item.endTime},
            #{item.desDept},
            #{item.diameter},
            #{item.consUnitId},
            now(),
            #{item.createBy},
            now(),
            #{item.tenantId})
        </foreach>
        on duplicate key update
        hole_num= values(hole_num),
        delta_x= values(delta_x),
        delta_y= values(delta_y),
        lng= values(lng),
        lat= values(lat),
        des_dept= values(des_dept),
        diameter= values(diameter),
        update_time = now()
    </insert>
    <update id="updateConsPileHoleInfo" parameterType="ConsPileHoleInfo">
        update cons_pile_hole_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="byteId != null and byteId != ''">byte_id = #{byteId},</if>
            <if test="holeKey != null and holeKey != ''">hole_key = #{holeKey},</if>
            <if test="holeNum != null and holeNum != ''">hole_num = #{holeNum},</if>
            <if test="deltaX != null">delta_x = #{deltaX},</if>
            <if test="deltaY != null">delta_y = #{deltaY},</if>
            <if test="lng != null">lng = #{lng},</if>
            <if test="lat != null">lat = #{lat},</if>
            <if test="consStatus != null">cons_status = #{consStatus},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="desDept != null">des_dept = #{desDept},</if>
            <if test="diameter != null">diameter = #{diameter},</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>

    <delete id="deleteConsPileHoleInfoById" parameterType="Long">
        delete
        from cons_pile_hole_info
        where id = #{id}
    </delete>

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

    <delete id="deleteConsPileHoleInfoByConsUnitId" parameterType="Long">
        delete
        from cons_pile_hole_info
        where cons_unit_id = #{consUnitId}
          and cons_status != '00'
    </delete>
</mapper>