ElectricFenceService.java
12.7 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package cn.com.glsx.admin.modules.service;
import cn.com.glsx.admin.common.constants.ConstantsApproval;
import cn.com.glsx.admin.common.constants.ConstantsElectricFence;
import cn.com.glsx.admin.modules.component.EFenceComponent;
import cn.com.glsx.admin.modules.entity.ElectricFence;
import cn.com.glsx.admin.modules.entity.ElectricFenceCopy;
import cn.com.glsx.admin.modules.entity.ElectricFenceSns;
import cn.com.glsx.admin.modules.entity.ElectricFenceSnsCopy;
import cn.com.glsx.admin.modules.mapper.ElectricFenceCopyMapper;
import cn.com.glsx.admin.modules.mapper.ElectricFenceMapper;
import cn.com.glsx.admin.modules.mapper.ElectricFenceSnsCopyMapper;
import cn.com.glsx.admin.modules.mapper.ElectricFenceSnsMapper;
import cn.com.glsx.admin.modules.model.ElectricFenceDTO;
import cn.com.glsx.admin.modules.model.ElectricFenceDetailsDTO;
import cn.com.glsx.admin.modules.model.ElectricFenceSnsDTO;
import cn.com.glsx.admin.modules.model.ElectricFenceSnsModel;
import cn.com.glsx.admin.modules.model.export.ElectricFenceExport;
import cn.com.glsx.admin.modules.model.param.ApprovalBo;
import cn.com.glsx.admin.modules.model.param.ElectricFenceBO;
import cn.com.glsx.admin.modules.model.param.ElectricFenceSearch;
import cn.com.glsx.auth.utils.ShieldContextHolder;
import cn.com.glsx.usercenter.api.dto.response.Organization;
import cn.com.glsx.usercenter.api.dto.response.User;
import cn.com.glsx.usercenter.api.feign.OrganizationService;
import cn.com.glsx.usercenter.api.feign.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.glsx.plat.common.utils.DateUtils;
import com.glsx.plat.core.web.R;
import com.google.common.collect.Lists;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class ElectricFenceService {
@Autowired
private EFenceComponent eFenceComponent;
@Autowired
private ElectricFenceMapper electricFenceMapper;
@Autowired
private ElectricFenceSnsMapper electricFenceSnsMapper;
@Autowired
private ElectricFenceCopyMapper electricFenceCopyMapper;
@Autowired
private ElectricFenceSnsCopyMapper electricFenceSnsCopyMapper;
@Autowired
private ApprovalService approvalService;
@Autowired
private UserService userService;
@Autowired
private OrganizationService organizationService;
@Autowired
private TrialDriveShopService trialDriveShopService;
public PageInfo<ElectricFenceDTO> search(ElectricFenceSearch search) {
Page page = PageHelper.startPage(search.getPageNumber(), search.getPageSize());
List<ElectricFenceDTO> list = electricFenceMapper.search(search);
Type type = new TypeToken<List<Organization>>() {
}.getType();
list.forEach(efd -> {
R<List<Organization>> listR = organizationService.getTenantPathsByUserId(efd.getCreatorId());
List<Organization> orgList = listR.getDataList(type);
if (CollectionUtils.isNotEmpty(orgList)) {
efd.setArea(orgList.get(1).getOrgName());
efd.setProvince(orgList.get(2).getOrgName());
efd.setCity(orgList.get(3).getOrgName());
}
// efd.getBizFenceType();
});
return new PageInfo<>(list);
}
public List<ElectricFenceExport> export(ElectricFenceSearch search) {
List<ElectricFenceExport> list = electricFenceMapper.export(search);
Type type = new TypeToken<List<Organization>>() {
}.getType();
list.forEach(efd -> {
R<List<Organization>> listR = organizationService.getTenantPathsByUserId(efd.getCreatorId());
List<Organization> orgList = listR.getDataList(type);
if (CollectionUtils.isNotEmpty(orgList)) {
efd.setArea(orgList.get(1).getOrgName());
efd.setProvince(orgList.get(2).getOrgName());
efd.setCity(orgList.get(3).getOrgName());
}
// efd.getBizFenceType();
});
return list;
}
/**
* 新增围栏
*
* @param fenceBO
*/
@Transactional(rollbackFor = Exception.class)
public ElectricFence addFence(ElectricFenceBO fenceBO) throws Exception {
//1.新增围栏信息
ElectricFence fence = new ElectricFence();
BeanUtils.copyProperties(fenceBO, fence);
//设置定位信息类型为gps类型 默认都是创建gps类型围栏
fence.setLocationType(1);
fence.setRecord("操作时间:" + DateUtils.formatNormal(new Date())
+ " 操作人:" + ShieldContextHolder.getAccount() + "(" + ShieldContextHolder.getUserId() + ")"
+ "<br/>");
if (fenceBO.getShopId() != null) {
fence.setCreatedBy(fenceBO.getShopId());
} else {
fence.setCreatedBy(ShieldContextHolder.getUserId());
}
fence.setCreatedDate(new Date());
int insertCnt = electricFenceMapper.insertUseGeneratedKeys(fence);
//2、关联设备
List<ElectricFenceSns> snsList = linkDevice(fence, fenceBO.getSnList());
if (CollectionUtils.isNotEmpty(snsList)) {
electricFenceSnsMapper.insertList(snsList);
}
//3.向大数据推送新增围栏信息(kafka)
if (insertCnt > 0) {
// eFenceComponent.addElectricFence(fence);
}
return fence;
}
@Transactional(rollbackFor = Exception.class)
public void updateFence(ElectricFenceBO fenceBO) throws Exception {
//1.更新围栏信息
ElectricFence localFence = electricFenceMapper.selectById(fenceBO.getId());
electricFenceSnsMapper.logicDeleteByFenceId(fenceBO.getId());
ElectricFenceCopy fenceCopy = new ElectricFenceCopy();
BeanUtils.copyProperties(fenceBO, fenceCopy);
if (fenceBO.getShapeType() != null) {
//围栏形状类型 1.圆形(默认) 2.矩形 3.多边形 4.行政区域
fenceCopy.setShapeType(fenceBO.getShapeType());
fenceCopy.setRadius(null);
fenceCopy.setAreaCode(null);
fenceCopy.setAreaText(null);
if (fenceBO.getShapeType() == ConstantsElectricFence.SHAPE_TYPE_CIRCLE) {
fenceCopy.setRadius(fenceBO.getRadius());
} else if (fenceBO.getShapeType() == ConstantsElectricFence.SHAPE_TYPE_POLYGON) {
//do nothing
} else if (fenceBO.getShapeType() == ConstantsElectricFence.SHAPE_TYPE_ADMINISTRATIVE) {
fenceCopy.setAreaCode(fenceBO.getAreaCode());
fenceCopy.setAreaText(fenceCopy.getAreaText());
}
}
//业务围栏类型 12试驾围栏,13特约店围栏(12,13直接和大数据那边类型对应)
fenceCopy.setBizFenceType(fenceBO.getBizFenceType());
fenceCopy.setLocationType(1);
fenceCopy.setRecord(localFence.getRecord() + "操作时间:" + DateUtils.formatNormal(new Date())
+ " 操作人:" + ShieldContextHolder.getAccount() + "(" + ShieldContextHolder.getUserId() + ")"
+ "<br/>");
fenceCopy.setStartTime(fenceBO.getStartTime());
fenceCopy.setEndTime(fenceBO.getEndTime());
fenceCopy.setUpdatedDate(new Date());
fenceCopy.setUpdatedBy(ShieldContextHolder.getUserId());
ElectricFenceCopy electricFenceCopy = electricFenceCopyMapper.selectByFenceId(fenceBO.getId());
if (electricFenceCopy != null) {
int updateCnt = electricFenceCopyMapper.insertUseGeneratedKeys(fenceCopy);
} else {
int updateCnt = electricFenceCopyMapper.updateByPrimaryKey(fenceCopy);
}
//2、关联设备
List<ElectricFenceSnsCopy> snsList = copyLinkDevice(fenceCopy, fenceBO.getSnList());
if (CollectionUtils.isNotEmpty(snsList)) {
electricFenceSnsCopyMapper.insertList(snsList);
}
//3.更新围栏 向大数据推送围栏更新(kafka),改成审核通过再同步
// if (updateCnt > 1) {
// eFenceComponent.updateElectricFence(fence);
// }
// 2021/5/24 围栏审核 特约店用户修改才提交
List<Long> shopOrgIds = trialDriveShopService.getShopOrgIds();
if (shopOrgIds.contains(ShieldContextHolder.getDepartmentId())) {
ApprovalBo approvalBo = new ApprovalBo(ConstantsApproval.VEL_FENCE_APPROVAL, fenceCopy.getFenceId());
approvalService.saveApproval(approvalBo);
}
}
public List<ElectricFenceSns> linkDevice(ElectricFence fence, List<ElectricFenceSnsModel> snList) {
List<ElectricFenceSns> snsList = Lists.newArrayList();
snList.forEach(efsm -> {
ElectricFenceSns fenceSns = new ElectricFenceSns();
fenceSns.setFenceId(fence.getId());
fenceSns.setSn(efsm.getSn());
fenceSns.setName(efsm.getName());
fenceSns.setVin(efsm.getVin());
fenceSns.setUserTel(efsm.getUserTel());
snsList.add(fenceSns);
});
return snsList;
}
/**
* 副本关联
*
* @param fenceCopy
* @param snList
* @return
*/
public List<ElectricFenceSnsCopy> copyLinkDevice(ElectricFenceCopy fenceCopy, List<ElectricFenceSnsModel> snList) {
List<ElectricFenceSnsCopy> snsList = Lists.newArrayList();
snList.forEach(efsm -> {
ElectricFenceSnsCopy fenceSnsCopy = new ElectricFenceSnsCopy();
fenceSnsCopy.setFenceId(fenceCopy.getId());
fenceSnsCopy.setSn(efsm.getSn());
fenceSnsCopy.setName(efsm.getName());
fenceSnsCopy.setVin(efsm.getVin());
fenceSnsCopy.setUserTel(efsm.getUserTel());
snsList.add(fenceSnsCopy);
});
return snsList;
}
public ElectricFenceDetailsDTO getInfoById(Long fenceId) {
ElectricFenceDetailsDTO detailsDTO = null;
ElectricFence electricFence = electricFenceMapper.selectById(fenceId);
if (electricFence != null) {
detailsDTO = new ElectricFenceDetailsDTO();
BeanUtils.copyProperties(electricFence, detailsDTO);
List<ElectricFenceSnsDTO> snsList = Lists.newLinkedList();
List<ElectricFenceSns> fenceSnsList = electricFenceSnsMapper.selectByFenceId(fenceId);
fenceSnsList.forEach(fs -> {
ElectricFenceSnsDTO snsDTO = new ElectricFenceSnsDTO();
BeanUtils.copyProperties(fs, snsDTO);
snsList.add(snsDTO);
});
detailsDTO.setSnList(snsList);
R<User> userR = userService.getById(electricFence.getCreatedBy());
User user = userR.getData(User.class);
if (user != null) {
detailsDTO.setShopId(user.getId());
detailsDTO.setShopName(user.getUsername());
} else {
detailsDTO.setShopId(electricFence.getCreatedBy());
}
}
return detailsDTO;
}
public ElectricFenceDetailsDTO getCopyInfoById(Long fenceId) {
ElectricFenceDetailsDTO detailsDTO = null;
ElectricFenceCopy fenceCopy = electricFenceCopyMapper.selectByFenceId(fenceId);
if (fenceCopy != null) {
detailsDTO = new ElectricFenceDetailsDTO();
BeanUtils.copyProperties(fenceCopy, detailsDTO);
List<ElectricFenceSnsDTO> snsList = Lists.newLinkedList();
List<ElectricFenceSnsCopy> fenceSnsList = electricFenceSnsCopyMapper.selectByFenceId(fenceId);
fenceSnsList.forEach(fs -> {
ElectricFenceSnsDTO snsDTO = new ElectricFenceSnsDTO();
BeanUtils.copyProperties(fs, snsDTO);
snsList.add(snsDTO);
});
detailsDTO.setSnList(snsList);
R<User> userR = userService.getById(fenceCopy.getCreatedBy());
User user = userR.getData(User.class);
if (user != null) {
detailsDTO.setShopId(user.getId());
detailsDTO.setShopName(user.getUsername());
} else {
detailsDTO.setShopId(fenceCopy.getCreatedBy());
}
}
return detailsDTO;
}
public void deleteById(Long fenceId) {
ElectricFence electricFence = electricFenceMapper.selectById(fenceId);
// eFenceComponent.deleteElectricFence();
electricFenceMapper.logicDeleteById(fenceId);
electricFenceSnsMapper.logicDeleteByFenceId(fenceId);
}
}