<?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.usky.iot.mapper.BaseFacilityTypeMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.usky.iot.domain.BaseFacilityType">
        <result column="id" property="id" />
        <result column="type_code" property="typeCode" />
        <result column="type_abbrevia" property="typeAbbrevia" />
        <result column="type_name" property="typeName" />
        <result column="type_domain" property="typeDomain" />
        <result column="type_img" property="typeImg" />
        <result column="type_guise" property="typeGuise" />
        <result column="data_field" property="dataField" />
        <result column="type_desc" property="typeDesc" />
        <result column="created_by" property="createdBy" />
        <result column="created_time" property="createdTime" />
        <result column="updated_by" property="updatedBy" />
        <result column="updated_time" property="updatedTime" />
        <result column="tenant_id" property="tenantId" />
    </resultMap>
    <select id="typeNumList" resultType="com.usky.iot.service.vo.BaseGgpFacilityTypeNumVO">
        SELECT d.id,d.type_name as typeName,d.type_code as typeCode,d.type_img as typeImg,IF(facility_type IS
        NULL,0,COUNT(*)) AS
        typeFacilityNum
        FROM base_facility_type d
        LEFT JOIN base_ggp_facility e ON e.facility_type = d.type_code
        <where>
            <if test="tenantId != null and tenantId != ''">
                and e.tenant_id = #{tenantId}
            </if>
        </where>
        GROUP BY d.id
    </select>
    <select id="facilitTypeNumList" resultType="com.usky.iot.service.vo.BaseGgpFacilityTypeNumVO">
        SELECT d.id,d.type_name as typeName,d.type_code as typeCode,d.type_img as typeImg
        FROM base_facility_type d
    </select>
    <select id="typeNormalNumList" resultType="com.usky.iot.service.vo.BaseGgpFacilityTypeNumVO">
        SELECT d.id,IF(facility_type IS NULL,0,COUNT(*)) AS
        normalFacilityNum
        FROM base_facility_type d
        LEFT JOIN base_ggp_facility e ON e.facility_type = d.type_code
        <where>
            <if test="tenantId != null and tenantId != ''">
                and e.tenant_id = #{tenantId}
            </if>
                and e.facility_status = 0
        </where>
        GROUP BY d.id
    </select>
    <select id="typeUpkeepNumList" resultType="com.usky.iot.service.vo.BaseGgpFacilityTypeNumVO">
        SELECT d.id,IF(facility_type IS NULL,0,COUNT(*)) AS
        upkeepFacilityNum
        FROM base_facility_type d
        LEFT JOIN base_ggp_facility e ON e.facility_type = d.type_code
        <where>
            <if test="tenantId != null and tenantId != ''">
                and e.tenant_id = #{tenantId}
            </if>
            and e.facility_status = 1
        </where>
        GROUP BY d.id
    </select>
    <select id="typeCloseNumList" resultType="com.usky.iot.service.vo.BaseGgpFacilityTypeNumVO">
        SELECT d.id,IF(facility_type IS NULL,0,COUNT(*)) AS
        closeFacilityNum
        FROM base_facility_type d
        LEFT JOIN base_ggp_facility e ON e.facility_type = d.type_code
        <where>
            <if test="tenantId != null and tenantId != ''">
                and e.tenant_id = #{tenantId}
            </if>
            and e.facility_status = 2
        </where>
        GROUP BY d.id
    </select>
    <select id="typeList" resultType="com.usky.iot.domain.BaseFacilityType">
        SELECT id,type_code,type_name as typeName,type_abbrevia as typeAbbrevia
        FROM base_facility_type
        <where>
            <if test="typeId != null and typeId != ''">
                and type_code = #{typeId}
            </if>
        </where>
    </select>
</mapper>
