liuxiaolong
2019-05-06 71af9c46c24b562acc00a71ec6c97c04eb048d9c
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
package com.cloud.device.controller;
 
import com.cloud.device.constants.Constants;
import com.cloud.device.filter.AuthNoneIgnore;
import com.cloud.device.model.Device;
import com.cloud.device.model.Dic;
import com.cloud.device.model.Org;
import com.cloud.device.service.DeviceService;
import com.cloud.device.service.OrgService;
import com.cloud.device.vo.MenuTreeVo;
import com.cloud.model.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.NamedBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
 
import java.util.ArrayList;
import java.util.List;
 
@Slf4j
@Api(value = "DicController", description = "组织机构管理")
@RequestMapping("/data/api-d/org")
@RestController
public class OrgController {
 
    @Autowired
    private OrgService orgService;
    @Autowired
    private DeviceService deviceService;
 
    @ApiOperation(value="保存组织架构",notes = "保存组织架构",httpMethod = "POST",produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "org", value = "组织机构实例",  required = true, dataType = "Org", paramType = "body")
 
    })
    @RequestMapping("/saveOrg")
    public Result saveOrg(@RequestBody Org org) {
        try {
            log.info("父级id:"+org.getParentId()+",组织名称:"+org.getName());
            if(org.getId() !=null && org.getId() >0){
                return Result.ok("保存成功", orgService.updateByIdSelective(org));
            }else{
                return Result.ok("添加成功", orgService.insertSelective(org));
            }
        }
        catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return Result.error(e);
        }
 
    }
 
    @GetMapping("/findOrg")
    @ApiOperation(value = "获取组织树", notes = "获取组织树", httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "组织名称", required = false, dataType = "String", paramType = "query")
    })
    public List<MenuTreeVo> findOrg(@RequestParam String name){
        List<MenuTreeVo> list = new ArrayList<>();
        try {
            return orgService.findOrg(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return list;
    }
 
    @ApiOperation(value = "删除组织机构", notes = "删除组织机构", httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "query")
    })
    @RequestMapping("/deleteOrg")
    public Result deleteOrg(@RequestParam Long id){
        log.info("删除orgId:"+id);
        //先判断是否有子组织
        List<Org> children = orgService.findChildById(id);
        if(children !=null && children.size() >0){
            return Result.custom("此组织下有子节点,不允许删除!",401,false,"删除失败!");
        }
        List<Device> devices = deviceService.findByOrgId(id);
        if(devices !=null && devices.size() >0){
            return Result.custom("此组织下有设备信息,不允许删除!",401,false,"删除失败!");
        }
        return Result.ok(Constants.RESULT_DELETE_OK,orgService.deleteById(id));
    }
}