/*
 *      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.security.system.user.feign;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.security.system.user.entity.User;
import org.security.system.user.entity.UserDept;
import org.security.system.user.entity.UserInfo;
import org.security.system.user.entity.UserOauth;
import org.security.system.user.enums.UserEnum;
import org.security.system.user.service.IUserService;
import org.security.system.user.vo.UserVO;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户服务Feign实现类
 *
 * @author Chill
 */
@NonDS
@RestController
@AllArgsConstructor
public class UserClient implements IUserClient {

	private final IUserService service;

	@Override
	@GetMapping(USER_INFO_BY_ID)
	public R<User> userInfoById(Long userId) {
		return R.data(service.getById(userId));
	}

	@Override
	@GetMapping(USER_INFO_BY_ACCOUNT)
	public R<User> userByAccount(String tenantId, String account) {
		return R.data(service.userByAccount(tenantId, account));
	}

	@Override
	@GetMapping(USER_INFO)
	public R<UserInfo> userInfo(String tenantId, String account) {
		return R.data(service.userInfo(tenantId, account));
	}

	@Override
	@GetMapping(USER_INFO_BY_TYPE)
	public R<UserInfo> userInfo(String tenantId, String account, String userType) {
		return R.data(service.userInfo(tenantId, account, UserEnum.of(userType)));
	}

	@Override
	@PostMapping(USER_AUTH_INFO)
	public R<UserInfo> userAuthInfo(@RequestBody UserOauth userOauth) {
		return R.data(service.userInfo(userOauth));
	}

	@Override
	@PostMapping(SAVE_USER)
	public R saveUser(@RequestBody User user) {
		return R.data(service.submit(user).isSuccess());
	}

	@Override
	@PostMapping(REMOVE_USER)
	public R removeUser(String tenantIds) {
		return R.data(service.remove(Wrappers.<User>query().lambda().in(User::getTenantId, Func.toStrList(tenantIds))));
	}

	@Override
	public R<UserVO> getUserByCode(String code) {
		return R.data(service.getUserByCode(code));
	}
	@Override
	@GetMapping(BRANCH_LEADERS)
	public R<List<UserVO>> branchLeaders(Long userId) {
		return R.data(service.getBranchLeaders(userId));
	}

	@Override
	@PostMapping(SYN_REMOVE_USER_DEPT)
	public R removeUserDept(List<String> userIds) {
		return R.status(service.removeUserDept(userIds));
	}

	@Override
	@PostMapping(SYN_SAVE_USER_DEPT)
	public R saveUserDept(List<UserDept> userDeptList) {
		return R.status(service.saveUserDept(userDeptList));
	}

	@Override
	@PostMapping(SYN_SAVE_USER_DATA)
	public R saveUserData(List<User> userList) {
		return R.status(service.saveUserData(userList));
	}

	@Override
	@GetMapping(TASK_GET_USER_BYROLEID)
	public R<List<UserVO>> getUserByRoleId(@RequestParam String roleId) {
		return R.data(service.getUserByRoleId(roleId));
	}

	@Override
	@GetMapping(HEAD_LEADERS)
	public R<List<UserVO>> headLeaders(Long userId) {
		return R.data(service.getHeadLeaders(userId));
	}

	@Override
	@GetMapping(HEAD_SEC_LEADERS)
	public R<List<UserVO>> headSecLeaders(Long userId) {
		return R.data(service.getHeadSecLeaders(userId));
	}

	@Override
	@GetMapping(HEAD_SAFERS)
	public R<List<UserVO>> headSafers(Long userId) {
		return R.data(service.getHeadSafers(userId));
	}

	@Override
	@GetMapping(BRANCH_SAFERS)
	public R<List<UserVO>> branchSafers(Long userId) {
		return R.data(service.getBranchSafers(userId));
	}

	@Override
	@GetMapping(SUB_LEADERS)
	public R<List<UserVO>> subLeaders(Long userId) {
		return R.data(service.getSubLeaders(userId));
	}

	@Override
	@GetMapping(SUB_SAFERS)
	public R<List<UserVO>> subSafers(Long userId) {
		return R.data(service.getSubSafers(userId));
	}

	@Override
	@GetMapping(DEPT_LEADERS)
	public R<List<UserVO>> deptLeaders(Long userId) {
		return R.data(service.getDeptLeader(userId));
	}

	@Override
	@GetMapping(HEAD_SEC_CENTER_ALL)
	public R<List<UserVO>> headSecCenterAll() {
		return R.data(service.getHeadSecCenterAll());
	}

	@Override
	@GetMapping(BRANCH_SEC_CENTER_ALL)
	public R<List<UserVO>> branchSecCenterAll() {
		return R.data(service.getBranchSecCenterAll());
	}

	@Override
	@GetMapping(USERS_BY_IDS)
	public R getUsersByUserIds(String userIds) {
		return service.getUsersByUserIds(userIds);
	}

	@Override
	@GetMapping(USER_HAS_ROLE_ALIAS)
	public R userHasRoleAlias(String tenantId, Long userId, String roleAlias) {
		return service.userHasRoleAlias(tenantId, userId, roleAlias);
	}
}
