package cn.xluobo.business.sys.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.xluobo.business.sys.admin.domain.req.ReqSearchSysUser;
import cn.xluobo.business.sys.admin.repo.mapper.ISysUserMapper;
import cn.xluobo.business.sys.admin.service.ISysUserService;
import cn.xluobo.business.sys.admin.service.ISysUserTenantService;
import cn.xluobo.core.page.RespPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ISysUserServiceImpl extends ServiceImpl<ISysUserMapper, SysUser> implements ISysUserService {
    @Autowired
    private ISysUserTenantService userTenantService;

    @Override
    public UserDetails loadUserDetailByUserName(String userName) {
        QueryWrapper<SysUser> qw = new QueryWrapper<>();
        qw.eq("username",userName);
        SysUser sysUser = baseMapper.selectOne(qw);
        if(null == sysUser){
            throw new UsernameNotFoundException("username: " + userName + " do not exist!");
        }
        Long tenantId = userTenantService.selectUserDefaultTenant(sysUser.getUserId());
        if(ObjectUtil.isEmpty(tenantId)) {
            throw new AccountExpiredException("账户无关联机构或机构已过期!");
        }
//        LoginUser loginUser = LoginUser.buildBySysUser(sysUser);
//        loginUser.setAuthorities(new ArrayList<>());
        return null;
//        return loadUserExtendInfo(sysUser,null);
    }

    @Override
    public List<SysUser> selectUserList(ReqSearchSysUser reqSearchSysUser, RespPage page) {
        return baseMapper.selectUserList(reqSearchSysUser, page);
    }

    @Override
    public boolean checkUsernameUnique(String username) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("username",username);
        int count = this.count(qw);
        if(count == 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public SysUser getByBindByWechatUserId(Long wechatUserId) {
        QueryWrapper<SysUser> qw = new QueryWrapper<>();
        qw.eq("wechat_user_id",wechatUserId);
        return this.getOne(qw);
    }

    @Override
    public SysUser getByUserName(String username) {
        QueryWrapper<SysUser> qw = new QueryWrapper<>();
        qw.eq("username",username);
        SysUser sysUser = baseMapper.selectOne(qw);
        return sysUser;
    }

    @Override
    public boolean checkSuperTenantUserHasRole(Long userId, String roleCode) {
        int userRoleCount = baseMapper.selectSuperTenantUserRoleCount(userId, roleCode);
        return userRoleCount > 0;
    }

    @Override
    public boolean checkUserHasAnyRole(String userId, String tenantId, String[] roleCodes) {
        int hasAnyRoleCount = baseMapper.selectUserHasAnyRoleCount(userId, tenantId, roleCodes);
        return hasAnyRoleCount > 0;
    }

}
