<?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.fs.crm.mapper.CrmCustomerHisOrderMapper">
    
    <resultMap type="CrmCustomerHisOrder" id="CrmCustomerHisOrderResult">
        <result property="orderId"    column="order_id"    />
        <result property="customerId"    column="customer_id"    />
        <result property="customerCode"    column="customer_code"    />
        <result property="orderJson"    column="order_json"    />
        <result property="itemJson"    column="item_json"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectCrmCustomerHisOrderVo">
        select order_id, customer_id, customer_code, order_json, item_json, create_time from crm_customer_his_order
    </sql>

    <select id="selectCrmCustomerHisOrderList" parameterType="CrmCustomerHisOrder" resultMap="CrmCustomerHisOrderResult">
        <include refid="selectCrmCustomerHisOrderVo"/>
        <where>  
            <if test="customerId != null "> and customer_id = #{customerId}</if>
            <if test="customerCode != null  and customerCode != ''"> and customer_code = #{customerCode}</if>
            <if test="orderJson != null  and orderJson != ''"> and order_json = #{orderJson}</if>
            <if test="itemJson != null  and itemJson != ''"> and item_json = #{itemJson}</if>
        </where>
    </select>
    
    <select id="selectCrmCustomerHisOrderById" parameterType="Long" resultMap="CrmCustomerHisOrderResult">
        <include refid="selectCrmCustomerHisOrderVo"/>
        where order_id = #{orderId}
    </select>
        
    <insert id="insertCrmCustomerHisOrder" parameterType="CrmCustomerHisOrder" useGeneratedKeys="true" keyProperty="orderId">
        insert into crm_customer_his_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="customerId != null">customer_id,</if>
            <if test="customerCode != null">customer_code,</if>
            <if test="orderJson != null">order_json,</if>
            <if test="itemJson != null">item_json,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="customerId != null">#{customerId},</if>
            <if test="customerCode != null">#{customerCode},</if>
            <if test="orderJson != null">#{orderJson},</if>
            <if test="itemJson != null">#{itemJson},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateCrmCustomerHisOrder" parameterType="CrmCustomerHisOrder">
        update crm_customer_his_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="customerId != null">customer_id = #{customerId},</if>
            <if test="customerCode != null">customer_code = #{customerCode},</if>
            <if test="orderJson != null">order_json = #{orderJson},</if>
            <if test="itemJson != null">item_json = #{itemJson},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where order_id = #{orderId}
    </update>

    <delete id="deleteCrmCustomerHisOrderById" parameterType="Long">
        delete from crm_customer_his_order where order_id = #{orderId}
    </delete>

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