/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.system.oauthuser.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.entity.UserInfo;
import org.springblade.modules.system.service.IUserService;
import org.springblade.system.oauthuser.dto.UserInfoEx;
import org.springblade.system.oauthuser.entity.UserOauth;
import org.springblade.system.oauthuser.vo.UserOauthVO;
import org.springblade.system.oauthuser.mapper.UserOauthMapper;
import org.springblade.system.oauthuser.service.IUserOauthService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;

/**
 * 用户第三方认证表 服务实现类
 *
 * @author BladeX
 * @since 2022-10-17
 */
@Service
public class UserOauthServiceImpl extends ServiceImpl<UserOauthMapper, UserOauth> implements IUserOauthService {

	@Autowired
	private IUserService userService;

	@Override
	public IPage<UserOauthVO> selectUserOauthPage(IPage<UserOauthVO> page, UserOauthVO userOauth) {
		return page.setRecords(baseMapper.selectUserOauthPage(page, userOauth));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public UserInfoEx userInfo(UserOauth userOauth) {
		UserOauth uo = this.getOne(Wrappers.<UserOauth>query().lambda().eq(UserOauth::getUuid, userOauth.getUuid()).eq(UserOauth::getSource, userOauth.getSource()));
		UserInfoEx userInfoEx = null;
		if(uo == null){
			User user = userService.lambdaQuery().eq(User::getAccount, userOauth.getUserName()).one();
			if(user != null){
				userOauth.setUserId(user.getId());
				this.save(userOauth);
			}else{
				throw new ServiceException("授权登录失败，请联系平台管理员先开通社区平台账号");
			}
		}else{
			userOauth.setUserId(uo.getUserId());
		}
		UserInfo userInfo = userService.userInfo(userOauth.getUserId());
		userInfoEx = new UserInfoEx();
		userInfoEx.setUser(userInfo.getUser());
		userInfoEx.setPermissions(userInfo.getPermissions());
		userInfoEx.setRoles(userInfo.getRoles());
		userInfoEx.setOauthId(Func.toStr(userOauth.getId()));
		return userInfoEx;
	}

}
