<?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.CrmCustomerExtMapper">
    
    <resultMap type="CrmCustomerExt" id="CrmCustomerExtResult">
        <result property="extId"    column="ext_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="name"    column="name"    />
        <result property="type"    column="type"    />
        <result property="status"    column="status"    />
        <result property="sort"    column="sort"    />
    </resultMap>

    <sql id="selectCrmCustomerExtVo">
        select ext_id, company_id, name, type, status, sort from crm_customer_ext
    </sql>

    <select id="selectCrmCustomerExtList" parameterType="CrmCustomerExt" resultMap="CrmCustomerExtResult">
        <include refid="selectCrmCustomerExtVo"/>
        <where>  
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="sort != null "> and sort = #{sort}</if>
        </where>
        order by sort asc
    </select>
    
    <select id="selectCrmCustomerExtById" parameterType="Long" resultMap="CrmCustomerExtResult">
        <include refid="selectCrmCustomerExtVo"/>
        where ext_id = #{extId}
    </select>
        
    <insert id="insertCrmCustomerExt" parameterType="CrmCustomerExt" useGeneratedKeys="true" keyProperty="extId">
        insert into crm_customer_ext
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="name != null">name,</if>
            <if test="type != null">type,</if>
            <if test="status != null">status,</if>
            <if test="sort != null">sort,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="name != null">#{name},</if>
            <if test="type != null">#{type},</if>
            <if test="status != null">#{status},</if>
            <if test="sort != null">#{sort},</if>
         </trim>
    </insert>

    <update id="updateCrmCustomerExt" parameterType="CrmCustomerExt">
        update crm_customer_ext
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="name != null">name = #{name},</if>
            <if test="type != null">type = #{type},</if>
            <if test="status != null">status = #{status},</if>
            <if test="sort != null">sort = #{sort},</if>
        </trim>
        where ext_id = #{extId}
    </update>

    <delete id="deleteCrmCustomerExtById" parameterType="Long">
        delete from crm_customer_ext where ext_id = #{extId}
    </delete>

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