<?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.ProductCategoryMapper">
    
    <resultMap type="com.ruoyi.web.v1.model.ProductCategory" id="ProductCategoryResult">
        <result property="id"    column="id"    />
        <result property="productName"    column="product_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"    />
        <result property="unit"    column="unit"    />
        <result property="onSaleFlag"    column="on_sale_flag"    />
        <result property="eqWeight"    column="eq_weight"    />
        <result property="animalType"    column="animal_type"    />
    </resultMap>

    <sql id="selectProductCategoryVo">
        select id, product_name, create_time, create_by, update_time, update_by, del_flag,unit, on_sale_flag  ,  eq_weight
          , animal_type  from product_category
    </sql>

    <select id="selectProductCategoryList" parameterType="com.ruoyi.web.v1.model.ProductCategory" resultMap="ProductCategoryResult">
        <include refid="selectProductCategoryVo"/>
        <where>
            del_flag = '0'
            <if test="productName != null  and productName != ''"> and product_name like concat('%', #{productName}, '%')</if>
            <if test="onSaleFlag != null"> and on_sale_flag = #{onSaleFlag}</if>
            <if test="animalType != null  and animalType != ''"> and animal_type = #{animalType}</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectProductCategoryById" parameterType="Long" resultMap="ProductCategoryResult">
        <include refid="selectProductCategoryVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="checkProductNameUnique" parameterType="String" resultMap="ProductCategoryResult">
        select id, product_name from product_category where product_name = #{productName} and del_flag = '0' limit 1
    </select>

    <insert id="insertProductCategory" parameterType="com.ruoyi.web.v1.model.ProductCategory" useGeneratedKeys="true" keyProperty="id">
        insert into product_category
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="productName != null and productName != ''">product_name,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="unit != null">unit,</if>
            <if test="onSaleFlag != null">on_sale_flag,</if>
            <if test="animalType != null">animal_type,</if>
            <if test="eqWeight != null">eq_weight,</if>
            del_flag
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="productName != null and productName != ''">#{productName},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="unit != null">#{unit},</if>
            <if test="onSaleFlag != null">#{onSaleFlag},</if>
            <if test="animalType != null">#{animalType},</if>
            <if test="eqWeight != null">#{eqWeight},</if>
            '0'
         </trim>
    </insert>

    <update id="updateProductCategory" parameterType="com.ruoyi.web.v1.model.ProductCategory">
        update product_category
        <trim prefix="SET" suffixOverrides=",">
            <if test="productName != null and productName != ''">product_name = #{productName},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="unit != null">unit = #{unit},</if>
            <if test="onSaleFlag != null">on_sale_flag = #{onSaleFlag},</if>
            <if test="eqWeight != null">eq_weight = #{eqWeight},</if>
            <if test="animalType != null">animal_type = #{animalType},</if>
        </trim>
        where id = #{id}
    </update>

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

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

    <update id="updateHookDefault" parameterType="com.ruoyi.web.v1.model.ProductCategory">
        update product_category set default_flag = 0 where default_flag = 1
    </update>
</mapper>