<?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.ruoyi.project.sems.mapper.TReportPatrolMapper">

    <resultMap type="TReportPatrol" id="TReportPatrolResult">
        <result property="id" column="id"/>
        <result property="reportId" column="report_id"/>
        <result property="devno" column="devno"/>
        <result property="startDate" column="start_date"/>
        <result property="endDate" column="end_date"/>
        <result property="problem" column="problem"/>
        <result property="inspector" column="inspector"/>
        <result property="approach" column="approach"/>
        <result property="dealUnit" column="deal_unit"/>
        <result property="remarks" column="remarks"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createrCode" column="creater_code"/>
        <result property="createdate" column="createdate"/>
        <result property="updaterCode" column="updater_code"/>
        <result property="updatedate" column="updatedate"/>
    </resultMap>

    <sql id="selectTReportPatrolVo">
        select d.id,
               d.report_id,
               d.devno,
               d.start_date,
               d.end_date,
               d.problem,
               d.inspector,
               d.approach,
               d.deal_unit,
               d.remarks,
               d.del_flag,
               d.creater_code,
               d.createdate,
               d.updater_code,
               d.updatedate
        from t_report_patrol d
    </sql>

    <select id="selectTReportPatrolList" parameterType="TReportPatrol" resultMap="TReportPatrolResult">
        <include refid="selectTReportPatrolVo"/>
        <where>
            <!--<if test="reportId != null  and reportId != ''">and report_id = #{reportId}</if>-->
            <if test="reportId != null  and reportId != ''">
                <!-- 根据记录id获取装置名称，反向获取装置id查询所有的巡查记录 -->
                and d.report_id in(
                select t1.id from t_report_month t1 where t1.plant_code =
                (select t2.plant_code from t_report_month t2 where
                t2.id = #{reportId} )
                )
            </if>
            and d.del_flag = 0
            <!-- 根据结束时间进行倒序 -->
            order by d.start_date desc
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>

    <select id="selectTReportPatrolByMonthId" parameterType="Long" resultMap="TReportPatrolResult">
        <include refid="selectTReportPatrolVo"/>
        where report_id = #{reportId}
    </select>

    <select id="selectTReportPatrolById" parameterType="Long" resultMap="TReportPatrolResult">
        <include refid="selectTReportPatrolVo"/>
        where id = #{id}
    </select>

    <insert id="insertTReportPatrol" parameterType="TReportPatrol">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_report_patrol.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_report_patrol
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="reportId != null and reportId != ''">report_id,</if>
            <if test="devno != null">devno,</if>
            <if test="startDate != null">start_date,</if>
            <if test="endDate != null">end_date,</if>
            <if test="problem != null">problem,</if>
            <if test="inspector != null">inspector,</if>
            <if test="approach != null">approach,</if>
            <if test="dealUnit != null">deal_unit,</if>
            <if test="remarks != null">remarks,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createrCode != null">creater_code,</if>
            <if test="createdate != null">createdate,</if>
            <if test="updaterCode != null">updater_code,</if>
            <if test="updatedate != null">updatedate,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="reportId != null and reportId != ''">#{reportId},</if>
            <if test="devno != null">#{devno},</if>
            <if test="startDate != null">#{startDate},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="problem != null">#{problem},</if>
            <if test="inspector != null">#{inspector},</if>
            <if test="approach != null">#{approach},</if>
            <if test="dealUnit != null">#{dealUnit},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createrCode != null">#{createrCode},</if>
            <if test="createdate != null">#{createdate},</if>
            <if test="updaterCode != null">#{updaterCode},</if>
            <if test="updatedate != null">#{updatedate},</if>
        </trim>
    </insert>

    <update id="updateTReportPatrol" parameterType="TReportPatrol">
        update t_report_patrol
        <trim prefix="SET" suffixOverrides=",">
            <if test="reportId != null and reportId != ''">report_id = #{reportId},</if>
            <if test="devno != null">devno = #{devno},</if>
            <if test="startDate != null">start_date = #{startDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="problem != null">problem = #{problem},</if>
            <if test="inspector != null">inspector = #{inspector},</if>
            <if test="approach != null">approach = #{approach},</if>
            <if test="dealUnit != null">deal_unit = #{dealUnit},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createrCode != null">creater_code = #{createrCode},</if>
            <if test="createdate != null">createdate = #{createdate},</if>
            <if test="updaterCode != null">updater_code = #{updaterCode},</if>
            <if test="updatedate != null">updatedate = #{updatedate},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteTReportPatrolById" parameterType="Long">
        update t_report_patrol
        set del_flag = 2
        where id = #{id}
    </update>

    <update id="deleteTReportPatrolByIds" parameterType="String">
        update t_report_patrol set del_flag = 2 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>