package com.template.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.template.api.AdminAPI;
import com.template.common.utils.RSAUtils;
import com.template.config.MySecurity;
import com.template.model.dto.SaveAdminDto;
import com.template.model.dto.UpdateAdminDto;
import com.template.model.dto.UsersPageDto;
import com.template.model.pojo.*;
import com.template.model.result.CommonResult;
import com.template.model.vo.AdminVo;
import com.template.model.vo.UsersVo;
import com.template.services.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* 前端控制器
*
*
* @author ceshi
* @since 2023-12-13
*/
@RestController
public class AdminController implements AdminAPI {
@Autowired
AdminService adminService;
@Autowired
AdminMenuService adminMenuService;
@Autowired
PermissionSettingService permissionSettingService;
@Autowired
UsersService usersService;
@Autowired
MySecurity mySecurity;
@Autowired
OrganizationService organizationService;
@Override
public CommonResult getListPage(int permissionSettingId, int page, int size, String keyWord, String startTime, String endTime, String status) {
PermissionSetting permissionSetting = permissionSettingService.getById(permissionSettingId);
if (ObjectUtils.isEmpty(permissionSetting)) {
return CommonResult.fail("非法进入");
}
// 判断该用户是否拥有权限
// 判断该用户是否拥有权限
// String accountManagement = permissionSetting.getAccountManagement();
// if (!"0".equals(accountManagement) && !accountManagement.contains("4")) {
// return CommonResult.fail("此账号暂无该权限");
// }
if (ObjectUtils.isEmpty(page) && page <= 0) {
page = 1;
}
if (ObjectUtils.isEmpty(size) && size <= 0) {
size = 10;
}
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime), Admin::getCreateTime, startTime, endTime)
.eq(ObjectUtils.isNotEmpty(status), Admin::getStatus, status)
.like(ObjectUtils.isNotEmpty(keyWord), Admin::getName, keyWord)
.orderByDesc(Admin::getCreateTime);
IPage page1 = adminService.page(new Page<>(page, size), wrapper);
Page adminVoPage = new Page<>();
List records = page1.getRecords();
ArrayList adminVos = new ArrayList<>();
for (Admin record : records) {
AdminVo adminVo = new AdminVo();
BeanUtils.copyProperties(record,adminVo);
String adminMenuId = record.getAdminMenuId();
AdminMenu adminMenu = adminMenuService.getById(adminMenuId);
adminVo.setAdminMenu(adminMenu);
Users byId = usersService.getById(record.getUsersId());
if (ObjectUtils.isNotEmpty(byId)) {
adminVo.setCardNumber(byId.getCardNumber());
}
List checkOrgList= new ArrayList<>();
if (ObjectUtils.isNotEmpty(record.getCheckOrg())) {
List checkOrg= Arrays.asList(record.getCheckOrg().split(","));
for(int i=0;i wrapper=new LambdaQueryWrapper<>();
wrapper.eq(Users::getCardNumber,cardNumber);
Users users = usersService.getOne(wrapper);
if (ObjectUtils.isEmpty(users)) {
return CommonResult.fail("暂无该用户");
}
// 通过用户id找管理端用户,如果有就不能在绑定
// LambdaQueryWrapper wrapperA=new LambdaQueryWrapper<>();
// wrapperA.eq(Admin::getUsersId,users.getId());
// Admin one = adminService.getOne(wrapperA);
// if (ObjectUtils.isNotEmpty(one)) {
// return CommonResult.fail("该用户已绑定,请更换绑定用户");
// }
// 密码加密
String passWord = saveAdminDto.getPassWord();
try {
Admin admin = new Admin();
String encrypt = RSAUtils.encrypt(passWord, RSAUtils.getPublicKey(mySecurity.publicKey));
BeanUtils.copyProperties(saveAdminDto, admin);
admin.setPassWord(encrypt);
admin.setStatus("1");
// admin.setCheckOrg(saveAdminDto.getCheckOrg().toString());
admin.setUsersId(users.getId()+"");
adminService.save(admin);
return CommonResult.ok();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public CommonResult update(UpdateAdminDto updateAdminDto) {
String permissionSettingId = updateAdminDto.getPermissionSettingId();
PermissionSetting permissionSetting = permissionSettingService.getById(permissionSettingId);
if (ObjectUtils.isEmpty(permissionSetting)) {
return CommonResult.fail("非法进入");
}
// 判断该用户是否拥有权限
// 判断该用户是否拥有权限
String accountManagement = permissionSetting.getAccountManagement();
if (!"0".equals(accountManagement) && !accountManagement.contains("2")) {
return CommonResult.fail("此账号暂无该权限");
}
// 通过cardNumber找到对应的用户
String cardNumber = updateAdminDto.getCardNumber();
LambdaQueryWrapper wrapper=new LambdaQueryWrapper<>();
wrapper.eq(Users::getCardNumber,cardNumber);
Users users = usersService.getOne(wrapper);
if (ObjectUtils.isEmpty(users)) {
return CommonResult.fail("暂无该用户");
}
Admin admin = updateAdminDto.getAdmin();
Admin byId = adminService.getById(admin.getId());
if (ObjectUtils.isEmpty(byId)) {
return CommonResult.fail("该管理员不存在");
}
String userNumber = admin.getUserNumber();
String userNumber1 = byId.getUserNumber();
if (!userNumber.equals(userNumber1)) {
Admin result = adminService.getAdminByAccount(userNumber);
if (ObjectUtils.isNotEmpty(result)) {
return CommonResult.fail("该账号已存在");
}
}
// 当前可以重复
// 用户不能重复绑定
// String usersId = admin.getUsersId();
// String usersId1 = byId.getUsersId();
// if (!usersId.equals(usersId1)) {
// LambdaQueryWrapper wrapperA=new LambdaQueryWrapper<>();
// wrapperA.eq(Admin::getUsersId,users.getId());
// Admin one = adminService.getOne(wrapperA);
// if (ObjectUtils.isNotEmpty(one)) {
// return CommonResult.fail("该用户已绑定,请更换绑定用户");
// }
// }
try {
String passWord = admin.getPassWord();
String encrypt = RSAUtils.encrypt(passWord, RSAUtils.getPublicKey(mySecurity.publicKey));
admin.setPassWord(encrypt);
admin.setUsersId(users.getId()+"");
boolean update = adminService.updateById(admin);
if (update) {
return CommonResult.ok();
}
return CommonResult.fail();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public CommonResult delete(int permissionSettingId, int id) {
PermissionSetting permissionSetting = permissionSettingService.getById(permissionSettingId);
if (ObjectUtils.isEmpty(permissionSetting)) {
return CommonResult.fail("非法进入");
}
// 判断该用户是否拥有权限
// 判断该用户是否拥有权限
String accountManagement = permissionSetting.getAccountManagement();
if (!"0".equals(accountManagement) && !accountManagement.contains("3")) {
return CommonResult.fail("此账号暂无该权限");
}
boolean b = adminService.removeById(id);
if (b) {
return CommonResult.ok();
}
return CommonResult.fail();
}
@Override
public CommonResult getUsersPage(UsersPageDto usersPageDto) {
int size = usersPageDto.getSize();
Integer page = usersPageDto.getPage();
Integer permissionSettingId = usersPageDto.getPermissionSettingId();
String keyWord = usersPageDto.getKeyWord();
if (ObjectUtils.isEmpty(page) && page <= 0) {
page = 1;
}
if (ObjectUtils.isEmpty(size) && size <= 0) {
size = 10;
}
PermissionSetting permissionSetting = permissionSettingService.getById(permissionSettingId);
if (ObjectUtils.isEmpty(permissionSetting)) {
return CommonResult.fail("非法进入");
}
// 判断该用户是否拥有权限
// 判断该用户是否拥有权限
// String accountManagement = permissionSetting.getAccountManagement();
// if (!"0".equals(accountManagement) && !accountManagement.contains("1")) {
// return CommonResult.fail("此账号暂无该权限");
// }
LambdaQueryWrapper wrapperU = new LambdaQueryWrapper<>();
wrapperU.like(ObjectUtils.isNotEmpty(keyWord), Users::getCardNumber, keyWord)
.or()
.like(ObjectUtils.isNotEmpty(keyWord), Users::getUserName, keyWord);
IPage page1 = usersService.page(new Page<>(page, size), wrapperU);
IPage page2 = new Page<>();
BeanUtils.copyProperties(page1,page2);
List records = page1.getRecords();
ArrayList usersVos = new ArrayList<>();
for (Users record : records) {
UsersVo usersVo = new UsersVo();
BeanUtils.copyProperties(record,usersVo);
String department = record.getDepartment();
if ("0".equals(department)) {
usersVo.setDepartmentName("其他");
}else if ("1".equals(department)){
usersVo.setDepartmentName("学生");
}else if ("4".equals(department)){
usersVo.setDepartmentName("教职工");
}else if ("5".equals(department)){
usersVo.setDepartmentName("校友");
}else if ("6".equals(department)){
usersVo.setDepartmentName("访客");
}else if ("7".equals(department)){
usersVo.setDepartmentName("临时人员");
}
usersVos.add(usersVo);
}
page2.setRecords(usersVos);
return CommonResult.ok(page2);
}
@Override
public CommonResult userList(String adminMenuId) {
LambdaQueryWrapper wrapper=new LambdaQueryWrapper<>();
wrapper.eq(Admin::getAdminMenuId,adminMenuId);
List list = adminService.list(wrapper);
return CommonResult.ok(list);
}
}