<?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.web.v1.mapper.PigpenMapper">

    <resultMap type="com.ruoyi.web.v1.model.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"/>
        <result property="isUse" column="is_use"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <resultMap type="com.ruoyi.web.v1.DTO.PigpenDTO" id="PigpenRequest">
        <result property="pigpenName" column="pigpen_name"/>
        <result property="maxCapacity" column="max_capacity"/>
    </resultMap>

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

    <select id="selectPigpenList" parameterType="com.ruoyi.web.v1.model.Pigpen" resultMap="PigpenResult">
        <include refid="selectPigpenVo"/>
        <where>
            del_flag = '0'
            <if test="isUse != null ">and is_use = #{isUse}</if>
            <if test="pigpenName != null  and pigpenName != ''">and pigpen_name like concat('%', #{pigpenName}, '%')
            </if>
            <if test="maxCapacity != null ">and max_capacity = #{maxCapacity}</if>
        </where>
        order by pigpen_name asc
    </select>

    <select id="printPigpenList" parameterType="com.ruoyi.web.v1.model.Pigpen" resultMap="PigpenRequest">
        <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>
        order by create_time desc
    </select>

    <select id="selectPigpenById" parameterType="Long" resultMap="PigpenResult">
        <include refid="selectPigpenVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="selectLastPigpen"  resultMap="PigpenResult">
        <include refid="selectPigpenVo"/>
        <where>
            del_flag = '0'
        </where>
        order by id desc
        limit 1
    </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="com.ruoyi.web.v1.model.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>
            <if test="remark != null">remark,</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>
            <if test="remark != null">#{remark},</if>
            '0'
        </trim>
    </insert>

    <update id="updatePigpen" parameterType="com.ruoyi.web.v1.model.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>
            <if test="isUse != null">is_use = #{isUse},</if>
            <if test="remark != null">remark = #{remark},</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>