<?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.TCheckEnvironmentMapper">

    <resultMap type="TCheckEnvironment" id="TCheckEnvironmentResult">
        <result property="id"    column="id"    />
        <result property="plantId"    column="plant_id"    />
        <result property="instrumentId"    column="instrument_id"    />
        <result property="assemblyCode"    column="assembly_code"    />
        <result property="backgroundValue"    column="background_value"    />
        <result property="ambientTemp"    column="ambient_temp"    />
        <result property="ambientHumidity"    column="ambient_humidity"    />
        <result property="atmos"    column="atmos"    />
        <result property="windDirection"    column="wind_direction"    />
        <result property="windSpeed"    column="wind_speed"    />
        <result property="tester"    column="tester"    />
        <result property="testTime"    column="test_time"    />
        <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="position1"    column="position1"    />
        <result property="position2"    column="position2"    />
        <result property="position3"    column="position3"    />
        <result property="position4"    column="position4"    />
        <result property="position5"    column="position5"    />
    </resultMap>

    <sql id="selectTCheckEnvironmentVo">
        select id, plant_id, instrument_id, assembly_code, background_value, ambient_temp, ambient_humidity, atmos, wind_direction, wind_speed, tester, test_time,position1,position2,position3,position4,position5, remarks, dept_id, del_flag, creater_code, createdate, updater_code, updatedate from t_check_environment
    </sql>

    <select id="selectTCheckEnvironmentList" parameterType="TCheckEnvironment" resultMap="TCheckEnvironmentResult">
        select d.*,su.user_name updater,bp.plant_code plantCode,bp.plant_name plantName,ci.model instrumentModel,ci.code instrumentCode from t_check_environment d
        left join sys_user su on su.user_id=d.updater_code
        left join t_base_plant bp on bp.plant_id =d.plant_id
        left join t_check_instrument ci on ci.id=d.instrument_id
        <where>
            <if test="plantId != null "> and plant_id = #{plantId}</if>
            <if test="instrumentId != null "> and instrument_id = #{instrumentId}</if>
            <if test="assemblyCode != null  and assemblyCode != ''"> and assembly_code = #{assemblyCode}</if>
            <if test="backgroundValue != null  and backgroundValue != ''"> and background_value = #{backgroundValue}</if>
            <if test="ambientTemp != null  and ambientTemp != ''"> and ambient_temp = #{ambientTemp}</if>
            <if test="ambientHumidity != null  and ambientHumidity != ''"> and ambient_humidity = #{ambientHumidity}</if>
            <if test="atmos != null  and atmos != ''"> and atmos = #{atmos}</if>
            <if test="windDirection != null  and windDirection != ''"> and wind_direction = #{windDirection}</if>
            <if test="windSpeed != null  and windSpeed != ''"> and wind_speed = #{windSpeed}</if>
            <if test="tester != null  and tester != ''"> and tester = #{tester}</if>
            <if test="testTime != null "> and test_time = #{testTime}</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="instrumentCode != null "> and ci.code = #{instrumentCode}</if>
            <if test="plantName != null "> and bp.plant_name = #{plantName}</if>
        and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScopePlant}
    </select>

    <select id="selectTCheckEnvironmentListByYear" parameterType="TCheckEnvironment" resultMap="TCheckEnvironmentResult">
        select d.*,su.user_name updater,bp.plant_code plantCode,bp.plant_name plantName,ci.model instrumentModel,ci.code instrumentCode from t_check_environment d
        left join sys_user su on su.user_id=d.updater_code
        left join t_base_plant bp on bp.plant_id =d.plant_id
        left join t_check_instrument ci on ci.id=d.instrument_id
        where d.updatedate > subdate(now(),interval 365 day) and d.del_flag = 0
        order by d.id desc
    </select>


    <select id="selectTCheckEnvironmentById" parameterType="Long" resultMap="TCheckEnvironmentResult">
        <include refid="selectTCheckEnvironmentVo"/>
        where id = #{id}
    </select>

    <insert id="insertTCheckEnvironment" parameterType="TCheckEnvironment" useGeneratedKeys="true" keyProperty="id">
        insert into t_check_environment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="plantId != null">plant_id,</if>
            <if test="instrumentId != null">instrument_id,</if>
            <if test="assemblyCode != null">assembly_code,</if>
            <if test="backgroundValue != null">background_value,</if>
            <if test="ambientTemp != null">ambient_temp,</if>
            <if test="ambientHumidity != null">ambient_humidity,</if>
            <if test="atmos != null">atmos,</if>
            <if test="windDirection != null">wind_direction,</if>
            <if test="windSpeed != null">wind_speed,</if>
            <if test="tester != null">tester,</if>
            <if test="testTime != null">test_time,</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="position1 != null">position1,</if>
            <if test="position2 != null">position2,</if>
            <if test="position3 != null">position3,</if>
            <if test="position4 != null">position4,</if>
            <if test="position5 != null">position5,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="plantId != null">#{plantId},</if>
            <if test="instrumentId != null">#{instrumentId},</if>
            <if test="assemblyCode != null">#{assemblyCode},</if>
            <if test="backgroundValue != null">#{backgroundValue},</if>
            <if test="ambientTemp != null">#{ambientTemp},</if>
            <if test="ambientHumidity != null">#{ambientHumidity},</if>
            <if test="atmos != null">#{atmos},</if>
            <if test="windDirection != null">#{windDirection},</if>
            <if test="windSpeed != null">#{windSpeed},</if>
            <if test="tester != null">#{tester},</if>
            <if test="testTime != null">#{testTime},</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="position1 != null">#{position1},</if>
            <if test="position2 != null">#{position2},</if>
            <if test="position3 != null">#{position3},</if>
            <if test="position4 != null">#{position4},</if>
            <if test="position5 != null">#{position5},</if>
         </trim>
    </insert>

    <update id="updateTCheckEnvironment" parameterType="TCheckEnvironment">
        update t_check_environment
        <trim prefix="SET" suffixOverrides=",">
            <if test="plantId != null">plant_id = #{plantId},</if>
            <if test="instrumentId != null">instrument_id = #{instrumentId},</if>
            <if test="assemblyCode != null">assembly_code = #{assemblyCode},</if>
            <if test="backgroundValue != null">background_value = #{backgroundValue},</if>
            <if test="ambientTemp != null">ambient_temp = #{ambientTemp},</if>
            <if test="ambientHumidity != null">ambient_humidity = #{ambientHumidity},</if>
            <if test="atmos != null">atmos = #{atmos},</if>
            <if test="windDirection != null">wind_direction = #{windDirection},</if>
            <if test="windSpeed != null">wind_speed = #{windSpeed},</if>
            <if test="tester != null">tester = #{tester},</if>
            <if test="testTime != null">test_time = #{testTime},</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="position1 != null">position1 = #{position1},</if>
            <if test="position2 != null">position2 = #{position2},</if>
            <if test="position3 != null">position3 = #{position3},</if>
            <if test="position4 != null">position4 = #{position4},</if>
            <if test="position5 != null">position5 = #{position5},</if>
        </trim>
        where id = #{id}
    </update>

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

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