<?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.hw.dms.mapper.DmsBaseDeviceTypeMapper">

    <resultMap type="DmsBaseDeviceType" id="DmsBaseDeviceTypeResult">
        <result property="deviceTypeId"    column="device_type_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="typeCode"    column="type_code"    />
        <result property="typeName"    column="type_name"    />
        <result property="isFlag"    column="is_flag"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectDmsBaseDeviceTypeVo">
        select device_type_id, parent_id, type_code, type_name, is_flag, ancestors, remark, create_by, create_time, update_by, update_time from dms_base_device_type
    </sql>

    <select id="selectDmsBaseDeviceTypeList" parameterType="DmsBaseDeviceType" resultMap="DmsBaseDeviceTypeResult">
        <include refid="selectDmsBaseDeviceTypeVo"/>
        <where>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="typeCode != null  and typeCode != ''"> and type_code = #{typeCode}</if>
            <if test="typeName != null  and typeName != ''"> and type_name like concat('%', #{typeName}, '%')</if>
            <if test="isFlag != null "> and is_flag = #{isFlag}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
        </where>
    </select>

    <select id="selectDmsBaseDeviceTypeByDeviceTypeId" parameterType="Long" resultMap="DmsBaseDeviceTypeResult">
        <include refid="selectDmsBaseDeviceTypeVo"/>
        where device_type_id = #{deviceTypeId}
    </select>

    <insert id="insertDmsBaseDeviceType" parameterType="DmsBaseDeviceType" useGeneratedKeys="true" keyProperty="deviceTypeId">
        insert into dms_base_device_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="typeCode != null and typeCode != ''">type_code,</if>
            <if test="typeName != null and typeName != ''">type_name,</if>
            <if test="isFlag != null">is_flag,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="remark != null">remark,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="typeCode != null and typeCode != ''">#{typeCode},</if>
            <if test="typeName != null and typeName != ''">#{typeName},</if>
            <if test="isFlag != null">#{isFlag},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="remark != null">#{remark},</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>
         </trim>
    </insert>

    <update id="updateDmsBaseDeviceType" parameterType="DmsBaseDeviceType">
        update dms_base_device_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="typeCode != null and typeCode != ''">type_code = #{typeCode},</if>
            <if test="typeName != null and typeName != ''">type_name = #{typeName},</if>
            <if test="isFlag != null">is_flag = #{isFlag},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="remark != null">remark = #{remark},</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>
        </trim>
        where device_type_id = #{deviceTypeId}
    </update>

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

    <delete id="deleteDmsBaseDeviceTypeByDeviceTypeIds" parameterType="String">
        delete from dms_base_device_type where device_type_id in
        <foreach item="deviceTypeId" collection="array" open="(" separator="," close=")">
            #{deviceTypeId}
        </foreach>
    </delete>
    <select id="selectDevicetypeByDeviceId" parameterType="Long" resultMap="DmsBaseDeviceTypeResult">
        select a.device_type_id, a.parent_id, a.type_code, a.type_name, a.is_flag, a.ancestors, a.remark, a.create_by, a.create_time, a.update_by, a.update_time
        from dms_base_device_type a
        left join dms_base_device_ledger b on a.device_type_id = b.device_type_id
        <where>
            <if test="deviceId != null  and deviceId != ''"> and b.device_id = #{deviceId}</if>
        </where>
    </select>
</mapper>
