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

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

/**
 * 报销流程信息 Mapper
 *
 * @author dp
 */
@Mapper
public interface OaExpenseMapper extends BaseMapperX<OaExpenseDO> {

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

    }

}