<?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.CaseEventMapper">

  <resultMap id="caseEventMap" type="digital.laboratory.platform.entrustment.entity.CaseEvent">
    <id property="id" column="id"/>
    <result property="caseNo" column="case_no"/>
    <result property="thirdPartySysNo" column="third_party_sys_no"/>
    <result property="caseName" column="case_name"/>
    <result property="caseType" column="case_type"/>
    <result property="happenTime" column="happen_time"/>
    <result property="caseAddress" column="case_address"/>
    <result property="caseArea" column="case_area"/>
    <result property="caseOwnOrgId" column="case_own_org_id"/>
    <result property="caseRank" column="case_rank"/>
    <result property="caseBrief" column="case_brief"/>
    <result property="comments" column="comments"/>
    <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="caseEventVOMap" type="digital.laboratory.platform.entrustment.vo.CaseEventVO" extends="caseEventMap">
    <result property="caseOrgName" column="case_org_name"/>
    <result property="caseOrgShortName" column="case_org_short_name"/>
    <result property="caseCreateUserName" column="case_create_user_name"/>
    <result property="caseUpdateUserName" column="case_update_user_name"/>
    <result property="caseAreaName" column="case_area_name"/>

    <result property="caseEntrustmentCount" column="case_entrustment_count"/>
    <result property="caseEvidenceCount" column="case_evidence_count"/>
  </resultMap>

  <select id="getCaseVoList" parameterType="String"  resultType="digital.laboratory.platform.entrustment.vo.CaseEventVO">
    SELECT cj.*,
           org.name AS case_org_name,
           org.alias AS case_org_short_name,
           (
             SELECT user.name
             FROM dlp_base.sys_user user
             WHERE user.user_id=cj.create_by
           ) AS case_create_user_name,
           (
            SELECT user.name
            FROM dlp_base.sys_user user
            WHERE user.user_id=cj.update_by
           ) AS case_update_user_name,
           (
               SELECT area.name
               FROM dlp_base.b_area area
               WHERE area.id=cj.case_area
           ) AS case_area_name,

           (
             SELECT COUNT(*)
             FROM b_entrustment e
             WHERE e.case_id=cj.id
           ) AS case_entrustment_count,
           (
             SELECT COUNT(*)
             FROM b_case_evidence ce
             WHERE ce.case_id=cj.id
           ) AS case_evidence_count
    FROM b_case_event cj, dlp_base.sys_org org WHERE org.org_id=cj.case_own_org_id
    <if test="fromDate !=null and fromDate !=''">
      AND cj.happen_time &gt;= STR_TO_DATE(#{fromDate},'%Y-%m-%d')
    </if>
    <if test="toDate !=null and toDate !=''">
      AND cj.happen_time &lt; STR_TO_DATE(#{toDate},'%Y-%m-%d')
    </if>
    <if test="caseName !=null and caseName !=''">
      AND cj.case_name like CONCAT('%',#{caseName},'%')
    </if>
    <if test="caseType !=null and caseType !=''">
      AND cj.case_type = #{caseType}
    </if>
  </select>


  <select id="getCaseVoPage" resultType="digital.laboratory.platform.entrustment.vo.CaseEventVO">
      SELECT cj.*,
             org.name  AS case_org_name,
             org.alias AS case_org_short_name,
             e.accept_no,
             (SELECT user.name
              FROM dlp_base.sys_user user
      WHERE user.user_id=cj.create_by
          ) AS case_create_user_name
          , (
      SELECT user.name
      FROM dlp_base.sys_user user
      WHERE user.user_id=cj.update_by
          ) AS case_update_user_name
          , (
      SELECT area.name
      FROM dlp_base.b_area area
      WHERE area.id=cj.case_area
          ) AS case_area_name

      FROM b_case_event cj
          LEFT JOIN dlp_base.sys_org org ON org.org_id=cj.case_own_org_id
          LEFT JOIN b_entrustment e ON cj.id=e.case_id
          ${ew.customSqlSegment}
  </select>


    <select id="getCaseVOById" resultType="digital.laboratory.platform.entrustment.vo.CaseEventVO" parameterType="String">
        SELECT cj.*,

               org.name AS case_org_name,
               org.alias AS case_org_short_name,

               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=cj.create_by
               ) AS case_create_user_name,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=cj.update_by
               ) AS case_update_user_name,
               (
                   SELECT area.name
                   FROM dlp_base.b_area area
                   WHERE area.id=cj.case_area
               ) AS case_area_name,

               (
                   SELECT COUNT(*)
                   FROM b_entrustment e
                   WHERE e.case_id=cj.id
               ) AS case_entrustment_count,
               (
                   SELECT COUNT(*)
                   FROM b_case_evidence ce
                   WHERE ce.case_id=cj.id
               ) AS case_evidence_count
        FROM b_case_event cj
                 LEFT JOIN dlp_base.sys_org org on org.org_id=cj.case_own_org_id
        WHERE cj.id = #{id}
    </select>

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


</mapper>
