<?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.insreportdata.mapper.InspectionReportDataMapper">

    <resultMap type="InspectionReportData" id="InspectionReportDataResult">
        <result property="lineId" column="line_id"/>
        <result property="code" column="code"/>
        <result property="area" column="area"/>
        <result property="timeInterval" column="time_interval"/>
        <result property="eqName" column="eq_name"/>
        <result property="parts" column="parts"/>
        <result property="pointName" column="point_name"/>
        <result property="dataSources" column="data_sources"/>
        <result property="acquisitionTime" column="acquisition_time"/>
        <result property="inspectionResults" column="inspection_results"/>
        <result property="pointStatus" column="point_status"/>
        <result property="inspectionImg" column="inspection_img"/>
        <result property="reportId" column="report_id"/>
        <result property="algName" column="alg_name"/>
        <result property="taskName" column="task_name"/>
        <result property="taskPatrolledId" column="task_patrolled_id"/>
        <result property="taskResultId" column="task_result_id"/>
    </resultMap>

    <sql id="selectInspectionReportDataVo">
        select line_id,
               code,
               report_id,
               area,
               time_interval,
               eq_name,
               parts,
               point_name,
               data_sources,
               acquisition_time,
               inspection_results,
               point_status,
               inspection_img,
               alg_name,
               task_name,
               task_patrolled_id,
               task_result_id
        from inspection_report_data
    </sql>

    <select id="selectInspectionReportDataList" parameterType="InspectionReportData"
            resultMap="InspectionReportDataResult">
        <include refid="selectInspectionReportDataVo"/>
        <where>
            <if test="code != null ">and code = #{code}</if>
            <if test="area != null  and area != ''">and area = #{area}</if>
            <if test="timeInterval != null  and timeInterval != ''">and time_interval = #{timeInterval}</if>
            <if test="eqName != null  and eqName != ''">and eq_name like concat('%', #{eqName}, '%')</if>
            <if test="parts != null  and parts != ''">and parts = #{parts}</if>
            <if test="pointName != null  and pointName != ''">and point_name like concat('%', #{pointName}, '%')</if>
            <if test="dataSources != null  and dataSources != ''">and data_sources = #{dataSources}</if>
            <if test="acquisitionTime != null ">and acquisition_time = #{acquisitionTime}</if>
            <if test="inspectionResults != null  and inspectionResults != ''">and inspection_results =
                #{inspectionResults}
            </if>
            <if test="pointStatus != null  and pointStatus != ''">and point_status = #{pointStatus}</if>
            <if test="inspectionImg != null  and inspectionImg != ''">and inspection_img = #{inspectionImg}</if>
            <if test="reportId != null  and reportId != ''">and report_id = #{reportId}</if>
            <if test="params.reportIds != null  and params.reportIds != ''">and report_id in
                <foreach collection="params.reportIds" item="reportId" open="(" separator="," close=")">
                    #{reportId}
                </foreach>
            </if>
        </where>
    </select>
    <select id="selectListByStatus" parameterType="InspectionReportData" resultMap="InspectionReportDataResult">
        SELECT count(*) as area ,point_status FROM inspection_report_data
        <where>
            <if test="code != null ">and code = #{code}</if>
            <if test="area != null  and area != ''">and area = #{area}</if>
            <if test="timeInterval != null  and timeInterval != ''">and time_interval = #{timeInterval}</if>
            <if test="eqName != null  and eqName != ''">and eq_name like concat('%', #{eqName}, '%')</if>
            <if test="parts != null  and parts != ''">and parts = #{parts}</if>
            <if test="pointName != null  and pointName != ''">and point_name like concat('%', #{pointName}, '%')</if>
            <if test="dataSources != null  and dataSources != ''">and data_sources = #{dataSources}</if>
            <if test="acquisitionTime != null ">and acquisition_time = #{acquisitionTime}</if>
            <if test="inspectionResults != null  and inspectionResults != ''">and inspection_results =
                #{inspectionResults}
            </if>
            <if test="pointStatus != null  and pointStatus != ''">and point_status = #{pointStatus}</if>
            <if test="inspectionImg != null  and inspectionImg != ''">and inspection_img = #{inspectionImg}</if>
            <if test="reportId != null  and reportId != ''">and report_id = #{reportId}</if>
        </where>
        GROUP BY point_status
    </select>

    <select id="selectInspectionReportDataByLineId" parameterType="Long" resultMap="InspectionReportDataResult">
        <include refid="selectInspectionReportDataVo"/>
        where line_id = #{lineId}
    </select>

    <insert id="insertInspectionReportData" parameterType="InspectionReportData" useGeneratedKeys="true"
            keyProperty="lineId">
        insert into inspection_report_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="lineId != null">line_id,</if>
            <if test="code != null">code,</if>
            <if test="area != null">area,</if>
            <if test="timeInterval != null">time_interval,</if>
            <if test="eqName != null">eq_name,</if>
            <if test="parts != null">parts,</if>
            <if test="pointName != null">point_name,</if>
            <if test="dataSources != null">data_sources,</if>
            <if test="acquisitionTime != null">acquisition_time,</if>
            <if test="inspectionResults != null">inspection_results,</if>
            <if test="pointStatus != null">point_status,</if>
            <if test="inspectionImg != null">inspection_img,</if>
            <if test="reportId != null">report_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="lineId != null">#{lineId},</if>
            <if test="code != null">#{code},</if>
            <if test="area != null">#{area},</if>
            <if test="timeInterval != null">#{timeInterval},</if>
            <if test="eqName != null">#{eqName},</if>
            <if test="parts != null">#{parts},</if>
            <if test="pointName != null">#{pointName},</if>
            <if test="dataSources != null">#{dataSources},</if>
            <if test="acquisitionTime != null">#{acquisitionTime},</if>
            <if test="inspectionResults != null">#{inspectionResults},</if>
            <if test="pointStatus != null">#{pointStatus},</if>
            <if test="inspectionImg != null">#{inspectionImg},</if>
            <if test="reportId != null">#{reportId},</if>
        </trim>
    </insert>

    <update id="updateInspectionReportData" parameterType="InspectionReportData">
        update inspection_report_data
        <trim prefix="SET" suffixOverrides=",">
            <if test="code != null">code = #{code},</if>
            <if test="area != null">area = #{area},</if>
            <if test="timeInterval != null">time_interval = #{timeInterval},</if>
            <if test="eqName != null">eq_name = #{eqName},</if>
            <if test="parts != null">parts = #{parts},</if>
            <if test="pointName != null">point_name = #{pointName},</if>
            <if test="dataSources != null">data_sources = #{dataSources},</if>
            <if test="acquisitionTime != null">acquisition_time = #{acquisitionTime},</if>
            <if test="inspectionResults != null">inspection_results = #{inspectionResults},</if>
            <if test="pointStatus != null">point_status = #{pointStatus},</if>
            <if test="inspectionImg != null">inspection_img = #{inspectionImg},</if>
            <if test="reportId != null">report_id = #{reportId},</if>
        </trim>
        where line_id = #{lineId}
    </update>

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

    <delete id="deleteInspectionReportDataByLineIds" parameterType="String">
        delete from inspection_report_data where line_id in
        <foreach item="lineId" collection="array" open="(" separator="," close=")">
            #{lineId}
        </foreach>
    </delete>
    <insert id="batchInsertInspectionReportData" parameterType="java.util.List">
        insert into inspection_report_data
        (line_id,code,report_id,area,time_interval,eq_name,parts,point_name,data_sources,acquisition_time,inspection_results,point_status,inspection_img,alg_name,task_name,task_patrolled_id,task_result_id)
        values
        <foreach collection="list" item="t" index="index" separator=",">
            (#{t.lineId}, #{t.code}, #{t.reportId}, #{t.area}, #{t.timeInterval}, #{t.eqName}, #{t.parts},
            #{t.pointName},#{t.dataSources},#{t.acquisitionTime},#{t.inspectionResults},#{t.pointStatus},#{t.inspectionImg},#{t.algName},#{t.taskName},#{t.taskPatrolledId},#{t.taskResultId})
        </foreach>
    </insert>
</mapper>
