<?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.persagy.dmp.rwd.digital.dao.ObjectDigitalMapper">

    <resultMap id="BaseResultMap" type="com.persagy.dmp.digital.entity.ObjectDigital">
            <id property="id" column="id" jdbcType="VARCHAR"/>
            <result property="name" column="name" jdbcType="VARCHAR"/>
            <result property="localId" column="local_id" jdbcType="VARCHAR"/>
            <result property="localName" column="local_name" jdbcType="VARCHAR"/>
            <result property="groupCode" column="group_code" jdbcType="VARCHAR"/>
            <result property="projectId" column="project_id" jdbcType="VARCHAR"/>
            <result property="objType" column="obj_type" jdbcType="VARCHAR"/>
            <result property="classCode" column="class_code" jdbcType="VARCHAR"/>
            <result property="grouping" column="grouping" jdbcType="INTEGER"/>
            <result property="sequenceId" column="sequenceId" jdbcType="INTEGER"/>
            <result property="infos" column="infos" jdbcType="VARCHAR" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
            <result property="virtualCodes" column="virtual_codes" jdbcType="VARCHAR" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
            <result property="createApp" column="create_app" jdbcType="VARCHAR"/>
            <result property="updateApp" column="update_app" jdbcType="VARCHAR"/>
            <result property="creator" column="creator" jdbcType="VARCHAR"/>
            <result property="creationTime" column="creation_time" jdbcType="CHAR"/>
            <result property="modifier" column="modifier" jdbcType="VARCHAR"/>
            <result property="modifiedTime" column="modified_time" jdbcType="CHAR"/>
            <result property="valid" column="valid" jdbcType="TINYINT"/>
            <result property="ts" column="ts" jdbcType="TIMESTAMP"/>
            <result property="modelId" column="modelId" jdbcType="VARCHAR"/>
            <result property="objFromIds" column="objFromIds" jdbcType="VARCHAR" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
            <result property="objToIds" column="objToIds" jdbcType="VARCHAR" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
            <result property="relObjs" column="relObjs" jdbcType="VARCHAR"  typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
            <result property="bimLocation" column="bimLocation" jdbcType="VARCHAR"  typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
            <result property="outLines" column="outLines" jdbcType="VARCHAR"  typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
            <result property="objTypeClassCodes" column="objTypeClassCodes" jdbcType="VARCHAR"  typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
    </resultMap>
    <resultMap id="count" type="com.persagy.dmp.digital.entity.ObjectDigital">
        <result property="totalCount" column="totalCount" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,`name`,local_id,
        local_name,group_code,project_id,
        obj_type,class_code,grouping,
        infos,virtual_codes,create_app,
        update_app,creator,creation_time,
        modifier,modified_time,valid,
        ts
    </sql>

    <sql id="commonJoinQueryParam">
        <if test="param.projectId!=null and param.projectId.length>0">
            AND dto.project_id=#{param.projectId}
        </if>
        <if test="param.objTypes!=null and param.objTypes.size()>0">
            <foreach collection="param.objTypes" open="AND dto.obj_type IN(" close=")" separator="," item="objType">
                #{objType}
            </foreach>
        </if>
        <if test="param.classCodes!=null and param.classCodes.size()>0">
            <foreach collection="param.classCodes" open="AND ( " close=")" separator=" OR " item="classCode">
                dto.class_code LIKE CONCAT(#{classCode},'%')
            </foreach>
        </if>
        <if test="param.keyword!=null and param.keyword.length>0">
            AND dto.local_name LIKE CONCAT('%',#{param.keyword},'%')
        </if>
        <choose>
            <when test="param.validSet!=null and param.validSet.size()>0">
                <foreach collection="param.validSet" open="AND dto.valid IN(" close=")" separator="," item="valid">
                    #{valid}
                </foreach>
            </when>
            <otherwise>
                AND dto.valid = TRUE
            </otherwise>
        </choose>
        <choose>
            <when test="param.createTimeScope!=null">
                <if test="param.createTimeScope.startEq!=null and param.createTimeScope.startEq.length>0">
                    AND dto.creation_time&gt;=#{param.createTimeScope.startEq}
                </if>
                <if test="param.createTimeScope.endEq!=null and param.createTimeScope.endEq.length>0">
                    AND dto.creation_time&lt;=#{param.createTimeScope.endEq}
                </if>
            </when>
        </choose>
    </sql>
    <sql id="queryObjByRel">
        (
        <include refid="commonQueryObjByRel">
            <property name="param" value="#{param}"/>
        </include>
        )
        <include refid="queryObjDataUnionEcData">
            <property name="param" value="#{param}"/>
        </include>
    </sql>
    <sql id="commonQueryObjByRel">
        (
        <include refid="commonQueryObjByRelSubTo">
            <property name="param" value="#{param}"/>
        </include>
        <if test="param.relCodes!=null and param.relCodes.size()>0">
            <foreach collection="param.relCodes" open="AND dtr.rel_code IN (" close=")" separator="," item="relCode">
                #{relCode}
            </foreach>
        </if>
        GROUP BY dto.id
        )
        UNION
        (
        <include refid="commonQueryObjByRelSubFrom">
            <property name="param" value="#{param}"/>
        </include>
        <if test="param.relCodes!=null and param.relCodes.size()>0">
            <foreach collection="param.relCodes" open="AND dtr.rel_code IN (" close=")" separator="," item="relCode">
                #{relCode}
            </foreach>
        </if>
        GROUP BY dto.id
        )

    </sql>
    <sql id="commonQueryObjByRelSubTo">
        SELECT DISTINCT
        dto.*,JSON_ARRAYAGG(dtr.obj_from) AS objFromIds,JSON_ARRAYAGG(dtr.obj_to) AS objToIds
        FROM dt_object AS dto
        LEFT JOIN (SELECT * FROM dt_relation AS dtrA WHERE dtrA.obj_to=#{param.filterObjId} OR
        dtrA.obj_from=#{param.filterObjId} AND dtrA.valid=TRUE) AS dtr ON dtr.obj_to=dto.id
        WHERE 1=1 AND dtr.valid=TRUE
        <include refid="commonJoinQueryParam">
            <property name="param" value="#{param}"/>
        </include>
    </sql>
    <sql id="commonQueryObjByRelSubFrom">
        SELECT DISTINCT
        dto.*,JSON_ARRAYAGG(dtr.obj_from) AS objFromIds,JSON_ARRAYAGG(dtr.obj_to) AS objToIds
        FROM dt_object AS dto
        LEFT JOIN (SELECT * FROM dt_relation AS dtrA WHERE dtrA.obj_to=#{param.filterObjId} OR dtrA.obj_from=#{param.filterObjId} AND dtrA.valid=TRUE) AS dtr ON dtr.obj_from=dto.id
        WHERE 1=1
        <include refid="commonJoinQueryParam">
            <property name="param" value="#{param}"/>
        </include>
    </sql>
    <sql id="queryObjDataUnionEcData">
        <if test="param.queryEcFlag!=null and param.queryEcFlag==true">
            UNION
            (<include refid="commonQueryObjByRelSubTo">
                <property name="param" value="#{param}"/>
            </include>
            <if test="param.ecRelCodes!=null and param.ecRelCodes.size()>0">
                <foreach collection="param.ecRelCodes" open="AND dtr.rel_code IN (" close=")" separator="," item="relCode">
                    #{relCode}
                </foreach>
            </if>
            AND dtr.obj_from IN(
                SELECT DISTINCT
                dto.id
                FROM dt_relation AS dtr
                LEFT JOIN dt_object AS dto ON (dtr.obj_from=dto.id OR dtr.obj_to=dto.id AND dtr.valid=TRUE)
                WHERE 1=1
                <include refid="commonJoinQueryParam">
                    <property name="param" value="#{param}"/>
                </include>
                AND dto.obj_type='equipment'
            ) GROUP BY dto.id)
        </if>
    </sql>

    <sql id="commonQueryObjectListByParam">
        SELECT DISTINCT
        dto.*,
        JSON_ARRAYAGG( dtr.obj_from ) AS objFromIds,
        JSON_ARRAYAGG( dtr.obj_to ) AS objToIds
        FROM
        dt_object AS dto
        INNER JOIN ( SELECT * FROM dt_relation AS dtrA WHERE 1=1
        <if test="null!=param.relId and param.relId.length>0">
            AND (dtrA.obj_from = #{param.relId} OR dtrA.obj_to=#{param.relId})
        </if>
        <if test="null!=param.projectId and param.projectId.length>0">
            AND dtrA.project_id = #{param.projectId}
        </if>
        <if test="null!=param.relCode and param.relCode.length>0">
            AND dtrA.rel_code = #{param.relCode}
        </if>
        <if test="null!=param.groupCode and param.groupCode.length>0">
            AND dtrA.group_code = #{param.groupCode}
        </if>
        ) AS dtr ON (dtr.obj_to = dto.id OR dtr.obj_from = dto.id)
        <include refid="commonQueryObjectListByParamDtObjectWhereCondition">
            <property name="param" value="#{param}"/>
        </include>


    </sql>
    <sql id="commonQueryObjectListByParamDtObjectWhereCondition">
        WHERE
        1 = 1
        <if test="null!=param.projectId and param.projectId.length>0">
            AND dto.project_id = #{param.projectId}
        </if>
        <if test="null!=param.objType and param.objType.length>0">
            AND dto.obj_type = #{param.objType}
        </if>
        <if test="null!=param.keyword and param.keyword.length>0">
            AND dto.local_name LIKE CONCAT('%',#{param.keyword},'%')
        </if>
        <if test="null!=param.groupCode and param.groupCode.length>0">
            AND dto.group_code = #{param.groupCode}
        </if>
        AND dto.valid = TRUE
    </sql>

    <select id="queryObjectDigitalsByProjectId" resultMap="BaseResultMap">
        SELECT DISTINCT *
        FROM dt_object dto
        WHERE 1=1
         <include refid="commonJoinQueryParam">
             <property name="param" value="#{param}"/>
         </include>
    </select>
    <select id="queryObjectListBySuperiorId" resultMap="BaseResultMap">
        SELECT DISTINCT dto.*
        FROM
        (
        <include refid="queryObjByRel">
            <property name="param" value="#{param}"/>
        </include>
        ) AS dto
    </select>
    <select id="queryObjectListByFloorIdAndRoomFuncType" resultMap="BaseResultMap">
        <include refid="commonQueryObjectListByParam">
            <property name="param" value="#{param}"/>
        </include>
        <if test="null!=param.roomFuncType and param.roomFuncType.length>0">
            AND JSON_UNQUOTE(JSON_EXTRACT(dto.infos, '$.roomFuncType'))=#{param.roomFuncType}
        </if>
        GROUP BY dto.id
    </select>
    <select id="queryObjectListByRelIdAndClassCode" resultMap="BaseResultMap">
        SELECT DISTINCT
        dto.*,
        JSON_ARRAYAGG( dtr.obj_from ) AS objFromIds,
        JSON_ARRAYAGG( dtr.obj_to ) AS objToIds
        FROM
        dt_object AS dto
        INNER JOIN ( SELECT * FROM dt_relation AS dtrA WHERE 1=1
        <if test="null!=param.relId and param.relId.length>0">
            AND (dtrA.obj_from = #{param.relId} OR dtrA.obj_to=#{param.relId})
        </if>
        <if test="null!=param.projectId and param.projectId.length>0">
            AND dtrA.project_id = #{param.projectId}
        </if>
        <if test="null!=param.relCode and param.relCode.length>0">
            AND dtrA.rel_code = #{param.relCode}
        </if>
        <if test="null!=param.groupCode and param.groupCode.length>0">
            AND dtrA.group_code = #{param.groupCode}
        </if>
        ) AS dtr ON (dtr.obj_to = dto.id OR dtr.obj_from = dto.id)
        <include refid="commonQueryObjectListByParamDtObjectWhereCondition">
            <property name="param" value="#{param}"/>
        </include>
        <if test="null!=param.classCode and param.classCode.length>0">
            AND dto.class_code=#{param.classCode}
        </if>
        GROUP BY dto.id
    </select>
    <select id="queryEquipListByClassCode" resultMap="BaseResultMap">
        SELECT DISTINCT
        dto.*
        FROM
        dt_object AS dto
        <include refid="commonQueryObjectListByParamDtObjectWhereCondition">
            <property name="param" value="#{param}"/>
        </include>
        <if test="null!=param.classCode and param.classCode.length>0">
            AND dto.class_code LIKE CONCAT(#{param.classCode},'%')
        </if>
        GROUP BY dto.id
    </select>

    <sql id="queryMasterObjSql">
        FROM
        dt_object AS dobj
        WHERE
        1=1
        <choose>
            <when test='null!=param.type and param.type=="id" and null!=param.mainContent and param.mainContent.length>0'>
                AND dobj.id=#{param.mainContent}
            </when>
            <when test='null!=param.type and param.type=="name" and null!=param.mainContent and param.mainContent.length>0'>
                AND dobj.name=#{param.mainContent}
            </when>
            <when test='null!=param.type and param.type=="localId" and null!=param.mainContent and param.mainContent.length>0'>
                AND dobj.local_id=#{param.mainContent}
            </when>
            <when test='null!=param.type and param.type=="localName" and null!=param.mainContent and param.mainContent.length>0'>
                AND dobj.local_name=#{param.mainContent}
            </when>
            <when test='null!=param.type and param.type=="cadId" and null!=param.mainContent and param.mainContent.length>0'>
                AND JSON_UNQUOTE(JSON_EXTRACT(dobj.infos, '$.cADID')) =#{param.mainContent}
            </when>
        </choose>
        <if test="null!=param.groupCode and param.groupCode.length>0">
            AND dobj.group_code=#{param.groupCode}
        </if>
        <if test="null!=param.projectId and param.projectId.length>0">
            AND dobj.project_id=#{param.projectId}
        </if>
        <if test="null!=param.masterObjType and param.masterObjType.length>0">
            AND dobj.obj_type=#{param.masterObjType}
        </if>
        AND dobj.valid=TRUE
    </sql>

    <select id="queryObjectListByIdOrNameOrCadId" resultType="com.persagy.dmp.digital.entity.ObjectDigital">
        SELECT
            dobj.*,
            true AS mainFlag,
            IFNULL(JSON_UNQUOTE(JSON_EXTRACT(dobj.infos, '$.cADID')),'') AS cadId
        <include refid="queryMasterObjSql">
            <property name="param" value="#{param}"/>
        </include>
        UNION
        SELECT
        dobj.*,
        false AS mainFlag,
        IFNULL(JSON_UNQUOTE(JSON_EXTRACT(dobj.infos, '$.cADID')),'') AS cadId
        FROM
        dt_object AS dobj
        WHERE
        1=1
        <choose>
            <when test='null!=param.type and param.type=="id" and null!=param.slaveContent and param.slaveContent.size()>0'>
                <foreach collection="param.slaveContent" open="AND dobj.id IN(" close=")" separator="," item="keyword">
                    #{keyword}
                </foreach>
            </when>
            <when test='null!=param.type and param.type=="name" and null!=param.slaveContent and param.slaveContent.size()>0'>
                <foreach collection="param.slaveContent" open="AND dobj.name IN(" close=")" separator="," item="keyword">
                    #{keyword}
                </foreach>
            </when>
            <when test='null!=param.type and param.type=="localId" and null!=param.slaveContent and param.slaveContent.size()>0'>
                <foreach collection="param.slaveContent" open="AND dobj.local_id IN(" close=")" separator="," item="keyword">
                    #{keyword}
                </foreach>
            </when>
            <when test='null!=param.type and param.type=="localName" and null!=param.slaveContent and param.slaveContent.size()>0'>
                <foreach collection="param.slaveContent" open="AND dobj.local_name IN(" close=")" separator="," item="keyword">
                    #{keyword}
                </foreach>
            </when>
            <when test='null!=param.type and param.type=="cadId" and null!=param.slaveContent and param.slaveContent.size()>0'>
                <foreach collection="param.slaveContent" open="AND JSON_UNQUOTE(JSON_EXTRACT(dobj.infos, '$.cADID')) IN(" close=")" separator="," item="keyword">
                    #{keyword}
                </foreach>
            </when>
        </choose>
        <if test="null!=param.groupCode and param.groupCode.length>0">
            AND dobj.group_code=#{param.groupCode}
        </if>
        <if test="null!=param.projectId and param.projectId.length>0">
            AND dobj.project_id=#{param.projectId}
        </if>
        <if test="null!=param.slaveObjType and param.slaveObjType.length>0">
            AND dobj.obj_type=#{param.slaveObjType}
        </if>
        AND dobj.id NOT IN(SELECT id <include refid="queryMasterObjSql"><property name="param" value="#{param}"/></include>)
        <if test="null!=param.hasFilterClassCode and param.hasFilterClassCode">
            AND dobj.class_code IN(SELECT class_code <include refid="queryMasterObjSql"><property name="param" value="#{param}"/></include>)
        </if>
        AND dobj.valid=TRUE
    </select>
    <select id="queryObjectListByGraphCodeAndRelCode" resultMap="BaseResultMap">
        SELECT DISTINCT
        dtoA.*,
        IF(COUNT( dtoB.id )> 0,
            JSON_ARRAYAGG(
            JSON_OBJECT(
            'id',
            dtoB.id,
            'name',
            IFNULL( dtoB.`name`, '' ),
            'localId',
            IFNULL( dtoB.local_id, '' ),
            'localName',
            IFNULL( dtoB.local_name, '' ),
            'infos',
            IFNULL(dtoB.infos,JSON_OBJECT()),
            'groupCode',
            IFNULL( dtoB.group_code, '' ),
            'projectId',
            IFNULL( dtoB.project_id, '' ),
            'objType',
            IFNULL( dtoB.obj_type, '' ),
            'classCode',
            IFNULL( dtoB.class_code, '' ),
            'grouping',
            IFNULL( dtoB.grouping, '' ),
            'valid',
            IFNULL( dtoB.valid, 1 ))),
        JSON_ARRAY())  AS relObjs
        FROM
        dt_object dtoA
        INNER JOIN dt_relation dtr ON ( dtoA.id = dtr.obj_from AND dtr.valid = TRUE )
        LEFT JOIN dt_object dtoB ON ( dtr.obj_to = dtoB.id AND dtr.valid = TRUE )
        WHERE
        dtoA.valid=TRUE AND dtoB.valid=TRUE
        <if test="null!=param.relCode and param.relCode.length>0">
            AND dtr.rel_code = #{param.relCode}
        </if>
        <if test="null!=param.graphCode and param.graphCode.length>0">
            AND dtr.graph_code = #{param.graphCode}
        </if>
        <if test="null!=param.relValue and param.relValue.length>0">
            AND dtr.rel_value = #{param.relValue}
        </if>
        <if test="null!=param.projectId and param.projectId.length>0">
            AND dtr.project_id = #{param.projectId}
        </if>
        <if test="null!=param.groupCode and param.groupCode.length>0">
            AND dtr.group_code = #{param.groupCode}
        </if>
        <if test="null!=param.mainContent and param.mainContent.length>0">
            AND (
            dtoA.id = #{param.mainContent}
                OR dtoA.`name` LIKE CONCAT( '%', #{param.mainContent}, '%' )
                OR dtoA.local_name LIKE CONCAT( '%', #{param.mainContent}, '%' )
                OR dtoA.local_id LIKE CONCAT( '%', #{param.mainContent}, '%' )
                OR JSON_EXTRACT(dtoA.infos, '$.cADID' ) LIKE CONCAT( '%', #{param.mainContent}, '%' )
            )
        </if>
        <if test="null!=param.slaveContent and param.slaveContent.size()>0">
            <foreach collection="param.slaveContent" item="keyword" open="AND (" close=")" separator=" OR ">
                dtoB.id = #{keyword}
                OR dtoB.`name` LIKE CONCAT( '%', #{keyword}, '%' )
                OR dtoB.local_name LIKE CONCAT( '%', #{keyword}, '%' )
                OR dtoB.local_id LIKE CONCAT( '%', #{keyword}, '%' )
            </foreach>
        </if>
        GROUP BY
        dtoA.id
    </select>

    <select id="queryToData" resultMap="BaseResultMap">
        SELECT
            id,
            group_code,
            project_id,
            JSON_UNQUOTE(
                JSON_EXTRACT(
                    JSON_UNQUOTE( JSON_EXTRACT( infos, CONCAT('$.',#{toInfoCode}) ) ),
                IF
                    (
                        JSON_DEPTH( JSON_UNQUOTE( JSON_EXTRACT( infos, CONCAT('$.',#{toInfoCode}) ) ) ) = 3,
                        '$[*]',
                    IF
                        ( JSON_DEPTH( JSON_UNQUOTE( JSON_EXTRACT( infos, CONCAT('$.',#{toInfoCode}) ) ) ) = 4, '$[0][*]', '$[0][0][*]' )
                    )
                )
            ) AS outLines,
            class_code,
            obj_type
        FROM
            dt_object
        WHERE
            valid = TRUE
        <if test="null!=toIds and toIds.size>0">
            <foreach collection="toIds" open="AND id IN (" close=")" item="toId" separator=",">
                #{toId}
            </foreach>
        </if>
        <if test="null!=param.projectId and param.projectId.length>0">
            AND project_id = #{param.projectId}
        </if>
        <if test="null!=toObjType and toObjType.length>0">
            AND obj_type = #{toObjType}
        </if>
        <if test="null!=param.groupCode and param.groupCode.length>0">
            AND group_code = #{param.groupCode}
        </if>
        ;
    </select>
    <select id="queryFromData" resultMap="BaseResultMap">
        SELECT
            id,
            group_code,
            project_id,
            JSON_OBJECT(
                'X',
                JSON_EXTRACT(
                    JSON_UNQUOTE(
                        CONCAT(
                            '[',
                            SUBSTRING(JSON_EXTRACT(infos, CONCAT('$.',#{fromInfoCode})) FROM 2 FOR LENGTH(JSON_EXTRACT(infos, CONCAT('$.',#{fromInfoCode})))-2),
                            ']'
                        )
                    ),
                    '$[0]'
                ),
                'Y',
                JSON_EXTRACT(
                    JSON_UNQUOTE(
                        CONCAT(
                            '[',
                            SUBSTRING(JSON_EXTRACT(infos, CONCAT('$.',#{fromInfoCode})) FROM 2 FOR LENGTH(JSON_EXTRACT(infos, CONCAT('$.',#{fromInfoCode})))-2),
                            ']'
                        )
                    ),
                    '$[1]'
                )
            ) AS bimLocation,
            class_code,
            obj_type
        FROM
            dt_object
        WHERE
           valid = TRUE
        <if test="null!=fromIds and fromIds.size>0">
            <foreach collection="fromIds" open="AND id IN (" close=")" item="fromId" separator=",">
                #{fromId}
            </foreach>
        </if>
        <if test="null!=param.projectId and param.projectId.length>0">
            AND project_id = #{param.projectId}
        </if>
        <if test="null!=fromObjType and fromObjType.length>0">
            AND obj_type = #{fromObjType}
        </if>
        <if test="null!=param.groupCode and param.groupCode.length>0">
            AND group_code = #{param.groupCode}
        </if>
    </select>
    <select id="queryObjectListByIds" resultMap="BaseResultMap">
        SELECT
            dto.*,
            JSON_ARRAYAGG( dtdt.`code` ) AS objTypeClassCodes
        FROM
            dt_object AS dto
            LEFT JOIN dt_define_type AS dtdt ON ( dtdt.obj_type = dto.obj_type AND dtdt.valid = TRUE )
        WHERE 1=1
        <foreach collection="objIds" item="objId" open=" AND dto.id IN(" close=")" separator=",">
            #{objId}
        </foreach>
            AND dto.valid = TRUE
        GROUP BY
            dto.id;
    </select>
    <resultMap id="SpaceCalResultMap" type="com.persagy.dmp.rwd.digital.domain.SpaceCalDTO">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="modelId" column="modelId" jdbcType="VARCHAR"/>
        <result property="firstPoint" column="firstPoint" jdbcType="VARCHAR"  typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
        <result property="secondPoint" column="secondPoint" jdbcType="VARCHAR" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
        <result property="outLines" column="outLines" jdbcType="VARCHAR" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
    </resultMap>
    <select id="queryEquipmentsByModelIds" resultMap="SpaceCalResultMap">
        SELECT
            id,
            JSON_UNQUOTE(JSON_EXTRACT( dto.infos, '$.modelId' )) AS modelId,
            JSON_UNQUOTE(JSON_EXTRACT(JSON_UNQUOTE(JSON_EXTRACT(infos, '$.outline')), '$[0][0]')) AS firstPoint,
            JSON_UNQUOTE(JSON_EXTRACT(JSON_UNQUOTE(JSON_EXTRACT(infos, '$.outline')), '$[1][0]')) AS secondPoint
        FROM
            dt_object AS dto
        WHERE
            JSON_EXTRACT( dto.infos, '$.outline' ) IS NOT NULL
          AND JSON_EXTRACT( dto.infos, '$.modelId' ) IS NOT NULL
          AND dto.obj_type = 'equipment'
          AND dto.valid=TRUE
        <if test="null!=param.groupCode and param.groupCode.length>0">
            AND dto.group_code = #{param.groupCode}
        </if>
        <if test="null!=param.projectId and param.projectId.length>0">
            AND dto.project_id = #{param.projectId}
        </if>
        <if test="null!=classCode and classCode.length>0">
            AND dto.class_code = #{classCode}
        </if>
        <if test="null!=objIds and objIds.size()>0">
            <foreach collection="objIds" open="AND JSON_EXTRACT( dto.infos, '$.modelId' ) IN (" close=")" separator="," item="objId">
                #{objId}
            </foreach>
        </if>
        ;
    </select>
    <select id="queryIspacesByModelIds" resultMap="SpaceCalResultMap">
        SELECT
        id,
        JSON_UNQUOTE(JSON_EXTRACT( dto.infos, '$.modelId' )) AS modelId,
        JSON_UNQUOTE(JSON_EXTRACT(JSON_UNQUOTE(JSON_EXTRACT(infos, '$.outline')), '$[0][*]')) AS outLines
        FROM
        dt_object AS dto
        WHERE
        JSON_EXTRACT( dto.infos, '$.outline' ) IS NOT NULL
        AND JSON_EXTRACT( dto.infos, '$.modelId' ) IS NOT NULL
        AND dto.obj_type = 'ispace'
        AND dto.class_code = 'ispace'
        AND dto.valid=TRUE
        <if test="null!=param.groupCode and param.groupCode.length>0">
            AND dto.group_code = #{param.groupCode}
        </if>
        <if test="null!=param.projectId and param.projectId.length>0">
            AND dto.project_id = #{param.projectId}
        </if>
        <if test="null!=objIds and objIds.size()>0">
            <foreach collection="objIds" open="AND JSON_EXTRACT( dto.infos, '$.modelId' ) IN (" close=")" separator="," item="objId">
                #{objId}
            </foreach>
        </if>
        ;
    </select>

    <resultMap id="FloorResultMap" type="com.persagy.dmp.rwd.digital.domain.FloorCalDTO">
        <result property="floorId" column="floorId" jdbcType="VARCHAR"/>
        <result property="floorName" column="floorName" jdbcType="VARCHAR"/>
        <result property="floorLocalId" column="floorLocalId" jdbcType="VARCHAR"/>
        <result property="floorLocalName" column="floorLocalName" jdbcType="VARCHAR"/>
        <result property="modelId" column="modelId" jdbcType="VARCHAR"/>
        <result property="groupCode" column="groupCode" jdbcType="VARCHAR"/>
        <result property="projectId" column="projectId" jdbcType="VARCHAR"/>
        <result property="sequenceId" column="sequenceId" jdbcType="VARCHAR"/>
        <result property="buildingId" column="buildingId" jdbcType="VARCHAR"/>
        <result property="classCode" column="classCode" jdbcType="VARCHAR"/>
        <result property="objType" column="objType" jdbcType="VARCHAR"/>
    </resultMap>

    <select id="queryFloorsByProjectId" resultMap="FloorResultMap">
        SELECT
            dto.id AS floorId,
            dto.name AS floorName,
            dto.local_id AS floorLocalId,
            dto.local_name AS floorLocalName,
            JSON_UNQUOTE(JSON_EXTRACT(dto.infos, '$.modelId')) AS modelId,
            dto.group_code AS groupCode,
            dto.project_id AS projectId,
            JSON_EXTRACT(dto.infos, '$.floorSequenceID') AS sequenceId,
            dtr.obj_from AS buildingId,
            dto.obj_type AS objType,
            dto.class_code AS classCode
        FROM
            dt_object AS dto
                LEFT JOIN dt_relation AS dtr ON(dto.id=dtr.obj_to AND dtr.graph_code='ArchSubset' AND dtr.rel_code='Bd2Fl')
        WHERE
            dto.class_code='floor'
          AND JSON_UNQUOTE(JSON_EXTRACT(dto.infos, '$.modelId')) IS NOT NULL
        <if test="null!=param.projectId and param.projectId.length>0">
            AND dto.project_id = #{param.projectId}
        </if>
        <if test="null!=param.groupCode and param.groupCode.length>0">
            AND dto.group_code = #{param.groupCode}
        </if>
        ORDER BY JSON_EXTRACT(dto.infos, '$.floorSequenceID') DESC
    </select>
    <select id="queryConnectors" resultMap="BaseResultMap">
        SELECT
            *
        FROM
            dt_object AS dto
        WHERE dto.obj_type='equipment'
            AND dto.class_code='OTMCCT'
        <if test="null!=modelId and modelId.length>0">
            AND JSON_UNQUOTE(JSON_EXTRACT(dto.infos, '$.modelId')) = #{modelId}
        </if>
        <if test="null!=useType and useType.length>0">
            AND JSON_UNQUOTE(JSON_EXTRACT(dto.infos, '$.useType')) = #{useType}
        </if>
        <if test="null!=projectId and projectId.length>0">
            AND dto.project_id = #{projectId}
        </if>
        <if test="null!=groupCode and groupCode.length>0">
            AND dto.group_code = #{groupCode}
        </if>
    </select>
    <select id="queryModelElements" resultType="com.persagy.dmp.digital.entity.ObjectDigital">
        SELECT
        *
        FROM
        dt_object AS dto
        WHERE dto.obj_type='equipment'
        AND dto.class_code IN('OTMCEM','OTMCCN','OTMCOT','OTMCJO','OTMCPL','OTMCDU')
        <if test="null!=modelId and modelId.length>0">
            AND JSON_UNQUOTE(JSON_EXTRACT(dto.infos, '$.modelId')) = #{modelId}
        </if>
        <if test="null!=projectId and projectId.length>0">
            AND dto.project_id = #{projectId}
        </if>
        <if test="null!=groupCode and groupCode.length>0">
            AND dto.group_code = #{groupCode}
        </if>
    </select>

    <select id="queryObjectClassCode" resultType="java.lang.String">
        SELECT DISTINCT class_code FROM dt_object AS dto
        WHERE dto.valid = TRUE
        <if test="null!=param.projectId and param.projectId.length>0">
            AND dto.project_id = #{param.projectId}
        </if>
        <if test="null!=param.groupCode and param.groupCode.length>0">
            AND dto.group_code = #{param.groupCode}
        </if>
        <if test="null!=param.buildingId and param.buildingId.length>0">
            AND JSON_UNQUOTE(JSON_EXTRACT(dto.infos, '$.buildingId')) = #{param.buildingId}
        </if>
        <if test="null!=param.floorId and param.floorId.length>0">
            AND JSON_UNQUOTE(JSON_EXTRACT(dto.infos, '$.floorId')) = #{param.floorId}
        </if>
        <if test="null!=param.objType and param.objType.length>0">
            AND dto.obj_type = #{param.objType}
        </if>
    </select>
    <select id="queryEquipBimLocationsByProjectIdAndFloorId" resultMap="BaseResultMap">
        SELECT DISTINCT dto.id AS id,JSON_OBJECT('X',
            JSON_EXTRACT(JSON_UNQUOTE(CONCAT('[',SUBSTRING(JSON_EXTRACT(infos, '$.bimLocation') FROM 2 FOR LENGTH(JSON_EXTRACT(infos, '$.bimLocation'))-2),']')),'$[0]'),
            'Y',
            JSON_EXTRACT(JSON_UNQUOTE(CONCAT('[',SUBSTRING(JSON_EXTRACT(infos, '$.bimLocation') FROM 2 FOR LENGTH(JSON_EXTRACT(infos, '$.bimLocation'))-2),']')),'$[1]'),
            'Z',JSON_EXTRACT(JSON_UNQUOTE(CONCAT('[',SUBSTRING(JSON_EXTRACT(infos, '$.bimLocation') FROM 2 FOR LENGTH(JSON_EXTRACT(infos, '$.bimLocation'))-2),']')),'$[2]'))
            AS bimLocation FROM dt_relation AS dtr
            INNER JOIN dt_object AS dto ON (dto.id=dtr.obj_from)
        WHERE dtr.graph_code='MechInArch'
          AND dtr.rel_code='Eq2Fl'
          AND dtr.group_code=#{groupCode}
          AND dtr.project_id=#{projectId}
          AND dtr.obj_to=#{floorId}
          AND dtr.valid=TRUE
          AND dto.valid=TRUE
          AND JSON_UNQUOTE(JSON_EXTRACT(dto.infos, '$.bimLocation')) IS NOT NULL
    </select>
    <select id="querySpacesByProjectIdAndFloorId" resultMap="BaseResultMap">
        SELECT DISTINCT dto.id AS id,
               JSON_UNQUOTE(JSON_EXTRACT(infos,'$.outline' )) AS outLines,
               dto.class_code AS class_code FROM dt_relation AS dtr
        INNER JOIN dt_object AS dto ON (dto.id=dtr.obj_to)
        WHERE dtr.graph_code='ArchSubset'
          AND dtr.rel_code='Fl2Sp'
          AND dtr.group_code=#{groupCode}
          AND dtr.project_id=#{projectId}
          AND dtr.obj_from=#{floorId}
          AND dtr.valid=TRUE
          AND dto.valid=TRUE
          AND JSON_UNQUOTE(JSON_EXTRACT(dto.infos, '$.outline')) IS NOT NULL
    </select>


</mapper>
