<?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.aucma.report.mapper.RecordElectricalInspectionMapper">

    <resultMap type="RecordElectricalInspection" id="RecordElectricalInspectionResult">
        <result property="uuid" column="uuid"/>
        <result property="result" column="result"/>
        <result property="barcode" column="barcode"/>
        <result property="testTime" column="testtime"/>
        <result property="testData" column="testdata"/>
        <result property="recordTime" column="recordtime"/>
    </resultMap>

    <resultMap id="RecordElectricalInspectionDetailElectricalinspectionResult" type="RecordElectricalInspection"
               extends="RecordElectricalInspectionResult">
        <collection property="detailElectricalinspectionList" notNullColumn="sub_uuid" javaType="java.util.List"
                    resultMap="DetailElectricalinspectionResult"/>
    </resultMap>

    <resultMap type="DetailElectricalinspection" id="DetailElectricalinspectionResult">
        <result property="uuid" column="sub_uuid"/>
        <result property="serialnumber" column="sub_serialnumber"/>
        <result property="projectname" column="sub_projectname"/>
        <result property="testcondition" column="sub_testcondition"/>
        <result property="testvalue" column="sub_testvalue"/>
        <result property="testresult" column="sub_testresult"/>
        <result property="recordtime" column="sub_recordtime"/>
    </resultMap>

    <sql id="selectRecordElectricalInspectionVo">
        select uuid, result, barcode, testtime, testdata, recordtime
        from record_electricalinspection
    </sql>

    <select id="selectRecordElectricalInspectionList" parameterType="RecordElectricalInspection"
            resultMap="RecordElectricalInspectionResult">
        <include refid="selectRecordElectricalInspectionVo"/>
        <where>
            <if test="uuid != null  and uuid != ''">and uuid = #{uuid}</if>
            <if test="result != null  and result != ''">and result = #{result}</if>
            <if test="barcode != null  and barcode != ''">and barcode = #{barcode}</if>
            <if test="testTime != null  and testTime != ''">and testtime = #{testTime}</if>
            <if test="testData != null  and testData != ''">and testdata = #{testData}</if>
            <if test="params.beginRecordTime != null and params.beginRecordTime != '' and params.endRecordTime != null and params.endRecordTime != ''">
                and recordtime between to_date(#{params.beginRecordTime}, 'yyyy-mm-dd hh24:mi:ss') and
                to_date(#{params.endRecordTime}, 'yyyy-mm-dd hh24:mi:ss')
            </if>
        </where>
    </select>

    <select id="selectRecordElectricalInspectionByUuid" parameterType="String"
            resultMap="RecordElectricalInspectionDetailElectricalinspectionResult">
        select a.uuid,
               a.result,
               a.barcode,
               a.testtime,
               a.testdata,
               a.recordtime,
               b.uuid          as sub_uuid,
               b.serialnumber  as sub_serialnumber,
               b.projectname   as sub_projectname,
               b.testcondition as sub_testcondition,
               b.testvalue     as sub_testvalue,
               b.testresult    as sub_testresult,
               b.recordtime    as sub_recordtime
        from record_electricalinspection a
                 left join detail_electricalinspection b on b.uuid = a.testdata
        where a.uuid = #{uuid}
    </select>

    <insert id="insertRecordElectricalInspection" parameterType="RecordElectricalInspection">
        insert into record_electricalinspection
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="uuid != null">uuid,</if>
            <if test="result != null">result,</if>
            <if test="barcode != null">barcode,</if>
            <if test="testTime != null">testtime,</if>
            <if test="testData != null">testdata,</if>
            <if test="recordTime != null">recordtime,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="uuid != null">#{uuid},</if>
            <if test="result != null">#{result},</if>
            <if test="barcode != null">#{barcode},</if>
            <if test="testTime != null">#{testTime},</if>
            <if test="testData != null">#{testData},</if>
            <if test="recordTime != null">#{recordTime},</if>
        </trim>
    </insert>

    <update id="updateRecordElectricalInspection" parameterType="RecordElectricalInspection">
        update record_electricalinspection
        <trim prefix="SET" suffixOverrides=",">
            <if test="result != null">result = #{result},</if>
            <if test="barcode != null">barcode = #{barcode},</if>
            <if test="testTime != null">testtime = #{testTime},</if>
            <if test="testData != null">testdata = #{testData},</if>
            <if test="recordTime != null">recordtime = #{recordTime},</if>
        </trim>
        where uuid = #{uuid}
    </update>

    <delete id="deleteRecordElectricalInspectionByUuid" parameterType="String">
        delete
        from record_electricalinspection
        where uuid = #{uuid}
    </delete>

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

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

    <delete id="deleteDetailElectricalinspectionByUuid" parameterType="String">
        delete
        from detail_electricalinspection
        where uuid = #{uuid}
    </delete>

    <insert id="batchDetailElectricalinspection">
        insert all
        <foreach item="item" index="index" collection="list">
            into detail_electricalinspection( uuid, serialnumber, projectname, testcondition, testvalue, testresult,
            recordtime) values ( #{item.uuid}, #{item.serialnumber}, #{item.projectname}, #{item.testcondition},
            #{item.testvalue}, #{item.testresult}, #{item.recordtime})
        </foreach>
        SELECT 1 FROM DUAL
    </insert>
</mapper>