|
@@ -0,0 +1,150 @@
|
|
|
+package com.free.controller.system;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import javax.validation.Valid;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.cglib.beans.BeanCopier;
|
|
|
+import org.springframework.web.bind.annotation.DeleteMapping;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.type.CollectionType;
|
|
|
+import com.free.annotation.PassToken;
|
|
|
+import com.free.dto.system.menus.MenusCreateDTO;
|
|
|
+import com.free.entity.system.Menus;
|
|
|
+import com.free.frame.CustomizationException;
|
|
|
+import com.free.frame.ExceptionEnum;
|
|
|
+import com.free.frame.ResponseFormat;
|
|
|
+import com.free.mapper.system.MenusMapper;
|
|
|
+import com.free.service.system.MenusService;
|
|
|
+import com.free.utils.Utils;
|
|
|
+
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/menus")
|
|
|
+@Api(tags = "目录")
|
|
|
+public class MenusController {
|
|
|
+ @Autowired
|
|
|
+ private MenusService service;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MenusMapper menusMapper;
|
|
|
+
|
|
|
+ /** 创建数据 */
|
|
|
+ @ApiOperation("创建数据")
|
|
|
+ @PostMapping("")
|
|
|
+ public Object save(@RequestBody @Valid MenusCreateDTO data) {
|
|
|
+ BeanCopier copier = BeanCopier.create(MenusCreateDTO.class, Menus.class, false);
|
|
|
+ Menus insertData = new Menus();
|
|
|
+ copier.copy(data, insertData, null);
|
|
|
+ this.service.save(insertData);
|
|
|
+ QueryWrapper qw = new QueryWrapper<>();
|
|
|
+ qw.eq("id", insertData.getId());
|
|
|
+ Map returnData = this.service.getMap(qw);
|
|
|
+ return ResponseFormat.success(returnData);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 修改数据 */
|
|
|
+ @ApiOperation("修改数据")
|
|
|
+ @PostMapping("/{id}")
|
|
|
+ public Object update(@PathVariable long id, @RequestBody Menus data) {
|
|
|
+ QueryWrapper<Menus> qw = new QueryWrapper<>();
|
|
|
+ qw.eq("id", id);
|
|
|
+ Long num = this.service.count(qw);
|
|
|
+ if (num <= 0)
|
|
|
+ throw new CustomizationException(ExceptionEnum.NOT_FOUND);
|
|
|
+ data.setId(id);
|
|
|
+ this.service.updateById(data);
|
|
|
+ Object newData = this.service.getById(id);
|
|
|
+ return ResponseFormat.success(newData);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 递归查询 */
|
|
|
+ @ApiOperation("目录递归查询")
|
|
|
+ @GetMapping()
|
|
|
+ public Object list() {
|
|
|
+ List returnData = this.service.recursionMenus();
|
|
|
+ return ResponseFormat.success(returnData);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 根据id查询 */
|
|
|
+ @ApiOperation("查询数据")
|
|
|
+ @GetMapping("/{id}")
|
|
|
+ public Object fetch(@PathVariable long id) {
|
|
|
+ Object newData = service.getById(id);
|
|
|
+ return ResponseFormat.success(newData);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 根据id删除数据 */
|
|
|
+ @ApiOperation("删除数据")
|
|
|
+ @DeleteMapping("/{id}")
|
|
|
+ public Object delete(@PathVariable long id) {
|
|
|
+ QueryWrapper qw = new QueryWrapper<>();
|
|
|
+ qw.eq("id", id);
|
|
|
+ Long num = service.count(qw);
|
|
|
+ if (num <= 0)
|
|
|
+ throw new CustomizationException(ExceptionEnum.NOT_FOUND);
|
|
|
+ // TODO: 递归删除
|
|
|
+ service.removeById(id);
|
|
|
+ return ResponseFormat.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @PassToken
|
|
|
+ @ApiOperation("初始化目录")
|
|
|
+ @PostMapping("/initData")
|
|
|
+ public Object initData() throws JsonProcessingException {
|
|
|
+ String filePath = "db/menus.json";
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ String str = Utils.readJsonFile(filePath);
|
|
|
+ CollectionType javaType = mapper.getTypeFactory().constructCollectionType(List.class, Map.class);
|
|
|
+ List<Map> list = mapper.readValue(str, javaType);
|
|
|
+ // 整理数据
|
|
|
+ menusMapper.truncate();
|
|
|
+ this.toInsertMenusData(list, null);
|
|
|
+ return ResponseFormat.success(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void toInsertMenusData(List<Map> list, Long parent_id) {
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
+ Map m = list.get(i);
|
|
|
+ // 获取children,然后把map里的children删了,要不影响之后转换成Menus
|
|
|
+ List<Map> children = (List<Map>) m.get("children");
|
|
|
+ m.remove("children");
|
|
|
+ // 获取排序,没有顺序就填充
|
|
|
+ Integer on = (Integer) m.get("order_num");
|
|
|
+ Long order_num = null;
|
|
|
+ if (null == on) {
|
|
|
+ order_num = i + 1L;
|
|
|
+ } else {
|
|
|
+ order_num = Long.valueOf(on);
|
|
|
+ }
|
|
|
+ // 如果有parent_id,说明是子目录,需要加上
|
|
|
+ m.put("parent_id", parent_id);
|
|
|
+ // 转船成menus对象
|
|
|
+ Menus menuData = mapper.convertValue(m, Menus.class);
|
|
|
+ menuData.setOrder_num(order_num);
|
|
|
+ // 创建数据,并获得当前id
|
|
|
+ this.service.save(menuData);
|
|
|
+ if (null == children) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 处理children,获取parent_id
|
|
|
+ Long children_parent_id = menuData.getId();
|
|
|
+ toInsertMenusData(children, children_parent_id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|