<?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.app.mapper.PigpenMapper">
    
    <resultMap type="Pigpen" id="PigpenResult">
        <result property="id"    column="id"    />
        <result property="pigpenName"    column="pigpen_name"    />
        <result property="maxCapacity"    column="max_capacity"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectPigpenVo">
        select id, pigpen_name, max_capacity, create_time, create_by, update_time, update_by, del_flag from pigpen
    </sql>

    <select id="selectPigpenList" parameterType="Pigpen" resultMap="PigpenResult">
        <include refid="selectPigpenVo"/>
        <where>
            del_flag = '0'
            <if test="pigpenName != null  and pigpenName != ''"> and pigpen_name like concat('%', #{pigpenName}, '%')</if>
            <if test="maxCapacity != null "> and max_capacity = #{maxCapacity}</if>
        </where>
    </select>
    
    <select id="selectPigpenById" parameterType="Long" resultMap="PigpenResult">
        <include refid="selectPigpenVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="checkPigpenNameUnique" parameterType="String" resultMap="PigpenResult">
        select id, pigpen_name from pigpen where pigpen_name = #{pigpenName} and del_flag = '0' limit 1
    </select>

    <insert id="insertPigpen" parameterType="Pigpen" useGeneratedKeys="true" keyProperty="id">
        insert into pigpen
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="pigpenName != null and pigpenName != ''">pigpen_name,</if>
            <if test="maxCapacity != null">max_capacity,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            del_flag
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="pigpenName != null and pigpenName != ''">#{pigpenName},</if>
            <if test="maxCapacity != null">#{maxCapacity},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            '0'
         </trim>
    </insert>

    <update id="updatePigpen" parameterType="Pigpen">
        update pigpen
        <trim prefix="SET" suffixOverrides=",">
            <if test="pigpenName != null and pigpenName != ''">pigpen_name = #{pigpenName},</if>
            <if test="maxCapacity != null">max_capacity = #{maxCapacity},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePigpenById" parameterType="Long">
        update pigpen set del_flag = '2' where id = #{id}
    </delete>

    <delete id="deletePigpenByIds" parameterType="String">
        update pigpen set del_flag = '2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>