<?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.casic.missiles.modular.system.dao.DeviceModelsMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.casic.missiles.modular.system.model.DeviceModels">
        <id column="ID" property="id"/>
        <result column="MODEL_NAME" property="modelName"/>
        <result column="DEVICE_TYPE" property="deviceType"/>
        <result column="COMMUNICATION" property="communication"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        ID AS id, MODEL_NAME AS "modelName", DEVICE_TYPE AS "deviceType", COMMUNICATION AS communication
    </sql>

    <select id="countDeviceByModels" resultType="java.lang.Integer">
        select count(*)
        from bus_device
        where VERSION = #{modelId}
    </select>

    <select id="selectModelList" resultType="com.casic.missiles.modular.system.model.DeviceModels">
        SELECT
        bdm.ID AS "id",
        bdm.MODEL_NAME AS "modelName",
        bdm.COMMUNICATION AS "communication",
        bdm.DEVICE_TYPE AS "deviceType",
        bdt.TYPE_NAME AS "deviceTypeName"
        FROM
        base_device_models bdm
        LEFT JOIN base_device_type bdt ON bdm.DEVICE_TYPE = bdt.ID
        <where>
            <if test="deviceType != null and deviceType != ''">
                bdm.DEVICE_TYPE = #{deviceType}
            </if>
        </where>
    </select>


    <select id="getDictNameByCode" resultType="MAP">
     SELECT pid  AS "pid",
		MAX(CASE CODE  WHEN  '0'   THEN  NAME  ELSE  '0'  END) AS "0",
		MAX(CASE CODE  WHEN  '1'  THEN  NAME  ELSE  '0'  END) AS "1",
		MAX(CASE CODE  WHEN  '2'   THEN  NAME  ELSE  '0'  END)  AS "2",
		MAX(CASE CODE  WHEN  '3'   THEN  NAME  ELSE  '0'  END)  AS "3",
		MAX(CASE CODE  WHEN  '4'   THEN  NAME  ELSE  '0'  END)  AS  "4"
	FROM sys_dict 	 WHERE pid='69'
    GROUP  BY  pid
    </select>

    <sql id="fuzzy_sql" databaseId="oracle">
        '%' ||#{communication}||'%'
    </sql>

    <sql id="fuzzy_sql" databaseId="mysql">
        CONCAT('%',#{communication},'%')
    </sql>

    <sql id="fuzzy_sql" databaseId="pgsql">
        CONCAT('%',#{communication},'%')
    </sql>

    <select id="getListPage" resultType="com.casic.missiles.modular.system.model.DeviceModels">
         SELECT   ID AS id,
                  MODEL_NAME AS "modelName",
                  COMMUNICATION AS communication,
                  DEVICE_TYPE AS "deviceType"
         FROM base_device_models
        WHERE 1=1
        <if test="deviceType != -1">
            AND DEVICE_TYPE= #{deviceType}
        </if>
        <if test="communication != null and communication !=''">
            AND COMMUNICATION  like   <include refid="fuzzy_sql"/>
        </if>
    </select>

    <select id="getDeviceTypeList" resultType="MAP">
        SELECT  id AS  "id",type_name AS  "typeName"
        FROM base_device_type
    </select>

</mapper>
