mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
初始化数据
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package cn.lihongjie.coal.dao;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import cn.lihongjie.coal.entity.OrganizationEntity;
|
||||
|
||||
@Repository
|
||||
public interface OrganizationRepository extends BaseRepository<OrganizationEntity> {
|
||||
@Nullable
|
||||
OrganizationEntity findByCode(String code);
|
||||
}
|
||||
@@ -6,4 +6,7 @@ import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface UserRepository extends BaseRepository<UserEntity> {
|
||||
|
||||
|
||||
public UserEntity findByUsername(String username);
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
package cn.lihongjie.coal.entity.base;
|
||||
|
||||
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
import cn.lihongjie.coal.common.Ctx;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.hibernate.annotations.*;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
|
||||
@@ -18,22 +17,16 @@ import java.time.LocalDateTime;
|
||||
public class BaseEntity {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.UUID)
|
||||
private String id;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Comment("创建用户ID")
|
||||
private String createUserId;
|
||||
|
||||
|
||||
@Formula("(select '')")
|
||||
@Formula("(select name from t_user where id = create_user_id)")
|
||||
private String createUserName;
|
||||
|
||||
@Comment("创建时间")
|
||||
@@ -41,10 +34,9 @@ public class BaseEntity {
|
||||
private LocalDateTime createTime;
|
||||
|
||||
|
||||
|
||||
@Comment("更新用户ID")
|
||||
private String updateUserId;
|
||||
@Formula("(select '')")
|
||||
@Formula("((select name from t_user where id = create_user_id))")
|
||||
private String updateUserName;
|
||||
|
||||
@Comment("更新时间")
|
||||
@@ -52,5 +44,19 @@ public class BaseEntity {
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
|
||||
@PrePersist
|
||||
public void prePersist() {
|
||||
if (StringUtils.isEmpty(this.createUserId)) {
|
||||
|
||||
this.createUserId = Ctx.isLoggedIn() ? Ctx.getUserId() : "";
|
||||
}
|
||||
}
|
||||
|
||||
@PreUpdate
|
||||
public void preUpdate() {
|
||||
|
||||
this.updateUserId = Ctx.isLoggedIn() ? Ctx.getUserId() : "";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package cn.lihongjie.coal.entity.base;
|
||||
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
import jakarta.persistence.PrePersist;
|
||||
import jakarta.persistence.PreUpdate;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.hibernate.annotations.Comment;
|
||||
@@ -37,4 +39,17 @@ public class CommonEntity extends BaseEntity {
|
||||
return Objects.equals(status, 0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void prePersist() {
|
||||
this.status = this.status == null ? 1 : this.status;
|
||||
super.prePersist();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preUpdate() {
|
||||
this.status = this.status == null ? 1 : this.status;
|
||||
super.preUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package cn.lihongjie.coal.entity.base;
|
||||
|
||||
import cn.lihongjie.coal.common.Ctx;
|
||||
import cn.lihongjie.coal.entity.OrganizationEntity;
|
||||
import cn.lihongjie.coal.entity.base.BaseEntity;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
@MappedSuperclass
|
||||
@Getter
|
||||
@@ -12,8 +14,21 @@ import lombok.Setter;
|
||||
public class OrgBaseEntity extends BaseEntity {
|
||||
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "organization_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
|
||||
private OrganizationEntity organization;
|
||||
private String organizationId;
|
||||
|
||||
|
||||
@Override
|
||||
public void prePersist() {
|
||||
if (StringUtils.isEmpty(organizationId)) {
|
||||
|
||||
this.organizationId = Ctx.isLoggedIn() ? Ctx.currentUser().getOrganizationId() : organizationId;
|
||||
}
|
||||
super.prePersist();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preUpdate() {
|
||||
super.preUpdate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,20 +1,34 @@
|
||||
package cn.lihongjie.coal.entity.base;
|
||||
|
||||
import cn.lihongjie.coal.common.Ctx;
|
||||
import cn.lihongjie.coal.entity.OrganizationEntity;
|
||||
import cn.lihongjie.coal.entity.base.CommonEntity;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
@MappedSuperclass
|
||||
@Getter
|
||||
@Setter
|
||||
public class OrgCommonEntity extends CommonEntity {
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "organization_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
|
||||
@JsonIgnore
|
||||
private OrganizationEntity organization;
|
||||
|
||||
private String organizationId;
|
||||
|
||||
|
||||
@Override
|
||||
public void prePersist() {
|
||||
if (StringUtils.isEmpty(organizationId)) {
|
||||
|
||||
this.organizationId = Ctx.isLoggedIn() ? Ctx.currentUser().getOrganizationId() : organizationId;
|
||||
}
|
||||
super.prePersist();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preUpdate() {
|
||||
super.preUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
37
src/main/java/cn/lihongjie/coal/runner/InitDataRunner.java
Normal file
37
src/main/java/cn/lihongjie/coal/runner/InitDataRunner.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package cn.lihongjie.coal.runner;
|
||||
|
||||
import cn.lihongjie.coal.entity.OrganizationEntity;
|
||||
import cn.lihongjie.coal.entity.UserEntity;
|
||||
import cn.lihongjie.coal.service.OrganizationService;
|
||||
import cn.lihongjie.coal.service.UserService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class InitDataRunner implements CommandLineRunner {
|
||||
|
||||
@Autowired
|
||||
OrganizationService organizationService;
|
||||
|
||||
|
||||
@Autowired
|
||||
UserService userService;
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
|
||||
|
||||
OrganizationEntity e = organizationService.initOrGetAdminOrg();
|
||||
|
||||
UserEntity u = userService.initOrGetAdminUser(e);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -78,4 +78,26 @@ public class OrganizationService extends BaseService<OrganizationEntity, Organiz
|
||||
return page.map(this.mapper::toDto);
|
||||
|
||||
}
|
||||
|
||||
public OrganizationEntity initOrGetAdminOrg() {
|
||||
|
||||
|
||||
OrganizationEntity adminOrg = this.repository.findByCode("adminOrg");
|
||||
|
||||
if (adminOrg == null) {
|
||||
|
||||
adminOrg = new OrganizationEntity();
|
||||
|
||||
adminOrg.setParent(null);
|
||||
adminOrg.setName("管理员机构");
|
||||
adminOrg.setCode("adminOrg");
|
||||
adminOrg.setStatus(1);
|
||||
|
||||
this.repository.save(adminOrg);
|
||||
}
|
||||
|
||||
|
||||
return adminOrg;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package cn.lihongjie.coal.service;
|
||||
|
||||
import cn.lihongjie.coal.dto.CaptchaDto;
|
||||
import cn.lihongjie.coal.dto.LoginDto;
|
||||
import cn.lihongjie.coal.entity.OrganizationEntity;
|
||||
import cn.lihongjie.coal.entity.UserEntity;
|
||||
import cn.lihongjie.coal.exception.BizException;
|
||||
import com.wf.captcha.SpecCaptcha;
|
||||
@@ -59,6 +60,9 @@ public class SessionService {
|
||||
|
||||
}
|
||||
|
||||
@Autowired
|
||||
OrganizationService organizationService;
|
||||
|
||||
public void login(LoginDto dto) {
|
||||
|
||||
|
||||
@@ -82,7 +86,8 @@ public class SessionService {
|
||||
throw new BizException("用户被禁用");
|
||||
}
|
||||
|
||||
if (user.getOrganization().isDisabled()) {
|
||||
OrganizationEntity organization = organizationService.get(user.getOrganizationId());
|
||||
if (organization.isDisabled()) {
|
||||
throw new BizException("用户所在单位被禁用");
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package cn.lihongjie.coal.service;
|
||||
|
||||
import cn.lihongjie.coal.dao.UserRepository;
|
||||
import cn.lihongjie.coal.dto.*;
|
||||
import cn.lihongjie.coal.entity.OrganizationEntity;
|
||||
import cn.lihongjie.coal.entity.UserEntity;
|
||||
import cn.lihongjie.coal.exception.BizException;
|
||||
import cn.lihongjie.coal.mapper.UserMapper;
|
||||
@@ -47,7 +48,7 @@ public class UserService extends BaseService<UserEntity, UserRepository>{
|
||||
|
||||
public UserDto create(CreateUserDto request){
|
||||
|
||||
|
||||
request.setPassword(passwordEncoder.encode(request.getPassword()));
|
||||
UserEntity entity = mapper.toEntity(request);
|
||||
|
||||
|
||||
@@ -136,4 +137,32 @@ public class UserService extends BaseService<UserEntity, UserRepository>{
|
||||
public boolean isValidPassword(String rawPassword, String encodedPassword) {
|
||||
return passwordEncoder.matches(rawPassword, encodedPassword);
|
||||
}
|
||||
|
||||
public UserEntity initOrGetAdminUser(OrganizationEntity e) {
|
||||
|
||||
UserEntity adminuser = this.repository.findByUsername("adminuser");
|
||||
|
||||
if (adminuser == null) {
|
||||
|
||||
adminuser = new UserEntity();
|
||||
|
||||
adminuser.setUsername("adminuser");
|
||||
|
||||
adminuser.setPassword(passwordEncoder.encode("abc@123"));
|
||||
|
||||
adminuser.setOrgAdmin(true);
|
||||
adminuser.setName("超级管理员");
|
||||
|
||||
adminuser.setSysAdmin(true);
|
||||
|
||||
|
||||
adminuser.setOrganizationId(e.getId());
|
||||
|
||||
this.repository.save(adminuser);
|
||||
}
|
||||
|
||||
return adminuser;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user