package cn.iocoder.yudao.module.bpm.dal.mysql.oa.leave;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.leave.vo.OaLeavePageReqVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.leave.OaLeaveDO;
import cn.iocoder.yudao.module.system.api.permission.dto.DeptDataPermissionRespDTO;
import org.apache.ibatis.annotations.Mapper;

/**
 * 请假流程信息 Mapper
 *
 * @author dp
 */
@Mapper
public interface OaLeaveMapper extends BaseMapperX<OaLeaveDO> {

    default PageResult<OaLeaveDO> selectPage(OaLeavePageReqVO reqVO) {
        LambdaQueryWrapperX<OaLeaveDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<OaLeaveDO>()
                .likeIfPresent(OaLeaveDO::getEmployeeName, reqVO.getEmployeeName())
                .likeIfPresent(OaLeaveDO::getEmployeePhone, reqVO.getEmployeePhone())
                .eqIfPresent(OaLeaveDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(OaLeaveDO::getPostId, reqVO.getPostId())
                .eqIfPresent(OaLeaveDO::getLeaveType, reqVO.getLeaveType())
                .betweenIfPresent(OaLeaveDO::getStartDate, reqVO.getStartDate())
                .betweenIfPresent(OaLeaveDO::getEndDate, reqVO.getEndDate())
                .eqIfPresent(OaLeaveDO::getAuditStatus, reqVO.getAuditStatus())
                .betweenIfPresent(OaLeaveDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(OaLeaveDO::getId);
        // 数据权限
        DeptDataPermissionRespDTO deptDataPermission = reqVO.getDeptDataPermission();
        if (deptDataPermission != null) {
            if (deptDataPermission.getAll()) {
                // 全部数据权限
            } else {
                if (CollUtil.isNotEmpty(deptDataPermission.getDeptIds()) && deptDataPermission.getSelf()) {
                    lambdaQueryWrapperX.and(wrapper ->
                            wrapper.in(OaLeaveDO::getDeptId, deptDataPermission.getDeptIds()))
                            .or(wrapper -> wrapper.eq(OaLeaveDO::getCreator, reqVO.getUserId()));
                } else if (CollUtil.isNotEmpty(deptDataPermission.getDeptIds())) {
                    lambdaQueryWrapperX.in(OaLeaveDO::getCreator, deptDataPermission.getDeptIds());
                } else if (deptDataPermission.getSelf()) {
                    lambdaQueryWrapperX.eq(OaLeaveDO::getCreator, reqVO.getUserId());
                } else {
                    return PageResult.empty();
                }
            }
        } else {
            return PageResult.empty();
        }
        return selectPage(reqVO, lambdaQueryWrapperX);

    }

}