‘liusuyi’
2023-12-05 745d5b90be7d8cdb8873b18d18b286fdc4b6913b
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
package com.ard.alarm.external.service.impl;
 
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
import com.ard.alarm.external.domain.ArdEquipExternal;
import com.ard.alarm.external.mapper.ArdEquipExternalMapper;
import com.ard.alarm.external.service.IArdEquipExternalService;
import com.ard.utils.sdk.hiksdk.service.impl.HikClientUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
 
 
/**
 * externalService业务层处理
 *
 * @author zj
 * @date 2023-03-13
 */
@Service
@Slf4j(topic = "external")
@Order(5)
public class ArdEquipExternalServiceImpl implements IArdEquipExternalService, ApplicationRunner {
 
    private final static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
 
    public static List<ArdEquipExternal> ardEquipExternalList = new ArrayList<>();
    @Resource
    private ArdEquipExternalMapper ardEquipExternalMapper;
 
    @Override
    public void run(ApplicationArguments args) {
        List<String> targetTypes = Arrays.asList("1", "5", "6");
        List<ArdEquipExternal> ardEquipExternals = selectArdEquipExternalListByTypes(targetTypes);
        HikClientUtil.loginAllArdEquipExternals(ardEquipExternals);
        ardEquipExternalList.addAll(ardEquipExternals);
        log.debug("外联设备尝试登录");
        //同步任务
        syncTask();
    }
 
    /**
     * 同步任务
     * 刘苏义
     * 2023/8/11 9:09:27
     */
    private void syncTask() {
        scheduler.scheduleAtFixedRate(() -> {
            try {
                //region 定时同步外联设备
                List<String> targetTypes = Arrays.asList("1", "5", "6");
                List<ArdEquipExternal> ardEquipExternalsNew = selectArdEquipExternalListByTypes(targetTypes);
                //需要更新的数据
                List<ArdEquipExternal> updateList = sameListWithDifferent(ardEquipExternalList, ardEquipExternalsNew);
                if (updateList.size() > 0) {
                    HikClientUtil.logoutAllArdEquipExternals(updateList);
                    HikClientUtil.loginAllArdEquipExternals(updateList);
                    ardEquipExternalList.clear();
                    ardEquipExternalList.addAll(ardEquipExternalsNew);
                }
                //需要删除的数据
                List<ArdEquipExternal> delList = diffList(ardEquipExternalList, ardEquipExternalsNew);
                if (delList.size() > 0) {
                    HikClientUtil.logoutAllArdEquipExternals(delList);
                    for (ArdEquipExternal ardEquipExternal : delList) {
                        ardEquipExternalList.remove(ardEquipExternal);
                    }
                }
                //需要新增的数据
                List<ArdEquipExternal> inserList = diffList(ardEquipExternalsNew, ardEquipExternalList);
                if (inserList.size() > 0) {
                    HikClientUtil.loginAllArdEquipExternals(inserList);
                    for (ArdEquipExternal ardEquipExternal : inserList) {
                        ardEquipExternalList.add(ardEquipExternal);
                    }
                }
                //endregion
            } catch (Exception e) {
                log.error("同步外联任务执行出错" + e.getMessage());
            }
        }, 10, 10, TimeUnit.SECONDS);
    }
 
    /**
     * 查询external
     *
     * @param ardEquipExternal external
     * @return external集合
     */
    @Override
    public ArdEquipExternal selectArdEquipExternal(ArdEquipExternal ardEquipExternal) {
        QueryWrapper<ArdEquipExternal> queryWrapper = new QueryWrapper<>(ardEquipExternal);
        return ardEquipExternalMapper.selectOne(queryWrapper);
    }
 
    /**
     * 按类型列表查询external列表
     *
     * @param types 类型列表
     * @return external
     */
    @Override
    public List<ArdEquipExternal> selectArdEquipExternalListByTypes(List<String> types) {
        QueryWrapper<ArdEquipExternal> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("type", types);
        return ardEquipExternalMapper.selectList(queryWrapper);
    }
 
    //求两个对象List的交集字段不同Id相同
    private List<ArdEquipExternal> sameListWithDifferent(List<ArdEquipExternal> oldList, List<ArdEquipExternal> newList) {
        List<ArdEquipExternal> differentFieldsList = newList.stream()
                .filter(newItem -> {
                    ArdEquipExternal oldItem = oldList.stream()
                            .filter(item -> item.getId().equals(newItem.getId()))
                            .findFirst()
                            .orElse(null);
 
                    return oldItem == null ||
                            !Objects.equals(oldItem.getUpdateTime(), newItem.getUpdateTime()) ||
                            !Objects.equals(oldItem.getIp(), newItem.getIp()) ||
                            !Objects.equals(oldItem.getPort(), newItem.getPort()) ||
                            !Objects.equals(oldItem.getUsername(), newItem.getUsername()) ||
                            !Objects.equals(oldItem.getPassword(), newItem.getPassword());
                })
                .collect(Collectors.toList());
 
        return differentFieldsList;
    }
    //  求两个对象List的差集
    private List<ArdEquipExternal> diffList(List<ArdEquipExternal> oldArrayList, List<ArdEquipExternal> newArrayList) {
        List<ArdEquipExternal> resultList = oldArrayList.stream()
                .filter(item -> !newArrayList.stream().map(e -> e.getId()).collect(Collectors.toList()).contains(item.getId()))
                .collect(Collectors.toList());
        return resultList;
    }
}