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

    <resultMap type="CrmCustomerLogs" id="CrmCustomerLogsResult">
        <result property="logsId"    column="logs_id"    />
        <result property="customerId"    column="customer_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="title"    column="title"    />
        <result property="remark"    column="remark"    />
        <result property="createTime"    column="create_time"    />
        <result property="logsType"    column="logs_type"    />
        <result property="userName"    column="user_name"    />
    </resultMap>

    <sql id="selectCrmCustomerLogsVo">
        select ccl.*,cu.user_name from crm_customer_logs ccl left join crm_customer cc on ccl.customer_id = cc.customer_id left join company_user cu on cc.create_user_id = cu.user_id
    </sql>

    <select id="selectCrmCustomerLogsList" parameterType="CrmCustomerLogs" resultMap="CrmCustomerLogsResult">
        <include refid="selectCrmCustomerLogsVo"/>
        <where>
            <if test="customerId != null "> and ccl.customer_id = #{customerId}</if>
            <if test="companyUserId != null "> and ccl.company_user_id = #{companyUserId}</if>
            <if test="title != null  and title != ''"> and ccl.title = #{title}</if>
            <if test="logsType != null "> and ccl.logs_type = #{logsType}</if>
        </where>
        order by ccl.logs_id desc
    </select>

    <select id="selectCrmCustomerLogsById" parameterType="Long" resultMap="CrmCustomerLogsResult">
        <include refid="selectCrmCustomerLogsVo"/>
        where logs_id = #{logsId}
    </select>

    <insert id="insertCrmCustomerLogs" parameterType="CrmCustomerLogs" useGeneratedKeys="true" keyProperty="logsId">
        insert into crm_customer_logs
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="customerId != null">customer_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="title != null">title,</if>
            <if test="remark != null">remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="logsType != null">logs_type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="customerId != null">#{customerId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="title != null">#{title},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="logsType != null">#{logsType},</if>
         </trim>
    </insert>

    <update id="updateCrmCustomerLogs" parameterType="CrmCustomerLogs">
        update crm_customer_logs
        <trim prefix="SET" suffixOverrides=",">
            <if test="customerId != null">customer_id = #{customerId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="title != null">title = #{title},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="logsType != null">logs_type = #{logsType},</if>
        </trim>
        where logs_id = #{logsId}
    </update>

    <delete id="deleteCrmCustomerLogsById" parameterType="Long">
        delete from crm_customer_logs where logs_id = #{logsId}
    </delete>

    <delete id="deleteCrmCustomerLogsByIds" parameterType="String">
        delete from crm_customer_logs where logs_id in
        <foreach item="logsId" collection="array" open="(" separator="," close=")">
            #{logsId}
        </foreach>
    </delete>

</mapper>
