<?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.CrmCustomerVisitMapper">
    
    <resultMap type="CrmCustomerVisit" id="CrmCustomerVisitResult">
        <result property="visitId"    column="visit_id"    />
        <result property="customerId"    column="customer_id"    />
        <result property="visitType"    column="visit_type"    />
        <result property="content"    column="content"    />
        <result property="photos"    column="photos"    />
        <result property="nextTime"    column="next_time"    />
        <result property="lng"    column="lng"    />
        <result property="lat"    column="lat"    />
        <result property="address"    column="address"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="companyId"    column="company_id"    />
        <result property="customerUserStatus"    column="customer_user_status"    />
    </resultMap>

    <sql id="selectCrmCustomerVisitVo">
        select visit_id, customer_id, visit_type, content, photos, next_time, lng, lat, address, company_user_id, create_time, company_id,customer_user_status from crm_customer_visit
    </sql>

    <select id="selectCrmCustomerVisitList" parameterType="CrmCustomerVisit" resultMap="CrmCustomerVisitResult">
        <include refid="selectCrmCustomerVisitVo"/>
        <where>  
            <if test="customerId != null "> and customer_id = #{customerId}</if>
            <if test="visitType != null "> and visit_type = #{visitType}</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="photos != null  and photos != ''"> and photos = #{photos}</if>
            <if test="nextTime != null "> and next_time = #{nextTime}</if>
            <if test="lng != null  and lng != ''"> and lng = #{lng}</if>
            <if test="lat != null  and lat != ''"> and lat = #{lat}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="customerUserStatus != null "> and customer_user_status = #{customerUserStatus}</if>
        </where>
    </select>
    
    <select id="selectCrmCustomerVisitById" parameterType="Long" resultMap="CrmCustomerVisitResult">
        <include refid="selectCrmCustomerVisitVo"/>
        where visit_id = #{visitId}
    </select>
        
    <insert id="insertCrmCustomerVisit" parameterType="CrmCustomerVisit" useGeneratedKeys="true" keyProperty="visitId">
        insert into crm_customer_visit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="customerId != null">customer_id,</if>
            <if test="visitType != null">visit_type,</if>
            <if test="content != null">content,</if>
            <if test="photos != null">photos,</if>
            <if test="nextTime != null">next_time,</if>
            <if test="lng != null">lng,</if>
            <if test="lat != null">lat,</if>
            <if test="address != null">address,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="companyId != null">company_id,</if>
            <if test="customerUserStatus != null">customer_user_status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="customerId != null">#{customerId},</if>
            <if test="visitType != null">#{visitType},</if>
            <if test="content != null">#{content},</if>
            <if test="photos != null">#{photos},</if>
            <if test="nextTime != null">#{nextTime},</if>
            <if test="lng != null">#{lng},</if>
            <if test="lat != null">#{lat},</if>
            <if test="address != null">#{address},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="customerUserStatus != null">#{customerUserStatus},</if>
         </trim>
    </insert>

    <update id="updateCrmCustomerVisit" parameterType="CrmCustomerVisit">
        update crm_customer_visit
        <trim prefix="SET" suffixOverrides=",">
            <if test="customerId != null">customer_id = #{customerId},</if>
            <if test="visitType != null">visit_type = #{visitType},</if>
            <if test="content != null">content = #{content},</if>
            <if test="photos != null">photos = #{photos},</if>
            <if test="nextTime != null">next_time = #{nextTime},</if>
            <if test="lng != null">lng = #{lng},</if>
            <if test="lat != null">lat = #{lat},</if>
            <if test="address != null">address = #{address},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="customerUserStatus != null">customer_user_status = #{customerUserStatus},</if>
        </trim>
        where visit_id = #{visitId}
    </update>

    <delete id="deleteCrmCustomerVisitById" parameterType="Long">
        delete from crm_customer_visit where visit_id = #{visitId}
    </delete>

    <delete id="deleteCrmCustomerVisitByIds" parameterType="String">
        delete from crm_customer_visit where visit_id in 
        <foreach item="visitId" collection="array" open="(" separator="," close=")">
            #{visitId}
        </foreach>
    </delete>


    <select id="selectCrmCustomerVisitCounts" resultType="com.alibaba.fastjson.JSONObject">
        SELECT  ifnull(count( distinct t.customer_id),0) AS customerCount,  t.type,  ifnull(count(t.visit_id),0) AS visitCount
        FROM
        (
        SELECT DATE_FORMAT(v.create_time, #{sqlDateFormat} ) AS type,  v.visit_id,v.customer_id
        FROM
        crm_customer_visit AS v
        WHERE
        (DATE_FORMAT(v.create_time,#{sqlDateFormat}) between #{beginTime} and #{finalTime})
        AND v.company_user_id IN
        <foreach item="item" collection="userIds" open="(" separator="," close=")">
            #{item}
        </foreach>
        ) t
        GROUP BY t.type
    </select>
    
</mapper>