<?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.testor.module.hazard.dao.THazardWorkPlanMonitoringRecordProjectDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.testor.module.hazard.model.domain.THazardWorkPlanMonitoringRecordProject">
        <id column="id" property="id"/>
        <result column="project_name" property="projectName"/>
        <result column="project_value" property="projectValue"/>
        <result column="hazard_work_plan_monitoring_record_id" property="hazardWorkPlanMonitoringRecordId"/>
        <result column="status" property="status"/>

        <!-- 继承父类的字段 -->
        <result column="create_by" property="createBy"/>
        <result column="create_date" property="createDate"/>
        <result column="update_by" property="updateBy"/>
        <result column="update_date" property="updateDate"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, project_name, project_value, hazard_work_plan_monitoring_record_id, status,
        create_by, create_date, update_by, update_date
    </sql>

    <!-- 根据监测记录ID查询项目列表 -->
    <select id="selectByRecordId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_hazard_work_plan_monitoring_record_project
        WHERE hazard_work_plan_monitoring_record_id = #{recordId}
        ORDER BY create_date ASC
    </select>

    <!-- 根据监测记录ID列表查询项目列表 -->
    <select id="selectByRecordIds" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_hazard_work_plan_monitoring_record_project
        WHERE hazard_work_plan_monitoring_record_id IN
        <foreach collection="recordIds" item="recordId" open="(" separator="," close=")">
            #{recordId}
        </foreach>
        ORDER BY create_date ASC
    </select>

    <!-- 批量插入监测记录项目 -->
    <insert id="insertBatch">
        INSERT INTO t_hazard_work_plan_monitoring_record_project (
        id, project_name, project_value, hazard_work_plan_monitoring_record_id, status,
        create_by, create_date, update_by, update_date
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.id}, #{item.projectName}, #{item.projectValue},
            #{item.hazardWorkPlanMonitoringRecordId}, #{item.status},
            #{item.createBy}, #{item.createDate}, #{item.updateBy}, #{item.updateDate}
            )
        </foreach>
    </insert>

    <!-- 根据监测记录ID删除项目 -->
    <update id="deleteByRecordId">
        UPDATE t_hazard_work_plan_monitoring_record_project
        SET
            update_by = #{updateBy},
            update_date = NOW()
        WHERE hazard_work_plan_monitoring_record_id = #{recordId}
    </update>

    <!-- 根据监测记录ID列表批量删除项目 -->
    <update id="deleteByRecordIds">
        UPDATE t_hazard_work_plan_monitoring_record_project
        SET
        update_by = #{updateBy},
        update_date = NOW()
        WHERE hazard_work_plan_monitoring_record_id IN
        <foreach collection="recordIds" item="recordId" open="(" separator="," close=")">
            #{recordId}
        </foreach>
    </update>
</mapper>