<?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.tuoheng.admin.mapper.InspectionMapper">
    
    <resultMap id="InspectionResult" type="com.tuoheng.admin.entity.domain.Inspection" >
        <result property="id"    column="id"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="code"    column="code"    />
        <result property="name"    column="name"    />
        <result property="type"    column="type"    />
        <result property="inspectionType"    column="inspection_type"    />
        <result property="airportId"    column="airport_id"    />
        <result property="airportName"    column="airport_name"    />
        <result property="droneId"    column="drone_id"    />
        <result property="inspectionLine"    column="inspection_line"    />
        <result property="inspectionLineName"    column="inspection_line_name"    />
        <result property="equipmentId"    column="equipment_id"    />
        <result property="equipmentName"    column="equipment_name"    />
        <result property="equipmentMountId"    column="equipment_mount_id"    />
        <result property="equipmentMountType"    column="equipment_mount_type"    />
        <result property="equipmentMountCode"    column="equipment_mount_code"    />
        <result property="equipmentMountName"    column="equipment_mount_name"    />
        <result property="cloudBoxId"    column="cloud_box_id"    />
        <result property="cloudBoxName"    column="cloud_box_name"    />
        <result property="boxSn"    column="box_sn"    />
        <result property="flightHand"    column="flight_hand"    />
        <result property="flightHandName"    column="flight_hand_name"    />
        <result property="inspectionTime"    column="inspection_time"    />
        <result property="executionStartTime"    column="execution_start_time"    />
        <result property="executionEndTime"    column="execution_end_time"    />
        <result property="isLive"    column="is_live"    />
        <result property="isTaken"    column="is_taken"    />
        <result property="isTilt"    column="is_tilt"    />
        <result property="videoUrl"    column="video_url"    />
        <result property="aiVideoUrl"    column="ai_video_url"    />
        <result property="reportUrl"    column="report_url"    />
        <result property="srtUrl"    column="srt_url"    />
        <result property="status"    column="status"    />
        <result property="analyseStatus"    column="analyse_status"    />
        <result property="progressbar"    column="progressbar"    />
        <result property="note"    column="note"    />
        <result property="weather"    column="weather"    />
        <result property="flyHeight"    column="fly_height"    />
        <result property="srtName"    column="srt_name"    />
        <result property="heartbeatTime"    column="heartbeat_time"    />
        <result property="executionStatus"    column="execution_status"    />
        <result property="startLongitude"    column="start_longitude"    />
        <result property="startLatitude"    column="start_latitude"    />
        <result property="endLongitude"    column="end_longitude"    />
        <result property="endLatitude"    column="end_latitude"    />
        <result property="mobile"    column="mobile"    />
        <result property="patrolLocation"    column="patrol_location"    />
        <result property="dspServiceInstId"    column="dsp_service_inst_id"    />
        <result property="createUser"    column="create_user"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateUser"    column="update_user"    />
        <result property="updateTime"    column="update_time"    />
        <result property="mark"    column="mark"    />
    </resultMap>

    <sql id="Base_Column_list">
        id, tenant_id, code, name, type, inspection_type,
        airport_id, airport_name, drone_id, inspection_line, inspection_line_name, equipment_id, equipment_name, equipment_mount_id, equipment_mount_type, equipment_mount_code
        equipment_mount_name, cloud_box_id, cloud_box_name, box_sn, flight_hand, flight_hand_name, inspection_time,
        execution_start_time, execution_end_time, is_live, is_taken, is_tilt, video_url, ai_video_url, report_url, srt_url,
        status, analyse_status, progressbar, note, weather, fly_height, srt_name, heartbeat_time, execution_status,
        start_longitude, start_latitude, end_longitude, end_latitude, mobile, patrol_location, dsp_service_inst_id, emergency_data_inspection_id,
        create_user, create_time, update_user, update_time, mark
    </sql>

    <sql id="selectInspectionVo">
        select id, tenant_id, code, name, type, inspection_type,
        airport_id, airport_name, drone_id, inspection_line, inspection_line_name, equipment_id, equipment_name, equipment_mount_id,
        equipment_mount_name, equipment_mount_type,equipment_mount_code,cloud_box_id, cloud_box_name, box_sn, flight_hand, flight_hand_name, inspection_time,
        execution_start_time, execution_end_time, is_live, is_taken, is_tilt, video_url, ai_video_url, report_url, srt_url,
        status, analyse_status, progressbar, note, weather, fly_height, srt_name, heartbeat_time, execution_status,
        start_longitude, start_latitude, end_longitude, end_latitude, mobile, patrol_location, dsp_service_inst_id,
        create_user, create_time, update_user, update_time, mark
        from th_inspection
    </sql>

    <select id="selectOneById" parameterType="com.tuoheng.admin.entity.request.inspection.QueryInspectionPageListRequest" resultMap="InspectionResult">
        <include refid="selectInspectionVo"/>
        where id = #{id}
        and mark = 1
        and tenant_id = #{tenantId}
    </select>

    <select id="selectPageList" parameterType="com.tuoheng.admin.entity.request.inspection.QueryInspectionPageListRequest" resultMap="InspectionResult">
        <include refid="selectInspectionVo"/>
        <where>
            <if test="1 == 1"> and mark = 1</if>
            <if test="request.tenantId != null and request.tenantId != ''"> and tenant_id = #{request.tenantId} </if>
            <if test="request.beginTime != null"> and inspection_time &gt;= #{request.beginTime} </if>
            <if test="request.endTime != null"> and inspection_time &lt;= #{request.endTime} </if>
            <if test="request.status != null"> and status = #{request.status} </if>
            <if test="request.name != null and request.name != ''"> and name like concat('%', #{request.name}, '%') </if>
        </where>
        order by create_time desc
    </select>

    <!-- 根据云盒ID查询执行中的巡检任务-->
    <select id="getListByBox" resultType="com.tuoheng.admin.entity.domain.Inspection">
        <include refid="selectInspectionVo"/>
        where box_sn = #{boxSn, jdbcType=VARCHAR}
        and status = 10
        and mark = 1
        order by update_time desc

    </select>
    <!--根据任务状态进行自定义排序-->
    <select id="selectInspectionList" resultType="com.tuoheng.admin.entity.domain.Inspection">
       <include refid="selectInspectionVo"/>
       <where>
           mark = 1 and status in (5,6,10,15)
           <if test="tenantId != null and tenantId != ''">and tenant_id = #{tenantId}</if>
       </where>
       order by field(status,10,6,5,15), create_time desc
           limit 5
    </select>

    <update id="update" parameterType="com.tuoheng.admin.entity.domain.Inspection">
        update th_inspection
        <trim prefix="SET" suffixOverrides=",">
            <if test="tenantId != null and tenantId != ''">tenant_id = #{tenantId},</if>
            <if test="code != null">code = #{code},</if>
            <if test="name != null">name = #{name},</if>
            <if test="type != null">type = #{type},</if>
            <if test="inspectionType != null">inspection_type = #{inspectionType},</if>
            <if test="airportId != null">airport_id = #{airportId},</if>
            <if test="airportName != null">airport_name = #{airportName},</if>
            <if test="droneId != null"> drone_id = #{droneId},</if>
            <if test="inspectionLine != null">inspection_line = #{inspectionLine},</if>
            <if test="inspectionLineName != null">inspection_line_name = #{inspectionLineName},</if>
            <if test="equipmentId != null">equipment_id = #{equipmentId},</if>
            <if test="equipmentName != null">equipment_name = #{equipmentName},</if>
            <if test="equipmentMountId != null">equipment_mount_id = #{equipmentMountId},</if>
            <if test="equipmentMountType != null">equipment_mount_type = #{equipmentMountType},</if>
            <if test="equipmentMountCode != null">equipment_mount_code = #{equipmentMountCode},</if>
            <if test="equipmentMountName != null">equipment_mount_name = #{equipmentMountName},</if>
            <if test="cloudBoxId != null">cloud_box_id = #{cloudBoxId},</if>
            <if test="cloudBoxName != null">cloud_box_name = #{cloudBoxName},</if>
            <if test="boxSn != null">box_sn = #{boxSn},</if>
            <if test="flightHand != null">flight_hand = #{flightHand},</if>
            <if test="flightHandName != null">flight_hand_name = #{flightHandName},</if>
            <if test="inspectionTime != null">inspection_time = #{inspectionTime},</if>
            <if test="executionStartTime != null">execution_start_time = #{executionStartTime},</if>
            <if test="executionEndTime != null">execution_end_time = #{executionEndTime},</if>
            <if test="isLive != null">is_live = #{isLive},</if>
            <if test="isTaken != null">is_taken = #{isTaken},</if>
            <if test="isTilt != null">is_tilt = #{isTilt},</if>
            <if test="videoUrl != null">video_url = #{videoUrl},</if>
            <if test="aiVideoUrl != null">ai_video_url = #{aiVideoUrl},</if>
            <if test="reportUrl != null">report_url = #{reportUrl},</if>
            <if test="srtUrl != null">srt_url = #{srtUrl},</if>
            <if test="status != null">status = #{status},</if>
            <if test="analyseStatus != null">analyse_status = #{analyseStatus},</if>
            <if test="progressbar != null">progressbar = #{progressbar},</if>
            <if test="note != null">note = #{note},</if>
            <if test="weather != null">weather = #{weather},</if>
            <if test="flyHeight != null">fly_height = #{flyHeight},</if>
            <if test="srtName != null">srt_name = #{srtName},</if>
            <if test="heartbeatTime != null">heartbeat_time = #{heartbeatTime},</if>
            <if test="executionStatus != null">execution_status = #{executionStatus},</if>
            <if test="startLongitude != null">start_longitude = #{startLongitude},</if>
            <if test="startLatitude != null">start_latitude = #{startLatitude},</if>
            <if test="endLongitude != null">end_longitude = #{endLongitude},</if>
            <if test="endLatitude != null">end_latitude = #{endLatitude},</if>
            <if test="mobile != null">mobile = #{mobile},</if>
            <if test="patrolLocation != null">patrol_location = #{patrolLocation},</if>
            <if test="createUser != null">create_user = #{createUser},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateUser != null">update_user = #{updateUser},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="mark != null">mark = #{mark},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateStatus" parameterType="com.tuoheng.admin.entity.domain.Inspection">
        update th_inspection
        <trim prefix="SET" suffixOverrides=",">
            <if test="status != null"> status = #{status}, </if>
            <if test="updateUser != null and updateUser != ''"> update_user = #{updateUser},</if>
            <if test="updateTime != null"> update_time = #{updateTime},</if>
            <if test="executionStartTime != null"> execution_start_time = #{executionStartTime},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateByPrimaryKeySelective" parameterType="com.tuoheng.admin.entity.domain.Inspection">
        update th_inspection
        <set>
            <if test="code != null">
                code = #{code,jdbcType=VARCHAR},
            </if>
            <if test="name != null">
                `name` = #{name,jdbcType=VARCHAR},
            </if>
            <if test="equipmentId != null">
                equipment_id = #{equipmentId,jdbcType=VARCHAR},
            </if>
            <if test="equipmentMountId != null">
                equipment_mount_id = #{equipmentMountId,jdbcType=VARCHAR},
            </if>
            <if test="equipmentMountName != null">
                equipment_mount_name = #{equipmentMountName,jdbcType=VARCHAR},
            </if>
            <if test="cloudBoxId != null">
                cloud_box_id = #{cloudBoxId,jdbcType=VARCHAR},
            </if>
            <if test="flightHand != null">
                flight_hand = #{flightHand,jdbcType=VARCHAR},
            </if>
            <if test="flightHandName != null">
                flight_hand_name = #{flightHandName,jdbcType=VARCHAR},
            </if>
            <if test="inspectionTime != null">
                inspection_time = #{inspectionTime},
            </if>
            <if test="executionStartTime != null">
                execution_start_time = #{executionStartTime},
            </if>
            <if test="executionEndTime != null">
                execution_end_time = #{executionEndTime},
            </if>
            <if test="isLive != null">
                is_live = #{isLive},
            </if>
            <if test="isTaken != null">
                is_taken = #{isTaken},
            </if>
            <if test="isTilt != null">
                is_tilt = #{isTilt},
            </if>
            <if test="videoUrl != null">
                video_url = #{videoUrl,jdbcType=VARCHAR},
            </if>
            <if test="aiVideoUrl != null">
                ai_video_url = #{aiVideoUrl,jdbcType=VARCHAR},
            </if>
            <if test="reportUrl != null">
                report_url = #{reportUrl,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                `status` = #{status,jdbcType=TINYINT},
            </if>
            <if test="analyseStatus != null">
                analyse_status = #{analyseStatus,jdbcType=TINYINT},
            </if>
            <if test="note != null">
                note = #{note,jdbcType=VARCHAR},
            </if>
            <if test="weather != null">
                weather = #{weather},
            </if>
            <if test="flyHeight != null">
                fly_height = #{flyHeight},
            </if>
            <if test="srtName != null">
                srt_name = #{srtName},
            </if>
            <if test="airportId != null">
                airport_id = #{airportId},
            </if>
            <if test="airportName != null">
                airport_name = #{airportName},
            </if>
            <if test="inspectionLine != null">
                inspection_line = #{inspectionLine},
            </if>
            <if test="inspectionLineName != null">
                inspection_line_name = #{inspectionLineName},
            </if>
            <if test="progressbar != null">
                progressbar = #{progressbar},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="heartbeatTime != null">
                heartbeat_time = #{heartbeatTime},
            </if>
            <if test="startLatitude != null">
                start_latitude = #{startLatitude,jdbcType=VARCHAR},
            </if>
            <if test="startLongitude != null">
                start_longitude = #{startLongitude,jdbcType=VARCHAR},
            </if>
            <if test="endLongitude != null">
                end_longitude = #{endLongitude,jdbcType=VARCHAR},
            </if>
            <if test="endLatitude != null">
                end_latitude = #{endLatitude,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id} and mark = 1 and tenant_id = #{tenantId}
    </update>

    <!-- 修改  -->
    <update id="updateByPrimaryKey" parameterType="com.tuoheng.admin.entity.domain.Inspection">
        update th_inspection
        set code                 = #{code,jdbcType=VARCHAR},
        `name`               = #{name,jdbcType=VARCHAR},
        equipment_id         = #{equipmentId,jdbcType=VARCHAR},
        equipment_mount_id   = #{equipmentMountId,jdbcType=VARCHAR},
        cloud_box_id         = #{cloudBoxId,jdbcType=VARCHAR},
        flight_hand          = #{flightHand,jdbcType=VARCHAR},
        inspection_time      = #{inspectionTime,jdbcType=TIMESTAMP},
        execution_start_time = #{executionStartTime,jdbcType=TIMESTAMP},
        execution_end_time   = #{executionEndTime,jdbcType=TIMESTAMP},
        is_taken             = #{isTaken,jdbcType=BOOLEAN},
        is_tilt              = #{isTilt,jdbcType=BOOLEAN},
        video_url            = #{videoUrl,jdbcType=VARCHAR},
        ai_video_url         = #{aiVideoUrl,jdbcType=VARCHAR},
        `status`             = #{status,jdbcType=TINYINT},
        analyse_status       = #{analyseStatus,jdbcType=TINYINT},
        progressbar          = #{progressbar,jdbcType=DOUBLE},
        note                 = #{note,jdbcType=VARCHAR},
        update_user          = #{updateUser,jdbcType=INTEGER},
        update_time          = #{updateTime,jdbcType=TIMESTAMP},
        start_longitude      = #{startLongitude,jdbcType=VARCHAR},
        start_latitude       = #{startLatitude,jdbcType=VARCHAR},
        end_longitude        = #{endLongitude,jdbcType=VARCHAR},
        end_latitude         = #{endLatitude,jdbcType=VARCHAR},
        patrol_location      = #{patrolLocation,jdbcType=VARCHAR},
        mobile               = #{mobile,jdbcType=VARCHAR},
        equipment_name       = #{equipmentName,jdbcType=VARCHAR},
        equipment_mount_name = #{equipmentMountName,jdbcType=VARCHAR},
        cloud_box_name       = #{cloudBoxName,jdbcType=VARCHAR},
        flight_hand_name     = #{flightHandName,jdbcType=VARCHAR},
        mark                 = #{mark,jdbcType=TINYINT},
        box_sn               = #{boxSn,jdbcType=TINYINT}
        where id = #{id}
    </update>

    <delete id="deleteByLogic" parameterType="com.tuoheng.admin.entity.domain.Inspection">
        update th_inspection
        <trim prefix="SET" suffixOverrides=",">
            <if test="updateUser != null  and updateUser != ''"> update_user = #{updateUser}, </if>
            <if test="updateTime != null"> update_time = #{updateTime}, </if>
            <if test="mark != null"> mark = #{mark}, </if>
        </trim>
        where id = #{id}
    </delete>

    <delete id="deleteByPhysical" parameterType="String">
        delete from th_inspection where id = #{id}
    </delete>

</mapper>