package com.zmdev.goldenbag.web;
import com.zmdev.goldenbag.domain.*;
import com.zmdev.goldenbag.service.*;
import com.zmdev.goldenbag.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@Controller
@RequestMapping(value = "/setup", produces = "application/json;charset=UTF-8")
public class SetupController extends BaseController {
private PermissionService permissionService;
private RoleService roleService;
private UserService userService;
private QuarterService quarterService;
private DepartmentService departmentsService;
private AssessmentTemplateService templateService;
private AssessmentInputService assessmentInputService;
private AssessmentProjectService assessmentProjectService;
private AssessmentProjectItemRepository assessmentProjectItemRepository;
private BasePermission[] basePermissions = {
new BasePermission("view", "查看", true),
new BasePermission("add", "添加", true),
new BasePermission("edit", "编辑", true),
new BasePermission("delete", "删除", true),
};
@Autowired
public void setPermissionService(PermissionService permissionService) {
this.permissionService = permissionService;
}
@Autowired
public void setQuarterService(QuarterService quarterService) {
this.quarterService = quarterService;
}
@Autowired
public void setRoleService(RoleService roleService) {
this.roleService = roleService;
}
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
@Autowired
public void setDepartmentsService(DepartmentService departmentsService) {
this.departmentsService = departmentsService;
}
@Autowired
public void setTemplateService(AssessmentTemplateService templateService) {
this.templateService = templateService;
}
@Autowired
public void setAssessmentInputService(AssessmentInputService assessmentInputService) {
this.assessmentInputService = assessmentInputService;
}
@Autowired
public void setAssessmentProjectService(AssessmentProjectService assessmentProjectService) {
this.assessmentProjectService = assessmentProjectService;
}
@Autowired
public void setAssessmentProjectItemRepository(AssessmentProjectItemRepository assessmentProjectItemRepository) {
this.assessmentProjectItemRepository = assessmentProjectItemRepository;
}
private void storePermission(String topModuleString, String moduleName, BasePermission[] basePermissions) {
for (BasePermission basePermission : basePermissions) {
Permission p = new Permission();
p.setName(topModuleString + "." + moduleName + "." + basePermission.getActionName());
p.setDisplayName(basePermission.getDisplayName());
p.setDescription(basePermission.getDisplayName() + Permission.getModules().get(moduleName).substring(0, 2));
p.setCreatedAt(new Date());
p.setUpdatedAt(new Date());
p.setMenuable(basePermission.getMenuable());
permissionService.save(p);
}
}
private void setupPermission() {
Map<String, String[]> resources = new HashMap<>();
resources.put("basic", new String[]{"department", "user", "role"});
for (Map.Entry<String, String[]> entry : resources.entrySet()) {
for (String moduleName : entry.getValue()) {
storePermission(entry.getKey(), moduleName, basePermissions);
}
}
Permission p = new Permission();
p.setName("basic.role.permission");
p.setDisplayName("查看指定角色的权限");
p.setDescription("查看指定角色的权限");
p.setCreatedAt(new Date());
p.setUpdatedAt(new Date());
p.setMenuable(true);
permissionService.save(p);
storePermission("basic", "quarter",
new BasePermission[]{new BasePermission("view", "查看", true), new BasePermission("add", "添加", true), new BasePermission("edit", "编辑", true)});
storePermission("basic", "permission", new BasePermission[]{new BasePermission("view", "查看", false)});
// p = new Permission();
// p.setName("basic.permission.allMenus");
// p.setDisplayName("获取菜单");
// p.setDescription("获取菜单");
// p.setCreatedAt(new Date());
// p.setUpdatedAt(new Date());
// p.setMenuable(false);
// permissionService.save(p);
storePermission("template_module", "template", new BasePermission[]{
new BasePermission("view", "查看", true),
new BasePermission("add", "添加", true),
new BasePermission("edit", "编辑", true),
});
p = new Permission();
p.setName("template_module.template.export");
p.setDisplayName("导出");
p.setDescription("导出模板");
p.setCreatedAt(new Date());
p.setUpdatedAt(new Date());
p.setMenuable(true);
permissionService.save(p);
p = new Permission();
p.setName("assessment.directManagerScore.add");
p.setDisplayName("直接经理评分");
p.setDescription("直接经理评分");
p.setCreatedAt(new Date());
p.setUpdatedAt(new Date());
p.setMenuable(true);
permissionService.save(p);
p = new Permission();
p.setName("assessment.indirectManagerAuditComments.add");
p.setDisplayName("间接经理建议");
p.setDescription("间接经理建议");
p.setCreatedAt(new Date());
p.setUpdatedAt(new Date());
p.setMenuable(true);
permissionService.save(p);
p = new Permission();
p.setName("assessment.selfEvaluation.add");
p.setDisplayName("员工自评");
p.setDescription("员工自评");
p.setCreatedAt(new Date());
p.setUpdatedAt(new Date());
p.setMenuable(true);
permissionService.save(p);
p = new Permission();
p.setName("assessment.summary.show");
p.setDisplayName("显示指定的考核记录");
p.setDescription("显示指定的考核记录");
p.setCreatedAt(new Date());
p.setUpdatedAt(new Date());
p.setMenuable(false);
permissionService.save(p);
p = new Permission();
p.setName("assessment.summary.view");
p.setDisplayName("查看");
p.setDescription("查看考核记录");
p.setCreatedAt(new Date());
p.setUpdatedAt(new Date());
p.setMenuable(true);
permissionService.save(p);
p = new Permission();
p.setName("assessment.summary.batchExportByAssessmentIds");
p.setDisplayName("批量导出");
p.setDescription("批量导出考核记录");
p.setCreatedAt(new Date());
p.setUpdatedAt(new Date());
p.setMenuable(true);
permissionService.save(p);
p = new Permission();
p.setName("assessment.summary.batchExportByQuarterIds");
p.setDisplayName("按季度批量导出");
p.setDescription("按季度批量导出考核记录");
p.setCreatedAt(new Date());
p.setUpdatedAt(new Date());
p.setMenuable(true);
permissionService.save(p);
}
private void setupRole() {
Role adminRole = new Role();
adminRole.setName("管理员");
adminRole.setDescription("管理员拥有所有权限");
adminRole.setPermissions(permissionService.findAll());
roleService.save(adminRole);
Role directManagerR