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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.qms.resource.record.controller.admin.recordpermission.vo.RecordPermissionPageReqVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordrecord.vo.RecordJoinPermissionRespVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordrecord.vo.RecordRecordPageReqVO;
import com.zt.plat.module.qms.resource.record.dal.dataobject.recordpermission.RecordPermissionDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 记录权限 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface RecordPermissionMapper extends BaseMapperX<RecordPermissionDO> {

    default PageResult<RecordPermissionDO> selectPage(RecordPermissionPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<RecordPermissionDO>()
                .eqIfPresent(RecordPermissionDO::getParentId, reqVO.getParentId())
//                .eqIfPresent(RecordPermissionDO::getCategoryId, reqVO.getCategoryId())
                .eqIfPresent(RecordPermissionDO::getSourceType, reqVO.getSourceType())
                .eqIfPresent(RecordPermissionDO::getSourceId, reqVO.getSourceId())
                .eqIfPresent(RecordPermissionDO::getTargetType, reqVO.getTargetType())
                .eqIfPresent(RecordPermissionDO::getTargetId, reqVO.getTargetId())
                .eqIfPresent(RecordPermissionDO::getPermission, reqVO.getPermission())
                .eqIfPresent(RecordPermissionDO::getConfidentialLevel, reqVO.getConfidentialLevel())
                .eqIfPresent(RecordPermissionDO::getPermanently, reqVO.getPermanently())
                .betweenIfPresent(RecordPermissionDO::getExpirationDate, reqVO.getExpirationDate())
                .eqIfPresent(RecordPermissionDO::getExpirationStatus, reqVO.getExpirationStatus())
                .eqIfPresent(RecordPermissionDO::getCancelFlag, reqVO.getCancelFlag())
                .eqIfPresent(RecordPermissionDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .eqIfPresent(RecordPermissionDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(RecordPermissionDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(RecordPermissionDO::getId));
    }

    /**
     * 检查用户是否有指定分类的管理员权限
     * 查询条件：permission='管理员' AND
     * 1. 用户是管理员 ( targetId=userId AND targetType='用户')
     * 2. 或用户所在部门有权限 (targetType='部门' AND targetId in userDepts)
     * 3. 或用户的角色有权限 (targetType='角色' AND targetId in userRoles)
     */
    List<RecordPermissionDO> selectPermissionList(@Param("recordP") RecordPermissionDO recordP); // permission: 管理员/可编辑/可查看

    /**
     * 检查用户是否有记录的权限
     * 查询条件：permission='可查看' AND
     * 1. 用户 ( targetId=userId AND targetType='用户')
     * 2. 部门 (targetType='部门' AND targetId in userDepts)
     */
    List<RecordPermissionDO> selectRecordPermissionList(@Param("recordP") RecordPermissionDO recordP); // permission: 管理员/可编辑/可查看

    /**
     * 通过权限查文件
     *
     * @param page
     * @param recordP
     * @return
     */
    IPage<RecordJoinPermissionRespVO> selectRecordByPermission(Page<RecordPermissionPageReqVO> page, RecordPermissionPageReqVO recordP);

    // 通过文件权限查询
    IPage<RecordJoinPermissionRespVO> selectPermissionByRecord(Page<RecordPermissionPageReqVO> page, RecordRecordPageReqVO recordP);

    //  权限回收情况查询 TODO
    IPage<RecordJoinPermissionRespVO> getExpireAndCancelRecordPermissionList(Page<RecordPermissionPageReqVO> page, RecordRecordPageReqVO recordP);
}