尝试增加组织架构

This commit is contained in:
2025-03-27 18:38:42 +08:00
parent bbf7a2a5d8
commit 7c6c387a07
6 changed files with 146 additions and 12 deletions

View File

@@ -0,0 +1,9 @@
package com.lanyuanxiaoyao.server.configuration;
/**
* @author lanyuanxiaoyao
* @version 20250327
*/
public interface Constants {
String DATABASE_TABLE_PREFIX = "system_";
}

View File

@@ -1,11 +1,17 @@
package com.lanyuanxiaoyao.server.configuration.database;
import java.io.Serializable;
import java.lang.annotation.Retention;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* 使用雪花算法作为ID生成器
@@ -13,16 +19,24 @@ import org.slf4j.LoggerFactory;
* @author lanyuanxiaoyao
* @date 2024-11-14
*/
public class SnowflakeIdGenerator implements IdentifierGenerator {
private static final Logger logger = LoggerFactory.getLogger(SnowflakeIdGenerator.class);
@Slf4j
public class SnowflakeId {
@IdGeneratorType(IdGenerator.class)
@ValueGenerationType(generatedBy = IdGenerator.class)
@Retention(RUNTIME)
@Target({FIELD, METHOD})
public @interface Generator {
}
@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) {
try {
return Snowflake.next();
} catch (Exception e) {
logger.error("Generate snowflake id failed", e);
throw new RuntimeException(e);
public static final class IdGenerator implements IdentifierGenerator {
@Override
public Object generate(SharedSessionContractImplementor session, Object object) {
try {
return Snowflake.next();
} catch (Exception e) {
log.error("Generate snowflake id failed", e);
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,45 @@
package com.lanyuanxiaoyao.server.entity;
import com.lanyuanxiaoyao.server.configuration.Constants;
import com.lanyuanxiaoyao.server.configuration.database.SnowflakeId;
import jakarta.persistence.ConstraintMode;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.Set;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
* 组织
*
* @author lanyuanxiaoyao
* @version 20250327
*/
@Getter
@Setter
@ToString
@Entity
@Table(name = Constants.DATABASE_TABLE_PREFIX + "department")
public class Department {
@Id
@SnowflakeId.Generator
private Long departmentId;
private String departmentName;
@ManyToOne
@JoinColumn(nullable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private Organization organization;
@ManyToOne
private Department parent;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "departmentParent")
@ToString.Exclude
private Set<Department> children;
}

View File

@@ -0,0 +1,35 @@
package com.lanyuanxiaoyao.server.entity;
import com.lanyuanxiaoyao.server.configuration.Constants;
import com.lanyuanxiaoyao.server.configuration.database.SnowflakeId;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.Set;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
* 组织
*
* @author lanyuanxiaoyao
* @version 20250327
*/
@Getter
@Setter
@ToString
@Entity
@Table(name = Constants.DATABASE_TABLE_PREFIX + "organization")
public class Organization {
@Id
@SnowflakeId.Generator
private Long organizationId;
private String organizationName;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "organization")
@ToString.Exclude
private Set<Department> departments;
}

View File

@@ -0,0 +1,30 @@
package com.lanyuanxiaoyao.server.entity;
import com.lanyuanxiaoyao.server.configuration.Constants;
import com.lanyuanxiaoyao.server.configuration.database.SnowflakeId;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
* 用户
*
* @author lanyuanxiaoyao
* @version 20250327
*/
@Getter
@Setter
@ToString
@Entity
@Table(name = Constants.DATABASE_TABLE_PREFIX + "user")
public class User {
@Id
@SnowflakeId.Generator
private Long userId;
private String userCode;
private String userNickName;
private String userSecret;
}