CZT
2023-09-12 e8a5205acec61a431868c9b083fb62c2f187dd34
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
package com.fzzy.sys;
 
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.fzzy.api.Constant;
import com.fzzy.api.entity.ApiTrigger;
import com.fzzy.data.ConfigData;
import com.fzzy.sys.entity.SysDept;
import com.fzzy.sys.entity.SysUser;
import com.fzzy.sys.repository.DeptRepository;
import com.fzzy.sys.repository.UserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
/**
 * 部门管理
 */
@Component("sys.deptPR")
public class DeptPR {
 
    @Autowired
    private ConfigData configData;
    @Autowired
    private DeptRepository deptRepository;
    @Autowired
    private UserRepository userRepository;
 
    /**
     * 根据parentId查询组织架构及库区信息
     * sys.deptPR#listDept
     * @return
     */
    @DataProvider
    public List<SysDept> listDept() {
        return deptRepository.listDept();
    }
 
    /**
     * sys.deptPR#findAll
     * @return
     */
    @DataProvider
    public List<SysDept> findAll() {
        return deptRepository.findAll();
    }
 
    /**
     * 根据parentId查询组织架构及库区信息
     * sys.deptPR#findDeptByParentId
     *
     * @param parentId
     * @return
     */
    @DataProvider
    public List<SysDept> findDeptByParentId(String parentId) {
        return deptRepository.listDeptByParent(parentId);
    }
 
 
    /**
     * sys.deptPR#updateSave
     *
     * @param depts
     */
    @DataResolver
    public void updateSave(List<SysDept> depts) {
        if(depts != null){
            EntityState state = null;
            for(SysDept dept : depts){
                state = EntityUtils.getState(dept);
                if(state.equals(EntityState.NEW)){
                    this.saveData(dept);
                }
                if (state.equals(EntityState.MODIFIED)
                        || state.equals(EntityState.MOVED)) {
                    this.saveData(dept);
                }
                if (dept.getChildren() != null) {
                    SysDept[] sysDepts = new SysDept[dept
                            .getChildren().size()];
                    updateSave(Arrays.asList(dept.getChildren().toArray(sysDepts)));
                }
                if (state.equals(EntityState.DELETED)) {
                    if (countChildren(dept.getId()) == 0
                            && getUserByDeptId(dept.getId()).size() == 0) {
                        this.delData(dept);
                    } else {
                        throw new RuntimeException("请先删除子部门或是部门下是用户!");
                    }
                }
 
            }
        }
 
    }
 
    /**
     * sys.deptPR#countChildren
     *
     * @param parentId
     * @return
     */
    @Expose
    public int countChildren(String parentId) {
        List<SysDept> depts = deptRepository.listDeptByParent(parentId);
        return depts.size();
    }
 
    /**
     * sys.deptPR#delData
     *
     * @param data
     */
    @Expose
    public String delData(SysDept data) {
        deptRepository.deleteById(data.getId());
        return null;
    }
 
    /**
     * 获取组织类型(10表示省市区县,20表示公司)
     * ${dorado.getDataProvider("sys.deptPR#triggerDeptType").getResult()}
     *
     * @return
     */
    @DataProvider
    public List<ApiTrigger> triggerDeptType() {
        List<ApiTrigger> list = new ArrayList<ApiTrigger>();
        list.add(new ApiTrigger(Constant.DEPT_TYPE_10, "公司"));
        list.add(new ApiTrigger(Constant.DEPT_TYPE_20, "库点"));
        list.add(new ApiTrigger(Constant.DEPT_TYPE_30, "部门"));
 
        return list;
    }
 
 
    private SysDept findLastDeptId(String parentId) {
//        if (StringUtils.isEmpty(companyId)) {
//            companyId = ContextUtil.getCompanyId(true);
//        }
        List<SysDept> list = deptRepository.listDeptByParent(parentId);
        if (list != null && list.size() > 0) {
            return list.get(list.size() - 1);
        } else {
            return null;
        }
    }
 
    //保存
    public void saveData(SysDept entity) {
        // 手动将doradoEntity对象转换为标准Bean对象
        SysDept data = new SysDept();
        BeanUtils.copyProperties(entity, data);
 
        deptRepository.save(data);
    }
 
    public List<SysUser> getUserByDeptId(String deptId) {
        List<SysUser> list = userRepository.getUserByDeptId(deptId);
        if(list == null){
            list = new ArrayList<>();
        }
        return list;
    }
}