lidongdong
2022-10-08 72ca0ce4affcdcb626ef7cc726802169f40ba13c
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.dg.core.controller;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dg.core.ResultData;
import com.dg.core.annotation.Authorization;
import com.dg.core.annotation.CurrentUser;
import com.dg.core.db.gen.entity.OrganizationChartEntity;
import com.dg.core.db.gen.entity.SysUser;
import com.dg.core.service.IOrganizationChartService;
import com.dg.core.util.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
 
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
 
 
@Api(tags = {"组织架构接口"})
@RestController
@RequestMapping("/organization")
public class OrganizationController extends BaseController
{
 
    @Autowired(required = true)
    IOrganizationChartService iOrganizationChartService;
 
    /**
     * 查询机构列表
     */
    @ApiOperation(value = "查询机构列表",response = OrganizationChartEntity.class)
    @GetMapping("/getList")
    public TableDataInfo selectConfigList()
    {
        return getDataTable(iOrganizationChartService.selectConfigList("",""));
    }
 
    /**
     * 新增机构
     */
    @ApiOperation(value = "新增机构",response = OrganizationChartEntity.class)
    @PostMapping("/add")
    @Authorization
    public ResultData insertConfig(@RequestBody OrganizationChartEntity entity)
    {
        Assert.notNull(entity.getGrade(), "等级 grade 不能为空");
 
        if (StringUtils.equals("1",entity.getGrade()))
        {
            if(!StringUtils.isEmpty(entity.getParentId()))
            {
                return ResultData.error("一级部门不能有父级部门");
            }
        }
        else
        {
            if(StringUtils.isEmpty(entity.getParentId()))
            {
                return ResultData.error("非一级部门父级部门不能为空");
            }
        }
        return toAjax(iOrganizationChartService.insertConfig(entity));
    }
 
    /**
     * 更新机构
     */
    @ApiOperation(value = "更新机构",response = OrganizationChartEntity.class)
    @PostMapping("/update")
    @Authorization
    public ResultData updateConfig(@RequestBody OrganizationChartEntity entity,@CurrentUser SysUser sysUser)
    {
        entity.setUpdateTime(LocalDateTime.now());
        entity.setUpdateUserId(Integer.parseInt(String.valueOf(sysUser.getUserId())));
        return toAjax(iOrganizationChartService.updateConfig(entity));
    }
 
    /**
     * 删除机构
     */
    @ApiOperation("删除机构")
    @DeleteMapping("/delete")
    @Authorization
    public ResultData deleteConfigById(@RequestParam(value = "Id",required = false)  String Id)
    {
        Assert.notNull(Id, "Id 不能为空");
        return toAjax(iOrganizationChartService.deleteConfigById(Id));
    }
 
    /**
     * 查询机构列表(不分级不分页按等级排序)
     */
    @ApiOperation(value = "查询机构列表(不分级不分页按等级排序)",response = OrganizationChartEntity.class)
    @GetMapping("/selectList")
    public TableDataInfo selectList(){
        return getDataTable(iOrganizationChartService.selectList());
    }
 
    /**
     * 根据id查取消组织机构
     */
    @ApiOperation(value = "根据id查取消组织机构",response = OrganizationChartEntity.class)
    @GetMapping("/selectById")
    ResultData selectConfigById(@RequestParam("id") String id){
          return ResultData.success(iOrganizationChartService.selectConfigById(id));
    }
 
 
    /**
     * 查询机构列表(分页)
     */
    @ApiOperation(value = "查询机构列表(分页)(部门应用)",response = OrganizationChartEntity.class)
    @GetMapping("/queryList")
    @Authorization
    public TableDataInfo queryList(@RequestParam(value = "pageNum",required = false) Integer pageNum,
                                            @RequestParam(value = "pageSize",required = false) Integer pageSize,
                                            @RequestParam(value = "organizationName",required = false)String organizationName){
        Assert.notNull(pageNum, "pageNum can not be empty");
        Assert.notNull(pageSize, "pageSize can not be empty");
        Page<OrganizationChartEntity> pageParam = new Page<>(pageNum,pageSize);
        return getDataTable(iOrganizationChartService.queryList(pageParam,pageSize,organizationName),iOrganizationChartService.countList(organizationName));
    }
 
 
    /**
     * 通过父级id查询对应机构下的全部部门id
     * @param departmentId
     * @return
     */
    @ApiOperation(value = "通过父级id查询对应机构下的全部部门",response = OrganizationChartEntity.class)
    @GetMapping("/getdepartment")
    public TableDataInfo getIds(@RequestParam(value = "department",required = false) String departmentId)
    {
        List<OrganizationChartEntity> ids=new ArrayList<>();
        List<OrganizationChartEntity> lists = iOrganizationChartService.selectConfigList(departmentId,"");
        if(lists.size()<1)
        {
            lists.add(iOrganizationChartService.selectConfigById(departmentId));
        }
        ids=disposestreetId(lists);
        if(ids.size()<1)
        {
            return null;
        }
//        ids.add(streetId);
        ids.add(iOrganizationChartService.selectConfigById(departmentId));
        return getDataTable(ids);
    }
 
 
    //递归取id
    private List<OrganizationChartEntity> disposestreetId(List<OrganizationChartEntity> lists)
    {
        List<OrganizationChartEntity> ids=new ArrayList<>();
        for (OrganizationChartEntity sysStreet:lists)
        {
            ids.add(sysStreet);
            if(sysStreet.getChild()!=null && sysStreet.getChild().size()>0)
            {
                ids.addAll(disposestreetId(sysStreet.getChild()));
            }
            else
            {
                ids.add(sysStreet);
            }
        }
        return ids;
    }
 
 
 
 
}