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

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="selectDetailAndRecordList" resultType="com.zt.plat.module.qms.resource.record.dal.dataobject.recordapplydetail.RecordApplyDetailDO">
        SELECT trad.ID          as id,
               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,
               trad.IS_MDF      as isModify,
               trad.MDF_CAUS    as modifyCause,
               trad.APL_STRT_DT as applyStartDate,
               trad.APL_END_DT  as applyEndDate,
               trr.NAME         AS recordName,
               trr.CD           AS recordCode,
               trr.VER          AS version,
               trr.PMNT         as recordPermanently,
               trr.EXPR_DT      as expirationDate
        FROM T_RCD_APL_DTL trad
                 LEFT JOIN T_RCD_RCD trr ON trad.DOC_ID = trr.ID
        WHERE trad.DELETED = 0
          and trad.APL_ID = #{applyId}
    </select>


    <select id="selectDistributionDetailAndRecordList" resultType="com.zt.plat.module.qms.resource.record.dal.dataobject.recordapplydetail.RecordApplyDetailDO">
        SELECT *
        FROM (SELECT trad.ID          as id,
                     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,
                     trad.IS_MDF      as isModify,
                     trad.MDF_CAUS    as modifyCause,
                     trad.APL_STRT_DT as applyStartDate,
                     trad.APL_END_DT  as applyEndDate,
                     trr.NAME         AS recordName,
                     trr.CD           AS recordCode,
                     trr.VER          AS version,
                     trr.PMNT         as recordPermanently,
                     trr.EXPR_DT      as expirationDate,
                     ROW_NUMBER() OVER (PARTITION BY trad.APL_ID ORDER BY trad.ID DESC) as rn
              FROM T_RCD_APL_DTL trad
                       LEFT JOIN T_RCD_RCD trr ON trad.DOC_ID = trr.ID
              WHERE trad.DELETED = 0
                and trad.APL_ID = #{applyId}) t
        WHERE t.rn = 1
    </select>

<!--    查询明细关联的申请记录-->
    <select id="selectDetailAndApplyList" resultType="com.zt.plat.module.qms.resource.record.controller.admin.recordapplydetail.vo.RecordApplyDetailJoinVO">
        SELECT trad.ID as id,
        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,
        trad.IS_MDF as isModify,
        trad.MDF_CAUS as modifyCause,
        trad.PMNT AS permanently,
        trad.FORM_DAT AS formData,
        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.TGT_USER_IDS AS targetUserIds,
        tra.APL_CNTT AS applyContent,
        tra.APL_STRT_DT as applyStartDate,
        tra.APL_END_DT as applyEndDate,
        # tra.APL_STRT_DT AS applyStartDate,
        # tra.APL_END_DT AS applyEndDate,
        tra.EXPR_DT AS expirationDate,
        tra.DELETED AS deleted
        FROM T_RCD_APL_DTL trad
        LEFT JOIN T_RCD_APL tra ON tra.ID = trad.APL_ID
        where tra.DELETED = 0 and (tra.BSN_TP = '文件分发' or tra.BSN_TP = '文件查看')
        <if test="reqVO.targetId != null">
            AND trr.TGT_ID = #{reqVO.targetId}
        </if>
        <if test="reqVO.targetName != null and reqVO.targetName != ''">
            AND trr.TGT_NAME LIKE CONCAT('%', #{reqVO.targetName}, '%')
        </if>
        <if test="reqVO.formData != null and reqVO.formData != ''">
            AND trr.FORM_DAT LIKE CONCAT('%', #{reqVO.formData}, '%')
        </if>
    </select>
    
</mapper>