<?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.ruoyi.platform.mapper.ModelsVersionDao">

    <resultMap type="com.ruoyi.platform.domain.ModelsVersion" id="ModelsVersionMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="modelsId" column="models_id" jdbcType="INTEGER"/>
        <result property="version" column="version" jdbcType="VARCHAR"/>
        <result property="url" column="url" jdbcType="VARCHAR"/>
        <result property="fileName" column="file_name" jdbcType="VARCHAR"/>
        <result property="fileSize" column="file_size" jdbcType="VARCHAR"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="description" column="description" jdbcType="VARCHAR"/>
        <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="state" column="state" jdbcType="INTEGER"/>
    </resultMap>
    <select id="queryByModelsId" resultMap="ModelsVersionMap">
        select
        id,models_id,version,url,file_name,file_size,status, description,create_by,create_time,update_by,update_time,state
        from models_version
        where models_id = #{modelsId} and state = 1
        order by create_time desc
    </select>

    <!--查询单个-->
    <select id="queryById" resultMap="ModelsVersionMap">
        select
        id,models_id,version,url,file_name,file_size,status, description,create_by,create_time,update_by,update_time,state
        from models_version
        where id = #{id} and state = 1
    </select>

    <select id="queryByModelsVersion" resultMap="ModelsVersionMap">
        select
        id,models_id,version,url,file_name,file_size,status, description,create_by,create_time,update_by,update_time,state
        from models_version
        <where>
            state = 1
            <if test="modelsVersion.modelsId != null">
                and models_id = #{modelsVersion.modelsId}
            </if>
            <if test="modelsVersion.version != null and modelsVersion.version != ''">
                and version = #{modelsVersion.version}
            </if>
        </where>
        limit 1
    </select>

    <!-- 查询模型同一个版本下的所有文件 -->
    <select id="queryAllByModelsVersion" resultMap="ModelsVersionMap">
        select
        id,models_id,version,url,file_name,file_size,status, description,create_by,create_time,update_by,update_time,state
        from models_version
        <where>
            state = 1
            <if test="modelsId != null">
                and models_id = #{modelsId}
            </if>
            <if test="version != null and version != ''">
                and version = #{version}
            </if>
        </where>
    </select>



    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="ModelsVersionMap">
        select
        id,models_id,version,url,file_name,file_size,status, description,create_by,create_time,update_by,update_time,state
        from models_version
        <where>
            state = 1
            <if test="modelsVersion.id != null">
                and id = #{modelsVersion.id}
            </if>
            <if test="modelsVersion.modelsId != null">
                and models_id = #{modelsVersion.modelsId}
            </if>
            <if test="modelsVersion.version != null and modelsVersion.version != ''">
                and version = #{modelsVersion.version}
            </if>
            <if test="modelsVersion.url != null and modelsVersion.url != ''">
                and url = #{modelsVersion.url}
            </if>
            <if test="modelsVersion.fileName != null and modelsVersion.fileName != ''">
                and file_name = #{modelsVersion.fileName}
            </if>
            <if test="modelsVersion.fileSize != null and modelsVersion.fileSize != ''">
                and file_size = #{modelsVersion.fileSize}
            </if>
            <if test="modelsVersion.status != null">
                and status = #{modelsVersion.status}
            </if>
            <if test="modelsVersion.description != null and modelsVersion.description != ''">
                and description = #{modelsVersion.description}
            </if>
            <if test="modelsVersion.createBy != null and modelsVersion.createBy != ''">
                and create_by = #{modelsVersion.createBy}
            </if>
            <if test="modelsVersion.createTime != null">
                and create_time = #{modelsVersion.createTime}
            </if>
            <if test="modelsVersion.updateBy != null and modelsVersion.updateBy != ''">
                and update_by = #{modelsVersion.updateBy}
            </if>
            <if test="modelsVersion.updateTime != null">
                and update_time = #{modelsVersion.updateTime}
            </if>
            <if test="modelsVersion.state != null">
                and state = #{modelsVersion.state}
            </if>
        </where>
        limit #{pageable.offset}, #{pageable.pageSize}
    </select>

    <!--统计总行数-->
    <select id="count" resultType="java.lang.Long">
        select count(1)
        from models_version
        <where>
            state = 1
            <if test="modelsVersion.id != null">
                and id = #{modelsVersion.id}
            </if>
            <if test="modelsVersion.modelsId != null">
                and models_id = #{modelsVersion.modelsId}
            </if>
            <if test="modelsVersion.version != null and modelsVersion.version != ''">
                and version = #{modelsVersion.version}
            </if>
            <if test="modelsVersion.url != null and modelsVersion.url != ''">
                and url = #{modelsVersion.url}
            </if>
            <if test="modelsVersion.fileName != null and modelsVersion.fileName != ''">
                and file_name = #{modelsVersion.fileName}
            </if>
            <if test="modelsVersion.fileSize != null and modelsVersion.fileSize != ''">
                and file_size = #{modelsVersion.fileSize}
            </if>
            <if test="modelsVersion.status != null">
                and status = #{modelsVersion.status}
            </if>
            <if test="modelsVersion.description != null and modelsVersion.description != ''">
                and description = #{modelsVersion.description}
            </if>

            <if test="modelsVersion.createBy != null and modelsVersion.createBy != ''">
                and create_by = #{modelsVersion.createBy}
            </if>
            <if test="modelsVersion.createTime != null">
                and create_time = #{modelsVersion.createTime}
            </if>
            <if test="modelsVersion.updateBy != null and modelsVersion.updateBy != ''">
                and update_by = #{modelsVersion.updateBy}
            </if>
            <if test="modelsVersion.updateTime != null">
                and update_time = #{modelsVersion.updateTime}
            </if>
            <if test="modelsVersion.state != null">
                and state = #{modelsVersion.state}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into models_version(models_id, version, url, file_name, file_size, status, description, create_by, create_time, update_by, update_time, state)
        values (#{modelsVersion.modelsId}, #{modelsVersion.version}, #{modelsVersion.url}, #{modelsVersion.fileName}, #{modelsVersion.fileSize}, #{modelsVersion.status}, #{modelsVersion.description}, #{modelsVersion.createBy}, #{modelsVersion.createTime}, #{modelsVersion.updateBy}, #{modelsVersion.updateTime}, #{modelsVersion.state})
    </insert>


    <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
        insert into models_version
        (models_id, version, url, file_name, file_size, status, description, create_by, create_time, update_by, update_time, state)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.modelsId}, #{entity.version}, #{entity.url}, #{entity.fileName}, #{entity.fileSize}, #{entity.status}, #{entity.description},#{entity.createBy}, #{entity.createTime}, #{entity.updateBy}, #{entity.updateTime}, #{entity.state})
        </foreach>
    </insert>


    <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
        insert into models_version
        (models_id, version, url, file_name, file_size, status, description, create_by, create_time, update_by, update_time, state)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.modelsId}, #{entity.version}, #{entity.url}, #{entity.fileName}, #{entity.fileSize}, #{entity.status},#{entity.description}, #{entity.createBy}, #{entity.createTime}, #{entity.updateBy}, #{entity.updateTime}, #{entity.state})
        </foreach>
        on duplicate key update
        models_id = values(models_id),
        version = values(version),
        url = values(url),
        file_name = values(file_name),
        file_size = values(file_size),
        status = values(status),
        create_by = values(create_by),
        create_time = values(create_time),
        update_by = values(update_by),
        update_time = values(update_time),
        state = values(state)
    </insert>


    <!--通过主键修改数据-->
    <update id="update">
        update models_version
        <set>
            <if test="modelsVersion.id != null">
                id = #{modelsVersion.id},
            </if>
            <if test="modelsVersion.modelsId != null">
                models_id = #{modelsVersion.modelsId},
            </if>
            <if test="modelsVersion.version != null and modelsVersion.version != ''">
                version = #{modelsVersion.version},
            </if>
            <if test="modelsVersion.url != null and modelsVersion.url != ''">
                url = #{modelsVersion.url},
            </if>
            <if test="modelsVersion.fileName != null and modelsVersion.fileName != ''">
                file_name = #{modelsVersion.fileName},
            </if>
            <if test="modelsVersion.fileSize != null and modelsVersion.fileSize != ''">
                file_size = #{modelsVersion.fileSize},
            </if>
            <if test="modelsVersion.status != null">
                status = #{modelsVersion.status},
            </if>
            <if test="modelsVersion.description != null and modelsVersion.description != ''">
                description = #{modelsVersion.description},
            </if>
            <if test="modelsVersion.createBy != null and modelsVersion.createBy != ''">
                create_by = #{modelsVersion.createBy},
            </if>
            <if test="modelsVersion.createTime != null">
                create_time = #{modelsVersion.createTime},
            </if>
            <if test="modelsVersion.updateBy != null and modelsVersion.updateBy != ''">
                update_by = #{modelsVersion.updateBy},
            </if>
            <if test="modelsVersion.updateTime != null">
                update_time = #{modelsVersion.updateTime},
            </if>
            <if test="modelsVersion.state != null">
                state = #{modelsVersion.state}
            </if>
        </set>
        where id = #{modelsVersion.id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from models_version where id = #{id}
    </delete>

</mapper>

