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

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

/**
 * 合同续签流程信息 Mapper
 *
 * @author dp
 */
@Mapper
public interface OaRenewMapper extends BaseMapperX<OaRenewDO> {

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

    }

}