diff --git a/client/src/views/management/Organization.vue b/client/src/views/management/Organization.vue index 7b34213..ffa34c5 100644 --- a/client/src/views/management/Organization.vue +++ b/client/src/views/management/Organization.vue @@ -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: '删除失败', + } + } + ] + } ], }, ], diff --git a/src/main/java/com/lanyuanxiaoyao/server/configuration/database/SnowflakeId.java b/src/main/java/com/lanyuanxiaoyao/server/configuration/database/SnowflakeId.java index 929146b..7faa837 100644 --- a/src/main/java/com/lanyuanxiaoyao/server/configuration/database/SnowflakeId.java +++ b/src/main/java/com/lanyuanxiaoyao/server/configuration/database/SnowflakeId.java @@ -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 { /** * 起始的时间戳 diff --git a/src/main/java/com/lanyuanxiaoyao/server/entity/Department.java b/src/main/java/com/lanyuanxiaoyao/server/entity/Department.java index dad871a..c2d18e7 100644 --- a/src/main/java/com/lanyuanxiaoyao/server/entity/Department.java +++ b/src/main/java/com/lanyuanxiaoyao/server/entity/Department.java @@ -48,4 +48,14 @@ public class Department { @OneToMany(fetch = FetchType.LAZY, mappedBy = "parent") @ToString.Exclude private Set children; + + @Getter + @Setter + @ToString + public static class SaveVO { + private Long id; + private String name; + private Long organizationId; + private Long parentId; + } } diff --git a/src/main/java/com/lanyuanxiaoyao/server/entity/Organization.java b/src/main/java/com/lanyuanxiaoyao/server/entity/Organization.java index c5ceb8a..393b334 100644 --- a/src/main/java/com/lanyuanxiaoyao/server/entity/Organization.java +++ b/src/main/java/com/lanyuanxiaoyao/server/entity/Organization.java @@ -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") diff --git a/src/main/java/com/lanyuanxiaoyao/server/entity/mapper/DepartmentMapper.java b/src/main/java/com/lanyuanxiaoyao/server/entity/mapper/DepartmentMapper.java index 625a31a..c30113d 100644 --- a/src/main/java/com/lanyuanxiaoyao/server/entity/mapper/DepartmentMapper.java +++ b/src/main/java/com/lanyuanxiaoyao/server/entity/mapper/DepartmentMapper.java @@ -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 { +public interface DepartmentMapper extends EntityMapper { @Mapping(target = "organization", source = "organizationId") @Mapping(target = "parent", source = "parentId") @Override - Department fromVO(DepartmentSaveVO saveVO); + Department fromVO(Department.SaveVO saveVO); } diff --git a/src/main/java/com/lanyuanxiaoyao/server/entity/mapper/OrganizationMapper.java b/src/main/java/com/lanyuanxiaoyao/server/entity/mapper/OrganizationMapper.java index 4badb39..19bf077 100644 --- a/src/main/java/com/lanyuanxiaoyao/server/entity/mapper/OrganizationMapper.java +++ b/src/main/java/com/lanyuanxiaoyao/server/entity/mapper/OrganizationMapper.java @@ -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 { - @Mapping(target = "code", defaultExpression = "java(String.valueOf(creationVO.getId()))") + @Mapping(target = "code", defaultExpression = "java(SnowflakeId.nextStrId())") @Override Organization fromVO(Organization.SaveVO creationVO); } diff --git a/src/main/java/com/lanyuanxiaoyao/server/entity/vo/DepartmentSaveVO.java b/src/main/java/com/lanyuanxiaoyao/server/entity/vo/DepartmentSaveVO.java deleted file mode 100644 index b8967e0..0000000 --- a/src/main/java/com/lanyuanxiaoyao/server/entity/vo/DepartmentSaveVO.java +++ /dev/null @@ -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; -} diff --git a/src/main/java/com/lanyuanxiaoyao/server/service/base/AbstractService.java b/src/main/java/com/lanyuanxiaoyao/server/service/base/AbstractService.java index 485d31b..dc2f124 100644 --- a/src/main/java/com/lanyuanxiaoyao/server/service/base/AbstractService.java +++ b/src/main/java/com/lanyuanxiaoyao/server/service/base/AbstractService.java @@ -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 { public abstract FenixJpaRepository getRepository(); @@ -11,6 +12,7 @@ public abstract class AbstractService { return getRepository().getReferenceById(id); } + @Transactional(rollbackFor = Exception.class) public ID save(ENTITY entity) { ENTITY saved = getRepository().saveOrUpdateByNotNullProperties(entity); return getId(saved);