<?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.task.mapper.TTaskInspectionPlanMapper">

    <resultMap type="TTaskInspectionPlan" id="TTaskInspectionPlanResult">
        <result property="id"    column="id"    />
        <result property="plantId"    column="plant_id"    />
        <result property="plantName"    column="plant_name"    />
        <result property="inspectionPlanNo"    column="inspection_plan_no"    />
        <result property="inspectionPlanName"    column="inspection_plan_name"    />
        <result property="detectionFrequency"    column="detection_frequency"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="pointNum"    column="point_num"    />
        <result property="remarks"    column="remarks"    />
        <result property="deptId"    column="dept_id"    />
        <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"    />
        <result property="creater"    column="creater"    />
        <result property="planQuarter"    column="plan_quarter"    />
        <result property="planYear"    column="plan_year"    />
        <result property="planCode"    column="plan_code"    />
    </resultMap>

    <sql id="selectTTaskInspectionPlanVo">
        select `plan_quarter`, `plan_year`, `plan_code`,id, plant_id, inspection_plan_no, inspection_plan_name, detection_frequency, start_time, end_time, point_num, remarks, dept_id, del_flag, creater_code, createdate, updater_code, updatedate from t_task_inspection_plan
    </sql>

    <select id="selectTTaskInspectionPlanList" parameterType="TTaskInspectionPlan" resultMap="TTaskInspectionPlanResult">
        select d.*,bp.plant_name,su.user_name creater from t_task_inspection_plan d
        left join t_base_plant bp on bp.plant_id=d.plant_id
        left join sys_user su on su.user_id=d.creater_code
        <where>
            <if test="plantId != null "> and d.plant_id = #{plantId}</if>
            <if test="inspectionPlanNo != null  and inspectionPlanNo != ''"> and d.inspection_plan_no = #{inspectionPlanNo}</if>
            <if test="inspectionPlanName != null  and inspectionPlanName != ''"> and d.inspection_plan_name like concat('%', #{inspectionPlanName}, '%')</if>
            <if test="detectionFrequency != null  and detectionFrequency != ''"> and d.detection_frequency = #{detectionFrequency}</if>
            <if test="startTime != null "> and d.start_time = #{startTime}</if>
            <if test="endTime != null "> and d.end_time = #{endTime}</if>
            <if test="pointNum != null  and pointNum != ''"> and d.point_num = #{pointNum}</if>
            <if test="remarks != null  and remarks != ''"> and d.remarks = #{remarks}</if>
            <if test="deptId != null "> and d.dept_id = #{deptId}</if>
            <if test="createrCode != null "> and d.creater_code = #{createrCode}</if>
            <if test="createdate != null "> and d.createdate = #{createdate}</if>
            <if test="updaterCode != null "> and d.updater_code = #{updaterCode}</if>
            <if test="updatedate != null "> and d.updatedate = #{updatedate}</if>
            <if test="planQuarter != null "> and d.plan_quarter = #{planQuarter}</if>
            <if test="planYear != null "> and d.plan_year = #{planYear}</if>
            <if test="planCode != null "> and d.plan_code = #{planCode}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScopePlant}
    </select>

    <select id="selectAllPlan" resultMap="TTaskInspectionPlanResult">
        <include refid="selectTTaskInspectionPlanVo"/> where del_flag = 0
    </select>

    <select id="selectTTaskInspectionPlanById" parameterType="Long" resultMap="TTaskInspectionPlanResult">
        <include refid="selectTTaskInspectionPlanVo"/>
        where id = #{id}
    </select>

    <insert id="insertTTaskInspectionPlan" parameterType="TTaskInspectionPlan" useGeneratedKeys="true" keyProperty="id">
        insert into t_task_inspection_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="plantId != null">plant_id,</if>
            <if test="inspectionPlanNo != null">inspection_plan_no,</if>
            <if test="inspectionPlanName != null">inspection_plan_name,</if>
            <if test="detectionFrequency != null">detection_frequency,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="pointNum != null">point_num,</if>
            <if test="remarks != null">remarks,</if>
            <if test="deptId != null">dept_id,</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>
            <if test="planQuarter != null ">`plan_quarter`,</if>
            <if test="planYear != null ">`plan_year`,</if>
            <if test="planCode != null ">`plan_code`,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="plantId != null">#{plantId},</if>
            <if test="inspectionPlanNo != null">#{inspectionPlanNo},</if>
            <if test="inspectionPlanName != null">#{inspectionPlanName},</if>
            <if test="detectionFrequency != null">#{detectionFrequency},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="pointNum != null">#{pointNum},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="deptId != null">#{deptId},</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>
            <if test="planQuarter != null ">#{planQuarter},</if>
            <if test="planYear != null ">#{planYear},</if>
            <if test="planCode != null ">#{planCode},</if>
         </trim>
    </insert>

    <insert id="insertTTaskInspectionPlanByList" parameterType="java.util.List" useGeneratedKeys="true">
        insert into t_task_inspection_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            plant_id,
            inspection_plan_no,
            inspection_plan_name,
            detection_frequency,
            start_time,
            end_time,
            point_num,
            creater_code,
            createdate,
            updater_code,
            updatedate,
            `plan_quarter`,
            `plan_year`,
            `plan_code`,
        </trim>
        values
        <foreach collection="list" index="index" separator="," item="item">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                #{item.plantId},
                #{item.inspectionPlanNo},
                #{item.inspectionPlanName},
                #{item.detectionFrequency},
                #{item.startTime},
                #{item.endTime},
                #{item.pointNum},
                #{item.createrCode},
                #{item.createdate},
                #{item.updaterCode},
                #{item.updatedate},
                #{item.planQuarter},
                #{item.planYear},
                #{item.planCode},
            </trim>
        </foreach>
    </insert>

    <update id="updateTTaskInspectionPlan" parameterType="TTaskInspectionPlan">
        update t_task_inspection_plan
        <trim prefix="SET" suffixOverrides=",">
            <if test="plantId != null">plant_id = #{plantId},</if>
            <if test="inspectionPlanNo != null">inspection_plan_no = #{inspectionPlanNo},</if>
            <if test="inspectionPlanName != null">inspection_plan_name = #{inspectionPlanName},</if>
            <if test="detectionFrequency != null">detection_frequency = #{detectionFrequency},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="pointNum != null">point_num = #{pointNum},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="deptId != null">dept_id = #{deptId},</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>
            <if test="planQuarter != null ">plan_quarter = #{planQuarter},</if>
            <if test="planYear != null ">plan_year = #{planYear},</if>
            <if test="planCode != null ">plan_code = #{planCode},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTTaskInspectionPlanById" parameterType="Long">
        update t_task_inspection_plan set del_flag=1 where id = #{id}
    </delete>

    <delete id="deleteTTaskInspectionPlanByIds" parameterType="String">
        update t_task_inspection_plan set del_flag=1 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
