<?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.warehouse.mapper.ProductInfoMapper">
    <resultMap type="ProductInfo" id="ProductInfoResult">
        <id property="id" column="id"/>
        <result property="productCode" column="product_code"/>
        <result property="productName" column="product_name"/>
        <result property="brand" column="brand"/>
        <result property="category" column="category"/>
        <result property="spec" column="spec"/>
        <result property="unit" column="unit"/>
        <result property="remark" column="remark"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectProductInfoVo">
        select id, product_code, product_name, brand, category, spec, unit, remark, create_time, update_time from product_info
    </sql>

    <select id="selectProductInfoList" parameterType="ProductInfo" resultMap="ProductInfoResult">
        <include refid="selectProductInfoVo"/>
        <where>
            <if test="productCode != null and productCode != ''">and product_code like concat('%', #{productCode}, '%')</if>
            <if test="productName != null and productName != ''">
                and lower(concat_ws(' ', product_name, brand, category, spec, unit))
                    like concat('%', lower(#{productName}), '%')
            </if>
            <if test="brand != null and brand != ''">and brand like concat('%', #{brand}, '%')</if>
            <if test="category != null and category != ''">and category like concat('%', #{category}, '%')</if>
            <if test="spec != null and spec != ''">and spec like concat('%', #{spec}, '%')</if>
        </where>
        order by id desc
    </select>

    <select id="selectProductInfoAll" resultMap="ProductInfoResult">
        <include refid="selectProductInfoVo"/> order by product_code
    </select>

    <select id="selectProductInfoById" parameterType="Long" resultMap="ProductInfoResult">
        <include refid="selectProductInfoVo"/> where id = #{id}
    </select>

    <select id="checkProductCodeUnique" parameterType="String" resultMap="ProductInfoResult">
        <include refid="selectProductInfoVo"/> where product_code = #{productCode} limit 1
    </select>

    <select id="checkProductInfoUnique" parameterType="ProductInfo" resultMap="ProductInfoResult">
        <include refid="selectProductInfoVo"/>
        where product_name = #{productName}
          and brand &lt;=&gt; #{brand}
          and category &lt;=&gt; #{category}
          and spec &lt;=&gt; #{spec}
        limit 1
    </select>

    <insert id="insertProductInfo" parameterType="ProductInfo" useGeneratedKeys="true" keyProperty="id">
        insert into product_info(product_code, product_name, brand, category, spec, unit, remark, create_time)
        values(#{productCode}, #{productName}, #{brand}, #{category}, #{spec}, #{unit}, #{remark}, sysdate())
    </insert>

    <update id="updateProductInfo" parameterType="ProductInfo">
        update product_info
        <set>
            <if test="productCode != null and productCode != ''">product_code = #{productCode},</if>
            <if test="productName != null and productName != ''">product_name = #{productName},</if>
            <if test="brand != null">brand = #{brand},</if>
            <if test="category != null">category = #{category},</if>
            <if test="spec != null">spec = #{spec},</if>
            <if test="unit != null">unit = #{unit},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_time = sysdate()
        </set>
        where id = #{id}
    </update>

    <delete id="deleteProductInfoByIds" parameterType="Long">
        delete from product_info where id in
        <foreach collection="array" item="id" open="(" separator="," close=")">#{id}</foreach>
    </delete>
</mapper>
