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

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

/**
 * 出差流程信息 Mapper
 *
 * @author dp
 */
@Mapper
public interface OaBusinessMapper extends BaseMapperX<OaBusinessDO> {

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

    }

}