<?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="digital.laboratory.platform.entrustment.mapper.IdentificationMapper">

  <resultMap id="identificationMap" type="digital.laboratory.platform.entrustment.entity.Identification">
    <id property="id" column="id"/>
    <result property="identificationNo" column="identification_no"/>
    <result property="status" column="status"/>
    <result property="createTime" column="create_time"/>
    <result property="createBy" column="create_by"/>
    <result property="updateTime" column="update_time"/>
    <result property="updateBy" column="update_by"/>
  </resultMap>

  <resultMap id="identificationVO" type="digital.laboratory.platform.entrustment.vo.IdentificationVO" extends="identificationMap">
    <result property="entrustmentId" column="entrustment_id"/>
    <result property="entrustmentNo" column="entrustment_no"/>
    <result property="entrustmentTime" column="entrustment_time"/>
    <result property="acceptTime" column="accept_time"/>
    <result property="entrustmentStatus" column="entrustment_status"/>
  </resultMap>

  <!-- 根据案件取 IdentificationVO 列表, 一个Identification可能会有多行, 一个委托对应一行, xxxx, 取消了-->
  <select id="getIdentificationVOList" resultMap="identificationVO"
          resultType="digital.laboratory.platform.entrustment.vo.IdentificationVO" parameterType="String">
    SELECT i.*,
           e.id as entrustment_id,
           e.entrustment_no,
           e.entrustment_time,
           e.accept_time,
           e.status as entrustment_status
    FROM b_entrustment e, b_identification i
    WHERE e.identification_id=i.id AND e.case_id=#{caseId};
  </select>
  <!-- 根据 caseId 取 Identification 列表, 供新建的补充委托选择老的 Identification(鉴定), 以使新建的委托作为老的鉴定的一部分 -->
  <select id="getIdentificationListByCaseId" resultMap="identificationMap"
          resultType="digital.laboratory.platform.entrustment.entity.Identification" parameterType="String">
    SELECT *
    FROM b_identification
    WHERE id IN (
    SELECT e.identification_id
    FROM b_entrustment e
    WHERE e.identification_id IS NOT NULL AND e.status <![CDATA[ >= ]]> 8 AND e.status <![CDATA[ <= ]]> 90 AND e.case_id=#{caseId})
  </select>


  <!-- 取最大鉴定号, 目的是为了生成新的鉴定号 -->
  <select id="getMaxIdentificationNo" parameterType="String" resultType="java.lang.String">
    SELECT MAX(identification_no) AS maxno
    FROM b_identification
    WHERE identification_no LIKE concat(#{prefix}, '%')
  </select>
</mapper>