<?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.base.mapper.TBaseDeviceMapper">

    <resultMap type="TBaseDevice" id="TBaseDeviceResult">
        <id property="devId"    column="dev_id"    />
        <result property="plantId"    column="plant_id"    />
        <result property="plantName"    column="plant_name"    />
        <result property="plantCode"    column="plant_code"    />
        <result property="regionId"    column="region_id"    />
        <result property="regionName"    column="region_name"    />
        <result property="regionCode"    column="region_code"    />
        <result property="devCode"    column="dev_code"    />
        <result property="devDescribe"    column="dev_describe"    />
        <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="updater"    column="updater"    />
        <result property="updatedate"    column="updatedate"    />
        <result property="material"    column="material"    />
        <result property="materialStatus"    column="material_status"    />
        <result property="responseFactor"    column="response_factor"    />
        <result property="responseFactorFrom"    column="response_factor_from"    />
    </resultMap>

    <sql id="selectTBaseDeviceVo">
        select d.dev_id, d.plant_id, d.region_id, d.`dev_code`, d.`dev_describe`, d.remarks, d.approve_status, d.approve_time, d.dept_id, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate,d.material,d.material_status,d.response_factor,d.response_factor_from from t_base_device d
    </sql>

    <select id="selectTBaseDeviceList" parameterType="TBaseDevice" resultMap="TBaseDeviceResult">
        select d.dev_id, d.plant_id,bp.plant_name,bp.plant_type,bp.plant_code, d.region_id,br.region_code,br.region_name, d.dev_code, d.dev_describe, d.remarks, d.approve_status, d.approve_time, d.dept_id, d.del_flag, d.creater_code, d.createdate, d.updater_code,su.user_name updater, d.updatedate,d.material,d.material_status,d.response_factor,d.response_factor_from from t_base_device 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_base_region br on br.region_id = d.region_id
        <where>
            <if test="plantId != null "> and d.plant_id = #{plantId}</if>
            <if test="regionId != null  and regionId != ''"> and d.region_id = #{regionId}</if>
            <if test="devCode != null  and devCode != ''"> and d.dev_code = #{devCode}</if>
            <if test="devDescribe != null  and devDescribe != ''"> and d.dev_describe = #{devDescribe}</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="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>
            and d.del_flag=0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScopePlant}
    </select>

    <select id="selectAllDeviceByRegionId" parameterType="long" resultMap="TBaseDeviceResult">
        select d.dev_id, concat(d.dev_code, '+',d.dev_describe) `dev_describe` from t_base_device d where d.region_id=#{regionId} and d.del_flag=0  and d.approve_status=2
    </select>

    <select id="selectTBaseDeviceByCode" parameterType="TBasePoint" resultMap="TBaseDeviceResult">
        select d.dev_id, d.plant_id, d.region_id, d.dev_code from t_base_device d
              LEFT JOIN  t_base_region r on d.region_id = r.region_id
              LEFT JOIN  t_base_plant p on p.plant_id = d.plant_id
        where d.dev_code = #{devCode} and d.dev_describe = #{devName}  and r.region_code = #{regionCode} and p.plant_code = #{plantCode}
    </select>

    <select id="selectTBaseDeviceById" parameterType="Long" resultMap="TBaseDeviceResult">
        <include refid="selectTBaseDeviceVo"/>
        where dev_id = #{devId}
    </select>


    <select id="selectDeviceByPlantRegionAndDevice" parameterType="TBaseDevice" resultMap="TBaseDeviceResult">
        <include refid="selectTBaseDeviceVo"/>
        where plant_id=#{plantId} and region_id=#{regionId} and `dev_code`=#{devCode} and `dev_describe`=#{devDescribe} and del_flag=0
    </select>

    <insert id="insertTBaseDevice" parameterType="TBaseDevice" useGeneratedKeys="true" keyProperty="devId">
        insert into t_base_device
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="plantId != null">plant_id,</if>
            <if test="regionId != null">region_id,</if>
            <if test="devCode != null">`dev_code`,</if>
            <if test="devDescribe != null">`dev_describe`,</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="material != null">material,</if>
            <if test="materialStatus != null">material_status,</if>
            <if test="responseFactor != null">response_factor,</if>
            <if test="responseFactorFrom != null">response_factor_from,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="plantId != null">#{plantId},</if>
            <if test="regionId != null">#{regionId},</if>
            <if test="devCode != null">#{devCode},</if>
            <if test="devDescribe != null">#{devDescribe},</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="material != null">#{material},</if>
            <if test="materialStatus != null">#{materialStatus},</if>
            <if test="responseFactor != null">#{responseFactor},</if>
            <if test="responseFactorFrom != null">#{responseFactorFrom},</if>
         </trim>
    </insert>

    <update id="updateTBaseDevice" parameterType="TBaseDevice">
        update t_base_device
        <trim prefix="SET" suffixOverrides=",">
            <if test="plantId != null">plant_id = #{plantId},</if>
            <if test="regionId != null">region_id = #{regionId},</if>
            <if test="devCode != null">`dev_code` = #{devCode},</if>
            <if test="devDescribe != null">`dev_describe` = #{devDescribe},</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="material != null">material=#{material},</if>
            <if test="materialStatus != null">material_status=#{materialStatus},</if>
            <if test="responseFactor != null">response_factor=#{responseFactor},</if>
            <if test="responseFactorFrom != null">response_factor_from=#{responseFactorFrom},</if>
        </trim>
        where dev_id = #{devId}
    </update>

    <delete id="deleteTBaseDeviceById" parameterType="Long">
        update t_base_device set del_flag=1 where dev_id = #{devId}
    </delete>

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

    <update id="updateTBaseDeviceByDevIds" parameterType="TBaseDevice">
        update t_base_device set approve_status=#{approveStatus},approve_time=#{approveTime}
        <where>
            dev_id in
            <foreach item="devId" collection="devIds" open="(" separator="," close=")">
                #{devId}
            </foreach>
            <if test="approveStatus ==1">
                and approve_status=0
            </if>
            <if test="approveStatus ==2">
                and approve_status=1
            </if>
        </where>
    </update>
</mapper>
