bonus_houqin
This commit is contained in:
parent
278d2497f4
commit
130ba93754
|
|
@ -1,6 +1,7 @@
|
||||||
package com.bonus.system.api.domain;
|
package com.bonus.system.api.domain;
|
||||||
|
|
||||||
import com.bonus.common.core.web.domain.BaseEntity;
|
import com.bonus.common.core.web.domain.BaseEntity;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||||
|
|
||||||
|
|
@ -19,9 +20,15 @@ import java.util.List;
|
||||||
public class SysDept extends BaseEntity {
|
public class SysDept extends BaseEntity {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 主键ID
|
||||||
|
*/
|
||||||
|
private Long id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 部门ID
|
* 部门ID
|
||||||
*/
|
*/
|
||||||
|
@JsonFormat(shape = JsonFormat.Shape.STRING)
|
||||||
private Long deptId;
|
private Long deptId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -274,6 +281,14 @@ public class SysDept extends BaseEntity {
|
||||||
this.level = level;
|
this.level = level;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
public Long getDeptId() {
|
public Long getDeptId() {
|
||||||
return deptId;
|
return deptId;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ public class SysUser extends BaseEntity {
|
||||||
* 部门ID
|
* 部门ID
|
||||||
*/
|
*/
|
||||||
@Excel(name = "部门编号", type = Type.IMPORT)
|
@Excel(name = "部门编号", type = Type.IMPORT)
|
||||||
|
@JsonFormat(shape = JsonFormat.Shape.STRING)
|
||||||
private Long deptId;
|
private Long deptId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package com.bonus.auth.form;
|
||||||
|
|
||||||
import com.bonus.auth.config.LoginType;
|
import com.bonus.auth.config.LoginType;
|
||||||
import com.bonus.auth.config.VerificationCodeType;
|
import com.bonus.auth.config.VerificationCodeType;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -50,5 +51,6 @@ public class LoginBody {
|
||||||
/**
|
/**
|
||||||
* i皖送登录数据库中没有用心信息的时候给个默认公司
|
* i皖送登录数据库中没有用心信息的时候给个默认公司
|
||||||
*/
|
*/
|
||||||
|
@JsonFormat(shape = JsonFormat.Shape.STRING)
|
||||||
private Long deptId;
|
private Long deptId;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -164,6 +164,16 @@
|
||||||
<version>1.10.19</version>
|
<version>1.10.19</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.data</groupId>
|
||||||
|
<artifactId>spring-data-redis</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.guava</groupId>
|
||||||
|
<artifactId>guava</artifactId>
|
||||||
|
<version>33.0.0-jre</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
package com.bonus.common.core.utils.id;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
|
||||||
|
public class Id {
|
||||||
|
private static final long EPOCH = LocalDateTime.of(2021, 12, 29, 9, 2).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
|
||||||
|
private static final long SEQUENCE_BITS = 12L;
|
||||||
|
private static final long WORKER_ID_BITS = 10L;
|
||||||
|
static final long WORKER_ID_MAX_VALUE = 1024L;
|
||||||
|
private static final long SEQUENCE_MASK = 4095L;
|
||||||
|
private static final long WORKER_ID_LEFT_SHIFT_BITS = 12L;
|
||||||
|
private static final long TIMESTAMP_LEFT_SHIFT_BITS = 22L;
|
||||||
|
static Long WORKER_ID;
|
||||||
|
private static long SEQUENCE;
|
||||||
|
private static long LAST_TIME;
|
||||||
|
|
||||||
|
public Id(Long workerId) {
|
||||||
|
WORKER_ID = workerId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long next() {
|
||||||
|
return nextKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String nextString() {
|
||||||
|
return String.valueOf(next());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static synchronized long nextKey() {
|
||||||
|
long currentMillis = System.currentTimeMillis();
|
||||||
|
Preconditions.checkState(LAST_TIME <= currentMillis, "Clock is moving backwards, last time is %d milliseconds, current time is %d milliseconds", LAST_TIME, currentMillis);
|
||||||
|
if (LAST_TIME == currentMillis) {
|
||||||
|
if (0L == (SEQUENCE = SEQUENCE + 1L & 4095L)) {
|
||||||
|
currentMillis = waitUntilNextTime(currentMillis);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
SEQUENCE = 0L;
|
||||||
|
}
|
||||||
|
|
||||||
|
LAST_TIME = currentMillis;
|
||||||
|
return currentMillis - EPOCH << 22 | WORKER_ID << 12 | SEQUENCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static long waitUntilNextTime(final long lastTime) {
|
||||||
|
long time;
|
||||||
|
for(time = System.currentTimeMillis(); time <= lastTime; time = System.currentTimeMillis()) {
|
||||||
|
}
|
||||||
|
|
||||||
|
return time;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.bonus.common.core.utils.id;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
@ConfigurationProperties(
|
||||||
|
prefix = "id"
|
||||||
|
)
|
||||||
|
public class IdProperties {
|
||||||
|
public static final String PREFIX = "id";
|
||||||
|
private Boolean enabled = true;
|
||||||
|
private String key = "worker_id_sequence";
|
||||||
|
private String workspace;
|
||||||
|
|
||||||
|
public Boolean getEnabled() {
|
||||||
|
return this.enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getKey() {
|
||||||
|
return this.key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getWorkspace() {
|
||||||
|
return this.workspace;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnabled(final Boolean enabled) {
|
||||||
|
this.enabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKey(final String key) {
|
||||||
|
this.key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWorkspace(final String workspace) {
|
||||||
|
this.workspace = workspace;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,123 @@
|
||||||
|
package com.bonus.common.core.utils.id;
|
||||||
|
|
||||||
|
import cn.hutool.core.text.CharSequenceUtil;
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.RandomAccessFile;
|
||||||
|
import java.nio.channels.FileChannel;
|
||||||
|
import java.nio.channels.FileLock;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@ConditionalOnClass({StringRedisTemplate.class})
|
||||||
|
@EnableConfigurationProperties({IdProperties.class})
|
||||||
|
@ConditionalOnProperty(
|
||||||
|
prefix = "id",
|
||||||
|
name = {"enabled"},
|
||||||
|
havingValue = "true",
|
||||||
|
matchIfMissing = true
|
||||||
|
)
|
||||||
|
public class IdWorkConfiguration {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(IdWorkConfiguration.class);
|
||||||
|
private final IdProperties idProperties;
|
||||||
|
|
||||||
|
public IdWorkConfiguration(StringRedisTemplate redisTemplate, IdProperties idProperties) throws IOException {
|
||||||
|
this.idProperties = idProperties;
|
||||||
|
Long workerId = this.getLocalWorkId();
|
||||||
|
|
||||||
|
try {
|
||||||
|
while(workerId == null) {
|
||||||
|
Long newWorkId = redisTemplate.opsForValue().increment(idProperties.getKey(), 1L);
|
||||||
|
this.saveLocalWorkId(String.valueOf(newWorkId));
|
||||||
|
workerId = this.getLocalWorkId();
|
||||||
|
}
|
||||||
|
} catch (Throwable var5) {
|
||||||
|
log.error("获取workID失败", var5);
|
||||||
|
throw var5;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (workerId > 1024L) {
|
||||||
|
throw new RuntimeException("超过最大启动实例");
|
||||||
|
} else {
|
||||||
|
Id.WORKER_ID = workerId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void saveLocalWorkId(String workId) throws IOException {
|
||||||
|
File workIdHome = this.getWorkIdHome();
|
||||||
|
String var10002 = String.valueOf(workIdHome.getAbsoluteFile());
|
||||||
|
FileUtils.writeStringToFile(new File(var10002 + "/" + String.valueOf(UUID.randomUUID()) + ".lock"), workId, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
|
private File getWorkIdHome() {
|
||||||
|
String workHome = this.idProperties.getWorkspace();
|
||||||
|
if (CharSequenceUtil.isBlank(workHome)) {
|
||||||
|
workHome = FileUtils.getUserDirectoryPath() + "/.workId/";
|
||||||
|
}
|
||||||
|
|
||||||
|
String var10002 = CharSequenceUtil.removeSuffix(workHome, "/");
|
||||||
|
return new File(var10002 + "/" + this.idProperties.getKey());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Long getLocalWorkId() throws IOException {
|
||||||
|
File workIdHome = this.getWorkIdHome();
|
||||||
|
if (!workIdHome.exists()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Collection<File> files = FileUtils.listFiles(workIdHome, new String[]{"lock"}, false);
|
||||||
|
if (CollectionUtils.isEmpty(files)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (File file : files) {
|
||||||
|
FileChannel channel = null;
|
||||||
|
FileLock fileLock = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
|
||||||
|
channel = randomAccessFile.getChannel();
|
||||||
|
fileLock = channel.tryLock();
|
||||||
|
|
||||||
|
if (fileLock != null) { // 成功获取文件锁
|
||||||
|
Long workId = Long.valueOf(randomAccessFile.readLine());
|
||||||
|
releaseResourcesOnShutdown(channel);
|
||||||
|
return workId;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Error accessing workId file", e);
|
||||||
|
} finally {
|
||||||
|
if (fileLock == null && channel != null) {
|
||||||
|
try {
|
||||||
|
channel.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Failed to close file channel", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void releaseResourcesOnShutdown(FileChannel channel) {
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||||
|
try {
|
||||||
|
channel.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Release WorkId file lock error", e);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.bonus.system.config;
|
||||||
|
|
||||||
|
import com.bonus.common.core.utils.id.IdWorkConfiguration;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@Import({IdWorkConfiguration.class})
|
||||||
|
public class CommonConfiguration {
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package com.bonus.system.domain;
|
package com.bonus.system.domain;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||||
|
|
||||||
|
|
@ -14,6 +15,7 @@ public class SysRoleDept
|
||||||
private Long roleId;
|
private Long roleId;
|
||||||
|
|
||||||
/** 部门ID */
|
/** 部门ID */
|
||||||
|
@JsonFormat(shape = JsonFormat.Shape.STRING)
|
||||||
private Long deptId;
|
private Long deptId;
|
||||||
|
|
||||||
public Long getRoleId()
|
public Long getRoleId()
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import com.bonus.common.core.utils.DateUtils;
|
import com.bonus.common.core.utils.DateUtils;
|
||||||
|
import com.bonus.common.core.utils.id.Id;
|
||||||
import com.bonus.common.core.web.domain.BaseEntity;
|
import com.bonus.common.core.web.domain.BaseEntity;
|
||||||
import com.bonus.config.SystemConfig;
|
import com.bonus.config.SystemConfig;
|
||||||
import com.bonus.system.api.domain.*;
|
import com.bonus.system.api.domain.*;
|
||||||
|
|
@ -306,6 +307,7 @@ public class SysDeptServiceImpl implements ISysDeptService
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public int insertDept(SysDept dept)
|
public int insertDept(SysDept dept)
|
||||||
{
|
{
|
||||||
|
dept.setDeptId(Id.next());
|
||||||
//增加根公司的处理
|
//增加根公司的处理
|
||||||
if (dept.getParentId() == null){
|
if (dept.getParentId() == null){
|
||||||
dept.setParentId(0L);
|
dept.setParentId(0L);
|
||||||
|
|
|
||||||
|
|
@ -187,7 +187,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
where dept_name=#{deptName} and parent_id = #{parentId} and del_flag = '0' limit 1
|
where dept_name=#{deptName} and parent_id = #{parentId} and del_flag = '0' limit 1
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<insert id="insertDept" parameterType="com.bonus.system.api.domain.SysDept" useGeneratedKeys="true" keyProperty="deptId">
|
<insert id="insertDept" parameterType="com.bonus.system.api.domain.SysDept" useGeneratedKeys="true" keyProperty="id">
|
||||||
insert into sys_dept(
|
insert into sys_dept(
|
||||||
<if test="deptId != null and deptId != 0">dept_id,</if>
|
<if test="deptId != null and deptId != 0">dept_id,</if>
|
||||||
<if test="parentId != null and parentId != 0">parent_id,</if>
|
<if test="parentId != null and parentId != 0">parent_id,</if>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue