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

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

/**
 * 采购流程信息 Mapper
 *
 * @author dp
 */
@Mapper
public interface OaPurchaseMapper extends BaseMapperX<OaPurchaseDO> {

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

    }

}