<?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.zt.plat.module.qms.resource.record.dal.mapper.RecordApplyMapper">

    <resultMap id="RecordApplyJoinPageVOMap" type="com.zt.plat.module.qms.resource.record.controller.admin.recordapply.vo.RecordApplyJoinPageVO">
        <!-- recordApply fields -->
        <id column="id" property="id"/>
        <result column="title" property="title"/>
        <result column="businessType" property="businessType"/>
        <result column="businessStatus" property="businessStatus"/>
        <result column="applyUserId" property="applyUserId"/>
        <result column="applyUserName" property="applyUserName"/>
        <result column="applyDepartmentId" property="applyDepartmentId"/>
        <result column="applyDepartmentName" property="applyDepartmentName"/>
        <result column="reviewRange" property="reviewRange"/>
        <result column="targetUserIds" property="targetUserIds"/>
        <result column="formData" property="formData"/>
        <result column="applyContent" property="applyContent"/>
        <result column="permanently" property="permanently"/>
        <result column="applyStartDate" property="applyStartDate"/>
        <result column="applyEndDate" property="applyEndDate"/>
        <result column="remark" property="remark"/>
        <!-- recordApplyDetail fields -->
        <result column="applyDetailId" property="applyDetailId"/>
        <result column="applyId" property="applyId"/>
        <result column="documentId" property="documentId"/>
        <result column="TargetType" property="TargetType"/>
        <result column="targetId" property="targetId"/>
        <!-- recordRecord fields -->
        <result column="recordId" property="recordId"/>
        <result column="categoryId" property="categoryId"/>
        <result column="code" property="code"/>
        <result column="name" property="name"/>
        <result column="version" property="version"/>
        <result column="currentFlag" property="currentFlag"/>
        <result column="makeDate" property="makeDate"/>
        <result column="submitFlag" property="submitFlag"/>
        <result column="recordStatus" property="recordStatus"/>
        <result column="expirationDate" property="expirationDate"/>
        <result column="effectiveDate" property="effectiveDate"/>
        <result column="expirationStatus" property="expirationStatus"/>
        <result column="cancelFlag" property="cancelFlag"/>
        <result column="collectionFlag" property="collectionFlag"/>
        <!-- 额外字段 -->
        <result column="targetName" property="reviewUserName"/>
    </resultMap>

    <select id="selectApplyByStatus" resultType="com.zt.plat.module.qms.resource.record.dal.dataobject.recordapply.RecordApplyDO">
        SELECT
        tra.ID AS id,
        tra.TTL AS title,
        tra.BSN_TP AS businessType,
        tra.BSN_STS AS businessStatus,
        tra.FLW_INSC_ID AS flowInstanceId,
        tra.APL_USER_ID AS applyUserId,
        tra.APL_USER_NAME AS applyUserName,
        tra.APL_DEPT_ID AS applyDepartmentId,
        tra.APL_DEPT_NAME AS applyDepartmentName,
        tra.RVW_TM AS reviewTime,
        tra.RVW_RNG AS reviewRange,
        tra.ASN_TGT AS assignTarget,
        tra.TGT_DEPT_IDS AS targetDepartmentIds,
        tra.TGT_USER_IDS AS targetUserIds,
        tra.FORM_DAT AS formData,
        tra.APL_CNTT AS applyContent,
        tra.CHG_BFR AS changeBefore,
        tra.CHG_AFT AS changeAfter,
        tra.PMNT AS permanently,
        tra.APL_STRT_DT AS applyStartDate,
        tra.APL_END_DT AS applyEndDate,
        tra.SYS_DEPT_CD AS systemDepartmentCode,
        tra.RMK AS remark,
        tra.DELETED AS deleted,
        tra.CREATE_TIME AS createTime,
        tra.CREATE_BY AS createBy,
        tra.UPDATE_TIME AS updateTime,
        tra.UPDATE_BY AS updateBy,
        tra.TENANT_ID AS tenantId
        FROM T_RCD_APL tra
        LEFT JOIN T_RCD_APL_DTL trad ON tra.ID = trad.APL_ID
        WHERE tra.DELETED = 0
        AND tra.BSN_TP = #{businessType}
        <if test="documentIds != null and documentIds.size() > 0">
            AND trad.DOC_ID IN
            <foreach collection="documentIds" item="docId" open="(" separator="," close=")">
                #{docId}
            </foreach>
        </if>
        <if test="businessStatuses != null and businessStatuses.size() > 0">
            AND tra.BSN_STS IN
            <foreach collection="businessStatuses" item="status" open="(" separator="," close=")">
                #{status}
            </foreach>
        </if>
    </select>

    <select id="recordDistributePage" resultMap="RecordApplyJoinPageVOMap">
        SELECT tra.ID            AS id,
               tra.BSN_TP        AS businessType,
               tra.BSN_STS       AS businessStatus,
               tra.APL_USER_ID   AS applyUserId,
               tra.APL_USER_NAME AS applyUserName,
               tra.CREATE_TIME   AS createTime,
               trad.DOC_ID       AS documentId,
               trad.TGT_TP       AS TargetType,
               trad.TGT_ID       AS targetId,
               trr.NAME          AS name,
               trr.VER           AS version
        FROM T_RCD_APL_DTL trad
                 LEFT JOIN T_RCD_APL tra ON tra.ID = trad.APL_ID
                 LEFT JOIN T_RCD_RCD trr ON trr.ID = trad.DOC_ID
        WHERE tra.APL_USER_ID = #{respVO.applyUserId}
          AND tra.BSN_TP = '文件分发'
    </select>

    <select id="recordUpdatePage" resultMap="RecordApplyJoinPageVOMap">
        SELECT tra.ID            AS id,
               tra.TTL           AS title,
               tra.BSN_TP        AS businessType,
               tra.APL_USER_ID   AS applyUserId,
               tra.APL_USER_NAME AS applyUserName,
               tra.APL_DEPT_ID   AS applyDepartmentId,
               tra.APL_DEPT_NAME AS applyDepartmentName,
               tra.RVW_RNG       AS reviewRange,
               tra.TGT_USER_IDS  AS targetUserIds,
               tra.FORM_DAT      AS formData,
               tra.APL_CNTT      AS applyContent,
               tra.PMNT          AS permanently,
               tra.APL_STRT_DT   AS applyStartDate,
               tra.APL_END_DT    AS applyEndDate,
               tra.RMK           AS remark,
               tra.CREATE_TIME   AS createTime,
               trad.ID           AS applyDetailId,
               trad.APL_ID       AS applyId,
               trad.DOC_ID       AS documentId,
               trad.TGT_TP       AS TargetType,
               trad.TGT_ID       AS targetId,
               trad.TGT_NAME     AS targetName,
               trr.ID            AS recordId,
               trr.CTGR_ID       AS categoryId,
               trr.CD            AS code,
               trr.NAME          AS name,
               trr.VER           AS version,
               trr.CRNT_FLG      AS currentFlag,
               trr.MKE_DT        AS makeDate,
               trr.SBM_FLG       AS submitFlag,
               trr.RCD_STS       AS recordStatus,
               trr.EXPR_DT       AS expirationDate,
               trr.EFCT_DT       AS effectiveDate,
               trr.EXPR_STS      AS expirationStatus,
               trr.CNL_FLG       AS cancelFlag,
               trr.COLT_FLG      AS collectionFlag
        FROM T_RCD_APL_DTL trad
                 LEFT JOIN T_RCD_APL tra ON tra.ID = trad.APL_ID
                 LEFT JOIN T_RCD_RCD trr ON trr.ID = trad.DOC_ID
        WHERE tra.APL_USER_ID = #{userId}
          AND tra.BSN_TP = '文件分发'
    </select>

    <select id="recordInvalidPage" resultMap="RecordApplyJoinPageVOMap">
        SELECT
        tra.ID AS id,
        tra.TTL AS title,
        tra.BSN_TP AS businessType,
        tra.BSN_STS AS businessStatus,
        tra.APL_USER_ID AS applyUserId,
        tra.APL_USER_NAME AS applyUserName,
        tra.APL_DEPT_ID AS applyDepartmentId,
        tra.APL_DEPT_NAME AS applyDepartmentName,
        tra.CREATE_TIME AS createTime,
        tra.APL_CNTT AS applyContent,
        trad.DOC_ID AS documentId,
        trad.TGT_TP AS targetType,
        trad.TGT_ID AS targetId,
        trad.TGT_NAME AS targetName,
        trr.CTGR_ID AS categoryId,
        trr.CD AS code,
        trr.NAME AS name,
        trr.VER AS version,
        trr.EXPR_DT AS expirationDate,
        trr.EFCT_DT AS effectiveDate,
        trr.EXPR_STS AS expirationStatus
        FROM T_RCD_APL tra
        LEFT JOIN T_RCD_APL_DTL trad ON tra.ID = trad.APL_ID
        LEFT JOIN T_RCD_RCD trr ON trr.ID = trad.DOC_ID
        WHERE tra.APL_USER_ID = #{respVO.applyUserId}
        AND tra.BSN_TP = '文件作废'
    </select>

</mapper>