package cn.iocoder.yudao.module.system.dal.mysql.user;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
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.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.tenant.core.db.TenantBaseDO;
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthLoginReqVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserPageReqVO;
import cn.iocoder.yudao.module.system.controller.app.user.UserInfoVO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

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

@Mapper
public interface AdminUserMapper extends BaseMapperX<AdminUserDO> {

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

    default AdminUserDO selectByUsernameRescue(String username) {
        QueryWrapper<AdminUserDO> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username)
                .ne("user_type", "02");
        return selectOne(wrapper);
    }

    default AdminUserDO selectByUsername(String username, Long tenantId,String userType) {
        return selectOne(new LambdaQueryWrapperX<AdminUserDO>().eq(AdminUserDO::getUsername, username).eq(ObjectUtil.isNotEmpty(tenantId),TenantBaseDO::getTenantId, tenantId)
                .eq(ObjectUtil.isNotEmpty(userType), AdminUserDO::getUserType, userType).last("limit 1"));
    }

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

    default AdminUserDO selectByEmail(String email, Long tenantId,String userType) {
        return selectOne(Wrappers.<AdminUserDO>lambdaQuery().eq(AdminUserDO::getEmail, email).eq(ObjectUtil.isNotEmpty(tenantId),TenantBaseDO::getTenantId, tenantId)
                .eq(ObjectUtil.isNotEmpty(userType), AdminUserDO::getUserType, userType).last("limit 1"));
    }

    default AdminUserDO selectByMobile(String mobile) {
        Long tenantId = null;
        try {
            LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
            tenantId = loginUser.getTenantId();

        } catch (Exception e) {

        }
        return getUserByMobileWithoutTenant(mobile, tenantId,null);
    }

    default PageResult<AdminUserDO> selectPage(UserPageReqVO reqVO, Collection<Long> deptIds) {
        return selectPage(reqVO, new LambdaQueryWrapperX<AdminUserDO>()
//                .likeIfPresent(AdminUserDO::getUsername, reqVO.getUsername())
                .likeIfPresent(AdminUserDO::getMobile, reqVO.getMobile())
                .eq(ObjectUtil.isNotEmpty(reqVO.getTenantId()), AdminUserDO::getTenantId, reqVO.getTenantId())
                .notIn(CollUtil.isNotEmpty(reqVO.getUserIds()), AdminUserDO::getId, reqVO.getUserIds())
                // 这里加了个模糊查询，用户名称搜索时可以模糊查用户昵称
                .likeIfPresent(AdminUserDO::getNickname, reqVO.getUsername())
                .eq(ObjectUtil.isNotEmpty(reqVO.getUserType()),AdminUserDO::getUserType, reqVO.getUserType())
                .eqIfPresent(AdminUserDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(AdminUserDO::getCreateTime, reqVO.getCreateTime())
                .inIfPresent(AdminUserDO::getDeptId, deptIds)
                .orderByDesc(AdminUserDO::getId));
    }

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

    default List<AdminUserDO> selectListByStatus(Integer status) {
        return selectList(AdminUserDO::getStatus, status);
    }

    default List<AdminUserDO> selectListByDeptIds(Collection<Long> deptIds) {
        return selectList(AdminUserDO::getDeptId, deptIds);
    }

    List<AdminUserDO> getUsersByRoleRescue(@Param("roleCode") String roleCode, @Param("searchValue") String searchValue, @Param("deptIds") List<String> deptIds);

    IPage<AdminUserDO> getUsersByRoleRescueTwo(Page<AdminUserDO> page, @Param("roleCode") String roleCode, @Param("searchValue") String searchValue, @Param("deptIds") List<String> deptIds);


    Integer getStudentNumberByUserId(Long jlId);

    List<String> getStudentNumber(Long id);

    AdminUserDO selectUserByPhone(String phone);

    AdminUserDO getUserByMobileWithoutTenant(@Param("phoneNumber") String phoneNumber, @Param("tenantId") Long tenantId,@Param("userType") String userType);

    int updateSetOpenId(@Param("userId") Long userId, @Param("openId") String openId);

    int updateCusInfo(@Param("entity") UserInfoVO userInfoVO);

    AdminUserDO getUserByMobileTenantIgnore(String mobile);

    List<AdminUserDO> selectByRoleId(Long roleId);

    /**
     * 根据用户名，查询用户信息(排除租户id)
     *
     * @param reqVO
     * @return
     */
    Map<String, Object> queryUserIgnoreTenant(AuthLoginReqVO reqVO);
}
