<?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.project.system.mapper.ProductMaterialMapper">

    <resultMap type="ProductMaterial" id="ProductMaterialResult">
        <result property="id" column="id"/>
        <result property="title" column="title"/>
        <result property="isUseSelf" column="is_use_self"/>
        <result property="compute" column="compute"/>
        <result property="calculateEnum" column="calculate_enum"
                typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler"/>
        <result property="computeStr" column="compute_str"/>
        <result property="productId" column="product_id"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectProductMaterialVo">
        select id,
               title,
               is_use_self,
               compute,
               compute_str,
               calculate_enum,
               product_id,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from product_material
    </sql>

    <select id="selectProductMaterialList" parameterType="ProductMaterial" resultMap="ProductMaterialResult">
        <include refid="selectProductMaterialVo"/>
        <where>
            <if test="title != null  and title != ''">and title = #{title}</if>
            <if test="isUseSelf != null ">and is_use_self = #{isUseSelf}</if>
            <if test="compute != null  and compute != ''">and compute = #{compute}</if>
            <if test="productId != null ">and product_id = #{productId}</if>
            and del_flag='0'
        </where>
    </select>

    <select id="selectProductMaterialById" parameterType="Long" resultMap="ProductMaterialResult">
        <include refid="selectProductMaterialVo"/>
        where id = #{id} and del_flag='0'
    </select>

    <insert id="insertProductMaterial" parameterType="ProductMaterial" useGeneratedKeys="true" keyProperty="id">
        insert into product_material
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title != null">title,</if>
            <if test="isUseSelf != null">is_use_self,</if>
            <if test="compute != null">compute,</if>
            <if test="productId != null">product_id,</if>
            <if test="calculateEnum != null">calculate_enum,</if>
            <if test="computeStr != null">compute_str,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            create_time,update_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="title != null">#{title},</if>
            <if test="isUseSelf != null">#{isUseSelf},</if>
            <if test="compute != null">#{compute},</if>
            <if test="productId != null">#{productId},</if>
            <if test="calculateEnum != null">#{calculateEnum.code},</if>
            <if test="computeStr != null">#{computeStr},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            sysdate(),sysdate()
        </trim>
    </insert>

    <update id="updateProductMaterial" parameterType="ProductMaterial">
        update product_material
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null">title = #{title},</if>
            <if test="isUseSelf != null">is_use_self = #{isUseSelf},</if>
            <if test="compute != null">compute = #{compute},</if>
            <if test="calculateEnum != null">calculate_enum = #{calculateEnum.code},</if>
            <if test="computeStr != null">compute_str = #{computeStr},</if>
            <if test="productId != null">product_id = #{productId},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            update_time = sysdate()
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteProductMaterialById" parameterType="Long">
        update product_material
        set del_flag = '2'
        where id = #{id}
          and del_flag = '0'
    </delete>

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