<?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.ruoyi.ibs.task.mapper.TaskVisitRecordMapper">
    <select id="getVisitRecordList" resultType="VisitRecordListVO">
        select a.id                                  as record_id,
               a.campaign_name,
               a.cust_name,
               concat(a.nick_name, '-', a.user_name) as user_name,
               a.duration,
               b.sign_in_time,
               b.sign_out_time,
               a.cust_type
        from task_visit_record a
        left join task_visit_sign b on a.latest_sign = b.id
        where a.user_name like concat( #{deptCode}, '%') and a.cust_id  = #{custId}
        order by b.sign_in_time
    </select>

    <select id="getVisitRecordListByCampaign" resultType="VisitRecordListVO">
        select a.id                                  as record_id,
               a.campaign_name,
               a.cust_name,
               concat(a.nick_name, '-', a.user_name) as user_name,
               a.duration,
               b.sign_in_time,
               b.sign_out_time,
               b.sign_out_remark,
               a.cust_type
        from task_visit_record a
        left join task_visit_sign b on a.latest_sign = b.id
        where a.user_name like concat( #{deptCode}, '%') and a.cust_id  = #{custId} and campaign_id = #{campaignId}
        order by b.sign_in_time
    </select>

    <select id="getVisitRecordListByUserName" parameterType="TaskRecordDTO" resultType="VisitRecordListVO">
        select a.id as record_id,
               a.campaign_name,
               a.cust_name,
               a.cust_type,
               concat(a.nick_name, '-', a.user_name) as user_name,
               CONCAT(ROUND(a.duration * 60), '分钟', a.duration * 3600 % 60, '秒')  AS duration,
               b.sign_in_time,
               b.sign_out_time
        from task_visit_record a
        left join task_visit_sign b on a.latest_sign = b.id
        left join sys_user c on c.user_name = a.user_name
        left join sys_dept d on d.dept_id = c.dept_id
        where
        <if test="post != null and post == 'manager'"> a.user_name = #{userName} </if>
        <if test="post != null and post == 'outlet'">  d.dept_id = #{deptId} </if>
        <if test="post != null and post == 'branch'">  d.dept_id = #{deptId} or find_in_set(#{deptId},d.ancestors) </if>
        <if test="post != null and post == 'head'">   left( d.dept_id,3) = left(#{deptId},3) </if>
        <if test="custName != null and custName != '' ">
            and a.cust_name like concat('%', #{custName}, '%')
        </if>
        <if test="campaignName != null and campaignName != '' ">
            and a.campaign_name like concat('%', #{campaignName}, '%')
        </if>
        <if test="visitType != null and visitType != ''">
            and a.visit_type = #{visitType}
        </if>
        <if test="startTimeMonth != null and startTimeMonth != '' ">
            and b.sign_in_time like concat(#{startTimeMonth}, '%')
        </if>
        order by b.sign_in_time desc
    </select>

    <select id="getVisitRecord" resultType="VisitRecordVO">
        select a.id as record_id,
               a.campaign_name,
               a.cust_name,
               concat(a.nick_name, '-', a.user_name) as user_name,
               a.duration,
               a.cust_type,
               b.sign_in_time,
               b.sign_in_address,
               b.sign_out_time,
               b.sign_out_address,
               b.sign_out_remark,
               a.feedback_id,
               a.feedback_type,
               a.feedback_template_id
        from task_visit_record a
        left join task_visit_sign b on a.latest_sign = b.id
        where a.id = #{recordId}
    </select>

    <select id="getVisitRecordListAll" resultType="VisitRecordListVO">
        select a.id as record_id,
        a.campaign_name,
        a.cust_name,
        a.cust_type,
        a.feedback_type,
        a.feedback_id,
        concat(a.nick_name, '-', a.user_name) as user_name,
        CONCAT(ROUND(a.duration * 60), '分钟', a.duration * 3600 % 60, '秒')  AS duration,
        b.sign_in_time,
        b.sign_out_time
        from task_visit_record a
        left join task_visit_sign b on a.latest_sign = b.id
        left join sys_user c on c.user_name = a.user_name
        left join sys_dept d on d.dept_id = c.dept_id
        where
        <if test="role != null and role == 'manager'"> a.user_name = #{userName} </if>
        <if test="role != null and role == 'outlet'">  d.dept_id = #{deptId} </if>
        <if test="role != null and role == 'branch'">  (d.dept_id = #{deptId} or find_in_set(#{deptId},d.ancestors)) </if>
        <if test="role != null and role in {'head', 'ops', 'public', 'private'}">   left( d.dept_id,3) = left(#{deptId},3) </if>
        order by b.sign_in_time desc
        limit 10000
    </select>

    <select id="getVisitRecordListByPage" resultType="VisitRecordListVO">
        select a.id as record_id,
        a.campaign_name,
        a.cust_name,
        a.cust_type,
        a.feedback_type,
        a.feedback_id,
        concat(a.nick_name, '-', a.user_name) as user_name,
        CONCAT(ROUND(a.duration * 60), '分钟', a.duration * 3600 % 60, '秒')  AS duration,
        b.sign_in_time,
        b.sign_out_time
        from task_visit_record a
        left join task_visit_sign b on a.latest_sign = b.id
        left join sys_user c on c.user_name = a.user_name
        left join sys_dept d on d.dept_id = c.dept_id
        where
        <if test="role != null and role == 'manager'"> a.user_name = #{userName} </if>
        <if test="role != null and role == 'outlet'">  d.dept_id = #{deptId} </if>
        <if test="role != null and role == 'branch'">  (d.dept_id = #{deptId} or find_in_set(#{deptId},d.ancestors)) </if>
        <if test="role != null and role in {'head', 'ops', 'public', 'private'}">   left( d.dept_id,3) = left(#{deptId},3) </if>
        order by b.sign_in_time desc
        limit #{offset}, #{pageSize}
    </select>

</mapper>