<?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.inspect.insreport.mapper.InspectionReportMapper">

    <resultMap type="InspectionReport" id="InspectionReportResult">
        <result property="lineId" column="line_id"/>
        <result property="stationName" column="station_name"/>
        <result property="voltLevel" column="volt_level"/>
        <result property="stationType" column="station_type"/>
        <result property="inspectionDate" column="inspection_date"/>
        <result property="inspectionTaskName" column="inspection_task_name"/>
        <result property="envInfo" column="env_info"/>
        <result property="inspectionStartTime" column="inspection_start_time"/>
        <result property="inspectionEndTime" column="inspection_end_time"/>
        <result property="patrolStatistics" column="patrol_statistics"/>
        <result property="checkPerson" column="check_person"/>
        <result property="checkTime" column="check_time"/>
        <result property="description" column="description"/>
        <result property="taskId" column="task_id"/>
        <result property="taskPatrolledId" column="task_patrolled_id"/>
        <result property="taskResultId" column="task_result_id"/>
        <result property="filePath" column="file_path"/>
        <result property="filter" column="filter"/>
    </resultMap>

    <sql id="selectInspectionReportVo">
        select line_id,
               task_id,
               task_patrolled_id,
               file_path,
               task_result_id,
               station_name,
               volt_level,
               station_type,
               inspection_date,
               inspection_task_name,
               env_info,
               inspection_start_time,
               inspection_end_time,
               patrol_statistics,
               check_person,
               check_time,
               description,
               filter
        from inspection_report
    </sql>

    <select id="selectInspectionReportList" parameterType="InspectionReport" resultMap="InspectionReportResult">
        <include refid="selectInspectionReportVo"/>
        <where>
            <if test="stationName != null  and stationName != ''">and station_name like concat('%', #{stationName},
                '%')
            </if>
            <if test="voltLevel != null  and voltLevel != ''">and volt_level = #{voltLevel}</if>
            <if test="stationType != null  and stationType != ''">and station_type = #{stationType}</if>
            <if test="inspectionDate != null ">and inspection_date = #{inspectionDate}</if>
            <if test="inspectionTaskName != null  and inspectionTaskName != ''">and inspection_task_name like
                concat('%', #{inspectionTaskName}, '%')
            </if>
            <if test="envInfo != null  and envInfo != ''">and env_info = #{envInfo}</if>
            <if test="inspectionStartTime != null ">and inspection_start_time = #{inspectionStartTime}</if>
            <if test="inspectionEndTime != null ">and inspection_end_time = #{inspectionEndTime}</if>
            <if test="patrolStatistics != null  and patrolStatistics != ''">and patrol_statistics =
                #{patrolStatistics}
            </if>
            <if test="checkPerson != null  and checkPerson != ''">and check_person = #{checkPerson}</if>
            <if test="checkTime != null ">and check_time = #{checkTime}</if>
            <if test="description != null  and description != ''">and description = #{description}</if>
            <if test="taskId != null  and taskId != ''">and task_id = #{taskId}</if>
            <if test="taskResultId != null  and taskResultId != ''">and task_result_id = #{taskResultId}</if>
            <if test="taskPatrolledId != null  and taskPatrolledId != ''">and task_patrolled_id = #{taskPatrolledId}
            </if>
            <if test="filePath != null  and filePath != ''">and file_path = #{filePath}</if>
            <if test="filter != null  and filter != ''">and filter = #{filter}</if>
        </where>
        ORDER BY line_id desc
    </select>
    <select id="selectByAllList" parameterType="InspectionReport" resultMap="InspectionReportResult">
        select a.* from inspection_report a
        left JOIN patrol_task b on a.task_id=b.task_id
        left JOIN basedata_device c on b.device_list = c.device_id
        left JOIN basedata_area d on b.area_id =d.area_id
--         left JOIN basedata_eqpbook e on find_in_set( e.patrol_device_code, b.dev_no )
        <where>
            <if test="areaName != null  and areaName != ''">and b.area_name like concat('%',#{areaName},'%')</if>
            <if test="areaType != null  and areaType != ''">and d.area_name like concat('%',#{areaType},'%') and
                area_type ='bayunit'
            </if>
            <if test="type != null ">and b.type = #{type}</if>
            <if test="cycleStartTime != null "><!-- 开始时间检索 -->
                AND date_format(a.inspection_date,'%y%m%d') &gt;= date_format(#{cycleStartTime},'%y%m%d')
            </if>
            <if test="cycleEndTime != null  "><!-- 结束时间检索 -->
                AND date_format(a.inspection_date,'%y%m%d') &lt;= date_format(#{cycleEndTime},'%y%m%d')
            </if>
            <if test="inspectionTaskName != null  and inspectionTaskName != ''">and a.inspection_task_name like
                concat('%',#{inspectionTaskName},'%')
            </if>
        </where>
        ORDER BY a.inspection_date DESC
    </select>

    <select id="selectInspectionReportByLineId" parameterType="Long" resultMap="InspectionReportResult">
        <include refid="selectInspectionReportVo"/>
        where line_id = #{lineId}
    </select>

    <insert id="insertInspectionReport" parameterType="InspectionReport" useGeneratedKeys="true" keyProperty="lineId">
        insert into inspection_report
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stationName != null">station_name,</if>
            <if test="voltLevel != null">volt_level,</if>
            <if test="stationType != null">station_type,</if>
            <if test="inspectionDate != null">inspection_date,</if>
            <if test="inspectionTaskName != null">inspection_task_name,</if>
            <if test="envInfo != null">env_info,</if>
            <if test="inspectionStartTime != null">inspection_start_time,</if>
            <if test="inspectionEndTime != null">inspection_end_time,</if>
            <if test="patrolStatistics != null">patrol_statistics,</if>
            <if test="checkPerson != null">check_person,</if>
            <if test="checkTime != null">check_time,</if>
            <if test="description != null">description,</if>
            <if test="taskId != null">task_id,</if>
            <if test="taskPatrolledId != null">task_patrolled_id,</if>
            <if test="taskResultId != null">task_result_id,</if>
            <if test="filePath != null">file_path,</if>
            <if test="filter != null">filter,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stationName != null">#{stationName},</if>
            <if test="voltLevel != null">#{voltLevel},</if>
            <if test="stationType != null">#{stationType},</if>
            <if test="inspectionDate != null">#{inspectionDate},</if>
            <if test="inspectionTaskName != null">#{inspectionTaskName},</if>
            <if test="envInfo != null">#{envInfo},</if>
            <if test="inspectionStartTime != null">#{inspectionStartTime},</if>
            <if test="inspectionEndTime != null">#{inspectionEndTime},</if>
            <if test="patrolStatistics != null">#{patrolStatistics},</if>
            <if test="checkPerson != null">#{checkPerson},</if>
            <if test="checkTime != null">#{checkTime},</if>
            <if test="description != null">#{description},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="taskPatrolledId != null">#{taskPatrolledId},</if>
            <if test="taskResultId != null">#{taskResultId},</if>
            <if test="filePath != null">#{filePath},</if>
            <if test="filter != null">#{filter},</if>
        </trim>
    </insert>

    <update id="updateInspectionReport" parameterType="InspectionReport">
        update inspection_report
        <trim prefix="SET" suffixOverrides=",">
            <if test="stationName != null">station_name = #{stationName},</if>
            <if test="voltLevel != null">volt_level = #{voltLevel},</if>
            <if test="stationType != null">station_type = #{stationType},</if>
            <if test="inspectionDate != null">inspection_date = #{inspectionDate},</if>
            <if test="inspectionTaskName != null">inspection_task_name = #{inspectionTaskName},</if>
            <if test="envInfo != null">env_info = #{envInfo},</if>
            <if test="inspectionStartTime != null">inspection_start_time = #{inspectionStartTime},</if>
            <if test="inspectionEndTime != null">inspection_end_time = #{inspectionEndTime},</if>
            <if test="patrolStatistics != null">patrol_statistics = #{patrolStatistics},</if>
            <if test="checkPerson != null">check_person = #{checkPerson},</if>
            <if test="checkTime != null">check_time = #{checkTime},</if>
            <if test="description != null">description = #{description},</if>
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="taskPatrolledId != null">task_patrolled_id = #{taskPatrolledId},</if>
            <if test="taskResultId != null">task_result_id = #{taskResultId},</if>
            <if test="filePath != null">file_path = #{filePath},</if>
        </trim>
        where line_id = #{lineId}
    </update>

    <delete id="deleteInspectionReportByLineId" parameterType="Long">
        delete
        from inspection_report
        where line_id = #{lineId}
    </delete>

    <delete id="deleteInspectionReportByLineIds" parameterType="String">
        delete from inspection_report where line_id in
        <foreach item="lineId" collection="array" open="(" separator="," close=")">
            #{lineId}
        </foreach>
    </delete>
</mapper>
