<?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.adm.server.algorithm.dao.AdmModelFileMapper">

    <resultMap id="BaseResultMap" type="com.persagy.adm.server.algorithm.entity.AdmModelFile">
            <id property="id" column="id" jdbcType="VARCHAR"/>
            <result property="originalName" column="original_name" jdbcType="VARCHAR"/>
            <result property="version" column="version" jdbcType="INTEGER"/>
            <result property="userId" column="user_id" jdbcType="VARCHAR"/>
            <result property="userName" column="user_name" jdbcType="VARCHAR"/>
            <result property="status" column="status" jdbcType="SMALLINT"/>
            <result property="note" column="note" jdbcType="OTHER" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
            <result property="revitFileId" column="revit_file_id" jdbcType="VARCHAR"/>
            <result property="dataFileId" column="data_file_id" jdbcType="VARCHAR"/>
            <result property="minDataFileId" column="min_data_file_id" jdbcType="VARCHAR"/>
            <result property="floorId" column="floor_id" jdbcType="VARCHAR"/>
            <result property="buildingId" column="building_id" jdbcType="VARCHAR"/>
            <result property="projectId" column="project_id" jdbcType="VARCHAR"/>
            <result property="groupCode" column="group_code" jdbcType="VARCHAR"/>
            <result property="removed" column="removed" jdbcType="BOOLEAN"/>
            <result property="syncRetryTimes" column="sync_retry_times" jdbcType="INTEGER"/>
            <result property="syncRetryDate" column="sync_retry_date" jdbcType="TIMESTAMP"/>
            <result property="syncErrorMsg" column="sync_error_msg" jdbcType="VARCHAR"/>
            <result property="level" column="level" jdbcType="DOUBLE"/>
            <result property="statusInfo" column="status_info" jdbcType="OTHER" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
            <result property="revitVersion" column="revit_version" 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"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,original_name,version,
        user_id,user_name,status,
        note,revit_file_id,data_file_id,
        min_data_file_id,floor_id,building_id,
        project_id,group_code,removed,
        sync_retry_times,sync_retry_date,sync_error_msg,
        level,status_info,revit_version,
        creator,creation_time,modifier,
        modified_time,valid,ts
    </sql>
    <insert id="insertEntity">
        INSERT INTO adm_model_file
        <trim prefix="(" suffix=")" suffixOverrides=",">
            `id`,
            <if test="null!=et.originalName and ''!=et.originalName">`original_name`,</if>
            `version`,
            <if test="null!=et.userId and ''!=et.userId">`user_id`,</if>
            <if test="null!=et.userName and ''!=et.userName">`user_name`,</if>
            <if test="null!=et.status">`status`,</if>
            <if test="null!=et.revitFileId and ''!=et.revitFileId">`revit_file_id`,</if>
            <if test="null!=et.dataFileId and ''!=et.dataFileId">`data_file_id`,</if>
            <if test="null!=et.floorId and ''!=et.floorId">`floor_id`,</if>
            <if test="null!=et.buildingId and ''!=et.buildingId">`building_id`,</if>
            <if test="null!=et.projectId and ''!=et.projectId">`project_id`,</if>
            <if test="null!=et.groupCode and ''!=et.groupCode">`group_code`,</if>
            <if test="null!=et.revitVersion and ''!=et.revitVersion">`revit_version`,</if>
            <if test="null!=et.creator and ''!=et.creator">`creator`,</if>
            <if test="null!=et.modifier and ''!=et.modifier">`modifier`,</if>
            `creation_time`,
            `modified_time`
        </trim>
        SELECT
        <trim suffixOverrides=",">
            #{et.id},
            <if test="null!=et.originalName and ''!=et.originalName">#{et.originalName},</if>
            (SELECT IFNULL(MAX(version),0)+1 AS version FROM adm_model_file WHERE floor_id=#{et.floorId}),
            <if test="null!=et.userId and ''!=et.userId">#{et.userId},</if>
            <if test="null!=et.userName and ''!=et.userName">#{et.userName},</if>
            <if test="null!=et.status">#{et.status},</if>
            <if test="null!=et.revitFileId and ''!=et.revitFileId">#{et.revitFileId},</if>
            <if test="null!=et.dataFileId and ''!=et.dataFileId">#{et.dataFileId},</if>
            <if test="null!=et.floorId and ''!=et.floorId">#{et.floorId},</if>
            <if test="null!=et.buildingId and ''!=et.buildingId">#{et.buildingId},</if>
            <if test="null!=et.projectId and ''!=et.projectId">#{et.projectId},</if>
            <if test="null!=et.groupCode and ''!=et.groupCode">#{et.groupCode},</if>
            <if test="null!=et.revitVersion and ''!=et.revitVersion">#{et.revitVersion},</if>
            #{et.creator},
            #{et.modifier},
            #{et.creationTime},
            #{et.modifiedTime}
        </trim>
        FROM DUAL;
    </insert>
    <select id="queryCurrentModelFileByFloorIds"
            resultType="com.persagy.adm.server.algorithm.entity.AdmModelFile">
        SELECT * FROM adm_model_file
                INNER JOIN (SELECT floor_id,MAX(version) AS version FROM adm_model_file
                    ${ew.customSqlSegment} GROUP BY floor_id
                HAVING MAX(version)=version) AS temp
                    ON(temp.floor_id=adm_model_file.floor_id AND temp.version=adm_model_file.version);
    </select>
</mapper>
