1
0
mirror of https://github.com/FatttSnake/Pinnacle-OA.git synced 2026-04-05 23:11:24 +08:00

Added StaffManagement

This commit is contained in:
2023-05-30 22:06:42 +08:00
parent 3aed237d5e
commit 804d056a89
17 changed files with 444 additions and 10 deletions

View File

@@ -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);
}
}
}

View File

@@ -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;
/**

View File

@@ -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;

View File

@@ -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;
/**
* 启用
*/

View File

@@ -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);
}
}

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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;
}
}

View File

@@ -2,4 +2,61 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cfive.pinnacle.mapper.StaffMapper">
<select id="getAllStaff" resultMap="staffMap">
select t_user.id as user_id,
t_user.username as user_username,
t_user.department_id as user_department_id,
t_user.deleted as user_deleted,
t_user.version as user_version,
ts.id as staff_id,
ts.user_id as staff_user_id,
ts.first_name as staff_first_name,
ts.last_name as staff_last_name,
ts.gender as staff_gender,
ts.birth as staff_birth,
ts.email as staff_email,
ts.tel as staff_tel,
ts.address as staff_address,
ts.deleted as staff_deleted,
ts.version as staff_version,
td.id as department_id,
td.name as department_name,
td.deleted as department_deleted,
td.version as department_version
from t_user
left join (select * from t_staff where deleted = 0) as ts on t_user.id = ts.user_id
left join (select * from t_department where deleted = 0) as td on td.id = t_user.department_id
where t_user.deleted = 0
<if test="departmentId != null">
and t_user.department_id = #{departmentId}
</if>
</select>
<resultMap id="staffMap" type="user">
<id property="id" column="user_id"/>
<result property="username" column="user_username"/>
<result property="departmentId" column="user_department_id"/>
<result property="deleted" column="user_deleted"/>
<result property="version" column="user_version"/>
<association property="staff" javaType="staff">
<id property="id" column="staff_id"/>
<result property="userId" column="staff_user_id"/>
<result property="firstName" column="staff_first_name"/>
<result property="lastName" column="staff_last_name"/>
<result property="gender" column="staff_gender"/>
<result property="birth" column="staff_birth"/>
<result property="email" column="staff_email"/>
<result property="tel" column="staff_tel"/>
<result property="address" column="staff_address"/>
<result property="deleted" column="staff_deleted"/>
<result property="version" column="staff_version"/>
</association>
<association property="department" javaType="department">
<id property="id" column="department_id"/>
<result property="name" column="department_name"/>
<result property="deleted" column="department_deleted"/>
<result property="version" column="department_version"/>
</association>
</resultMap>
</mapper>