<?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.CrmCustomerContactsMapper">
    
    <resultMap type="CrmCustomerContacts" id="CrmCustomerContactsResult">
        <result property="contactsId"    column="contacts_id"    />
        <result property="customerId"    column="customer_id"    />
        <result property="name"    column="name"    />
        <result property="mobile"    column="mobile"    />
        <result property="email"    column="email"    />
        <result property="weixin"    column="weixin"    />
        <result property="address"    column="address"    />
        <result property="remark"    column="remark"    />
        <result property="createUserId"    column="create_user_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDel"    column="is_del"    />
        <result property="companyId"    column="company_id"    />
    </resultMap>

    <sql id="selectCrmCustomerContactsVo">
        select contacts_id, customer_id, name, mobile, email, weixin, address, remark, create_user_id, create_time, update_time, is_del, company_id from crm_customer_contacts
    </sql>

    <select id="selectCrmCustomerContactsList" parameterType="CrmCustomerContacts" resultMap="CrmCustomerContactsResult">
        <include refid="selectCrmCustomerContactsVo"/>
        <where>  
            <if test="customerId != null "> and customer_id = #{customerId}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="mobile != null  and mobile != ''"> and mobile = #{mobile}</if>
            <if test="email != null  and email != ''"> and email = #{email}</if>
            <if test="weixin != null  and weixin != ''"> and weixin = #{weixin}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="createUserId != null "> and create_user_id = #{createUserId}</if>
            <if test="isDel != null "> and is_del = #{isDel}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
        </where>
    </select>
    
    <select id="selectCrmCustomerContactsById" parameterType="Long" resultMap="CrmCustomerContactsResult">
        <include refid="selectCrmCustomerContactsVo"/>
        where contacts_id = #{contactsId}
    </select>
        
    <insert id="insertCrmCustomerContacts" parameterType="CrmCustomerContacts" useGeneratedKeys="true" keyProperty="contactsId">
        insert into crm_customer_contacts
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="customerId != null">customer_id,</if>
            <if test="name != null">name,</if>
            <if test="mobile != null">mobile,</if>
            <if test="email != null">email,</if>
            <if test="weixin != null">weixin,</if>
            <if test="address != null">address,</if>
            <if test="remark != null">remark,</if>
            <if test="createUserId != null">create_user_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDel != null">is_del,</if>
            <if test="companyId != null">company_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="customerId != null">#{customerId},</if>
            <if test="name != null">#{name},</if>
            <if test="mobile != null">#{mobile},</if>
            <if test="email != null">#{email},</if>
            <if test="weixin != null">#{weixin},</if>
            <if test="address != null">#{address},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createUserId != null">#{createUserId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="companyId != null">#{companyId},</if>
         </trim>
    </insert>

    <update id="updateCrmCustomerContacts" parameterType="CrmCustomerContacts">
        update crm_customer_contacts
        <trim prefix="SET" suffixOverrides=",">
            <if test="customerId != null">customer_id = #{customerId},</if>
            <if test="name != null">name = #{name},</if>
            <if test="mobile != null">mobile = #{mobile},</if>
            <if test="email != null">email = #{email},</if>
            <if test="weixin != null">weixin = #{weixin},</if>
            <if test="address != null">address = #{address},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createUserId != null">create_user_id = #{createUserId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
        </trim>
        where contacts_id = #{contactsId}
    </update>

    <delete id="deleteCrmCustomerContactsById" parameterType="Long">
        delete from crm_customer_contacts where contacts_id = #{contactsId}
    </delete>

    <delete id="deleteCrmCustomerContactsByIds" parameterType="String">
        delete from crm_customer_contacts where contacts_id in 
        <foreach item="contactsId" collection="array" open="(" separator="," close=")">
            #{contactsId}
        </foreach>
    </delete>
    
</mapper>