<?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.yice.webadmin.app.dao.ModelManageMapper">
    <resultMap id="BaseResultMap" type="com.yice.webadmin.app.model.ModelManage">
        <id column="model_id" jdbcType="BIGINT" property="modelId"/>
        <result column="model_name" jdbcType="VARCHAR" property="modelName"/>
        <result column="model_type" jdbcType="TINYINT" property="modelType"/>
        <result column="model_describe" jdbcType="VARCHAR" property="modelDescribe"/>
        <result column="version_count" jdbcType="TINYINT" property="versionCount"/>
        <result column="business_label" jdbcType="VARCHAR" property="businessLabel"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="create_user_id" jdbcType="BIGINT" property="createUserId"/>
        <result column="update_user_id" jdbcType="BIGINT" property="updateUserId"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="is_base_model" jdbcType="TINYINT" property="isBaseModel"/>
        <result column="last_version_source" jdbcType="VARCHAR" property="lastVersionSource"/>
    </resultMap>

    <insert id="insertList">
        INSERT INTO lmp_model_manage
        (model_id,
        model_name,
        model_type,
        model_describe,
        version_count,
        business_label,
        create_time,
        create_user_id,
        update_user_id,
        update_time,
        is_base_model,
        last_version_source)
        VALUES
        <foreach collection="list" index="index" item="item" separator=",">
            (#{item.modelId},
            #{item.modelName},
            #{item.modelType},
            #{item.modelDescribe},
            #{item.versionCount},
            #{item.businessLabel},
            #{item.createTime},
            #{item.createUserId},
            #{item.updateUserId},
            #{item.updateTime},
            #{item.isBaseModel},
            #{item.lastVersionSource})
        </foreach>
    </insert>

    <!-- 如果有逻辑删除字段过滤，请写到这里 -->
    <sql id="filterRef">
        <!-- 这里必须加上全包名，否则当filterRef被其他Mapper.xml包含引用的时候，就会调用Mapper.xml中的该SQL片段 -->
        <include refid="com.yice.webadmin.app.dao.ModelManageMapper.inputFilterRef"/>
    </sql>

    <!-- 这里仅包含调用接口输入的主表过滤条件 -->
    <sql id="inputFilterRef">
        <if test="modelManageFilter != null">
            <if test="modelManageFilter.isBaseModel != null">
                AND lmp_model_manage.is_base_model = #{modelManageFilter.isBaseModel}
            </if>
            <if test="modelManageFilter.createUserId != null">
                AND lmp_model_manage.create_user_id = #{modelManageFilter.createUserId}
            </if>
            <if test="modelManageFilter.searchString != null and modelManageFilter.searchString != ''">
                <bind name="safeModelManageSearchString" value="'%' + modelManageFilter.searchString + '%'"/>
                AND CONCAT(IFNULL(lmp_model_manage.model_name,''), IFNULL(lmp_model_manage.business_label,'')) LIKE
                #{safeModelManageSearchString}
            </if>
        </if>
    </sql>

    <select id="getModelManageList" resultMap="BaseResultMap" parameterType="com.yice.webadmin.app.model.ModelManage">
        SELECT * FROM lmp_model_manage
        <where>
            <include refid="filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>

    <!-- 支持基于一对一或者一对多从表字段过滤的SQL_ID -->
    <select id="getModelManageListEx" resultMap="BaseResultMap">
        SELECT
        lmp_model_manage.*
        FROM
        lmp_model_manage
        <where>
            <include refid="filterRef"/>
            <if test="modelVersionFilter != null">
                AND EXISTS (SELECT * FROM lmp_model_version
                <where>
                    lmp_model_manage.model_id = lmp_model_version.model_id
                    <include refid="com.yice.webadmin.app.dao.ModelVersionMapper.filterRef"/>
                </where>
                )
            </if>
            <if test="modelTaskFilter != null">
                AND EXISTS (SELECT * FROM lmp_model_task
                <where>
                    lmp_model_manage.model_id = lmp_model_task.model_id
                    <include refid="com.yice.webadmin.app.dao.ModelTaskMapper.filterRef"/>
                </where>
                )
            </if>
            <if test="modelDeployFilter != null">
                AND EXISTS (SELECT * FROM lmp_model_deploy
                <where>
                    lmp_model_manage.model_id = lmp_model_deploy.model_id
                    <include refid="com.yice.webadmin.app.dao.ModelDeployMapper.filterRef"/>
                </where>
                )
            </if>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>
</mapper>
