package com.njcn.rdms.module.system.dal.mysql.user;

import com.njcn.rdms.framework.common.pojo.PageResult;
import com.njcn.rdms.framework.mybatis.core.mapper.BaseMapperX;
import com.njcn.rdms.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.njcn.rdms.module.system.controller.admin.user.vo.user.UserPageReqVO;
import com.njcn.rdms.module.system.dal.dataobject.user.AdminUserDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.List;

@Mapper
public interface AdminUserMapper extends BaseMapperX<AdminUserDO> {

    default AdminUserDO selectByUsername(String username) {
        return selectOne(AdminUserDO::getUsername, username);
    }

    default AdminUserDO selectByEmail(String email) {
        return selectOne(AdminUserDO::getEmail, email);
    }

    default AdminUserDO selectByMobile(String mobile) {
        return selectOne(AdminUserDO::getMobile, mobile);
    }

    default PageResult<AdminUserDO> selectPage(UserPageReqVO reqVO, Collection<Long> deptIds, Collection<Long> userIds) {
        return selectPage(reqVO, new LambdaQueryWrapperX<AdminUserDO>()
                .likeIfPresent(AdminUserDO::getUsername, reqVO.getUsername())
                .likeIfPresent(AdminUserDO::getNickname, reqVO.getNickname())
                .likeIfPresent(AdminUserDO::getMobile, reqVO.getMobile())
                .likeIfPresent(AdminUserDO::getCompany, reqVO.getCompany())
                .eqIfPresent(AdminUserDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(AdminUserDO::getCreateTime, reqVO.getCreateTime())
                .inIfPresent(AdminUserDO::getDeptId, deptIds)
                .inIfPresent(AdminUserDO::getId, userIds)
                .orderByAsc(AdminUserDO::getSort)
                .orderByDesc(AdminUserDO::getId));
    }

    default List<AdminUserDO> selectListByNickname(String nickname) {
        return selectList(new LambdaQueryWrapperX<AdminUserDO>()
                .like(AdminUserDO::getNickname, nickname)
                .orderByAsc(AdminUserDO::getSort)
                .orderByDesc(AdminUserDO::getId));
    }

    default List<AdminUserDO> selectListByStatus(Integer status) {
        return selectList(new LambdaQueryWrapperX<AdminUserDO>()
                .eq(AdminUserDO::getStatus, status)
                .orderByAsc(AdminUserDO::getSort)
                .orderByDesc(AdminUserDO::getId));
    }

    default List<AdminUserDO> selectListByDeptIds(Collection<Long> deptIds) {
        return selectList(new LambdaQueryWrapperX<AdminUserDO>()
                .in(AdminUserDO::getDeptId, deptIds)
                .orderByAsc(AdminUserDO::getSort)
                .orderByDesc(AdminUserDO::getId));
    }

    default List<AdminUserDO> selectListByPositionIds(Collection<Long> positionIds) {
        return selectList(new LambdaQueryWrapperX<AdminUserDO>()
                .in(AdminUserDO::getPositionId, positionIds)
                .orderByAsc(AdminUserDO::getSort)
                .orderByDesc(AdminUserDO::getId));
    }

}
