Skip to content

Commit

Permalink
[UPDATE] 添加部门管理相关功能及单元测试
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Yeh <[email protected]>
  • Loading branch information
alan-yeh committed Dec 1, 2024
1 parent c4be9c0 commit d586be0
Show file tree
Hide file tree
Showing 6 changed files with 611 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@

package central.studio.dashboard.controller.organization.controller;

import central.data.organization.Department;
import central.data.organization.Unit;
import central.starter.web.param.IdsParams;
import central.starter.web.query.IdQuery;
import central.studio.dashboard.controller.organization.param.DepartmentParams;
import central.studio.dashboard.controller.organization.param.UnitParams;
import central.studio.dashboard.controller.organization.query.DepartmentListQuery;
import central.studio.dashboard.controller.organization.query.UnitListQuery;
import central.studio.dashboard.logic.organization.UnitLogic;
import central.validation.group.Insert;
Expand All @@ -54,12 +57,18 @@
@RequiresAuthentication
@RequestMapping("/dashboard/api/organization/units")
public class UnitController {
public interface Permissions {

public interface Permissions {
String VIEW = "organization:unit:view";
String ADD = "organization:unit:add";
String EDIT = "organization:unit:edit";
String DELETE = "organization:unit:delete";


String DEPARTMENT_VIEW = "organization:unit:department:view";
String DEPARTMENT_ADD = "organization:unit:department:add";
String DEPARTMENT_EDIT = "organization:unit:department:edit";
String DEPARTMENT_DELETE = "organization:unit:department:delete";
}

@Setter(onMethod_ = @Autowired)
Expand Down Expand Up @@ -127,4 +136,68 @@ public Unit update(@RequestBody @Validated({Update.class, Default.class}) UnitPa
public long delete(@Validated IdsParams params, @RequestAttribute String accountId, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.deleteByIds(params.getIds(), accountId, tenant);
}

/**
* 按条件查询组织机构列表
*
* @param query 查询
* @param tenant 租户标识
* @return 分页结果
*/
@GetMapping("/departments")
public List<Department> listDepartments(@Validated DepartmentListQuery query, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.listDepartmentBy(query.build(), null, tenant);
}

/**
* 根据主键查询组织机构详情
*
* @param query 查询
* @param tenant 租户标识
* @return 详情
*/
@GetMapping("/departments/details")
public Department detailsDepartment(@Validated IdQuery<Department> query, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.findDepartmentById(query.getId(), tenant);
}

/**
* 新增组织机构
*
* @param params 组织机构入参
* @param accountId 当前登录帐号
* @param tenant 租户标识
* @return 新增后组织机构数据
*/
@PostMapping("/departments")
public Department addDepartment(@RequestBody @Validated({Insert.class, Default.class}) DepartmentParams params, @RequestAttribute String accountId, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.insertDepartment(params.toInput(), accountId, tenant);
}

/**
* 更新组织机构
*
* @param params 组织机构数据
* @param accountId 当前登录帐号
* @param tenant 租户标识
* @return 更新后组织机构数据
*/
@PutMapping("/departments")
public Department updateDepartment(@RequestBody @Validated({Update.class, Default.class}) DepartmentParams params, @RequestAttribute String accountId, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.updateDepartment(params.toInput(), accountId, tenant);
}

/**
* 根据主键删除组织机构数据
*
* @param params 组织机构数据
* @param accountId 当前登录帐号
* @param tenant 租户标识
* @return 受影响数据行数
*/
@DeleteMapping("/departments")
public long deleteDepartment(@Validated IdsParams params, @RequestAttribute String accountId, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.deleteDepartmentByIds(params.getIds(), accountId, tenant);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* MIT License
*
* Copyright (c) 2022-present Alan Yeh <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package central.studio.dashboard.controller.organization.param;

import central.data.organization.DepartmentInput;
import central.validation.Label;
import central.validation.group.Insert;
import central.validation.group.Update;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Department Params
* <p>
* 部门入参
*
* @author Alan Yeh
* @since 2024/12/01
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DepartmentParams {

@Label("主键")
@Null(groups = Insert.class)
@NotBlank(groups = Update.class)
@Size(min = 1, max = 32, groups = Insert.class)
private String id;

@Label("所属单位主键")
@NotBlank
@Size(min = 1, max = 32)
private String unitId;

@Label("父部门主键")
@Size(max = 32)
private String parentId;

@Label("标识")
@NotBlank
@Size(min = 1, max = 32)
@Pattern(regexp = "^[0-9a-zA-Z_-]+$", message = "${label}[${property}]只能由数字、英文字母、中划线、下划线组成")
@Pattern(regexp = "^(?!-)(?!_).*$", message = "${label}[${property}]不能由中划线或下划线开头")
@Pattern(regexp = "^(?!.*?[-_]$).*$", message = "${label}[${property}]不能由中划线或下划线结尾")
@Pattern(regexp = "^(?!.*(--).*$).*$", message = "${label}[${property}]不能出现连续中划线")
@Pattern(regexp = "^(?!.*(__).*$).*$", message = "${label}[${property}]不能出现连续下划线")
@Pattern(regexp = "^(?!.*(-_|_-).*$).*$", message = "${label}[${property}]不能出现连续中划线、下划线")
private String code;

@Label("名称")
@NotBlank
@Size(min = 1, max = 50)
private String name;

@Label("排序号")
@NotNull
private Integer order;

public DepartmentInput toInput() {
return DepartmentInput.builder()
.id(this.getId())
.unitId(this.getUnitId())
.parentId(this.getParentId())
.code(this.getCode())
.name(this.getName())
.order(this.getOrder())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public Conditions<Area> build() {
conditions.eq(Area::getParentId, this.getParentId());
}

// 精确搜索
// 精确字段搜索
if (Stringx.isNotEmpty(this.getId())) {
conditions.eq(Area::getId, this.getId());
}
Expand All @@ -88,7 +88,7 @@ public Conditions<Area> build() {
conditions.eq(Area::getType, this.getType());
}

// 模糊搜索
// 模糊字段搜索
for (String keyword : this.getKeywords()) {
conditions.and(filter -> filter.like(Area::getCode, keyword).or().like(Area::getName, keyword));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* MIT License
*
* Copyright (c) 2022-present Alan Yeh <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package central.studio.dashboard.controller.organization.query;

import central.data.organization.Department;
import central.lang.Stringx;
import central.sql.query.Conditions;
import central.starter.web.query.ListQuery;
import central.validation.Label;
import central.validation.group.Insert;
import central.validation.group.Update;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Null;
import jakarta.validation.constraints.Size;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serial;

/**
* Department List Query
* <p>
* 部门列表查询
*
* @author Alan Yeh
* @since 2024/12/01
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class DepartmentListQuery extends ListQuery<Department> {

Check warning on line 52 in central-dashboard/src/main/java/central/studio/dashboard/controller/organization/query/DepartmentListQuery.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Non-serializable class with 'serialVersionUID'

Non-serializable class `DepartmentListQuery` defines a 'serialVersionUID' field
@Serial
private static final long serialVersionUID = -5747968457061279810L;

@Label("单位主键")
@Null(groups = Insert.class)
@NotBlank(groups = Update.class)
@Size(min = 1, max = 32, groups = Insert.class)
private String unitId;

@Label("父节点主键")
private String parentId;

@Label("主键")
private String id;

@Label("名称")
private String name;

@Label("标识")
private String code;

@Override
public Conditions<Department> build() {
var conditions = Conditions.of(Department.class)
.eq(Department::getUnitId, this.getUnitId());

if (Stringx.isNotEmpty(this.getParentId())) {
conditions.eq(Department::getParentId, this.getParentId());
}

// 精确字段搜索
if (Stringx.isNotEmpty(this.getId())) {
conditions.eq(Department::getId, this.getId());
}
if (Stringx.isNotEmpty(this.getName())) {
conditions.like(Department::getName, this.getName());
}
if (Stringx.isNotEmpty(this.getCode())) {
conditions.like(Department::getCode, this.getCode());
}

// 模糊字段搜索
for (String keyword : this.getKeywords()) {
conditions.and(filter -> filter.like(Department::getCode, keyword).or().like(Department::getName, keyword));
}

return conditions;
}
}
Loading

0 comments on commit d586be0

Please sign in to comment.