<?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.system.mapper.ChargeDetailsMapper">
    
    <resultMap type="ChargeDetails" id="ChargeDetailsResult">
        <result property="id"    column="id"    />
        <result property="orderId"    column="order_id"    />
        <result property="productName"    column="product_name"    />
        <result property="productNum"    column="product_num"    />
        <result property="productUnit"    column="product_unit"    />
        <result property="unitPrice"    column="unit_price"    />
        <result property="totalPrice"    column="total_price"    />
    </resultMap>

    <sql id="selectChargeDetailsVo">
        select id, order_id, product_name, product_num, product_unit, unit_price, total_price from charge_details
    </sql>

    <select id="selectChargeDetailsList" parameterType="ChargeDetails" resultMap="ChargeDetailsResult">
        <include refid="selectChargeDetailsVo"/>
        <where>  
            <if test="orderId != null  and orderId != ''"> and order_id = #{orderId}</if>
            <if test="productName != null  and productName != ''"> and product_name like concat('%', #{productName}, '%')</if>
            <if test="productNum != null "> and product_num = #{productNum}</if>
            <if test="productUnit != null  and productUnit != ''"> and product_unit = #{productUnit}</if>
            <if test="unitPrice != null  and unitPrice != ''"> and unit_price = #{unitPrice}</if>
            <if test="totalPrice != null  and totalPrice != ''"> and total_price = #{totalPrice}</if>
        </where>
    </select>
    
    <select id="selectChargeDetailsById" parameterType="Long" resultMap="ChargeDetailsResult">
        <include refid="selectChargeDetailsVo"/>
        where id = #{id}
    </select>

    <insert id="insertChargeDetails" parameterType="ChargeDetails" useGeneratedKeys="true" keyProperty="id">
        insert into charge_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderId != null">order_id,</if>
            <if test="productName != null">product_name,</if>
            <if test="productNum != null">product_num,</if>
            <if test="productUnit != null">product_unit,</if>
            <if test="unitPrice != null">unit_price,</if>
            <if test="totalPrice != null">total_price,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderId != null">#{orderId},</if>
            <if test="productName != null">#{productName},</if>
            <if test="productNum != null">#{productNum},</if>
            <if test="productUnit != null">#{productUnit},</if>
            <if test="unitPrice != null">#{unitPrice},</if>
            <if test="totalPrice != null">#{totalPrice},</if>
         </trim>
    </insert>

    <update id="updateChargeDetails" parameterType="ChargeDetails">
        update charge_details
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="productName != null">product_name = #{productName},</if>
            <if test="productNum != null">product_num = #{productNum},</if>
            <if test="productUnit != null">product_unit = #{productUnit},</if>
            <if test="unitPrice != null">unit_price = #{unitPrice},</if>
            <if test="totalPrice != null">total_price = #{totalPrice},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteChargeDetailsById" parameterType="Long">
        delete from charge_details where id = #{id}
    </delete>

    <delete id="deleteChargeDetailsByIds" parameterType="String">
        delete from charge_details where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteChargeDetailsByOrderId" parameterType="String">
        delete from charge_details where order_id = #{orderId};
    </delete>
</mapper>