发表于: 2018-04-01 16:27:25

1 478


今日完成

1.角色管理接口

/**
* 后台:查询角色详情...
*
* @param model
* @param id    角色id
* @return
* @throws Exception
*/
@RequestMapping(value = "/a/u/adminRole/{id}", method = RequestMethod.GET)
public String getAdminRoleJson(ModelMap model, @PathVariable Long id)
throws Exception {

log.info("get data : id= " + id);
   try {
AdminRole adminRole = adminRoleService.getObjectById(id);
       
       List<Long> ids = authorityService.getAuthorityIdsByAdminRoleId(id, 0, Integer.MAX_VALUE);
       List<Authority> authorityList = authorityService.getObjectsByIds(ids);
       
       List<Long> moduleIds = MyListUtil.getList(Authority.class.getDeclaredField("moduleId"), authorityList);
       
       log.info("get adminRole data is " + adminRole);
       
       model.addAttribute("code", 0);
       
       model.addAttribute("adminRole", adminRole);
       
       model.addAttribute("moduleIds",moduleIds);
       
   } catch (Throwable t) {
t.printStackTrace();
       log.error(t.getMessage());
       log.error("get adminRole error,id is  " + id);
       model.addAttribute("code", -100);
   }

return "/polyFinance-lgd-server/adminRole/json/adminRoleDetailJson";
}


@RequestMapping(value = "/a/adminRole/{id}", method = RequestMethod.PUT)
@ResponseBody
public String updateAdminRoleJson(@PathVariable Long id, ModelMap model, AdminRole adminRole) throws Exception {
JSONObject a = new JSONObject();
   
   log.info("update adminRole : adminRole= " + adminRole);
   
   try {

adminRoleService.update(adminRole);
       
       model.addAttribute("code", 0);
       
       model.addAttribute("adminRole", adminRole);
       
   } catch (Throwable t) {
t.printStackTrace();
       log.error(t.getMessage());
       log.error("update adminRole error,id is  " + adminRole.getId());
       model.addAttribute("code", -6003);
       
   }

return a.toString();
}


/**
* 后台:新增角色
*
* @param model
* @return
* @throws Exception
*/
@RequestMapping(value = "/a/u/adminRole", method = RequestMethod.POST)

public String addAdminRoleJson(ModelMap model, @RequestBody JSONObject jsonObject) throws Exception {

log.info("insert adminRole : adminRole= " + jsonObject.get("roleName"));
   AdminRole adminRole = new AdminRole();
   adminRole.setRoleName(jsonObject.getString("roleName"));
   adminRole.setCreateBy(jsonObject.getString("createBy"));
   
   JSONArray jsonArray = jsonObject.getJSONArray("authority");
   
   try {
adminRole.setId(null);
       
       Long roleId = adminRoleService.insert(adminRole);
       
       List<Authority> authorityList = new ArrayList<>();
       if (jsonArray.size() > 0) {
for (Object id : jsonArray) {
Authority authority1 = new Authority();
               authority1.setAdminRoleId(roleId);
               authority1.setModuleId(Long.valueOf(id.toString()));
               authorityList.add(authority1);
           }
log.info("role authorty is " + authorityList);
           authorityService.insertList(authorityList);
       }
model.addAttribute("code", 0);
   } catch (Throwable t) {
t.printStackTrace();
       log.error(t.getMessage());
       log.error("add adminRole error ");
       model.addAttribute("code", -100);
   }

return "/polyFinance-lgd-server/adminRole/json/adminRoleDetailJson";
}


/**
* 后台:删除管理员角色接口
* (系统没有事务,自定义事务失败,从新插入后id会发生变化)
*
* @param model
* @param id    角色id
* @return
* @throws Exception
*/
@RequestMapping(value = "/a/u/adminRole/{id}", method = RequestMethod.DELETE)
@ResponseBody
public String deleteAdminRoleJson(ModelMap model, @PathVariable Long id)
throws Exception {
JSONObject a = new JSONObject();
   
   log.info("delete adminRole : id= " + id);
   List<Long> authorityIds = new ArrayList<>();
   AdminRole adminRoleSave = new AdminRole();
   List<Authority> authorities = new ArrayList<>();
   try {
adminRoleSave = adminRoleService.getObjectById(id);
       adminRoleService.delete(id);
       authorityIds = authorityService.getAuthorityIdsByAdminRoleId(id, 0, Integer.MAX_VALUE);
       authorities = authorityService.getObjectsByIds(authorityIds);
       authorityService.deleteList(Authority.class, authorityIds);
       
       
       log.info("delete adminRole and Authority success");
       a.put("code", 0);
       a.put("message", "success");
       
   } catch (Throwable t) {
AdminRole adminRole = adminRoleService.getObjectById(id);
       if (adminRole == null) {
adminRoleService.insert(adminRoleSave);
       }
List<Authority> authorities1 = authorityService.getObjectsByIds(authorityIds);
       if (authorities1 == null) {
authorityService.insertList(authorities);
       }

t.printStackTrace();
       log.error(t.getMessage());
       log.error("delete adminRole error,id is  " + id);
       a.put("code", -100);
       a.put("message", "Server has something wrong");
       return a.toString();
   }
return a.toString();
}

/**
* 后台:查询角色列表
*
* @param model
* @return
* @throws Exception
*/
@RequestMapping(value = "/a/u/adminRole/list", method = RequestMethod.GET)
public String getMultiAdminRoleJson(ModelMap model)
throws Exception {

try {

List<Long> idList = adminRoleService.getAdminRoleIds(0, Integer.MAX_VALUE);
       List<AdminRole> adminRoleList = adminRoleService.getObjectsByIds(idList);
       log.info("get  adminRole data is " + adminRoleList);
       
       model.addAttribute("code", 0);
       model.addAttribute("total", adminRoleList.size());
       
       model.addAttribute("adminRoleList", adminRoleList);
       
   } catch (Throwable t) {
log.error(t.getMessage());
       log.error("get adminRole list error ");
       model.addAttribute("code", -100);
   }

return "/polyFinance-lgd-server/adminRole/json/adminRoleListJson";
}

遇到问题

1.分布式事务的处理方式这个还不知道怎么解决。

2.spring mvc 接收json数组的处理方式

明日计划

1.后台管理模块

收获

1.spring mvc接收数组格式的参数


返回列表 返回列表
评论

    分享到