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

    <resultMap type="com.ruoyi.platform.domain.Image" id="ImageMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="description" column="description" jdbcType="VARCHAR"/>
        <result property="imageType" column="image_type" jdbcType="INTEGER"/>
        <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="ImageMap">
        select
            img.id,
            img.name,
            img.description,
            img.image_type,
            img.create_by,
            img.create_time,
            img.update_by,
            img.update_time,
            img.state,
            (SELECT COUNT(*) FROM image_version WHERE image_version.image_id = img.id AND image_version.state = 1) as versionCount
        from image img
        where img.id = #{id} and img.state = 1
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="ImageMap">
        select
        img.id, img.name, img.description, img.image_type, img.create_by, img.create_time, img.update_by, img.update_time, img.state,
        (SELECT COUNT(*) FROM image_version WHERE image_version.image_id = img.id and image_version.state = 1) as versionCount
        from image img
        <where>
            img.state = 1
            <if test="image.id != null">
                and img.id = #{image.id}
            </if>
            <if test="image.name != null and image.name != ''">
                and img.name like concat('%', #{image.name}, '%')
            </if>
            <if test="image.description != null and image.description != ''">
                and img.description = #{image.description}
            </if>
            <if test="image.imageType != null">
                and img.image_type = #{image.imageType}
            </if>
            <if test="image.createBy != null and image.createBy != ''">
                and img.create_by = #{image.createBy}
            </if>
            <if test="image.createTime != null">
                and img.create_time = #{image.createTime}
            </if>
            <if test="image.updateBy != null and image.updateBy != ''">
                and img.update_by = #{image.updateBy}
            </if>
            <if test="image.updateTime != null">
                and img.update_time = #{image.updateTime}
            </if>
            <if test="image.state != null and image.state != ''">
                and img.state = #{image.state}
            </if>
        </where>
        limit #{pageable.offset}, #{pageable.pageSize}
    </select>


    <!--通过镜像名字进行模糊查询-->
    <select id="queryByName" resultMap="ImageMap">
        select
        id, name, description, image_type, create_by, create_time, update_by, update_time, state
        from image
        <where>
            state = 1
            <if test="name != null and name != ''">
                and name like "%"#{name}"%"
            </if>
        </where>
    </select>

    <select id="getByName" resultMap="ImageMap">
        select
        id, name, description, image_type, create_by, create_time, update_by, update_time, state
        from image
        <where>
            state = 1
            <if test="name != null and name != ''">
                and name = #{name}
            </if>
        </where>
    </select>


    <!--统计总行数-->
    <select id="count" resultType="java.lang.Long">
        select count(1)
        from image
        <where>
            state = 1
            <if test="image.id != null">
                and id = #{image.id}
            </if>
            <if test="image.name != null and image.name != ''">
                and name like concat('%', #{image.name}, '%')
            </if>
            <if test="image.description != null and image.description != ''">
                and description = #{image.description}
            </if>
            <if test="image.imageType != null">
                and image_type = #{image.imageType}
            </if>
            <if test="image.createBy != null and image.createBy != ''">
                and create_by = #{image.createBy}
            </if>
            <if test="image.createTime != null">
                and create_time = #{image.createTime}
            </if>
            <if test="image.updateBy != null and image.updateBy != ''">
                and update_by = #{image.updateBy}
            </if>
            <if test="image.updateTime != null">
                and update_time = #{image.updateTime}
            </if>
            <if test="image.state != null and image.state != ''">
                and state = #{image.state}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into image(name,description,image_type,create_by,create_time,update_by,update_time,state)
        values (#{image.name}, #{image.description}, #{image.imageType}, #{image.createBy}, #{image.createTime}, #{image.updateBy}, #{image.updateTime}, #{image.state})
    </insert>


    <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
        insert into image(name,description,image_type,create_by,create_time,update_by,update_time,state)
        values
        <foreach collection="entities" item="entity" separator=",">
        (#{entity.name}#{entity.description}#{entity.imageType}#{entity.createBy}#{entity.createTime}#{entity.updateBy}#{entity.updateTime}#{entity.state})
        </foreach>
    </insert>

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

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

</mapper>

