109 lines
3.1 KiB
Java
109 lines
3.1 KiB
Java
package com.vverp.controller.admin;
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.vverp.entity.Menu;
|
|
import com.vverp.moli.util.Order;
|
|
import com.vverp.moli.util.Pageable;
|
|
import com.vverp.moli.util.RespData;
|
|
import com.vverp.service.MenuService;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.ui.ModelMap;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
@Controller
|
|
@RequestMapping("/admin/menu")
|
|
public class MenuController extends BaseController {
|
|
|
|
@Resource
|
|
private MenuService menuService;
|
|
|
|
@RequestMapping("/index")
|
|
public String index() {
|
|
return "/t-menu/index";
|
|
}
|
|
|
|
@RequestMapping("/dialog/add")
|
|
public String dialogAdd(ModelMap modelMap, String level, String parentId) {
|
|
modelMap.put("level", level);
|
|
modelMap.put("parentId", parentId);
|
|
return "/t-menu/dialog/add";
|
|
}
|
|
|
|
@RequestMapping("/dialog/edit")
|
|
public String dialogEdit(ModelMap modelMap, Long id) {
|
|
modelMap.put("menu", menuService.find(id));
|
|
return "/t-menu/dialog/edit";
|
|
}
|
|
|
|
@RequestMapping("/save")
|
|
@ResponseBody
|
|
public RespData save(Menu menu) {
|
|
if (menu.getLevel() == null) {
|
|
return RespData.error("类型不能为空");
|
|
}
|
|
if(menu.getSortFactor() == null) {
|
|
|
|
menu.setSortFactor(menuService.getMaxSortFactor(menu.getLevel(), menu.getParentId()) + 1);
|
|
}
|
|
menuService.save(menu);
|
|
return RespData.success();
|
|
}
|
|
|
|
@RequestMapping("/update")
|
|
@ResponseBody
|
|
public RespData update(Menu menu) {
|
|
menuService.updateMenu(menu);
|
|
return RespData.success();
|
|
}
|
|
|
|
@RequestMapping("/listAll")
|
|
@ResponseBody
|
|
public RespData listAll() {
|
|
Map<String, Object> map = new HashMap<>();
|
|
map.put("list", menuService.findList(null, null, Collections.singletonList(Order.asc("sortFactor"))));
|
|
return RespData.success(map);
|
|
}
|
|
|
|
@RequestMapping("/changeIndex")
|
|
@ResponseBody
|
|
public RespData changeIndex(String jsonStr) {
|
|
JSONArray jsonArray = JSONArray.parseArray(jsonStr);
|
|
for (Object item : jsonArray) {
|
|
JSONObject jsonObject = (JSONObject) item;
|
|
Long id = jsonObject.getLong("id");
|
|
Integer index = jsonObject.getInteger("index");
|
|
Menu menu = menuService.find(id);
|
|
menu.setSortFactor(index);
|
|
menuService.update(menu);
|
|
}
|
|
|
|
return RespData.success();
|
|
}
|
|
|
|
@RequestMapping("/delete")
|
|
@ResponseBody
|
|
public RespData delete(Long id) {
|
|
Menu menu = menuService.find(id);
|
|
if (menu.getLevel().equals(1)) {
|
|
List<Menu> list = menuService.findListByLevel(2, menu.getId());
|
|
list.add(menu);
|
|
for (Menu item : list) {
|
|
menuService.delete(item);
|
|
}
|
|
} else {
|
|
menuService.delete(menu);
|
|
}
|
|
|
|
return RespData.success();
|
|
}
|
|
}
|
|
|