<?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.HyPartnerInterviewMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.HyPartnerInterviewDO">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="status" jdbcType="TINYINT" property="status" />
    <result column="partner_line_id" jdbcType="BIGINT" property="partnerLineId" />
    <result column="interview_plan_id" jdbcType="BIGINT" property="interviewPlanId" />
    <result column="partner_id" jdbcType="VARCHAR" property="partnerId" />
    <result column="deadline" jdbcType="TIMESTAMP" property="deadline" />
    <result column="interviewer" jdbcType="VARCHAR" property="interviewer" />
    <result column="recorder" jdbcType="VARCHAR" property="recorder" />
    <result column="process_info" jdbcType="VARCHAR" property="processInfo" />
    <result column="record_time" jdbcType="TIMESTAMP" property="recordTime" />
    <result column="summary" jdbcType="VARCHAR" property="summary" />
    <result column="auth_code" jdbcType="VARCHAR" property="authCode" />
    <result column="pass_pdf_url" jdbcType="VARCHAR" property="passPdfUrl" />
    <result column="pass_image_url" jdbcType="VARCHAR" property="passImageUrl" />
    <result column="expiry_date" jdbcType="TIMESTAMP" property="expiryDate" />
    <result column="latest_log_message" jdbcType="VARCHAR" property="latestLogMessage" />
    <result column="pass_reason" jdbcType="VARCHAR" property="passReason" />
    <result column="certify_file" jdbcType="VARCHAR" property="certifyFile" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="approve_time" jdbcType="TIMESTAMP" property="approveTime" />
    <result column="partner_enter_time" jdbcType="TIMESTAMP" property="partnerEnterTime" />
    <result column="interviewer_enter_time" jdbcType="TIMESTAMP" property="interviewerEnterTime" />
    <result column="deleted" jdbcType="BIT" property="deleted" />
  </resultMap>

  <resultMap id="PartnerInterviewInfoVO" type="com.cool.store.vo.PartnerInterviewInfoVO">
    <id column="interviewId" property="interviewId"/>
    <result column="partnerId" property="partnerId"/>
    <result column="interviewerId" property="interviewerId"/>
    <association property="partnerName" column="partnerId" select="queryPartnerName" javaType="string"/>
    <association property="interviewerName" column="interviewerId" select="queryInterviewerName" javaType="string"/>
  </resultMap>

  <resultMap id="partnerEnterInterviewVO" type="com.cool.store.vo.EnterInterviewVO">
    <association property="partnerName" column="partner_id" select="queryPartnerName"></association>
    <association property="interviewerName" column="interviewer" select="queryInterviewerName"></association>
  </resultMap>

  <resultMap id="passLetterDetail" type="com.cool.store.vo.PartnerPassLetterDetailVO">
    <association property="partnerName" column="partner_id" select="queryPartnerName"></association>
    <association property="verifyCity" column="partner_line_id" select="getVerifyCity"></association>
  </resultMap>

  <sql id="Base_Column_List">
    id, status, partner_line_id, interview_plan_id, partner_id, deadline, interviewer,
    recorder, process_info, record_time, summary, auth_code, pass_pdf_url, pass_image_url, expiry_date,
    latest_log_message, pass_reason, certify_file, create_time, update_time, approve_time,
    partner_enter_time, interviewer_enter_time,deleted
  </sql>
  <insert id="insertSelective" keyColumn="id" keyProperty="record.id" useGeneratedKeys="true">
    insert into hy_partner_interview
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="record.status != null">
        status,
      </if>
      <if test="record.partnerLineId != null">
        partner_line_id,
      </if>
      <if test="record.interviewPlanId != null">
        interview_plan_id,
      </if>
      <if test="record.partnerId != null">
        partner_id,
      </if>
      <if test="record.deadline != null">
        deadline,
      </if>
      <if test="record.interviewer != null">
        interviewer,
      </if>
      <if test="record.recorder != null">
        recorder,
      </if>
      <if test="record.processInfo != null">
        process_info,
      </if>
      <if test="record.recordTime != null">
        record_time,
      </if>
      <if test="record.summary != null">
        summary,
      </if>
      <if test="record.authCode != null">
        auth_code,
      </if>
      <if test="record.passPdfUrl != null">
        pass_pdf_url,
      </if>
      <if test="record.passImageUrl != null">
        pass_image_url,
      </if>
      <if test="record.expiryDate != null">
        expiry_date,
      </if>
      <if test="record.latestLogMessage != null">
        latest_log_message,
      </if>
      <if test="record.passReason != null">
        pass_reason,
      </if>
      <if test="record.certifyFile != null">
        certify_file,
      </if>
      <if test="record.createTime != null">
        create_time,
      </if>
      <if test="record.updateTime != null">
        update_time,
      </if>
      <if test="record.approveTime != null">
        approve_time,
      </if>
      <if test="record.partnerEnterTime != null">
        partner_enter_time,
      </if>
      <if test="record.interviewerEnterTime != null">
        interviewer_enter_time,
      </if>
      <if test="record.deleted != null">
        deleted,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="record.status != null">
        #{record.status},
      </if>
      <if test="record.partnerLineId != null">
        #{record.partnerLineId},
      </if>
      <if test="record.interviewPlanId != null">
        #{record.interviewPlanId},
      </if>
      <if test="record.partnerId != null">
        #{record.partnerId},
      </if>
      <if test="record.deadline != null">
        #{record.deadline},
      </if>
      <if test="record.interviewer != null">
        #{record.interviewer},
      </if>
      <if test="record.recorder != null">
        #{record.recorder},
      </if>
      <if test="record.processInfo != null">
        #{record.processInfo},
      </if>
      <if test="record.recordTime != null">
        #{record.recordTime},
      </if>
      <if test="record.summary != null">
        #{record.summary},
      </if>
      <if test="record.authCode != null">
        #{record.authCode},
      </if>
      <if test="record.passPdfUrl != null">
        #{record.passPdfUrl},
      </if>
      <if test="record.passImageUrl != null">
        #{record.passImageUrl},
      </if>
      <if test="record.expiryDate != null">
        #{record.expiryDate},
      </if>
      <if test="record.latestLogMessage != null">
        #{record.latestLogMessage},
      </if>
      <if test="record.passReason != null">
        #{record.passReason},
      </if>
      <if test="record.certifyFile != null">
        #{record.certifyFile},
      </if>
      <if test="record.createTime != null">
        #{record.createTime},
      </if>
      <if test="record.updateTime != null">
        #{record.updateTime},
      </if>
      <if test="record.approveTime != null">
        #{record.approveTime},
      </if>
      <if test="record.partnerEnterTime != null">
        #{record.partnerEnterTime},
      </if>
      <if test="record.interviewerEnterTime != null">
        #{record.interviewerEnterTime},
      </if>
      <if test="record.deleted != null">
        #{record.deleted},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective">
    update hy_partner_interview
    <set>
      <if test="status != null">
        status = #{status},
      </if>
      <if test="partnerLineId != null">
        partner_line_id = #{partnerLineId},
      </if>
      <if test="interviewPlanId != null">
        interview_plan_id = #{interviewPlanId},
      </if>
      <if test="partnerId != null">
        partner_id = #{partnerId},
      </if>
      <if test="deadline != null">
        deadline = #{deadline},
      </if>
      <if test="interviewer != null">
        interviewer = #{interviewer},
      </if>
      <if test="recorder != null">
        recorder = #{recorder},
      </if>
      <if test="processInfo != null">
        process_info = #{processInfo},
      </if>
      <if test="recordTime != null">
        record_time = #{recordTime},
      </if>
      <if test="summary != null">
        summary = #{summary},
      </if>
      <if test="authCode != null">
        auth_code = #{authCode},
      </if>
      <if test="passCode != null">
        pass_code = #{passCode},
      </if>
      <if test="passPdfUrl != null">
        pass_pdf_url = #{passPdfUrl},
      </if>
      <if test="passImageUrl != null">
        pass_image_url = #{passImageUrl},
      </if>
      <if test="passTime != null">
        pass_time = #{passTime},
      </if>
      <if test="expiryDate != null">
        expiry_date = #{expiryDate},
      </if>
      <if test="latestLogMessage != null">
        latest_log_message = #{latestLogMessage},
      </if>
      <if test="passReason != null">
        pass_reason = #{passReason},
      </if>
      <if test="certifyFile != null">
        certify_file = #{certifyFile},
      </if>
      <if test="createTime != null">
        create_time = #{createTime},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime},
      </if>
      <if test="approveTime != null">
        approve_time = #{approveTime},
      </if>
      <if test="partnerEnterTime != null">
        partner_enter_time = #{partnerEnterTime},
      </if>
      <if test="interviewerEnterTime != null">
        interviewer_enter_time = #{interviewerEnterTime},
      </if>
      <if test="deleted != null">
        deleted = #{deleted},
      </if>
    </set>
    <where>
      1 = 0
      <if test="id != null">
        or id = #{id}
      </if>
      <if test="interviewPlanId != null">
        or interview_plan_id = #{interviewPlanId}
      </if>
    </where>
  </update>

  <!-- 根据加盟商id查询面试信息 -->
  <select id="queryByPartnerId" resultMap="PartnerInterviewInfoVO">
    SELECT t1.id interviewId, t1.id interview_id, t2.id as interviewPlanId, `status`, start_time, end_time, room_id, t1.partner_id partnerId, t1.interviewer interviewerId
      FROM hy_partner_interview t1
      LEFT JOIN hy_partner_interview_plan t2 ON t1.interview_plan_id = t2.id
      WHERE t1.partner_id = #{partnerId}
      AND t1.deleted = 0
      AND t2.deleted = 0
  </select>
  <select id="queryPartnerName" resultType="string">
    SELECT username
      FROM hy_partner_user_info
      WHERE partner_id = #{partnerId}
  </select>
  <select id="queryInterviewerName" resultType="string">
    SELECT name
      FROM enterprise_user
      WHERE deleted = 0
      AND user_id = #{interview}
  </select>

  <!-- 根据会议 id 获取面试官 id -->
  <select id="getInterviewerByInterviewPlanId" resultMap="partnerEnterInterviewVO">
    SELECT interviewer, interviewer interviewer_id, partner_id
      FROM hy_partner_interview
      WHERE interview_plan_id = #{interviewPlanId} and deleted = 0
  </select>

  <!-- 获取通知函详情 -->
  <select id="getPassLetterDetail" resultMap="passLetterDetail">
    SELECT auth_code, pass_code, pass_pdf_url, pass_image_url,
      expiry_date, pass_time as createTime,
      partner_id, partner_line_id
      FROM hy_partner_interview
      WHERE interview_plan_id = #{interviewPlanId} and deleted = 0
  </select>
  <!-- 获取意向开店区域 -->
  <select id="getVerifyCity" resultType="string">
    SELECT area_path
      FROM hy_open_area_info
      WHERE id = (
        SELECT want_shop_area
          FROM hy_partner_intent_info
          WHERE partner_line_id = #{partner_line_id}
      )
  </select>
  <select id="selectByPrimaryKeySelective" resultType="com.cool.store.entity.HyPartnerInterviewDO">
    select
    <include refid="Base_Column_List"/>
    from
    hy_partner_interview
    where
    id = #{interviewId}

  </select>

  <!-- 生成通过函 pdf 后修改 -->
  <update id="updatePassLetterInfo">
    UPDATE hy_partner_interview
      <set>
        <if test="passCode != null and passCode != ''">
          pass_code = #{passCode},
        </if>
        <if test="passPdfUrl != null and passPdfUrl != ''">
          pass_pdf_url = #{passPdfUrl},
        </if>
        <if test="passImageUrl != null and passImageUrl != ''">
          pass_image_url = #{passImageUrl},
        </if>
        <if test="expiryDate != null and expiryDate != ''">
          expiry_date = #{expiryDate}
        </if>
      </set>
      WHERE id = #{interviewId}
  </update>
  <update id="batchUpdateInterviewStatus">
    UPDATE hy_partner_interview
      <set>
        <if test="status != null and status != ''">
          status = #{status},
        </if>
        <if test="deleted != null ">
          deleted = #{deleted},
        </if>
      </set>
      WHERE id in
      <foreach collection="interviewIds" item="item" index="index" open="(" separator="," close=")">
        #{item}
      </foreach>
  </update>

    <!-- 根据 interviewId 获取意向区域 -->
  <select id="getVerifyCityByInterviewId" resultType="java.lang.String">
    SELECT area_path
      FROM hy_open_area_info
      WHERE id = (
        SELECT want_shop_area
          FROM hy_partner_intent_info
          WHERE partner_line_id = (
          SELECT partner_line_id
            FROM hy_partner_interview
            WHERE id = #{interviewId} and deleted = 0
        )
      )
  </select>
  <select id="getInterviewBaseInfoList" resultType="com.cool.store.entity.HyPartnerInterviewDO">
    select
    *
    from hy_partner_interview_plan hpip
    left join hy_partner_interview hpi on hpi.interview_plan_id = hpip.id
    <where>
        <if test="record.status != null">
            and hpi.status = #{record.status}
        </if>
        <if test="record.startTime != null">
            and hpip.start_time &gt;= #{record.startTime}
        </if>
        <if test="record.endTime != null">
            and hpip.end_time &lt;= #{record.endTime}
        </if>
        <if test="record.deleted != null">
            and hpi.deleted = #{record.deleted} and hpip.deleted = #{record.deleted}
        </if>
        <if test="record.isPartnerInterview != null">
            and hpip.is_partner_interview = #{record.isPartnerInterview}
        </if>
    </where>

  </select>


  <select id="getOverTimeReserveInterviewList" resultType="com.cool.store.entity.HyPartnerInterviewDO">
    SELECT
    <include refid="Base_Column_List"></include>
    FROM hy_partner_interview_plan where
    partner_line_id in ( select id from hy_partner_line_info
    WHERE workflow_flow_stage = #{workflowStage}
    AND workflow_flow_status = #{workflowStatus}
    AND deadline &lt;= NOW()
    AND deleted = 0) and deleted = 0
  </select>
  <!-- 根据面试计划id获取面试信息 -->
  <select id="getInterviewInfoByInterviewPlanId" resultType="com.cool.store.entity.HyPartnerInterviewDO">
    select
    <include refid="Base_Column_List"/>
    from
    hy_partner_interview
    where
    interview_plan_id = #{interviewPlanId}
  </select>

  <!-- 修改面试状态 -->
  <update id="updateInterviewStatus">
    UPDATE hy_partner_interview
      SET status = #{status}
      WHERE interview_plan_id = #{interviewPlanId}
  </update>
  <update id="batchUpdateStatusByLineIds">
    UPDATE hy_partner_interview
    <set>
      <if test="status != null and status != ''">
        status = #{status}
      </if>
    </set>
    WHERE partner_line_id in
    <foreach collection="lineIds" item="item" index="index" open="(" separator="," close=")">
      #{item}
    </foreach>
    and deleted = 0

  </update>

  <!-- 获取面试对应的线索id -->
  <select id="getLineId" resultType="java.lang.String">
    SELECT partner_line_id
      FROM hy_partner_interview
      WHERE interview_plan_id = #{interviewPlanId}
  </select>
  <select id="getInterviewBaseInfoListByLineIds" resultType="com.cool.store.entity.HyPartnerInterviewDO">
    select <include refid="Base_Column_List"/> from hy_partner_interview where partner_line_id in
    <foreach collection="lineIds" item="item" index="index" open="(" separator="," close=")">
      #{item}
    </foreach>
    and deleted = 0
  </select>

  <!-- 获取面试流程状态 -->
  <select id="getStatus" resultType="java.lang.String">
    SELECT status
      FROM hy_partner_interview
      WHERE interview_plan_id = #{interviewPlanId}
  </select>
  <select id="queryByPartnerLineId"  resultMap="PartnerInterviewInfoVO">
    SELECT t1.id interviewId, t1.id interview_id, t2.id as interviewPlanId, `status`, start_time, end_time, room_id, t1.partner_id partnerId, t1.interviewer interviewerId
    FROM hy_partner_interview_plan  t2
           LEFT JOIN hy_partner_interview t1 ON t1.interview_plan_id = t2.id
    WHERE t2.partner_line_id = #{partnerLineId}
      AND t1.deleted = 0
      AND t2.deleted = 0
  </select>

  <!-- 根据 room_id 添加 process_info -->
  <update id="addVideoUrl">
    UPDATE hy_partner_interview
      SET process_info = IF(process_info IS NULL, #{videoUrl}, CONCAT(process_info, ',' ,#{videoUrl}))
      WHERE interview_plan_id = (
        SELECT id
          FROM hy_partner_interview_plan
          WHERE room_id = #{roomId}
      )
  </update>

  <select id="hasVideoUrls" resultType="java.lang.Boolean">
    SELECT COUNT(*)
      FROM hy_partner_interview
      WHERE FIND_IN_SET(#{videoUrl}, process_info)
  </select>
  <select id="getInterviewInfoByQualifyVerifyId" resultType="com.cool.store.entity.HyPartnerInterviewDO">
    SELECT
      hpi.id, hpi.status, hpi.partner_line_id, hpi.interview_plan_id
    FROM
      hy_partner_interview hpi
        LEFT JOIN hy_partner_certification_info hpci ON hpci.partner_interview_id = hpi.id
    WHERE
      hpci.qualify_verify_id = #{qualifyVerifyId}

	AND hpi.deleted = 0
  </select>

  <update id="updateLineId">
    update hy_partner_interview
    set partner_line_id = #{newLineId} , partner_id = #{newPartnerId}
    where partner_line_id = #{oldLineId}
  </update>


</mapper>