初始化方法添加关联表清理

This commit is contained in:
thinkgem
2018-01-06 10:25:44 +08:00
parent b6a299d316
commit a3c4457cef
2 changed files with 442 additions and 395 deletions

View File

@@ -1,362 +1,409 @@
/** /**
* Copyright (c) 2013-Now http://jeesite.com All rights reserved. * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
*/ */
package com.jeesite.modules.db; package com.jeesite.modules.db;
import java.io.File; import java.io.File;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import com.jeesite.common.callback.MethodCallback; import com.jeesite.common.callback.MethodCallback;
import com.jeesite.common.idgen.IdGen; import com.jeesite.common.idgen.IdGen;
import com.jeesite.common.tests.BaseInitDataTests; import com.jeesite.common.tests.BaseInitDataTests;
import com.jeesite.modules.sys.dao.RoleMenuDao; import com.jeesite.modules.sys.dao.RoleMenuDao;
import com.jeesite.modules.sys.entity.Area; import com.jeesite.modules.sys.entity.Area;
import com.jeesite.modules.sys.entity.Company; import com.jeesite.modules.sys.entity.Company;
import com.jeesite.modules.sys.entity.Config; import com.jeesite.modules.sys.entity.CompanyOffice;
import com.jeesite.modules.sys.entity.DictData; import com.jeesite.modules.sys.entity.Config;
import com.jeesite.modules.sys.entity.DictType; import com.jeesite.modules.sys.entity.DictData;
import com.jeesite.modules.sys.entity.EmpUser; import com.jeesite.modules.sys.entity.DictType;
import com.jeesite.modules.sys.entity.Employee; import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.Log; import com.jeesite.modules.sys.entity.Employee;
import com.jeesite.modules.sys.entity.Menu; import com.jeesite.modules.sys.entity.EmployeePost;
import com.jeesite.modules.sys.entity.Module; import com.jeesite.modules.sys.entity.Log;
import com.jeesite.modules.sys.entity.Office; import com.jeesite.modules.sys.entity.Menu;
import com.jeesite.modules.sys.entity.Post; import com.jeesite.modules.sys.entity.Module;
import com.jeesite.modules.sys.entity.Role; import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.entity.RoleMenu; import com.jeesite.modules.sys.entity.Post;
import com.jeesite.modules.sys.entity.User; import com.jeesite.modules.sys.entity.Role;
import com.jeesite.modules.sys.service.AreaService; import com.jeesite.modules.sys.entity.RoleDataScope;
import com.jeesite.modules.sys.service.CompanyService; import com.jeesite.modules.sys.entity.RoleMenu;
import com.jeesite.modules.sys.service.ConfigService; import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.DictDataService; import com.jeesite.modules.sys.entity.UserDataScope;
import com.jeesite.modules.sys.service.DictTypeService; import com.jeesite.modules.sys.entity.UserRole;
import com.jeesite.modules.sys.service.EmpUserService; import com.jeesite.modules.sys.service.AreaService;
import com.jeesite.modules.sys.service.MenuService; import com.jeesite.modules.sys.service.CompanyService;
import com.jeesite.modules.sys.service.ModuleService; import com.jeesite.modules.sys.service.ConfigService;
import com.jeesite.modules.sys.service.OfficeService; import com.jeesite.modules.sys.service.DictDataService;
import com.jeesite.modules.sys.service.PostService; import com.jeesite.modules.sys.service.DictTypeService;
import com.jeesite.modules.sys.service.RoleService; import com.jeesite.modules.sys.service.EmpUserService;
import com.jeesite.modules.sys.service.UserService; import com.jeesite.modules.sys.service.MenuService;
import com.jeesite.modules.sys.service.ModuleService;
/** import com.jeesite.modules.sys.service.OfficeService;
* 初始化核心表数据 import com.jeesite.modules.sys.service.PostService;
* @author ThinkGem import com.jeesite.modules.sys.service.RoleService;
* @version 2017-10-22 import com.jeesite.modules.sys.service.UserService;
*/
//@Commit /**
public class InitCoreData extends BaseInitDataTests { * 初始化核心表数据
* @author ThinkGem
@Override * @version 2017-10-22
public void begin() { */
super.begin(); //@Commit
dataFile = new File(InitCoreData.class.getResource( public class InitCoreData extends BaseInitDataTests {
InitCoreData.class.getSimpleName() + ".xlsx").getFile());
} @Override
public void begin() {
public void initLog() throws Exception{ super.begin();
clearTable(new Log()); dataFile = new File(InitCoreData.class.getResource(
} InitCoreData.class.getSimpleName() + ".xlsx").getFile());
}
@Autowired
private ConfigService configService; /**
public void initConfig() throws Exception{ * 清理日志表
try{ */
clearTable(new Config()); public void initLog() throws Exception{
initExcelData(Config.class, new MethodCallback() { clearTable(new Log());
@Override }
public Object execute(Object... params) {
String action = (String)params[0]; @Autowired
if("save".equals(action)){ private ConfigService configService;
Config entity = (Config)params[1]; /**
entity.setId(IdGen.nextId()); * 参数配置表
entity.setIsNewRecord(true); */
configService.save(entity); public void initConfig() throws Exception{
return null; try{
} clearTable(new Config());
return null; initExcelData(Config.class, new MethodCallback() {
} @Override
}); public Object execute(Object... params) {
}catch(Exception e){ String action = (String)params[0];
e.printStackTrace(); if("save".equals(action)){
throw new Exception(e); Config entity = (Config)params[1];
} entity.setId(IdGen.nextId());
} entity.setIsNewRecord(true);
configService.save(entity);
@Autowired return null;
private ModuleService moduleService; }
public void initModule() throws Exception{ return null;
try{ }
clearTable(new Module()); });
initExcelData(Module.class, new MethodCallback() { }catch(Exception e){
@Override e.printStackTrace();
public Object execute(Object... params) { throw new Exception(e);
String action = (String)params[0]; }
if("save".equals(action)){ }
Module entity = (Module)params[1];
entity.setIsNewRecord(true); @Autowired
moduleService.save(entity); private ModuleService moduleService;
return null; /**
} * 系统模块表
return null; */
} public void initModule() throws Exception{
}); try{
}catch(Exception e){ clearTable(new Module());
e.printStackTrace(); initExcelData(Module.class, new MethodCallback() {
throw new Exception(e); @Override
} public Object execute(Object... params) {
} String action = (String)params[0];
if("save".equals(action)){
@Autowired Module entity = (Module)params[1];
private DictTypeService dictTypeService; entity.setIsNewRecord(true);
@Autowired moduleService.save(entity);
private DictDataService dictDataService; return null;
public void initDict() throws Exception{ }
try{ return null;
clearTable(new DictType()); }
initExcelData(DictType.class, new MethodCallback() { });
@Override }catch(Exception e){
public Object execute(Object... params) { e.printStackTrace();
String action = (String)params[0]; throw new Exception(e);
if("save".equals(action)){ }
DictType entity = (DictType)params[1]; }
entity.setId(IdGen.nextId());
entity.setIsNewRecord(true); @Autowired
dictTypeService.save(entity); private DictTypeService dictTypeService;
return null; @Autowired
} private DictDataService dictDataService;
return null; /**
} * 系统字典、用户字典表
}); */
public void initDict() throws Exception{
clearTable(new DictData()); try{
initExcelData(DictData.class, new MethodCallback() { clearTable(new DictType());
@Override initExcelData(DictType.class, new MethodCallback() {
public Object execute(Object... params) { @Override
String action = (String)params[0]; public Object execute(Object... params) {
if("save".equals(action)){ String action = (String)params[0];
DictData entity = (DictData)params[1]; if("save".equals(action)){
entity.setIsNewRecord(true); DictType entity = (DictType)params[1];
dictDataService.save(entity); entity.setId(IdGen.nextId());
return null; entity.setIsNewRecord(true);
} dictTypeService.save(entity);
return null; return null;
} }
}); return null;
}catch(Exception e){ }
e.printStackTrace(); });
throw new Exception(e);
} clearTable(new DictData());
} initExcelData(DictData.class, new MethodCallback() {
@Override
@Autowired public Object execute(Object... params) {
private RoleService roleService; String action = (String)params[0];
public void initRole() throws Exception{ if("save".equals(action)){
try{ DictData entity = (DictData)params[1];
clearTable(new Role()); entity.setIsNewRecord(true);
initExcelData(Role.class, new MethodCallback() { dictDataService.save(entity);
@Override return null;
public Object execute(Object... params) { }
String action = (String)params[0]; return null;
if("save".equals(action)){ }
Role entity = (Role)params[1]; });
entity.setIsNewRecord(true); }catch(Exception e){
roleService.save(entity); e.printStackTrace();
return null; throw new Exception(e);
} }
return null; }
}
}); @Autowired
}catch(Exception e){ private RoleService roleService;
e.printStackTrace(); /**
throw new Exception(e); * 角色表
} */
} public void initRole() throws Exception{
try{
@Autowired clearTable(new Role());
private MenuService menuService; clearTable(new RoleMenu());
@Autowired clearTable(new RoleDataScope());
private RoleMenuDao roleMenuDao; initExcelData(Role.class, new MethodCallback() {
public void initMenu() throws Exception{ @Override
try{ public Object execute(Object... params) {
clearTable(new Menu()); String action = (String)params[0];
clearTable(new RoleMenu()); if("save".equals(action)){
initExcelData(Menu.class, new MethodCallback() { Role entity = (Role)params[1];
@Override entity.setIsNewRecord(true);
public Object execute(Object... params) { roleService.save(entity);
String action = (String)params[0]; return null;
if("save".equals(action)){ }
Menu entity = (Menu)params[1]; return null;
entity.setIsNewRecord(true); }
menuService.save(entity); });
RoleMenu rm = new RoleMenu(); }catch(Exception e){
rm.setMenuCode(entity.getMenuCode()); e.printStackTrace();
rm.setRoleCode(Role.CORP_ADMIN_ROLE_CODE); throw new Exception(e);
roleMenuDao.insert(rm); }
return null; }
}
return null; @Autowired
} private MenuService menuService;
}); @Autowired
}catch(Exception e){ private RoleMenuDao roleMenuDao;
e.printStackTrace(); /**
throw new Exception(e); * 菜单表
} */
} public void initMenu() throws Exception{
try{
@Autowired clearTable(new Menu());
private UserService userService; clearTable(new RoleMenu());
public void initUser() throws Exception{ initExcelData(Menu.class, new MethodCallback() {
try{ @Override
clearTable(new User()); public Object execute(Object... params) {
initExcelData(User.class, new MethodCallback() { String action = (String)params[0];
@Override if("save".equals(action)){
public Object execute(Object... params) { Menu entity = (Menu)params[1];
String action = (String)params[0]; entity.setIsNewRecord(true);
if("save".equals(action)){ menuService.save(entity);
User entity = (User)params[1]; RoleMenu rm = new RoleMenu();
entity.setIsNewRecord(true); rm.setMenuCode(entity.getMenuCode());
entity.setPassword(UserService.encryptPassword(entity.getPassword())); rm.setRoleCode(Role.CORP_ADMIN_ROLE_CODE);
userService.save(entity); roleMenuDao.insert(rm);
return null; return null;
} }
return null; return null;
} }
}); });
}catch(Exception e){ }catch(Exception e){
e.printStackTrace(); e.printStackTrace();
throw new Exception(e); throw new Exception(e);
} }
} }
@Autowired @Autowired
private AreaService areaService; private UserService userService;
public void initArea() throws Exception{ /**
try{ * 用户表
clearTable(new Area()); */
initExcelData(Area.class, new MethodCallback() { public void initUser() throws Exception{
@Override try{
public Object execute(Object... params) { clearTable(new User());
String action = (String)params[0]; clearTable(new UserRole());
if("save".equals(action)){ clearTable(new UserDataScope());
Area entity = (Area)params[1]; initExcelData(User.class, new MethodCallback() {
entity.setIsNewRecord(true); @Override
areaService.save(entity); public Object execute(Object... params) {
return null; String action = (String)params[0];
} if("save".equals(action)){
return null; User entity = (User)params[1];
} entity.setIsNewRecord(true);
}); entity.setPassword(UserService.encryptPassword(entity.getPassword()));
}catch(Exception e){ userService.save(entity);
e.printStackTrace(); return null;
throw new Exception(e); }
} return null;
} }
});
@Autowired }catch(Exception e){
private OfficeService officeService; e.printStackTrace();
public void initOffice() throws Exception{ throw new Exception(e);
try{ }
clearTable(new Office()); }
initExcelData(Office.class, new MethodCallback() {
@Override @Autowired
public Object execute(Object... params) { private AreaService areaService;
String action = (String)params[0]; /**
if("save".equals(action)){ * 区域、行政区划表
Office entity = (Office)params[1]; */
entity.setIsNewRecord(true); public void initArea() throws Exception{
officeService.save(entity); try{
return null; clearTable(new Area());
} initExcelData(Area.class, new MethodCallback() {
return null; @Override
} public Object execute(Object... params) {
}); String action = (String)params[0];
}catch(Exception e){ if("save".equals(action)){
e.printStackTrace(); Area entity = (Area)params[1];
throw new Exception(e); entity.setIsNewRecord(true);
} areaService.save(entity);
} return null;
}
@Autowired return null;
private CompanyService companyService; }
public void initCompany() throws Exception{ });
try{ }catch(Exception e){
clearTable(new Company()); e.printStackTrace();
initExcelData(Company.class, new MethodCallback() { throw new Exception(e);
@Override }
public Object execute(Object... params) { }
String action = (String)params[0];
if("save".equals(action)){ @Autowired
Company entity = (Company)params[1]; private OfficeService officeService;
entity.setIsNewRecord(true); /**
companyService.save(entity); * 组织机构、部门表
return null; */
} public void initOffice() throws Exception{
return null; try{
} clearTable(new Office());
}); initExcelData(Office.class, new MethodCallback() {
}catch(Exception e){ @Override
e.printStackTrace(); public Object execute(Object... params) {
throw new Exception(e); String action = (String)params[0];
} if("save".equals(action)){
} Office entity = (Office)params[1];
entity.setIsNewRecord(true);
@Autowired officeService.save(entity);
private PostService postService; return null;
public void initPost() throws Exception{ }
try{ return null;
clearTable(new Post()); }
initExcelData(Post.class, new MethodCallback() { });
@Override }catch(Exception e){
public Object execute(Object... params) { e.printStackTrace();
String action = (String)params[0]; throw new Exception(e);
if("save".equals(action)){ }
Post entity = (Post)params[1]; }
entity.setIsNewRecord(true);
postService.save(entity); @Autowired
return null; private CompanyService companyService;
} /**
return null; * 公司表
} */
}); public void initCompany() throws Exception{
}catch(Exception e){ try{
e.printStackTrace(); clearTable(new Company());
throw new Exception(e); clearTable(new CompanyOffice());
} initExcelData(Company.class, new MethodCallback() {
} @Override
public Object execute(Object... params) {
@Autowired String action = (String)params[0];
private EmpUserService empUserService; if("save".equals(action)){
public void initEmpUser() throws Exception{ Company entity = (Company)params[1];
try{ entity.setIsNewRecord(true);
clearTable(new Employee()); companyService.save(entity);
initExcelData(EmpUser.class, new MethodCallback() { return null;
@Override }
public Object execute(Object... params) { return null;
String action = (String)params[0]; }
if("set".equals(action)){ });
EmpUser entity = (EmpUser)params[1]; }catch(Exception e){
String header = (String)params[2]; e.printStackTrace();
String val = (String)params[3]; throw new Exception(e);
if ("employee.employeePosts".equals(header)){ }
entity.getEmployee().setEmployeePosts(new String[]{val}); }
return true;
} @Autowired
} private PostService postService;
else if("save".equals(action)){ /**
EmpUser entity = (EmpUser)params[1]; * 岗位表
entity.setIsNewRecord(true); */
entity.setPassword(UserService.encryptPassword(entity.getPassword())); public void initPost() throws Exception{
empUserService.save(entity); try{
userService.saveAuth(entity); clearTable(new Post());
return null; initExcelData(Post.class, new MethodCallback() {
} @Override
return null; public Object execute(Object... params) {
} String action = (String)params[0];
}); if("save".equals(action)){
}catch(Exception e){ Post entity = (Post)params[1];
e.printStackTrace(); entity.setIsNewRecord(true);
throw new Exception(e); postService.save(entity);
} return null;
} }
} return null;
}
});
}catch(Exception e){
e.printStackTrace();
throw new Exception(e);
}
}
@Autowired
private EmpUserService empUserService;
/**
* 员工、用户表
*/
public void initEmpUser() throws Exception{
try{
clearTable(new Employee());
clearTable(new EmployeePost());
initExcelData(EmpUser.class, new MethodCallback() {
@Override
public Object execute(Object... params) {
String action = (String)params[0];
if("set".equals(action)){
EmpUser entity = (EmpUser)params[1];
String header = (String)params[2];
String val = (String)params[3];
if ("employee.employeePosts".equals(header)){
entity.getEmployee().setEmployeePosts(new String[]{val});
return true;
}
}
else if("save".equals(action)){
EmpUser entity = (EmpUser)params[1];
entity.setIsNewRecord(true);
entity.setPassword(UserService.encryptPassword(entity.getPassword()));
empUserService.save(entity);
userService.saveAuth(entity);
return null;
}
return null;
}
});
}catch(Exception e){
e.printStackTrace();
throw new Exception(e);
}
}
}

View File

@@ -1,33 +1,33 @@
/** /**
* Copyright (c) 2013-Now http://jeesite.com All rights reserved. * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
*/ */
package com.jeesite.test; package com.jeesite.test;
import org.junit.Test; import org.junit.Test;
import org.springframework.test.annotation.Commit; import org.springframework.test.annotation.Commit;
/** /**
* 初始化核心表数据 * 初始化核心表数据
* @author ThinkGem * @author ThinkGem
* @version 2017-10-22 * @version 2017-10-22
*/ */
@Commit @Commit
public class InitCoreData extends com.jeesite.modules.db.InitCoreData { public class InitCoreData extends com.jeesite.modules.db.InitCoreData {
@Test @Test
public void initCoreData() throws Exception{ public void initCoreData() throws Exception{
// initLog(); initLog();
// initConfig(); initConfig();
// initModule(); initModule();
initDict(); initDict();
// initRole(); initRole();
initMenu(); initMenu();
// initUser(); initUser();
// initArea(); // initArea();
// initOffice(); initOffice();
// initCompany(); initCompany();
// initPost(); initPost();
// initEmpUser(); initEmpUser();
} }
} }