<?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.qw.mapper.QwExternalContactCrmMapper">
    
    <resultMap type="QwExternalContactCrm" id="QwExternalContactCrmResult">
        <result property="id"    column="id"    />
        <result property="externalUserId"    column="external_user_id"    />
        <result property="openid"    column="openid"    />
        <result property="externalUserName"    column="external_user_name"    />
        <result property="customerId"    column="customer_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="corpId"    column="corp_id"    />
        <result property="avatar"    column="avatar"    />
        <result property="fsUserId"    column="fs_user_id"    />
    </resultMap>

    <sql id="selectQwExternalContactCrmVo">
        select id, external_user_id,openid, external_user_name, avatar, customer_id, company_id, corp_id, fs_user_id from qw_external_contact_crm
    </sql>

    <select id="selectQwExternalContactCrmList" parameterType="QwExternalContactCrm" resultMap="QwExternalContactCrmResult">
        <include refid="selectQwExternalContactCrmVo"/>
        <where>  
            <if test="externalUserId != null  and externalUserId != ''"> and external_user_id = #{externalUserId}</if>
            <if test="openid != null  and openid != ''"> and openid = #{openid}</if>
            <if test="externalUserName != null  and externalUserName != ''"> and external_user_name like concat('%', #{externalUserName}, '%')</if>
            <if test="customerId != null "> and customer_id = #{customerId}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="corpId != null "> and corp_id = #{corpId}</if>
            <if test="avatar != null "> and avatar = #{avatar}</if>
            <if test="fsUserId != null "> and fs_user_id = #{fsUserId}</if>
        </where>
    </select>
    
    <select id="selectQwExternalContactCrmById" parameterType="Long" resultMap="QwExternalContactCrmResult">
        <include refid="selectQwExternalContactCrmVo"/>
        where id = #{id}
    </select>

    <insert id="insertQwExternalContactCrm" parameterType="QwExternalContactCrm">
        insert into qw_external_contact_crm
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="externalUserId != null">external_user_id,</if>
            <if test="openid != null">openid,</if>
            <if test="externalUserName != null">external_user_name,</if>
            <if test="customerId != null">customer_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="corpId != null">corp_id,</if>
            <if test="avatar != null">avatar,</if>
            <if test="fsUserId != null">fs_user_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="externalUserId != null">#{externalUserId},</if>
            <if test="openid != null">#{openid},</if>
            <if test="externalUserName != null">#{externalUserName},</if>
            <if test="customerId != null">#{customerId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="corpId != null">#{corpId},</if>
            <if test="avatar != null">#{avatar},</if>
            <if test="fsUserId != null">#{fsUserId},</if>
         </trim>
    </insert>


    <insert id="insertOrUpdateQwExternalContactCrm" parameterType="QwExternalContactCrm">
        insert into qw_external_contact_crm
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="externalUserId != null">external_user_id,</if>
            <if test="openid != null">openid,</if>
            <if test="externalUserName != null">external_user_name,</if>
            <if test="customerId != null">customer_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="corpId != null">corp_id,</if>
            <if test="avatar != null">avatar,</if>
            <if test="fsUserId != null">fs_user_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="externalUserId != null">#{externalUserId},</if>
            <if test="openid != null">#{openid},</if>
            <if test="externalUserName != null">#{externalUserName},</if>
            <if test="customerId != null">#{customerId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="corpId != null">#{corpId},</if>
            <if test="avatar != null">#{avatar},</if>
            <if test="fsUserId != null">#{fsUserId},</if>
        </trim>
        on duplicate key update
        <trim  suffixOverrides=",">
            <if test="externalUserId != null">external_user_id = #{externalUserId},</if>
            <if test="openid != null">openid = #{openid},</if>
            <if test="externalUserName != null">external_user_name = #{externalUserName},</if>
            <if test="customerId != null">customer_id = #{customerId},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="corpId != null">corp_id = #{corpId},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="fsUserId != null">fs_user_id = #{fsUserId},</if>
        </trim>
    </insert>

    <update id="updateQwExternalContactCrm" parameterType="QwExternalContactCrm">
        update qw_external_contact_crm
        <trim prefix="SET" suffixOverrides=",">
            <if test="externalUserId != null">external_user_id = #{externalUserId},</if>
            <if test="openid != null">external_user_id = #{openid},</if>
            <if test="externalUserName != null">external_user_name = #{externalUserName},</if>
            <if test="customerId != null">customer_id = #{customerId},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="corpId != null">corp_id = #{corpId},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="fsUserId != null">fs_user_id = #{fsUserId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteQwExternalContactCrmById" parameterType="Long">
        delete from qw_external_contact_crm where id = #{id}
    </delete>

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