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
package cn.wisenergy.chnmuseum.party.service.impl;
import cn.wisenergy.chnmuseum.party.common.util.DateUtil80;
import cn.wisenergy.chnmuseum.party.mapper.TAreaMapper;
import cn.wisenergy.chnmuseum.party.model.TArea;
import cn.wisenergy.chnmuseum.party.model.TOrgan;
import cn.wisenergy.chnmuseum.party.mapper.TOrganMapper;
import cn.wisenergy.chnmuseum.party.service.TOrganService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
/**
* <pre>
* 用户 服务实现类
* </pre>
*
* @author Danny Lee
* @since 2021-03-22
*/
@Slf4j
@Service
public class TOrganServiceImpl extends ServiceImpl<TOrganMapper, TOrgan> implements TOrganService {
@Resource
private TOrganMapper organMapper;
@Resource
private TAreaMapper areaMapper;
@Override
public List<TOrgan> getTree() {
List<TOrgan> list = list(Wrappers.<TOrgan>lambdaQuery().eq(TOrgan::getIsDeleted, 0).orderByDesc(TOrgan::getLevel));
HashMap<String, TOrgan> map = new HashMap<>();
list.forEach(o-> map.put(o.getId(),o));
for (TOrgan o : list) {
TOrgan parentOrg= map.get(o.getParentId());
if(parentOrg!=null){
//说明有值
if(parentOrg.getChildren()==null){
parentOrg.setChildren(new ArrayList<>());
}
parentOrg.getChildren().add(o);
}
}
list = list.stream().filter(o -> o.getLevel() == 1).collect(Collectors.toList());
return list;
}
@Override
public boolean batchUpload(List excelList) {
boolean flag =false;
List<TOrgan> list1 = list();
HashMap<String, TOrgan> map1 = new HashMap<>();
list1.stream().forEach(o-> map1.put(o.getName(),o));
HashMap<String, String> area = new HashMap<>();
List<Map<String, Object>> mapList = organMapper.selectArea();
mapList.stream().forEach(m->area.put(m.get("name").toString(),m.get("id").toString()));
for (int i = 0; i < excelList.size(); i++) {
Map<String, String> map = null;
map = (Map<String, String>) excelList.get(i);
TOrgan organ = new TOrgan();
organ.setName(map.get("名称"));
String parent = map.get("上级机构");
if (map1.get(parent)==null){
organ.setParentId("0");
organ.setLevel(1);
}else {
organ.setParentId(map1.get(parent).getId());
organ.setLevel(map1.get(parent).getLevel()+1);
}
if (organ.getLevel()>3){
return false;
}
String organCode = getOrganCode(organ.getParentId());
organ.setCode(organCode);
organ.setAreaId(area.get(map.get("区域")));
organ.setRemarks(map.get("备注"));
organ.setIsDeleted(false);
organ.setCreateTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
organ.setUpdateTime(LocalDateTime.now());
flag = save(organ);
// list.add(organ);
}
// flag = saveBatch(list);
return flag;
}
@Override
public TOrgan selectById(String id) {
TOrgan organ = organMapper.getById(id);
String s = areaMapper.selectParent(organ.getAreaId());
List<TOrgan> list1 = list(new UpdateWrapper<TOrgan>().eq("parent_id", organ.getParentId()));
organ.setAreas(Arrays.asList(s.split(",")));
organ.setChildren(list1);
return organ;
}
@Override
public TOrgan selectCodeMax(String parentId) {
return organMapper.selectCodeMax(parentId);
}
@Override
public List<TOrgan> getAllList(TOrgan tOrgan) {
return organMapper.getAllList(tOrgan);
}
private String getOrganCode(String parentId) {
TOrgan max = selectCodeMax(parentId);
if (max==null){
max = getById(parentId);
String s = max.getCode() + "001";
return s;
}
String s = max.getCode().substring(max.getCode().length() - 3);
String s1 = max.getCode().substring(0,max.getCode().length() - 3);
Integer integer = Integer.valueOf(s)+1;
if (integer<10){
s1=s1+"00"+integer.toString();
}else if (integer<100){
s1=s1+"0"+integer.toString();
}else {
s1=s1+integer.toString();
}
return s1;
}
}