package com.zt.plat.module.qms.resource.record.dal.mapper;

import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;
import com.zt.plat.module.qms.resource.record.controller.admin.recordapplydetail.vo.RecordApplyDetailPageReqVO;
import com.zt.plat.module.qms.resource.record.dal.dataobject.recordapplydetail.RecordApplyDetailDO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordapplydetail.vo.RecordApplyDetailJoinVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;

/**
 * 文件记录通用申请明细 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface RecordApplyDetailMapper extends BaseMapperX<RecordApplyDetailDO> {

    default PageResult<RecordApplyDetailDO> selectPage(RecordApplyDetailPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<RecordApplyDetailDO>()
                .eqIfPresent(RecordApplyDetailDO::getApplyId, reqVO.getApplyId())
                .eqIfPresent(RecordApplyDetailDO::getDocumentId, reqVO.getDocumentId())
                .eqIfPresent(RecordApplyDetailDO::getPermanently, reqVO.getPermanently())
                .betweenIfPresent(RecordApplyDetailDO::getApplyStartDate, reqVO.getApplyStartDate())
                .betweenIfPresent(RecordApplyDetailDO::getApplyEndDate, reqVO.getApplyEndDate())
                .eqIfPresent(RecordApplyDetailDO::getSortNo, reqVO.getSortNo())
                .eqIfPresent(RecordApplyDetailDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .eqIfPresent(RecordApplyDetailDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(RecordApplyDetailDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(RecordApplyDetailDO::getId));
    }

    /**
     * 联合查询：根据文件ID和业务类型查询申请和申请明细
     * 对应SQL: SELECT tra.ID, tra.BSN_STS, tra.BSN_TP, tra.FLW_INSC_ID, trad.DOC_ID 
     *         FROM T_RCD_APL tra 
     *         LEFT JOIN T_RCD_APL_DTL trad ON tra.ID = trad.APL_ID 
     *         WHERE trad.DOC_ID = ? AND tra.BSN_TP = ?
     */
    @Select("SELECT tra.ID as applyId, tra.BSN_STS as businessStatus, tra.BSN_TP as businessType, " +
            "tra.FLW_INSC_ID as flowInstanceId, trad.ID as detailId, trad.DOC_ID as documentId, " +
            "trad.PMNT as permanently, trad.APL_STRT_DT as applyStartDate, trad.APL_END_DT as applyEndDate, " +
            "trad.SRT_NO as sortNo, trad.SYS_DEPT_CD as systemDepartmentCode, trad.RMK as remark " +
            "FROM t_rcd_apl tra " +
            "LEFT JOIN t_rcd_apl_dtl trad ON tra.ID = trad.APL_ID " +
            "WHERE trad.DOC_ID = #{documentId} AND tra.BSN_TP = #{businessType}")
    List<RecordApplyDetailJoinVO> selectJoinByDocumentIdAndBusinessType(Long documentId, String businessType);

}