<?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.CrmCustomerAssistMapper">

    <resultMap type="CrmCustomerAssist" id="CrmCustomerAssistResult">
        <result property="id"    column="id"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="companyUserName"    column="company_user_name"    />
        <result property="customerId"    column="customer_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="rate"    column="rate"    />
    </resultMap>

    <sql id="selectCrmCustomerAssistVo">
        select id, company_id, company_user_id,company_user_name, customer_id, create_time,rate from crm_customer_assist
    </sql>

    <select id="selectCrmCustomerAssistList" parameterType="CrmCustomerAssist" resultMap="CrmCustomerAssistResult">
        <include refid="selectCrmCustomerAssistVo"/>
        <where>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
            <if test="companyUserName != null and companyUserName != ''"> and company_user_name = #{companyUserName}</if>
            <if test="customerId != null "> and customer_id = #{customerId}</if>
        </where>
    </select>

    <select id="selectCrmCustomerAssistById" parameterType="Long" resultMap="CrmCustomerAssistResult">
        <include refid="selectCrmCustomerAssistVo"/>
        where id = #{id}
    </select>
    <select id="selectCompanyUserNameByCustomerId" resultType="java.lang.String">
        select  CONCAT(company_user_name, '(', company_user_id, ')','分佣:',rate,'%') AS assistUser
        from crm_customer_assist
        where customer_id = #{customerId}
    </select>
    <select id="selectCustomerIdByCompanyUserId" resultType="java.lang.Long">
        SELECT customer_id FROM crm_customer_assist where company_user_id = #{companyUserId}
    </select>
    <select id="selectCompanyUserIdByCustomerId" resultType="java.lang.Long">
        select company_user_id from crm_customer_assist where customer_id = #{customerId} and create_time &lt; #{createTime}
    </select>
    <select id="selectByCustomerId" resultType="com.fs.crm.domain.CrmCustomerAssist">
        <include refid="selectCrmCustomerAssistVo"/>
        where  customer_id = #{customerId}

    </select>

    <insert id="insertCrmCustomerAssist" parameterType="CrmCustomerAssist" useGeneratedKeys="true" keyProperty="id">
        insert into crm_customer_assist
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="companyUserName != null">company_user_name,</if>
            <if test="customerId != null">customer_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="rate != null">rate,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="companyUserName != null">#{companyUserName},</if>
            <if test="customerId != null">#{customerId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="rate != null">#{rate},</if>
         </trim>
    </insert>

    <update id="updateCrmCustomerAssist" parameterType="CrmCustomerAssist">
        update crm_customer_assist
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="companyUserName != null">company_user_name = #{companyUserName},</if>
            <if test="customerId != null">customer_id = #{customerId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="rate != null">rate = #{rate},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCrmCustomerAssistById" parameterType="Long">
        delete from crm_customer_assist where id = #{id}
    </delete>

    <delete id="deleteCrmCustomerAssistByIds" parameterType="String">
        delete from crm_customer_assist where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="removeByCustomer">
        delete from crm_customer_assist where customer_id in
        <foreach collection="maps.customerIds" item="customerId" open="(" separator="," close=")">
            #{customerId}
        </foreach>
        <if test="maps.companyUserId != null">
            and company_user_id = #{maps.companyUserId}
        </if>
    </delete>
    <delete id="deleteCrmCustomerAssistByCustomerId">
        delete from crm_customer_assist where customer_id = #{customerId}
    </delete>

</mapper>
