<?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.ModelsDao">

    <resultMap type="com.ruoyi.platform.domain.Models" id="ModelsMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="description" column="description" jdbcType="VARCHAR"/>
        <result property="availableRange" column="available_range" jdbcType="INTEGER"/>
        <result property="modelType" column="model_type" jdbcType="VARCHAR"/>
        <result property="modelTag" column="model_tag" 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="queryById" resultMap="ModelsMap">
        select
            id, name, description,available_range, model_type,model_tag, create_by, create_time, update_by, update_time, state
        from models
        where id = #{id} and state = 1
    </select>

    <!--查询单个-->
    <select id="findByName" resultMap="ModelsMap">
        select
            id, name, description,available_range, model_type,model_tag, create_by, create_time, update_by, update_time, state
        from models
        where name = #{name} and state = 1 limit 1

    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="ModelsMap">
        select
        id, name, description,available_range, model_type,model_tag, create_by, create_time, update_by, update_time, state
        from models
        <where>
            state = 1
            <if test="models.id != null">
                and id = #{models.id}
            </if>
            <if test="models.name != null and models.name != ''">
                and name like "%"#{models.name}"%"
            </if>
            <if test="models.description != null and models.description != ''">
                and description = #{models.description}
            </if>
            <if test="models.availableRange != null">
                and available_range = #{models.availableRange}
            </if>
            <if test="models.modelType != null">
                and model_type = #{models.modelType}
            </if>
            <if test="models.modelTag != null">
                and model_tag = #{models.modelTag}
            </if>
            <if test="models.createBy != null and models.createBy != ''">
                and create_by = #{models.createBy}
            </if>
            <if test="models.createTime != null">
                and create_time = #{models.createTime}
            </if>
            <if test="models.updateBy != null and models.updateBy != ''">
                and update_by = #{models.updateBy}
            </if>
            <if test="models.updateTime != null">
                and update_time = #{models.updateTime}
            </if>
            <if test="models.state != null">
                and state = #{models.state}
            </if>
        </where>
        limit #{pageable.offset}, #{pageable.pageSize}
    </select>

    <!--统计总行数-->
    <select id="count" resultType="java.lang.Long">
        select count(1)
        from models
        <where>
            state = 1
            <if test="models.id != null">
                and id = #{models.id}
            </if>
            <if test="models.name != null and models.name != ''">
                and name like "%"#{models.name}"%"
            </if>
            <if test="models.description != null and models.description != ''">
                and description = #{models.description}
            </if>
            <if test="models.availableRange != null">
                and available_range = #{models.availableRange}
            </if>
            <if test="models.modelType != null">
                and model_type = #{models.modelType}
            </if>
            <if test="models.modelTag != null">
                and model_tag = #{models.modelTag}
            </if>
            <if test="models.createBy != null and models.createBy != ''">
                and create_by = #{models.createBy}
            </if>
            <if test="models.createTime != null">
                and create_time = #{models.createTime}
            </if>
            <if test="models.updateBy != null and models.updateBy != ''">
                and update_by = #{models.updateBy}
            </if>
            <if test="models.updateTime != null">
                and update_time = #{models.updateTime}
            </if>
            <if test="models.state != null">
                and state = #{models.state}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into models(name, description, available_range, model_type,model_tag, create_by, create_time, update_by, update_time, state)
        values (#{models.name}, #{models.description},#{models.availableRange}, #{models.modelType}, #{models.modelTag}, #{models.createBy}, #{models.createTime}, #{models.updateBy}, #{models.updateTime}, #{models.state})
    </insert>

    <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
        insert into models(name, description, available_range, model_type,model_tag, create_by, create_time, update_by, update_time, state)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.name}, #{entity.description},#{models.availableRange}, #{entity.modelType}, #{entity.modelTag}, #{entity.createBy}, #{entity.createTime}, #{entity.updateBy}, #{entity.updateTime}, #{entity.state})
        </foreach>
    </insert>

    <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
        insert into models(name, description, available_range, model_type,model_tag, create_by, create_time, update_by, update_time, state)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.name}, #{entity.description}, #{entity.availableRange},#{entity.modelType}, #{entity.modelTag}, #{entity.createBy}, #{entity.createTime}, #{entity.updateBy}, #{entity.updateTime}, #{entity.state})
        </foreach>
        on duplicate key update
        name = values(name),
        description = values(description),
        available_range = values(available_range),
        model_type = values(model_type),
        model_tag = values(model_tag),
        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
        <set>
            <if test="models.name != null and models.name != ''">
                name = #{models.name},
            </if>
            <if test="models.description != null and models.description != ''">
                description = #{models.description},
            </if>
            <if test="models.availableRange != null and models.availableRange != ''">
                available_range = #{models.availableRange},
            </if>
            <if test="models.modelType != null">
                model_type = #{models.modelType},
            </if>
            <if test="models.modelTag != null">
                model_tag = #{models.modelTag},
            </if>
            <if test="models.createBy != null and models.createBy != ''">
                create_by = #{models.createBy},
            </if>
            <if test="models.createTime != null">
                create_time = #{models.createTime},
            </if>
            <if test="models.updateBy != null and models.updateBy != ''">
                update_by = #{models.updateBy},
            </if>
            <if test="models.updateTime != null">
                update_time = #{models.updateTime},
            </if>
            <if test="models.state != null">
                state = #{models.state}
            </if>
        </set>
        where id = #{models.id}
    </update>


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

</mapper>
