111
wang-hao-jie
2022-08-25 d90da0759bd93c7f429f6a20bc835782ad927c02
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.boying.controller;
 
import com.boying.common.BaseController;
import com.boying.entity.StudyDir;
import com.boying.service.StudyDirService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
 
@RestController
@RequestMapping("studyDir")
public class StudyDirController extends BaseController {
 
    @Autowired
    private StudyDirService studyDirService;
 
    @PostMapping("findPage")
    public Object findPage() {
        return success("",studyDirService.findAll("parentId", null));
    }
 
    @PostMapping("findByParentId")
    public Object findByParentId(Long id) {
        return success("",studyDirService.findAll("parentId", id));
    }
 
    @PostMapping("save")
    public Object save(StudyDir studyDir) {
        if(studyDir.getParentId()!=null&&studyDir.getId()==null){
            StudyDir byId = (StudyDir) studyDirService.findById(studyDir.getParentId());
            byId.setHasChildren(true);
            studyDirService.save(byId);
        }
        studyDirService.save(studyDir);
        return success("保存成功");
    }
 
    @PostMapping("delete")
    public Object delete(Long id) {
        StudyDir studyDir = (StudyDir) studyDirService.findById(id);
 
        studyDirService.delete(id);
 
        if(studyDir.getParentId()!=null){
            List parentId = studyDirService.findAll("parentId", id);
            if(parentId.size()==0){
                StudyDir studyDir2 = (StudyDir) studyDirService.findById(studyDir.getParentId());
                studyDir2.setHasChildren(false);
                studyDirService.save(studyDir2);
            }
        }
 
        return success("删除成功");
    }
 
}