<?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.template.mapper.SmartVisitorMapper">
    <resultMap type="com.template.model.vo.VisitorPageVo" id="visitorPageMap">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="userPhone" column="user_phone"/>
        <result property="userNumber" column="user_number"/>
        <result property="peerNum" column="peer_num"/>
        <result property="carNum" column="car_num"/>
        <result property="visitReason" column="visit_reason"/>
        <result property="statu" column="statu"/>
        <result property="visitorTime" column="visitor_time"/>
        <result property="visitorDeadline" column="visitor_deadline"/>
        <result property="respondent" column="respondent"/>
        <result property="responcode" column="responcode"/>
        <result property="departmentId" column="department_id"/>
        <result property="respondentPhone" column="respondent_phone"/>
        <result property="respondentName" column="respondent_name"/>
        <result property="visitorType" column="visitor_type"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <select id="queryVisitorPage" resultType="com.template.model.vo.VisitorPageVo" resultMap="visitorPageMap">
        select
        sv.id,sv.user_id,sv.user_name,sv.user_phone,sv.user_number,sv.peer_num,sv.car_num,sv.visit_reason,sv.statu,sv.visitor_time,sv.visitor_deadline,sv.respondent,
        sv.responcode,sv.department_id,sv.respondent_phone,sv.respondent_name,sv.visitor_type,sv.create_time,sv.image from
        smart_visitor sv
        inner join smart_user su on su.deleted = 0 and sv.user_id = su.id
        where sv.deleted = 0
        <if test="userId != null and userId != ''">
            and sv.user_id = #{userId}
        </if>
        <if test="createStartTime != null and createStartTime != '' and createEndTime != null and createEndTime != ''">
            and sv.create_time &gt;= #{createStartTime} and sv.create_time &lt;= #{createEndTime}
        </if>
        <if test="keyWord != null and keyWord != ''">
            and (sv.user_name like '%' #{keyWord} '%' or sv.user_phone like '%' #{keyWord} '%' or sv.respondent_name
            like '%' #{keyWord} '%'
            or sv.department_id in (select id from smart_department where deleted = 0 and name like '%' #{keyWord} '%')
            or sv.respondent_phone like '%' #{keyWord} '%' or sv.car_num like '%' #{keyWord} '%')
        </if>
        <if test="visitorStartTime != null and visitorStartTime != '' and visitorEndTime != null and visitorEndTime != ''">
            and sv.visitor_time &gt;= #{visitorStartTime} and sv.visitor_time &lt;= #{visitorEndTime}
        </if>
        <if test="statu != null and statu != ''">
            and sv.statu = #{statu}
        </if>
        order by sv.create_time desc
    </select>


    <select id="queryVisitorCount" resultType="java.lang.Integer">
        select Count(*) from smart_visitor
        where 1=1
        <if test="userId != null and userId != ''">
            and user_id = #{userId}
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and (
            <if test="startTime != null and startTime != ''">
                (visitor_time &lt; #{startTime} and visitor_deadline &gt; #{startTime})
            </if>
            <if test="endTime != null and endTime != ''">
                or (visitor_time &lt; #{endTime} and visitor_deadline &gt; #{endTime})
            </if>
            )
        </if>
        and deleted = 0 and statu != 2 and statu != 9
    </select>


    <resultMap type="com.template.model.pojo.SmartVisitor" id="visitorDatasMap">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="userPhone" column="user_phone"/>
        <result property="userNumber" column="user_number"/>
        <result property="peerNum" column="peer_num"/>
        <result property="carNum" column="car_num"/>
        <result property="visitReason" column="visit_reason"/>
        <result property="statu" column="statu"/>
        <result property="visitorTime" column="visitor_time"/>
        <result property="visitorDeadline" column="visitor_deadline"/>
        <result property="respondent" column="respondent"/>
        <result property="responcode" column="responcode"/>
        <result property="departmentId" column="department_id"/>
        <result property="respondentPhone" column="respondent_phone"/>
        <result property="respondentName" column="respondent_name"/>
        <result property="visitorType" column="visitor_type"/>
    </resultMap>
    <select id="queryVisitorPageDatas" resultType="com.template.model.pojo.SmartVisitor" resultMap="visitorPageMap">
        select * from smart_visitor
        where deleted = 0
        <if test="type != null and type != ''">
            and statu = #{type}
        </if>
        and (
        <if test="studentIds != null and studentIds.size() > 0">
            (respondent in
            <foreach collection="studentIds" item="studentId" index="index" open="(" close=")" separator=",">
                ${studentId}
            </foreach>
            and visitor_type = 1)
        </if>
        <if test="isAdmin != null and isAdmin != ''">
            ${isAdmin}
        </if>
        )
        order by create_time desc
    </select>

    <select id="getPage" resultType="com.template.model.vo.SmartVisitorPageVo">
        SELECT
        sv.id,sv.user_id,sv.user_name,sv.user_phone,sv.user_number,sv.peer_num,sv.car_num,sv.visit_reason,sv.statu,sv.visitor_time,sv.visitor_deadline,sv.respondent,
        sv.responcode,sv.department_id,sv.respondent_phone,sv.respondent_name,sv.visitor_type,sv.create_time,sv.image,sv.source,
        sd.name as departmentName
        FROM
            `smart_visitor` sv
        left join  smart_department sd on sv.department_id=sd.id
        WHERE  sv.deleted=0 and sv.visitor_type=#{type}
        <if test="status != null and status != ''">
            and sv.statu = #{status}
        </if>
        <if test="keyWord != null and keyWord != ''">
            and (sv.user_name like '%' #{keyWord} '%' or sv.user_phone like '%' #{keyWord} '%' or sv.respondent_name
            like '%' #{keyWord} '%'
            or sv.department_id in (select id from smart_department where deleted = 0 and name like '%' #{keyWord} '%')
            or sv.respondent_phone like '%' #{keyWord} '%' or sv.car_num like '%' #{keyWord} '%')
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and sv.create_time &gt;= #{startTime} and sv.create_time &lt;= #{endTime}
        </if>
        <if test="visitorStartTime != null and visitorStartTime != '' and visitorEndTime != null and visitorEndTime != ''">
            and sv.visitor_time &gt;= #{visitorStartTime} and sv.visitor_time &lt;= #{visitorEndTime}
        </if>
        order by sv.create_time desc
    </select>
    <select id="getScreenPage" resultType="com.template.model.vo.SmartVisitorScreenVo">
        SELECT
            sv.id,
            sv.visit_reason as visitReason,
            sv.image as image,
            sv.visitor_time as visitorTime,
            su.`name` as studentName,
            su.card_no as cardNo,
            sc.`name` as className,
            sv.statu as statu
        FROM
            `smart_visitor` sv
                LEFT JOIN smart_user su ON sv.respondent = su.id
                LEFT JOIN smart_class sc ON su.school_class = sc.id
        WHERE
            sv.deleted = 0
          AND sv.visitor_type =1
        <if test="status != null and status != ''">
            and sv.statu = #{status}
        </if>
        <if test="phone != null and phone != ''">
            and sv.user_phone = #{phone}
        </if>
        order by sv.visitor_time desc
    </select>

    <select id="parentsScreenPage" resultType="com.template.model.vo.SmartVisitorScreenVo">
        SELECT
        sv.id,
        sv.visit_reason as visitReason,
        sv.image as image,
        sv.visitor_time as visitorTime,
        su.`name` as studentName,
        su.card_no as cardNo,
        sc.`name` as className,
        sv.statu as statu
        FROM
        `smart_visitor` sv
        LEFT JOIN smart_user su ON sv.respondent = su.id
        LEFT JOIN smart_class sc ON su.school_class = sc.id
        WHERE
        sv.deleted = 0
        AND sv.visitor_type =1 and sv.user_id= #{userId}
        <if test="status != null and status != ''">
            and sv.statu = #{status}
        </if>
        order by sv.visitor_time desc
    </select>


</mapper>
