<?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.inspect.metadata.prodevmnt.mapper.BasedataDeviceTypeMapper">

    <resultMap type="BasedataDeviceType" id="BasedataDeviceTypeResult">
        <result property="deviceTypeId" column="device_type_id"/>
        <result property="parentId" column="parent_id"/>
        <result property="powerMainEquFlag" column="power_main_equ_flag"/>
        <result property="ancestors" column="ancestors"/>
        <result property="ancestorsAddr" column="ancestors_addr"/>
        <result property="deviceTypeName" column="device_type_name"/>
        <result property="deviceTypeCode" column="device_type_code"/>
        <result property="phaseNum" column="phase_num"/>
        <result property="phase" column="phase"/>
        <result property="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="professionType" column="profession_type"/>
        <result property="level" column="level"/>

    </resultMap>

    <sql id="selectBasedataDeviceTypeVo">
        select device_type_id,
               parent_id,
               power_main_equ_flag,
               ancestors,
               ancestors_addr,
               device_type_name,
               device_type_code,
               phase_num,
               phase,
               status,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time,
               profession_type,
               level
        from basedata_devicetype
    </sql>

    <select id="selectBasedataDeviceTypeList" parameterType="BasedataDeviceType" resultMap="BasedataDeviceTypeResult">
        <include refid="selectBasedataDeviceTypeVo"/>
        <where>
            <if test="parentId != null ">and parent_id = #{parentId}</if>
            <if test="powerMainEquFlag != null  and powerMainEquFlag != ''">and power_main_equ_flag =
                #{powerMainEquFlag}
            </if>
            <if test="ancestors != null  and ancestors != ''">and ancestors = #{ancestors}</if>
            <if test="ancestorsAddr != null  and ancestorsAddr != ''">and ancestors_addr = #{ancestorsAddr}</if>
            <if test="deviceTypeName != null  and deviceTypeName != ''">and device_type_name like concat('%',
                #{deviceTypeName}, '%')
            </if>
            <if test="deviceTypeCode != null  and deviceTypeCode != ''">and device_type_code = #{deviceTypeCode}</if>
            <if test="phaseNum != null  and phaseNum != ''">and phase_num = #{phaseNum}</if>
            <if test="phase != null  and phase != ''">and phase = #{phase}</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
            <if test="professionType != null  and professionType != ''">and profession_type = #{professionType}</if>
            <if test="level != null  and level != ''">and level = #{level}</if>
            <if test="deviceTypeId != null  and deviceTypeId != ''">and device_type_id = #{deviceTypeId}</if>


        </where>
    </select>

    <select id="selectBasedataDeviceTypeByDeviceTypeId" parameterType="Long" resultMap="BasedataDeviceTypeResult">
        <include refid="selectBasedataDeviceTypeVo"/>
        where device_type_id = #{deviceTypeId}
    </select>

    <insert id="insertBasedataDeviceType" parameterType="BasedataDeviceType" useGeneratedKeys="true"
            keyProperty="deviceTypeId">
        insert into basedata_devicetype
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="powerMainEquFlag != null">power_main_equ_flag,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="ancestorsAddr != null">ancestors_addr,</if>
            <if test="deviceTypeName != null">device_type_name,</if>
            <if test="deviceTypeCode != null">device_type_code,</if>
            <if test="phaseNum != null">phase_num,</if>
            <if test="phase != null">phase,</if>
            <if test="status != null">status,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="professionType != null">profession_type,</if>
            <if test="level != null">level,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="powerMainEquFlag != null">#{powerMainEquFlag},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="ancestorsAddr != null">#{ancestorsAddr},</if>
            <if test="deviceTypeName != null">#{deviceTypeName},</if>
            <if test="deviceTypeCode != null">#{deviceTypeCode},</if>
            <if test="phaseNum != null">#{phaseNum},</if>
            <if test="phase != null">#{phase},</if>
            <if test="status != null">#{status},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="professionType != null">#{professionType},</if>
            <if test="level != null">#{level},</if>
        </trim>
    </insert>

    <update id="updateBasedataDeviceType" parameterType="BasedataDeviceType">
        update basedata_devicetype
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="powerMainEquFlag != null">power_main_equ_flag = #{powerMainEquFlag},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="ancestorsAddr != null">ancestors_addr = #{ancestorsAddr},</if>
            <if test="deviceTypeName != null">device_type_name = #{deviceTypeName},</if>
            <if test="deviceTypeCode != null">device_type_code = #{deviceTypeCode},</if>
            <if test="phaseNum != null">phase_num = #{phaseNum},</if>
            <if test="phase != null">phase = #{phase},</if>
            <if test="status != null">status = #{status},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="professionType != null">profession_type = #{professionType},</if>
            <if test="level != null">level = #{level},</if>
        </trim>
        where device_type_id = #{deviceTypeId}
    </update>

    <delete id="deleteBasedataDeviceTypeByDeviceTypeId" parameterType="Long">
        delete
        from basedata_devicetype
        where device_type_id = #{deviceTypeId}
    </delete>

    <delete id="deleteBasedataDeviceTypeByDeviceTypeIds" parameterType="String">
        delete from basedata_devicetype where device_type_id in
        <foreach item="deviceTypeId" collection="array" open="(" separator="," close=")">
            #{deviceTypeId}
        </foreach>
    </delete>

    <select id="getDeviceTypeIdByDeviceTypeName" parameterType="BasedataDeviceType" resultType="map">
        SELECT
        a.device_type_id,a.parent_id,a.ancestors
        FROM
        basedata_devicetype a
        <where>
            <if test="deviceTypeName != null and deviceTypeName != ''">and device_type_name = #{deviceTypeName}</if>
            <if test="parentId != null and parentId != ''">and parent_id = #{parentId}</if>
            <if test="powerMainEquFlag != null  and powerMainEquFlag != ''">and power_main_equ_flag =
                #{powerMainEquFlag}
            </if>
        </where>
    </select>

    <select id="checkDeptNameUnique" resultMap="BasedataDeviceTypeResult">
        <include refid="selectBasedataDeviceTypeVo"/>
        where device_type_name=#{deviceTypeName} and parent_id = #{parentId} limit 1
    </select>


    <select id="getMaxDeviceTypeId" resultMap="BasedataDeviceTypeResult">
        select max(device_type_id)
        from basedata_devicetype
    </select>

    <select id="checkDeviceTypeNameUnique" resultMap="BasedataDeviceTypeResult">
        <include refid="selectBasedataDeviceTypeVo"/>
        where device_type_name=#{deviceTypeName} and parent_id = #{parentId} limit 1
    </select>

    <select id="checkDeviceTypeCodeUnique" resultMap="BasedataDeviceTypeResult">
        <include refid="selectBasedataDeviceTypeVo"/>
        <where>device_type_code=#{deviceTypeCode}
            <if test="deviceTypeId != null">and device_type_id != #{deviceTypeId}</if>
            limit 1
        </where>
    </select>
</mapper>