<?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.ximai.mes.md.mapper.MdProductCustomerMapper">

    <resultMap type="MdProductCustomer" id="MdProductCustomerResult">
        <result property="productCustomerId"    column="product_customer_id"    />
        <result property="itemId"    column="item_id"    />
        <result property="customerCode"    column="customer_code"    />
        <result property="customerName"    column="customer_name"    />
    </resultMap>

    <sql id="selectMdProductCustomerVo">
        select product_customer_id, item_id, customer_code, customer_name from md_product_customer
    </sql>

    <select id="selectMdProductCustomerList" parameterType="MdProductCustomer" resultMap="MdProductCustomerResult">
        <include refid="selectMdProductCustomerVo"/>
        <where>
            <if test="itemId != null "> and item_id = #{itemId}</if>
            <if test="customerCode != null  and customerCode != ''"> and customer_code = #{customerCode}</if>
            <if test="customerName != null  and customerName != ''"> and customer_name like concat('%', #{customerName}, '%')</if>
        </where>
    </select>

    <select id="selectMdProductCustomerByProductCustomerId" parameterType="Long" resultMap="MdProductCustomerResult">
        <include refid="selectMdProductCustomerVo"/>
        where product_customer_id = #{productCustomerId}
    </select>

    <insert id="insertMdProductCustomer" parameterType="MdProductCustomer" useGeneratedKeys="true" keyProperty="productCustomerId">
        insert into md_product_customer
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="itemId != null">item_id,</if>
            <if test="customerCode != null">customer_code,</if>
            <if test="customerName != null">customer_name,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="itemId != null">#{itemId},</if>
            <if test="customerCode != null">#{customerCode},</if>
            <if test="customerName != null">#{customerName},</if>
         </trim>
    </insert>

    <update id="updateMdProductCustomer" parameterType="MdProductCustomer">
        update md_product_customer
        <trim prefix="SET" suffixOverrides=",">
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="customerCode != null">customer_code = #{customerCode},</if>
            <if test="customerName != null">customer_name = #{customerName},</if>
        </trim>
        where product_customer_id = #{productCustomerId}
    </update>

    <delete id="deleteMdProductCustomerByProductCustomerId" parameterType="Long">
        delete from md_product_customer where product_customer_id = #{productCustomerId}
    </delete>

    <delete id="deleteMdProductCustomerByProductCustomerIds" parameterType="String">
        delete from md_product_customer where product_customer_id in
        <foreach item="productCustomerId" collection="array" open="(" separator="," close=")">
            #{productCustomerId}
        </foreach>
    </delete>
</mapper>
