<?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.fuyuanshen.equipment.mapper.DeviceTypeMapper">
    <resultMap id="BaseResultMap" type="com.fuyuanshen.equipment.domain.DeviceType">
        <id column="grant_id" property="id"/>
        <result column="type_name" property="typeName"/>
        <result column="device_type_id" property="deviceTypeId"/>
        <result column="is_support_ble" property="isSupportBle"/>
        <result column="locate_mode" property="locateMode"/>
        <result column="network_way" property="networkWay"/>
        <result column="create_by" property="createBy"/>
        <result column="update_by" property="updateBy"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="communication_mode" property="communicationMode"/>
    </resultMap>

    <sql id="Base_Column_List">
        id
        , type_name, is_support_ble, locate_mode, network_way, create_by, update_by, create_time, update_time,communication_mode
    </sql>

    <!-- 查询所有设备类型 -->
    <select id="findAll" resultMap="BaseResultMap">
        SELECT DISTINCT dt.* ,dg.id AS grant_id ,dg.create_time AS Dcreate_time,dt.id AS device_type_id
        FROM device_type dt
        JOIN device_type_grants dg ON dt.id = dg.device_type_id
        <where>
            <if test="criteria.typeName != null and criteria.typeName.trim() != ''">
                and dt.type_name like concat('%', TRIM(#{criteria.typeName}), '%')
            </if>
            <if test="criteria.customerId != null">
                and dg.customer_id = #{criteria.customerId}
            </if>
        </where>
        ORDER BY Dcreate_time DESC
    </select>

    <!-- 获取已经分配的设备类型 -->
    <select id="getAssignType" resultType="com.fuyuanshen.equipment.domain.DeviceType">
        SELECT *
        FROM device_type
        WHERE owner_customer_id = #{customerId}
          AND original_device_id = #{deviceType}
    </select>

    <!-- 根据名称查询设备类型 -->
    <select id="queryByName" resultMap="BaseResultMap"
            parameterType="com.fuyuanshen.equipment.domain.query.DeviceTypeQueryCriteria">
        SELECT dt.*, dg.id AS grant_id
        FROM device_type dt
        JOIN device_type_grants dg ON dt.id = dg.device_type_id
        <where>
            dt.type_name = #{criteria.typeName}
            <if test="criteria.customerId != null">
                and dg.customer_id = #{criteria.customerId}
            </if>
        </where>
    </select>

    <!-- 根据名称列表查询设备类型 -->
    <select id="selectByNames" resultMap="BaseResultMap">
        SELECT dt.*
        FROM device_type dt
        WHERE dt.type_name IN
        <foreach collection="typeNames" item="name" open="(" separator="," close=")">
            #{name}
        </foreach>
    </select>

</mapper>