ElectricFenceService.java
18.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
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.common.enums.ApprovalStatusEnum;
import cn.com.glsx.admin.common.enums.ResultEnum;
import cn.com.glsx.admin.common.exception.AdminException;
import cn.com.glsx.admin.modules.component.EFenceComponent;
import cn.com.glsx.admin.modules.entity.*;
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 cn.hutool.core.util.ObjectUtil;
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
* @throws Exception
*/
public void addFenceHandler(ElectricFenceBO fenceBO) throws Exception {
// 2021/5/24 围栏审核 特约店用户修改才提交
List<Long> shopOrgIds = trialDriveShopService.getShopOrgIds();
boolean submitFlag = shopOrgIds.contains(ShieldContextHolder.getDepartmentId());
if (submitFlag) {
ElectricFenceCopy fenceCopy = addFenceCopy(fenceBO);
//提交审核记录
ApprovalBo approvalBo = new ApprovalBo(ConstantsApproval.VEL_FENCE_APPROVAL, fenceCopy.getId());
approvalService.saveApproval(approvalBo);
} else {
ElectricFence fence = addFence(fenceBO);
//向大数据推送新增围栏信息(kafka)
// eFenceComponent.addElectricFence(fence);
}
}
/**
* 编辑围栏处理
*
* @param fenceBO
* @throws Exception
*/
public void editFenceHandler(ElectricFenceBO fenceBO) throws Exception {
// 2021/5/24 围栏审核 特约店用户修改才提交
List<Long> shopOrgIds = trialDriveShopService.getShopOrgIds();
boolean submitFlag = shopOrgIds.contains(ShieldContextHolder.getDepartmentId());
if (submitFlag) {
ElectricFenceCopy fenceCopy = updateFenceCopy(fenceBO);
//提交审核记录
ApprovalBo approvalBo = new ApprovalBo(ConstantsApproval.VEL_FENCE_APPROVAL, fenceCopy.getFenceId());
approvalService.saveApproval(approvalBo);
} else {
ElectricFence fence = updateFence(fenceBO);
//向大数据推送围栏更新(kafka)
//eFenceComponent.updateElectricFence(fence);
}
}
/**
* 审批回调处理
*
* @param approvalData
*/
public void approvaledHandler(Approval approvalData) {
if (ApprovalStatusEnum.APPROVAL_STATUS_PROCESSED.getCode().equals(approvalData.getStatus())) {
// TODO: 2021/5/27 将副本刷新到主数据
ElectricFence fence = null;
ElectricFenceCopy fenceCopy = electricFenceCopyMapper.selectByFenceId(approvalData.getBizId());
//如果副本存在并且围栏id为空,为新增围栏审核
boolean addFlag = ObjectUtil.isNotNull(fenceCopy) && ObjectUtil.isNull(fenceCopy.getFenceId());
if (addFlag) {
refreshElectricFenceCopy2ElectricFence(fenceCopy, null);
} else {
fence = electricFenceMapper.selectById(approvalData.getBizId());
refreshElectricFenceCopy2ElectricFence(fenceCopy, fence);
}
} else if (ApprovalStatusEnum.APPROVAL_STATUS_REJECT.getCode().equals(approvalData.getStatus())) {
}
}
/**
* 新增围栏
*
* @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);
}
return fence;
}
/**
* 新增围栏
*
* @param fenceBO
*/
@Transactional(rollbackFor = Exception.class)
public ElectricFenceCopy addFenceCopy(ElectricFenceBO fenceBO) throws Exception {
//1.新增围栏信息
ElectricFenceCopy fenceCopy = new ElectricFenceCopy();
BeanUtils.copyProperties(fenceBO, fenceCopy);
//设置定位信息类型为gps类型 默认都是创建gps类型围栏
fenceCopy.setLocationType(1);
fenceCopy.setRecord("操作时间:" + DateUtils.formatNormal(new Date())
+ " 操作人:" + ShieldContextHolder.getAccount() + "(" + ShieldContextHolder.getUserId() + ")"
+ "<br/>");
if (fenceBO.getShopId() != null) {
fenceCopy.setCreatedBy(fenceBO.getShopId());
} else {
fenceCopy.setCreatedBy(ShieldContextHolder.getUserId());
}
fenceCopy.setCreatedDate(new Date());
int insertCnt = electricFenceCopyMapper.insertUseGeneratedKeys(fenceCopy);
//2、关联设备
List<ElectricFenceSnsCopy> snsList = linkDeviceCopy(fenceCopy, fenceBO.getSnList());
if (CollectionUtils.isNotEmpty(snsList)) {
electricFenceSnsCopyMapper.insertList(snsList);
}
return fenceCopy;
}
@Transactional(rollbackFor = Exception.class)
public ElectricFence updateFence(ElectricFenceBO fenceBO) throws Exception {
//1.更新围栏信息
ElectricFence localFence = electricFenceMapper.selectById(fenceBO.getId());
electricFenceSnsMapper.logicDeleteByFenceId(fenceBO.getId());
ElectricFence fence = new ElectricFence();
BeanUtils.copyProperties(fenceBO, fence);
if (fenceBO.getShapeType() != null) {
//围栏形状类型 1.圆形(默认) 2.矩形 3.多边形 4.行政区域
fence.setShapeType(fenceBO.getShapeType());
fence.setRadius(null);
fence.setAreaCode(null);
fence.setAreaText(null);
if (fenceBO.getShapeType() == ConstantsElectricFence.SHAPE_TYPE_CIRCLE) {
fence.setRadius(fenceBO.getRadius());
} else if (fenceBO.getShapeType() == ConstantsElectricFence.SHAPE_TYPE_POLYGON) {
//do nothing
} else if (fenceBO.getShapeType() == ConstantsElectricFence.SHAPE_TYPE_ADMINISTRATIVE) {
fence.setAreaCode(fenceBO.getAreaCode());
fence.setAreaText(fence.getAreaText());
}
}
//业务围栏类型 12试驾围栏,13特约店围栏(12,13直接和大数据那边类型对应)
fence.setBizFenceType(fenceBO.getBizFenceType());
fence.setLocationType(1);
fence.setRecord(localFence.getRecord() + "操作时间:" + DateUtils.formatNormal(new Date())
+ " 操作人:" + ShieldContextHolder.getAccount() + "(" + ShieldContextHolder.getUserId() + ")"
+ "<br/>");
fence.setStartTime(fenceBO.getStartTime());
fence.setEndTime(fenceBO.getEndTime());
fence.setUpdatedDate(new Date());
fence.setUpdatedBy(ShieldContextHolder.getUserId());
int updateCnt = electricFenceMapper.updateByPrimaryKey(fence);
//2、关联设备
List<ElectricFenceSns> snsList = linkDevice(fence, fenceBO.getSnList());
if (CollectionUtils.isNotEmpty(snsList)) {
electricFenceSnsMapper.insertList(snsList);
}
return fence;
}
@Transactional(rollbackFor = Exception.class)
public ElectricFenceCopy updateFenceCopy(ElectricFenceBO fenceBO) throws Exception {
//1.更新围栏信息
ElectricFence localFence = electricFenceMapper.selectById(fenceBO.getId());
electricFenceSnsCopyMapper.logicDeleteByFenceId(fenceBO.getId());
ElectricFenceCopy fenceCopy = new ElectricFenceCopy();
BeanUtils.copyProperties(fenceBO, fenceCopy);
fenceCopy.setFenceId(localFence.getId());
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 = linkDeviceCopy(fenceCopy, fenceBO.getSnList());
if (CollectionUtils.isNotEmpty(snsList)) {
electricFenceSnsCopyMapper.insertList(snsList);
}
return fenceCopy;
}
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> linkDeviceCopy(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 ElectricFence refreshElectricFenceCopy2ElectricFence(ElectricFenceCopy fenceCopy, ElectricFence fence) {
if (ObjectUtil.isNull(fenceCopy)) {
throw new AdminException(ResultEnum.APPROVAL_BIZ_HANDLE_ERROR);
}
if (ObjectUtil.isNull(fence)) {
fence = new ElectricFence();
}
// TODO: 2021/5/27
BeanUtils.copyProperties(fenceCopy, fence);
return fence;
}
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);
}
}