<?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.cool.store.mapper.LineInterviewMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.LineInterviewDO">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="line_id" jdbcType="BIGINT" property="lineId" />
    <result column="partner_id" jdbcType="VARCHAR" property="partnerId" />
    <result column="region_id" jdbcType="BIGINT" property="regionId" />
    <result column="interview_date" jdbcType="DATE" property="interviewDate" />
    <result column="start_time" jdbcType="TIMESTAMP" property="startTime" />
    <result column="end_time" jdbcType="TIMESTAMP" property="endTime" />
    <result column="join_interview_status" jdbcType="TINYINT" property="joinInterviewStatus" />
    <result column="actual_start_time" jdbcType="TIMESTAMP" property="actualStartTime" />
    <result column="actual_end_time" jdbcType="TIMESTAMP" property="actualEndTime" />
    <result column="room_id" jdbcType="VARCHAR" property="roomId" />
    <result column="room_password" jdbcType="VARCHAR" property="roomPassword" />
    <result column="interviewer_user_id" jdbcType="VARCHAR" property="interviewerUserId" />
    <result column="room_status" jdbcType="TINYINT" property="roomStatus" />
    <result column="interview_status" jdbcType="TINYINT" property="interviewStatus" />
    <result column="interview_type" jdbcType="TINYINT" property="interviewType" />
    <result column="video_url" jdbcType="VARCHAR" property="videoUrl" />
    <result column="audit_id" jdbcType="BIGINT" property="auditId" />
    <result column="calendars_event_id" jdbcType="BIGINT" property="calendarsEventId" />
    <result column="deleted" jdbcType="BIT" property="deleted" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
  </resultMap>

  <select id="getInterviewInfo" resultMap="BaseResultMap">
    select * from xfsg_line_interview where line_id = #{lineId} and interview_type = #{interviewType} and deleted = '0'
  </select>

  <select id="getInterviewByLindIds" resultMap="BaseResultMap">
    select * from xfsg_line_interview where interview_type = #{interviewType} and deleted = '0'
    <if test="lineIds != null">
      <foreach collection="lineIds" item="item" separator="," open="and line_id in (" close=")">
        #{item}
      </foreach>
    </if>
  </select>

  <select id="getInterviewerPage" resultType="com.cool.store.dto.interview.LineInterviewPageDTO">
    select
      a.id as interviewId,
      a.line_id as lineId,
      a.start_time as startTime,
      a.end_time as endTime,
      a.actual_start_time as actualStartTime,
      a.actual_end_time as actualEndTime,
      a.interviewer_user_id as interviewerUserId,
      a.interview_status as interviewStatus,
      a.interview_type as interviewType,
      a.room_id as roomId,
      b.username as username,
      b.mobile as mobile,
      b.user_portrait as userPortrait,
      b.want_shop_area_id as wantShopAreaId,
      b.investment_manager as investmentManager,
      b.user_portrait as userPortrait,
      b.line_source as lineSource
    from
      xfsg_line_interview a inner join xfsg_line_info b on a.line_id = b.id
    where
      a.deleted = '0' and b.deleted = '0' and a.interview_type = #{interviewType}
    <if test="keyword != null and keyword != ''">
      and (b.username like concat('%', #{keyword}, '%') or b.mobile like concat('%', #{keyword}, '%'))
    </if>
    <if test="username != null and username != ''">
      and b.username like concat('%', #{username}, '%')
    </if>
    <if test="mobile != null and mobile != ''">
      and b.mobile like concat('%', #{mobile}, '%')
    </if>
    <if test="workflowStage != null and workflowStage != ''">
      and b.workflow_stage = #{workflowStage}
    </if>
    <if test="workflowSubStage != null and workflowSubStage != ''">
      and b.workflow_sub_stage = #{workflowSubStage}
    </if>
    <if test="interviewStatus != null and interviewStatus != ''">
      and a.interview_status = #{interviewStatus}
    </if>
    <if test="lineSource != null and lineSource != ''">
      and b.line_source = #{lineSource}
    </if>
    <if test="interviewStartTime != null and interviewEndTime != null ">
      and a.start_time between #{interviewStartTime} and #{interviewEndTime}
    </if>
    <if test="interviewerUserId != null and interviewerUserId != ''">
      and a.interviewer_user_id = #{interviewerUserId}
    </if>
    <if test="wantShopAreaIds != null and wantShopAreaIds.size() > 0">
      and b.want_shop_area_id in
      <foreach collection="wantShopAreaIds" item="item" separator="," open="(" close=")">
        #{item}
      </foreach>
    </if>
    <if test="areaPath != null">
      and b.want_shop_area_id in (select id from xfsg_open_area_info where area_path like concat(#{areaPath}, '%'))
    </if>
  </select>
  
  <select id="getInterviewInfoByRoomId" resultMap="BaseResultMap">
    select * from xfsg_line_interview where room_id = #{roomId} and deleted = '0'
  </select>

  <update id="deleteInterviewInfo">
    update xfsg_line_interview set deleted = '1' where id = #{interviewId}
  </update>

  <select id="getInterviewByLineId" resultMap="BaseResultMap">
    select * from
    xfsg_line_interview
    where line_id = #{lineId} and interview_status = 3
    and deleted = '0'
  </select>

  <select id="getWaitAuditInterview" resultMap="BaseResultMap">
    select id, line_id from xfsg_line_interview where interview_status = 1 and deleted = '0' and interview_type = 0 and now() >= start_time
  </select>
  
  <update id="batchUpdateInterviewStatus">
    update xfsg_line_interview set interview_status = #{interviewStatus} where id in
    <foreach collection="interviewIds" item="item" separator="," open="(" close=")">
      #{item}
    </foreach>
  </update>

  <select id="getInterviewRemindList" resultMap="BaseResultMap">
    select * from xfsg_line_interview where deleted = '0' and interview_type in (1, 2) and start_time between #{startTime} and #{endTime}
  </select>

</mapper>