/**
 * 
 */
package org.springblade.resource;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.constant.TokenConstant;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.auth.utils.TokenUtil;
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.client.UserClient;
import org.springblade.system.client.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.bean.BeanUtil;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author wookvn
 *
 */
@RestController
@ApiIgnore
public class UserResource implements UserClient {
	
	@Autowired
	private IUserService userService;

	@Override
	public UserModel getById(Long id) {
		User userEntity = userService.getById(id);
		return BeanUtil.copyProperties(userEntity, UserModel.class);
	}

	@Override
	public List<UserModel> listByIds(List<Long> ids) {
		List<User> userList = userService.listByIds(ids);
		return BeanUtil.copyToList(userList, UserModel.class);
	}

	@Override
	public List<UserModel> listByDeptId(Long deptId) {
		List<User> userList = null;
		if(deptId == null) {
			userList = userService.list();
		}else {
			IPage<User> userPage = userService.selectUserPage(new Page<User>(1, 100), new User(), deptId, null);
			userList = userPage != null ? userPage.getRecords() : new ArrayList<>(0);
		}
		return BeanUtil.copyToList(userList, UserModel.class);
	}

	@Override
	public String genAccessToken(Long id) {
		UserInfo userInfo = userService.userInfo(id);
		Kv authInfo = TokenUtil.createAuthInfo(userInfo);
		return authInfo != null ? authInfo.getStr(TokenConstant.ACCESS_TOKEN) : null;
	}


	@Override
	public UserModel getByThirdUserId(String thirdUserId) {
		User userEntity =userService.getOne(new LambdaQueryWrapper<User>()
				.eq(User::getThirdUserId,thirdUserId)
		);
		return BeanUtil.copyProperties(userEntity, UserModel.class);
	}

	@Override
	public String genToken(Long id,String expireIn) {
		UserInfo userInfo = userService.userInfo(id);
		userInfo.setExpireIn(expireIn);
		Kv authInfo = TokenUtil.createAuthInfo(userInfo);
		return authInfo != null ? authInfo.getStr(TokenConstant.ACCESS_TOKEN) : null;
	}

	@Override
	public List<UserModel> listByRoleId(String roleId) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().apply(StringUtil.isNotBlank(roleId), "find_in_set({0},role_id)", roleId)
				.eq(User::getStatus, 1)
				.eq(User::getIsDeleted, 0);

		List<User> userList = userService.list(queryWrapper);
		return BeanUtil.copyToList(userList, UserModel.class);
	}
	@Override
	public List<UserModel> listByPostId(String postId) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().apply(StringUtil.isNotBlank(postId), "find_in_set({0},post_id)", postId)
				.eq(User::getStatus, 1)
				.eq(User::getIsDeleted, 0);

		List<User> userList = userService.list(queryWrapper);
		return BeanUtil.copyToList(userList, UserModel.class);
	}

	/**
	 * 根据租户id查询用户信息
	 */
	@Override
	public List<UserModel> listByTenantId(String tenantId) {

		List<User> list = userService.list(Wrappers.<User>lambdaQuery()
				.eq(User::getTenantId, tenantId));
		return BeanUtil.copyToList(list,UserModel.class);
	}
}
