<?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.sapCustomer.mapper.SapCustomerMapper">

    <resultMap type="SapCustomer" id="SapCustomerResult">
        <result property="customerId"    column="customer_id"    />
        <result property="customerKunnr"    column="customer_kunnr"    />
        <result property="customerName1"    column="customer_name1"    />
        <result property="customerName2"    column="customer_name2"    />
        <result property="customerName"    column="customer_name"    />
        <result property="customerState"    column="customer_state"    />
    </resultMap>

    <sql id="selectSapCustomerVo">
        select customer_id, customer_kunnr, customer_name1,customer_name2,customer_name1 + isnull(customer_name2,'') customer_name, customer_state from sap_customer
    </sql>

    <select id="selectSapCustomerList" parameterType="SapCustomer" resultMap="SapCustomerResult">
        <include refid="selectSapCustomerVo"/>
        <where>
            <if test="customerKunnr != null  and customerKunnr != ''"> and customer_kunnr = #{customerKunnr}</if>
            <if test="customerName != null  and customerName != ''"> and customer_name1 like concat('%', #{customerName}, '%')</if>
            <if test="customerState != null  and customerState != ''"> and customer_state = #{customerState}</if>
        </where>
    </select>

    <select id="selectSapCustomerByCustomerId" parameterType="Long" resultMap="SapCustomerResult">
        <include refid="selectSapCustomerVo"/>
        where customer_id = #{customerId}
    </select>

    <insert id="insertSapCustomer" parameterType="SapCustomer" useGeneratedKeys="true" keyProperty="customerId">
        insert into sap_customer
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="customerKunnr != null and customerKunnr != ''">customer_kunnr,</if>
            <if test="customerName1 != null and customerName1 != ''">customer_name1,</if>
            <if test="customerName2 != null">customer_name2,</if>
            <if test="customerState != null and customerState != ''">customer_state,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="customerKunnr != null and customerKunnr != ''">#{customerKunnr},</if>
            <if test="customerName1 != null and customerName1 != ''">#{customerName1},</if>
            <if test="customerName2 != null">#{customerName2},</if>
            <if test="customerState != null and customerState != ''">#{customerState},</if>
         </trim>
    </insert>

    <update id="updateSapCustomer" parameterType="SapCustomer">
        update sap_customer
        <trim prefix="SET" suffixOverrides=",">
            <if test="customerKunnr != null and customerKunnr != ''">customer_kunnr = #{customerKunnr},</if>
            <if test="customerName1 != null and customerName1 != ''">customer_name1 = #{customerName1},</if>
            <if test="customerName2 != null">customer_name2 = #{customerName2},</if>
            <if test="customerState != null and customerState != ''">customer_state = #{customerState},</if>
        </trim>
        where customer_id = #{customerId}
    </update>

    <delete id="deleteSapCustomerByCustomerId" parameterType="Long">
        delete from sap_customer where customer_id = #{customerId}
    </delete>

    <delete id="deleteSapCustomerByCustomerIds" parameterType="String">
        delete from sap_customer where customer_id in
        <foreach item="customerId" collection="array" open="(" separator="," close=")">
            #{customerId}
        </foreach>
    </delete>
</mapper>
