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

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

/**
 * 用印流程信息 Mapper
 *
 * @author dp
 */
@Mapper
public interface OaStampMapper extends BaseMapperX<OaStampDO> {

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

    }

}