|
@@ -0,0 +1,133 @@
|
|
|
+package com.free.service;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.JsonMappingException;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.type.CollectionType;
|
|
|
+import com.free.entity.system.Admin;
|
|
|
+import com.free.service.system.AdminService;
|
|
|
+import com.free.service.system.DictDataService;
|
|
|
+import com.free.service.system.DictTypeService;
|
|
|
+import com.free.service.system.MenusService;
|
|
|
+import com.free.service.system.RoleService;
|
|
|
+import com.free.utils.BcryptUtil;
|
|
|
+import com.free.utils.Utils;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class InitDataService {
|
|
|
+ @Autowired
|
|
|
+ AdminService adminService;
|
|
|
+ @Autowired
|
|
|
+ MenusService menusService;
|
|
|
+ @Autowired
|
|
|
+ RoleService roleService;
|
|
|
+ @Autowired
|
|
|
+ DictTypeService dictTypeService;
|
|
|
+ @Autowired
|
|
|
+ DictDataService dictDataService;
|
|
|
+ @Autowired
|
|
|
+ QuestionService questionService;
|
|
|
+
|
|
|
+ public void init() {
|
|
|
+ admin();
|
|
|
+ menus();
|
|
|
+ role();
|
|
|
+ dictType();
|
|
|
+ dictData();
|
|
|
+ question();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void admin() {
|
|
|
+ QueryWrapper qw = new QueryWrapper<>();
|
|
|
+ qw.eq("account", "sadmin");
|
|
|
+ qw.eq("is_super", "0");
|
|
|
+ Long num = adminService.count(qw);
|
|
|
+ if (num > 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Admin admin = new Admin();
|
|
|
+ admin.setAccount("sadmin");
|
|
|
+ admin.setIs_super("0");
|
|
|
+ admin.setNick_name("超级管理员");
|
|
|
+ String pwd = "1qaz2wsx";
|
|
|
+ String cpwd = BcryptUtil.encryptPassword(pwd);
|
|
|
+ admin.setPassword(cpwd);
|
|
|
+ adminService.save(admin);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void menus() {
|
|
|
+ // 先查询有没有数据
|
|
|
+ Long num = menusService.count();
|
|
|
+ if (num > 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<Map> list = readFile("menus");
|
|
|
+ menusService.initData(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void role() {
|
|
|
+ // 先查询有没有数据
|
|
|
+ Long num = roleService.count();
|
|
|
+ if (num > 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<Map> list = readFile("role");
|
|
|
+ roleService.initData(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void dictType() {
|
|
|
+ // 先查询有没有数据
|
|
|
+ Long num = dictTypeService.count();
|
|
|
+ if (num > 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<Map> list = readFile("dictType");
|
|
|
+ dictTypeService.initData(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void dictData() {
|
|
|
+ // 先查询有没有数据
|
|
|
+ Long num = dictDataService.count();
|
|
|
+ if (num > 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<Map> list = readFile("dictData");
|
|
|
+ dictDataService.initData(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void question() {
|
|
|
+ // 先查询有没有数据
|
|
|
+ Long num = questionService.count();
|
|
|
+ if (num > 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<Map> list = readFile("question");
|
|
|
+ questionService.initData(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Map> readFile(String name) {
|
|
|
+ String filePath = "db/" + name + ".json";
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ String str = Utils.readJsonFile(filePath);
|
|
|
+ CollectionType javaType = mapper.getTypeFactory().constructCollectionType(List.class, Map.class);
|
|
|
+ try {
|
|
|
+ List<Map> list = mapper.readValue(str, javaType);
|
|
|
+ return list;
|
|
|
+ } catch (JsonMappingException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|