mirror of
https://github.com/FatttSnake/Pinnacle-OA.git
synced 2026-04-06 07:21:24 +08:00
Added StaffManagement
This commit is contained in:
@@ -1,7 +1,16 @@
|
||||
package com.cfive.pinnacle.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import com.cfive.pinnacle.entity.common.ResponseCode;
|
||||
import com.cfive.pinnacle.entity.common.ResponseResult;
|
||||
import com.cfive.pinnacle.entity.permission.User;
|
||||
import com.cfive.pinnacle.service.IStaffService;
|
||||
import com.cfive.pinnacle.utils.WebUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -11,8 +20,31 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
* @author FatttSnake
|
||||
* @since 2023-04-30
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/staff")
|
||||
public class StaffController {
|
||||
private IStaffService staffService;
|
||||
|
||||
@Autowired
|
||||
public void setStaffService(IStaffService staffService) {
|
||||
this.staffService = staffService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@PreAuthorize("hasAnyAuthority('staff:manege:get', 'staff:admin:get')")
|
||||
public ResponseResult<List<User>> getAllStaff() {
|
||||
return ResponseResult.databaseSelectSuccess(staffService.getAllStaff(WebUtil.hasAuthority("staff:admin:get") ? null : WebUtil.getLoginUser().getUser().getDepartmentId()));
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
@PreAuthorize("hasAnyAuthority('staff:manege:modify', 'staff:admin:modify')")
|
||||
public ResponseResult<?> modifyStaff(@RequestBody User user) {
|
||||
log.info(user.toString());
|
||||
if (staffService.modifyStaff(user)) {
|
||||
return ResponseResult.databaseUpdateSuccess(null);
|
||||
} else {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_SAVE_ERROR, "error", null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
package com.cfive.pinnacle.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.annotation.Version;
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
@@ -63,7 +59,7 @@ public class Staff implements Serializable {
|
||||
/**
|
||||
* 生日
|
||||
*/
|
||||
@TableField("birth")
|
||||
@TableField(value = "birth",updateStrategy = FieldStrategy.IGNORED)
|
||||
private LocalDate birth;
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,6 +23,7 @@ public class ResponseCode {
|
||||
public static final int DATABASE_TIMEOUT_ERROR = 20035;
|
||||
public static final int DATABASE_CONNECT_ERROR = 20036;
|
||||
public static final int DATABASE_DATA_TO_LONG = 20037;
|
||||
public static final int DATABASE_DATA_VALIDATION_FAILED = 20038;
|
||||
|
||||
public static final int UNAUTHORIZED = 30010;
|
||||
public static final int ACCESS_DENIED = 30030;
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import com.cfive.pinnacle.entity.Department;
|
||||
import com.cfive.pinnacle.entity.Staff;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
@@ -54,6 +55,9 @@ public class User implements Serializable {
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long departmentId;
|
||||
|
||||
@TableField(exist = false)
|
||||
private Department department;
|
||||
|
||||
/**
|
||||
* 启用
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.cfive.pinnacle.exception;
|
||||
|
||||
public class DataValidationFailedException extends RuntimeException{
|
||||
public DataValidationFailedException() {
|
||||
super("Data validation failed");
|
||||
}
|
||||
|
||||
public DataValidationFailedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public DataValidationFailedException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public DataValidationFailedException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public DataValidationFailedException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.cfive.pinnacle.handler;
|
||||
|
||||
import com.cfive.pinnacle.entity.common.ResponseCode;
|
||||
import com.cfive.pinnacle.entity.common.ResponseResult;
|
||||
import com.cfive.pinnacle.exception.DataValidationFailedException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
@@ -32,6 +33,9 @@ public class CustomExceptionHandler {
|
||||
if (e instanceof DataIntegrityViolationException) {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_DATA_TO_LONG, e.getMessage(), null);
|
||||
}
|
||||
if (e instanceof DataValidationFailedException) {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_DATA_VALIDATION_FAILED, e.getMessage(), null);
|
||||
}
|
||||
|
||||
log.debug(e.getMessage(), e);
|
||||
|
||||
|
||||
@@ -2,7 +2,11 @@ package com.cfive.pinnacle.mapper;
|
||||
|
||||
import com.cfive.pinnacle.entity.Staff;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.cfive.pinnacle.entity.permission.User;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -14,5 +18,5 @@ import org.apache.ibatis.annotations.Mapper;
|
||||
*/
|
||||
@Mapper
|
||||
public interface StaffMapper extends BaseMapper<Staff> {
|
||||
|
||||
List<User> getAllStaff(@Param("departmentId")Long departmentId);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@ package com.cfive.pinnacle.service;
|
||||
|
||||
import com.cfive.pinnacle.entity.Staff;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.cfive.pinnacle.entity.permission.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -12,5 +15,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
||||
* @since 2023-04-30
|
||||
*/
|
||||
public interface IStaffService extends IService<Staff> {
|
||||
List<User> getAllStaff(Long departmentId);
|
||||
|
||||
boolean modifyStaff(User user);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
package com.cfive.pinnacle.service.impl;
|
||||
|
||||
import com.cfive.pinnacle.entity.Staff;
|
||||
import com.cfive.pinnacle.entity.permission.User;
|
||||
import com.cfive.pinnacle.exception.DataValidationFailedException;
|
||||
import com.cfive.pinnacle.mapper.StaffMapper;
|
||||
import com.cfive.pinnacle.mapper.permission.UserMapper;
|
||||
import com.cfive.pinnacle.service.IStaffService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.cfive.pinnacle.utils.WebUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 员工 服务实现类
|
||||
@@ -16,5 +24,41 @@ import org.springframework.stereotype.Service;
|
||||
*/
|
||||
@Service
|
||||
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements IStaffService {
|
||||
private StaffMapper staffMapper;
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Autowired
|
||||
public void setStaffMapper(StaffMapper staffMapper) {
|
||||
this.staffMapper = staffMapper;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setUserMapper(UserMapper userMapper) {
|
||||
this.userMapper = userMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> getAllStaff(Long departmentId) {
|
||||
return staffMapper.getAllStaff(departmentId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean modifyStaff(User user) {
|
||||
Staff newStaff = user.getStaff();
|
||||
user = userMapper.getOneById(user.getId());
|
||||
Staff oldStaff = user.getStaff();
|
||||
if (!WebUtil.hasAuthority("staff:admin:modify")) {
|
||||
if (!Objects.equals(user.getDepartmentId(), WebUtil.getLoginUser().getUser().getDepartmentId())) {
|
||||
throw new DataValidationFailedException();
|
||||
}
|
||||
}
|
||||
if (oldStaff == null) {
|
||||
newStaff.setUserId(user.getId());
|
||||
staffMapper.insert(newStaff);
|
||||
} else {
|
||||
newStaff.setId(oldStaff.getId());
|
||||
staffMapper.updateById(newStaff);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user