<?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.dayou.mapper.VisitMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.Visit">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="customer_id" property="customerId" />
        <result column="visit_type" property="visitType" />
        <result column="visit_time" property="visitTime" />
        <result column="spot" property="spot" />
        <result column="content" property="content" />
        <result column="user_id" property="userId" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        v.id,
        v.deleted,
        v.created,
        v.modified,
        v.customer_id, v.visit_type, v.visit_time, v.spot, v.content,v.user_id
    </sql>

    <sql id="pageOrList">
        select
        <include refid="Base_Column_List"/>,c.name as customerName,u.name as userName,c.section as customerSection,
        c.department as customerDepartment,c.position as customerPosition,c.level as customerLevel
        from visit v left join customer c on v.customer_id = c.id left join user u on u.id = v.user_id
        where v.deleted = 0 and c.deleted = 0
        <if test="visit!=null and visit.visitType!=null and visit.visitType!='' ">
            and v.visit_type = #{visit.visitType}
        </if>
        <if test="keyword!=null and keyword!='' ">
            and(
            c.name like concat ('%',#{keyword},'%')
            or c.level like concat('%',#{keyword},'%')
            or c.department like concat('%',#{keyword},'%')
            or c.position like concat('%',#{keyword},'%')
            or c.section like concat('%',#{keyword},'%')
            )
        </if>
        <if test="visit!=null and visit.userName!=null and visit.userName!='' " >
            and u.name like concat ('%',#{visit.userName},'%')
        </if>
        <if test="visit!=null and visit.userIds!=null and visit.userIds.size!=0">
            and v.user_id in
            <foreach collection="visit.userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
        order by v.id DESC
    </sql>
    <select id="page" parameterType="com.dayou.vo.VisitVO" resultType="com.dayou.vo.VisitVO">
        <include refid="pageOrList"/>
    </select>

    <select id="detail" parameterType="java.lang.Long" resultType="com.dayou.vo.VisitVO">
        select
        <include refid="Base_Column_List"/>,c.name as customerName,u.name as userName,c.section as customerSection,
        c.department as customerDepartment,c.position as customerPosition,c.level as customerLevel
        from visit v left join customer c on v.customer_id = c.id left join user u on u.id = v.user_id
        where v.deleted = 0 and c.deleted = 0 and v.id = #{id}
    </select>

    <select id="getList" resultType="com.dayou.vo.VisitVO">
        <include refid="pageOrList"/>
    </select>
</mapper>
