优化组织创建

This commit is contained in:
2025-04-22 15:50:24 +08:00
parent de55ddbc48
commit fe89520621
8 changed files with 67 additions and 25 deletions

View File

@@ -17,10 +17,11 @@ onMounted(() => {
type: 'crud',
syncLocation: false,
headerToolbar: [
'reload',
{
type: 'action',
label: '新增组织',
level: 'primary',
icon: 'fa fa-plus',
label: '',
actionType: 'dialog',
dialog: {
title: '新增组织',
@@ -30,10 +31,17 @@ onMounted(() => {
method: 'post',
url: `${information.baseUrl}/organization/save`,
data: {
name: '${name}',
code: '${code|default:undefined}',
name: '${name|default:undefined}',
},
},
body: [
{
type: 'input-text',
name: 'code',
label: '组织编号',
placeholder: '不填则自动生成',
},
{
type: 'input-text',
name: 'name',
@@ -65,14 +73,37 @@ onMounted(() => {
hidden: true,
},
{
width: 150,
name: 'code',
label: '组织编号',
},
{
name: 'name',
label: '组织名称',
required: true,
},
{
width: 100,
label: '操作',
type: 'operation',
buttons: [
{
type: 'action',
icon: 'fa fa-trash',
label: '删除',
level: 'danger',
size: 'xs',
actionType: 'ajax',
api: {
method: 'delete',
url: `${information.baseUrl}/jsonapi/organization/\${id}`,
},
messages: {
success: '删除成功',
failed: '删除失败',
}
}
]
}
],
},
],

View File

@@ -5,7 +5,6 @@ import java.lang.annotation.Target;
import java.time.Instant;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.annotations.IdGeneratorType;
import org.hibernate.annotations.ValueGenerationType;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
@@ -39,6 +38,14 @@ public class SnowflakeId {
}
}
public static long nextId() {
return Snowflake.next();
}
public static String nextStrId() {
return String.valueOf(Snowflake.next());
}
private static class Snowflake {
/**
* 起始的时间戳

View File

@@ -48,4 +48,14 @@ public class Department {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")
@ToString.Exclude
private Set<Department> children;
@Getter
@Setter
@ToString
public static class SaveVO {
private Long id;
private String name;
private Long organizationId;
private Long parentId;
}
}

View File

@@ -3,6 +3,7 @@ package com.lanyuanxiaoyao.server.entity;
import com.lanyuanxiaoyao.server.configuration.Constants;
import com.lanyuanxiaoyao.server.configuration.database.SnowflakeId;
import com.yahoo.elide.annotation.Include;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
@@ -32,7 +33,9 @@ public class Organization {
@GeneratedValue(generator = "snowflakeId")
@GenericGenerator(name = "snowflakeId", type = SnowflakeId.IdGenerator.class)
private Long id;
@Column(unique = true, nullable = false)
private String code;
@Column(nullable = false)
private String name;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "organization")

View File

@@ -1,7 +1,6 @@
package com.lanyuanxiaoyao.server.entity.mapper;
import com.lanyuanxiaoyao.server.entity.Department;
import com.lanyuanxiaoyao.server.entity.vo.DepartmentSaveVO;
import com.lanyuanxiaoyao.server.service.DepartmentService;
import com.lanyuanxiaoyao.server.service.OrganizationService;
import org.mapstruct.Mapper;
@@ -19,9 +18,9 @@ import org.mapstruct.Mapping;
DepartmentService.class,
}
)
public interface DepartmentMapper extends EntityMapper<Department, DepartmentSaveVO> {
public interface DepartmentMapper extends EntityMapper<Department, Department.SaveVO> {
@Mapping(target = "organization", source = "organizationId")
@Mapping(target = "parent", source = "parentId")
@Override
Department fromVO(DepartmentSaveVO saveVO);
Department fromVO(Department.SaveVO saveVO);
}

View File

@@ -1,5 +1,6 @@
package com.lanyuanxiaoyao.server.entity.mapper;
import com.lanyuanxiaoyao.server.configuration.database.SnowflakeId;
import com.lanyuanxiaoyao.server.entity.Organization;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@@ -10,9 +11,13 @@ import org.mapstruct.Mapping;
* @author lanyuanxiaoyao
* @version 20250327
*/
@Mapper
@Mapper(
imports = {
SnowflakeId.class
}
)
public interface OrganizationMapper extends EntityMapper<Organization, Organization.SaveVO> {
@Mapping(target = "code", defaultExpression = "java(String.valueOf(creationVO.getId()))")
@Mapping(target = "code", defaultExpression = "java(SnowflakeId.nextStrId())")
@Override
Organization fromVO(Organization.SaveVO creationVO);
}

View File

@@ -1,15 +0,0 @@
package com.lanyuanxiaoyao.server.entity.vo;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class DepartmentSaveVO {
private Long id;
private String name;
private Long organizationId;
private Long parentId;
}

View File

@@ -1,6 +1,7 @@
package com.lanyuanxiaoyao.server.service.base;
import com.blinkfox.fenix.jpa.FenixJpaRepository;
import org.springframework.transaction.annotation.Transactional;
public abstract class AbstractService<ENTITY, ID> {
public abstract FenixJpaRepository<ENTITY, ID> getRepository();
@@ -11,6 +12,7 @@ public abstract class AbstractService<ENTITY, ID> {
return getRepository().getReferenceById(id);
}
@Transactional(rollbackFor = Exception.class)
public ID save(ENTITY entity) {
ENTITY saved = getRepository().saveOrUpdateByNotNullProperties(entity);
return getId(saved);