liuxiaolong
2019-05-06 f99bc8c6a1d10610373738edd7d0aa0181c81d99
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package com.cloud.user.controller;
 
import com.cloud.common.constants.GlobalDict;
import com.cloud.common.utils.OrgTypeUtil;
import com.cloud.model.common.Result;
import com.cloud.model.log.LogAnnotation;
import com.cloud.model.sys.SysOrganization;
import com.cloud.user.constants.UserAbstractLogModule;
import com.cloud.user.filter.AuthNoneIgnore;
import com.cloud.user.service.SysOrganizationService;
import com.cloud.user.vo.SysOrganizationVO;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
 
import java.util.*;
 
@RequestMapping("/data/api-u/org")
@RestController
@Api(value = "组织架构接口", description = "组织架构接口 - SysOrganizationController")
public class SysOrganizationController {
 
    @Autowired
    private SysOrganizationService orgService;
 
 
    /**
     * 组织机构添加
     * @param org
     * @return
     */
    @PostMapping("/save")
    //@LogAnnotation(module =UserAbstractLogModule.ORG_SAVE)
    /*@PreAuthorize("hasAuthority('sys:org:save')")*/
    @ApiOperation(value = "组织机构添加", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "org", value = "SysOrganization组织机构实体", required = true, dataType = "SysOrganization", paramType = "body"),
    })
    public Map<String,Object> save(@RequestBody SysOrganizationVO org) {
 
        return orgService.save(org);
    }
 
    /**
     * 组织机构修改
     * @param org
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "组织机构修改", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "org", value = "SysOrganization组织机构实体", required = true, dataType = "SysOrganization", paramType = "body"),
    })
    public Result update(@RequestBody SysOrganizationVO org) {
        int status = orgService.update(org);
        if(status > 0){
            return Result.custom("修改成功!",0,true,null);
        }else{
            return Result.custom("修改失败!",1,false,null);
        }
    }
 
    /**
     * 组织机构逻辑删除
     * @param params
     * @return
     */
    @GetMapping("/delete")
    @ApiOperation(value = "组织机构添加", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "org", value = "组织id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "机构id", required = true, dataType = "String", paramType = "query")
    })
    public Map<String,Object> deleteOrg(@RequestParam Map<String,Object> params){
 
        return orgService.deleteOrg(params);
    }
 
    /**
     * 查找组织树
     */
    @GetMapping("/tree")
    @AuthNoneIgnore
    @ApiOperation(value = "查找本机构及以下的组织机构,返回树形", httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgById", value = "组织机构ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "机构分库ID", required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "组织级别", required = false, dataType = "string", paramType = "query")
    })
    public List<SysOrganization> findOrgTree(@RequestParam Map<String,Object> params){
 
        List<SysOrganization> all = orgService.findAll(params);
        Long orgById =  Long.parseLong(params.get("orgById").toString());
        List<SysOrganization> list = new ArrayList<>();
        List<SysOrganization> datalist = new ArrayList<>();
        setOrgTree(orgById, all, list);
        for(SysOrganization org:all){
            if(org.getId().equals(orgById)){
                org.setChild(list);
                datalist.add(org);
            }
        }
        return datalist;
    }
 
    /**
     * 遍历循环查找组织树的方法
     */
    private void setOrgTree(Long parentId, List<SysOrganization> all, List<SysOrganization> list) {
        all.forEach(org -> {
            if (parentId.equals(org.getParentId())) {
                list.add(org);
 
                List<SysOrganization> child = new ArrayList<>();
                org.setChild(child);
                setOrgTree(org.getId(), all, child);
            }
        });
    }
 
 
    @GetMapping("/findUpByType")
    @ApiOperation(value = "查找上级第一个指定的机构类型", notes = "例如:查找学校", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "要查找的机构类型", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "currentId", value = "指定的机构id,查它之上的机构", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "有值分库查,没值查全部", dataType = "Long", paramType = "query")
    })
    SysOrganization findUpByType(Integer type, Long currentId,Long orgId){
 
        return orgService.findUpByType(type,currentId,orgId);
    }
 
    @GetMapping("/findUpById")
    @ApiOperation(value = "查找上级第一个指定的机构类型", notes = "例如:查找学校", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentId", value = "指定的机构id,查它之上的机构", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "有值分库查,没值查全部", dataType = "Long", paramType = "query")
    })
    SysOrganization findUpById(Long currentId,Long orgId){
        return orgService.findUpById(currentId,orgId);
    }
    @GetMapping("/findUpByType/feign")
    @ApiOperation(value = "查找上级第一个指定的机构类型", notes = "例如:查找学校", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "要查找的机构类型", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "currentId", value = "指定的机构id,查它之上的机构", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "有值分库查,没值查全部", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = " access_token", value = "token", dataType = "String", paramType = "query")
    })
    SysOrganization findUpByTypeFeign(Integer type, Long currentId,Long orgId, String access_token){
 
        return orgService.findUpByType(type,currentId,orgId);
    }
 
    @GetMapping("/findDownByType")
    @ApiOperation(value = "查找下级的指定类型的所有机构", notes = "例如;查找查找学校所有年级,年级所有的班级", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "要查找的机构类型", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "currentId", value = "指定的机构id,查它之下的机构", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "有值分库查,没值查全部", required = true, dataType = "Long", paramType = "query")
    })
    List<SysOrganization> findDownByType(Integer type, String currentId,String orgId){
        Long curId = null,orgid = null;
        if (currentId == null || "".equals( currentId) || "all".equalsIgnoreCase( currentId) ){
            }else {
         curId = Long.valueOf(currentId);
        }
        if (orgId == null || "".equals( orgId) || "all".equalsIgnoreCase( orgId) ){
            }else {
            orgid = Long.valueOf(orgId);
        }
        return orgService.findDownByType(type,curId,orgid);
    }
 
    @GetMapping("/findCheckSchool")
    @ApiOperation(value = "查找学区内的所有学校", notes = "查找学区内的所有学校仅展示id,name,address", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentId", value = "学区id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "searchName", value = "学校名字", required = true, dataType = "String", paramType = "query")
    })
    Map<String,Object> findCheckSchool(@RequestParam Map<String,Object> params){
 
        return orgService.findCheckSchool(params);
    }
 
    @GetMapping("/findHeadquarters")
    @ApiOperation(value = "查找所有的总部", notes = "", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentId", value = "从哪个节点下开始查找", required = true, dataType = "String", paramType = "query"),
    })
    List<SysOrganization> findHeadquarters(Long currentId){
        String type= OrgTypeUtil.getByKey(GlobalDict.ROOT_SCHOOL).getValue();
        return orgService.findHeadquarters(currentId,type);
    }
 
    @GetMapping("/findSubOrg")
    @ApiOperation(value = "查找下级的指定类型的所有子机构", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "headId", value = "总部Id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "isSelf", value = "返回值是否包括总部", required = true,  dataType = "boolean", paramType = "query")
    })
    List<SysOrganization> findSubOrg(Long headId , boolean isSelf){
        String type= OrgTypeUtil.getByKey(GlobalDict.ROOT_SCHOOL).getValue();
        return orgService.findSubOrg(headId,isSelf,type);
    }
 
    @GetMapping("/findById")
    @ApiOperation(value = "通过id查询组织机构列表", notes = "通过id查询组织机构列表", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "总部Id", required = true, dataType = "Long", paramType = "form"),
            @ApiImplicitParam(name = "orgId", value = "组织机构id", required = false,  dataType = "boolean", paramType = "form")
    })
    public SysOrganization findById(Long id , Long orgId){
        return orgService.findById(id, orgId);
    }
 
    @GetMapping("/findOrgByDep")
    @ApiOperation(value = "查找学校以上组织机构的部门", notes = "查找学校以上组织机构的部门", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentId", value = "机构id", required = true, dataType = "Long", paramType = "query")
    })
    List<SysOrganization> findOrgByDep(Long currentId){
        Integer type = Integer.parseInt(OrgTypeUtil.getByKey("部门").getValue());
        return orgService.findDownByType(type,currentId,null);
    }
 
    @GetMapping("/findOrgIds")
    @ApiOperation(value = "根据传来的组织id查找它上级的学校学区orgId集合", notes = "根据传来的orgId查找它上级的学校学区orgId集合", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "组织id", required = true, dataType = "Long", paramType = "query")
    })
    public List<Integer> findOrgIds(@RequestParam Integer id){
        List<Integer> list = orgService.findOrgIds(id);
        return list;
    }
 
    @GetMapping("/findOrgName")
    @ApiOperation(value = "根据传来的机构id查找机构名称", notes = "根据传来的机构id查找机构名称", httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "组织id", required = true, dataType = "Long", paramType = "query")
    })
    public String findOrgName(@RequestParam Integer id){
 
        return orgService.findOrgName(id);
    }
 
}