<?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.PigCategoryMapper">
    
    <resultMap type="PigCategory" id="PigCategoryResult">
        <result property="id"    column="id"    />
        <result property="categoryName"    column="category_name"    />
        <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="selectPigCategoryVo">
        select id, category_name, create_time, create_by, update_time, update_by, del_flag from pig_category
    </sql>

    <select id="selectPigCategoryList" parameterType="PigCategory" resultMap="PigCategoryResult">
        <include refid="selectPigCategoryVo"/>
        <where>
            del_flag = '0'
            <if test="categoryName != null  and categoryName != ''"> and category_name like concat('%', #{categoryName}, '%')</if>
        </where>
    </select>
    
    <select id="selectPigCategoryById" parameterType="Long" resultMap="PigCategoryResult">
        <include refid="selectPigCategoryVo"/>
        where id = #{id} and del_flag = '0'
    </select>
    <select id="checkCategoryNameUnique" parameterType="String" resultMap="PigCategoryResult">
        select id, category_name from pig_category where category_name = #{categoryName} and del_flag = '0' limit 1
    </select>
    <insert id="insertPigCategory" parameterType="PigCategory" useGeneratedKeys="true" keyProperty="id">
        insert into pig_category
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="categoryName != null and categoryName != ''">category_name,</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="categoryName != null and categoryName != ''">#{categoryName},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            '0'
         </trim>
    </insert>

    <update id="updatePigCategory" parameterType="PigCategory">
        update pig_category
        <trim prefix="SET" suffixOverrides=",">
            <if test="categoryName != null and categoryName != ''">category_name = #{categoryName},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

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

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