<?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.check.mapper.TCheckCheckpointsMapper">

    <resultMap type="TCheckCheckpoints" id="TCheckCheckpointsResult">
        <result property="checkId" column="check_id"/>
        <result property="pointId" column="point_id"/>
        <result property="inspectionId" column="inspection_id"/>
        <result property="instrumentId" column="instrument_id"/>
        <result property="testValue" column="test_value"/>
        <result property="netTestValue" column="net_test_value"/>
        <result property="plantId" column="plant_id"/>
        <result property="plantName" column="plant_name"/>
        <result property="regionId" column="region_id"/>
        <result property="regionName" column="region_name"/>
        <result property="layer" column="layer"/>
        <result property="devId" column="dev_id"/>
        <result property="devName" column="dev_name"/>
        <result property="devCode" column="dev_code"/>
        <result property="groupCode" column="group_code"/>
        <result property="extendCode" column="extend_code"/>
        <result property="pointType" column="point_type"/>
        <result property="instrumentCode" column="instrument_code"/>
        <result property="leakagePosition" column="leakage_position"/>
        <result property="checker" column="checker"/>
        <result property="checkDate" column="check_date"/>
        <result property="leakageDegree" column="leakage_degree"/>
        <result property="remarks" column="remarks"/>
        <result property="approveStatus" column="approve_status"/>
        <result property="approveTime" column="approve_time"/>
        <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="repairId" column="repair_id"/>
        <result property="planId" column="plan_id"/>
    </resultMap>

    <sql id="selectTCheckCheckpointsVo">
        select check_id,
               point_id,
               instrument_id,
               inspection_id,
               test_value,
               net_test_value,
               plant_id,
               plant_name,
               region_id,
               region_name,
               layer,
               dev_id,
               dev_name,
               dev_code,
               group_code,
               extend_code,
               point_type,
               instrument_code,
               leakage_position,
               checker,
               check_date,
               leakage_degree,
               remarks,
               approve_status,
               approve_time,
               dept_id,
               del_flag,
               creater_code,
               createdate,
               updater_code,
               updatedate,
               plan_id
        from t_check_checkpoints
    </sql>

    <select id="selectTCheckCheckpointsList" parameterType="TCheckCheckpoints" resultMap="TCheckCheckpointsResult">
        select d.plan_id,d.medium_type,d.plant_type,d.check_id,d.point_id, d.instrument_id, d.inspection_id, d.test_value,
        d.net_test_value, d.plant_id,d.plant_name,
        d.region_id, d.layer, d.dev_id, d.group_code, d.extend_code, d.point_type, d.instrument_code,
        leakage_position, d.checker, d.check_date, d.leakage_degree, d.remarks, d.approve_status, d.approve_time,
        d.dept_id, d.del_flag,
        d.creater_code, d.createdate, d.updater_code, d.updatedate,br.region_name,bd.dev_code,bd.dev_describe as
        dev_name from t_check_checkpoints d
        left join t_base_region br on d.region_id = br.region_id
        left join t_base_device bd on d.dev_id = bd.dev_id
        <where>
            <if test="pointId != null ">and d.point_id = #{pointId}</if>
            <if test="instrumentId != null ">and d.instrument_id = #{instrumentId}</if>
            <if test="inspectionId != null ">and d.inspection_id = #{inspectionId}</if>
            <if test="testValue != null  and testValue != ''">and d.test_value = #{testValue}</if>
            <if test="netTestValue != null  and netTestValue != ''">and d.net_test_value = #{netTestValue}</if>
            <if test="plantId != null  and plantId != ''">and d.plant_id = #{plantId}</if>
            <if test="plantName != null  and plantName != ''">and d.plant_name like concat('%', #{plantName}, '%')</if>
            <if test="regionId != null  and regionId != ''">and d.region_id= #{regionId}</if>
            <if test="regionName != null  and regionName != ''">and d.region_name like concat('%', #{regionName}, '%')
            </if>
            <if test="layer != null  and layer != ''">and d.layer = #{layer}</if>
            <if test="devId != null  and devId != ''">and d.dev_id = #{devId}</if>
            <if test="devName != null  and devName != ''">and d.dev_name like concat('%', #{devName}, '%')</if>
            <if test="devCode != null  and devCode != ''">and d.dev_code = #{devCode}</if>
            <if test="groupCode != null  and groupCode != ''">and d.group_code = #{groupCode}</if>
            <if test="extendCode != null  and extendCode != ''">and d.extend_code = #{extendCode}</if>
            <if test="pointType != null  and pointType != ''">and d.point_type = #{pointType}</if>
            <if test="instrumentCode != null  and instrumentCode != ''">and d.instrument_code = #{instrumentCode}</if>
            <if test="leakagePosition != null  and leakagePosition != ''">and d.leakage_position = #{leakagePosition}
            </if>
            <if test="checker != null  and checker != ''">and d.checker = #{checker}</if>
            <if test="checkDate != null ">and d.check_date = #{checkDate}</if>
            <if test="leakageDegree != null  and leakageDegree != ''">and d.leakage_degree = #{leakageDegree}</if>
            <if test="remarks != null  and remarks != ''">and d.remarks = #{remarks}</if>
            <if test="approveStatus != null ">and d.approve_status = #{approveStatus}</if>
            <if test="approveTime != null ">and d.approve_time = #{approveTime}</if>
            <if test="deptId != null ">and d.dept_id = #{deptId}</if>
            <if test="choose != null ">and d.inspection_id is null</if>
            <if test="repairId == null and repairId !=1">and d.repair_id is null</if>
            <if test="repairId != null and repairId !=1">and d.repair_id is not null</if>
            <if test="planId != null">and d.plan_id=#{planId}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScopePlant}
    </select>


    <select id="selectTCheckCheckpointByEntity" parameterType="TCheckCheckpoints" resultMap="TCheckCheckpointsResult">
        select d.plant_type,d.point_type,d.medium_type from t_check_checkpoints d
        <where>
            <if test="plantName != null  and plantName != ''">and d.plant_name = #{plantName}</if>
            <if test="groupCode != null  and groupCode != ''">and d.group_code = #{groupCode}</if>
            <if test="extendCode != null  and extendCode != ''">and d.extend_code = #{extendCode}</if>
            <if test="inspectionId != null  and inspectionId != ''">and d.inspection_id = #{inspectionId}</if>
        </where>
    </select>

    <select id="selectPointsByIds" parameterType="String" resultMap="TCheckCheckpointsResult">
        select d.plan_id,d.medium_type,d.plant_type,d.check_id,d.point_id, d.instrument_id, d.inspection_id, d.test_value,
        d.net_test_value, d.plant_id,d.plant_name,
        d.region_id, d.layer, d.dev_id, d.group_code, d.extend_code, d.point_type, d.instrument_code,
        leakage_position, d.checker, d.check_date, d.leakage_degree, d.remarks, d.approve_status, d.approve_time,
        d.dept_id, d.del_flag,
        d.creater_code, d.createdate, d.updater_code, d.updatedate,br.region_name,bd.dev_code,bd.dev_describe as
        dev_name from t_check_checkpoints d
        left join t_base_region br on d.region_id = br.region_id
        left join t_base_device bd on d.dev_id = bd.dev_id
        <where>
            and d.check_id in
            <foreach item="checkId" collection="array" open="(" separator="," close=")">
                #{checkId}
            </foreach>
            and d.approve_status=2
        </where>
    </select>

    <select id="selectTCheckCheckpointsByCheckId" parameterType="Long" resultMap="TCheckCheckpointsResult">
        <include refid="selectTCheckCheckpointsVo"/>
        where check_id = #{checkId}
    </select>

    <insert id="insertTCheckCheckpoints" parameterType="TCheckCheckpoints" useGeneratedKeys="true"
            keyProperty="checkId">
        insert into t_check_checkpoints
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="pointId != null">point_id,</if>
            <if test="instrumentId != null">instrument_id,</if>
            <if test="inspectionId != null">inspection_id,</if>
            <if test="testValue != null">test_value,</if>
            <if test="netTestValue != null">net_test_value,</if>
            <if test="plantId != null">plant_id,</if>
            <if test="plantName != null">plant_name,</if>
            <if test="regionId != null">region_id,</if>
            <if test="regionName != null">region_name,</if>
            <if test="layer != null">layer,</if>
            <if test="devId != null">dev_id,</if>
            <if test="devName != null">dev_name,</if>
            <if test="devCode != null">dev_code,</if>
            <if test="groupCode != null">group_code,</if>
            <if test="extendCode != null">extend_code,</if>
            <if test="pointType != null">point_type,</if>
            <if test="instrumentCode != null">instrument_code,</if>
            <if test="leakagePosition != null">leakage_position,</if>
            <if test="checker != null">checker,</if>
            <if test="checkDate != null">check_date,</if>
            <if test="leakageDegree != null">leakage_degree,</if>
            <if test="remarks != null">remarks,</if>
            <if test="approveStatus != null">approve_status,</if>
            <if test="approveTime != null">approve_time,</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="plantType != null">plant_type,</if>
            <if test="planId != null">plan_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="pointId != null">#{pointId},</if>
            <if test="instrumentId != null">#{instrumentId},</if>
            <if test="inspectionId != null">#{inspectionId},</if>
            <if test="testValue != null">#{testValue},</if>
            <if test="netTestValue != null">#{netTestValue},</if>
            <if test="plantId != null">#{plantId},</if>
            <if test="plantName != null">#{plantName},</if>
            <if test="regionId != null">#{regionId},</if>
            <if test="regionName != null">#{regionName},</if>
            <if test="layer != null">#{layer},</if>
            <if test="devId != null">#{devId},</if>
            <if test="devName != null">#{devName},</if>
            <if test="devCode != null">#{devCode},</if>
            <if test="groupCode != null">#{groupCode},</if>
            <if test="extendCode != null">#{extendCode},</if>
            <if test="pointType != null">#{pointType},</if>
            <if test="instrumentCode != null">#{instrumentCode},</if>
            <if test="leakagePosition != null">#{leakagePosition},</if>
            <if test="checker != null">#{checker},</if>
            <if test="checkDate != null">#{checkDate},</if>
            <if test="leakageDegree != null">#{leakageDegree},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="approveStatus != null">#{approveStatus},</if>
            <if test="approveTime != null">#{approveTime},</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="plantType != null">#{plantType},</if>
            <if test="planId != null">#{plan_id},</if>
        </trim>
    </insert>

    <insert id="insertTCheckCheckpointsByList" parameterType="TCheckCheckpoints" useGeneratedKeys="true"
            keyProperty="checkId">
        insert into t_check_checkpoints
        <trim prefix="(" suffix=")" suffixOverrides=",">
            point_id,
            plant_name,
            plant_id,
            region_id,
            layer,
            dev_id,
            group_code,
            extend_code,
            point_type,
            creater_code,
            createdate,
            updater_code,
            updatedate,
            plant_type,
            medium_type,
            plan_id,
            repair_id,
        </trim>
        values
        <foreach collection="list" index="index" separator="," item="item">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                #{item.pointId},
                #{item.plantName},
                #{item.plantId},
                #{item.regionId},
                #{item.layer},
                #{item.devId},
                #{item.groupCode},
                #{item.extendCode},
                #{item.pointType},
                #{item.createrCode},
                #{item.createdate},
                #{item.updaterCode},
                #{item.updatedate},
                #{item.plantType},
                #{item.mediumType},
                #{item.planId},
                <if test="item.repairId!=null">#{item.repairId},</if>
                <if test="item.repairId==null">null,</if>
            </trim>
        </foreach>
    </insert>

    <update id="updateTCheckCheckpoints" parameterType="TCheckCheckpoints">
        update t_check_checkpoints
        <trim prefix="SET" suffixOverrides=",">
            <if test="pointId != null">point_id = #{pointId},</if>
            <if test="inspectionId != null">inspection_id = #{inspectionId},</if>
            <if test="instrumentId != null">instrument_id = #{instrumentId},</if>
            <if test="testValue != null">test_value = #{testValue},</if>
            <if test="netTestValue != null">net_test_value = #{netTestValue},</if>
            <if test="plantName != null">plant_name = #{plantName},</if>
            <if test="regionName != null">region_name = #{regionName},</if>
            <if test="layer != null">layer = #{layer},</if>
            <if test="devName != null">dev_name = #{devName},</if>
            <if test="devCode != null">dev_code = #{devCode},</if>
            <if test="groupCode != null">group_code = #{groupCode},</if>
            <if test="extendCode != null">extend_code = #{extendCode},</if>
            <if test="pointType != null">point_type = #{pointType},</if>
            <if test="instrumentCode != null">instrument_code = #{instrumentCode},</if>
            <if test="leakagePosition != null">leakage_position = #{leakagePosition},</if>
            <if test="checker != null">checker = #{checker},</if>
            <if test="checkDate != null">check_date = #{checkDate},</if>
            <if test="leakageDegree != null">leakage_degree = #{leakageDegree},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="approveStatus != null">approve_status = #{approveStatus},</if>
            <if test="approveTime != null">approve_time = #{approveTime},</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="plantType != null">plant_type = #{plantType},</if>
            <if test="planId != null">plan_id = #{planId},</if>
        </trim>
        where check_id = #{checkId}
    </update>

    <update id="updateTCheckCheckpointsByUpload" parameterType="TCheckCheckpoints">
        update t_check_checkpoints
        <trim prefix="SET" suffixOverrides=",">
            <if test="testValue != null">test_value = #{testValue},</if>
            <if test="netTestValue != null">net_test_value = #{netTestValue},</if>
            <if test="leakagePosition != null">leakage_position = #{leakagePosition},</if>
            <if test="checker != null">checker = #{checker},</if>
            <if test="checkDate != null">check_date = #{checkDate},</if>
            <if test="instrumentCode != null">instrument_code = #{instrumentCode},</if>
            <if test="instrumentId != null">instrument_id = #{instrumentId},</if>
            <if test="leakageDegree != null">leakage_degree = #{leakageDegree},</if>
            <if test="updaterCode != null">updater_code = #{updaterCode},</if>
            <if test="updatedate != null">updatedate = #{updatedate},</if>
            <if test="approveStatus != null">approve_status = #{approveStatus},</if>
            <if test="planId != null">plan_id = #{planId},</if>
        </trim>
        where plant_name=#{plantName} and group_code=#{groupCode} and extend_code=#{extendCode} and
        inspection_id=#{inspectionId} and (approve_status is null or approve_status=0)
    </update>

    <update id="updateApproveStatusByIds" parameterType="TCheckCheckpoints">
        update t_check_checkpoints
        <trim prefix="SET" suffixOverrides=",">
            <if test="approveStatus != null">approve_status = #{approveStatus},</if>
        </trim>
        <where>
            check_id in
            <foreach item="checkId" collection="checkIds" open="(" separator="," close=")">
                #{checkId}
            </foreach>
            <if test="approveStatus ==1">
                and approve_status=0
            </if>
            <if test="approveStatus ==2">
                and approve_status=1
            </if>
            and test_value is not null
        </where>
    </update>


    <delete id="deleteTCheckCheckpointsByCheckId" parameterType="Long">
        update t_check_checkpoints
        set del_flag=1
        where check_id = #{checkId}
    </delete>

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

    <update id="updateTCheckCheckpointsByCheckIds" parameterType="TCheckCheckpoints">
        <if test="choose == 1">
            update t_check_checkpoints set inspection_id=#{inspectionId} where check_id in
            <foreach item="checkId" collection="checkIds" open="(" separator="," close=")">
                #{checkId}
            </foreach>
        </if>
        <if test="choose == 2">
            update t_check_checkpoints set inspection_id = null where check_id in
            <foreach item="checkId" collection="checkIds" open="(" separator="," close=")">
                #{checkId}
            </foreach>
        </if>
        <if test="choose == 3">
            update t_check_checkpoints set inspection_id = #{inspectionId} where check_id in
            (select t.check_id from (select check_id from t_check_checkpoints d
            <where>
                <if test="plantId != null  and plantId != ''">and d.plant_id = #{plantId}</if>
                <if test="regionId != null  and regionId != ''">and d.region_id= #{regionId}</if>
                <if test="devId != null  and devId != ''">and d.dev_id = #{devId}</if>
                and d.inspection_id is null
                <if test="repairId == null">and repair_id is null</if>
                <if test="repairId != null">and repair_id is not null</if>
                <if test="planId != null">and plan_id=#{planId}</if>
            </where>
            ) t)
        </if>
        <if test="choose == 4">
            update t_check_checkpoints set inspection_id = null where inspection_id =#{inspectionId}
        </if>
    </update>

    <delete id="deleteCheckpoints" parameterType="TCheckCheckpoints">
        <if test="choose == 2">
            delete from t_check_checkpoints where check_id in
            <foreach item="checkId" collection="checkIds" open="(" separator="," close=")">
                #{checkId}
            </foreach>
        </if>
        <if test="choose == 4">
            delete from t_check_checkpoints where plan_id =#{planId}
        </if>
    </delete>

</mapper>
