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

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.receive.vo.OaReceivePageReqVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.receive.OaReceiveDO;
import cn.iocoder.yudao.module.system.api.permission.dto.DeptDataPermissionRespDTO;
import org.apache.ibatis.annotations.Mapper;

/**
 * 领用流程信息 Mapper
 *
 * @author dp
 */
@Mapper
public interface OaReceiveMapper extends BaseMapperX<OaReceiveDO> {

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

    }

}