648540858
2023-04-04 b4048fbe80dba8e7756ae557a15ab60b4f80a44b
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
package com.genersoft.iot.vmp.service.impl;
 
import com.genersoft.iot.vmp.gb28181.bean.*;
import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent;
import com.genersoft.iot.vmp.service.IPlatformChannelService;
import com.genersoft.iot.vmp.storager.dao.DeviceChannelMapper;
import com.genersoft.iot.vmp.storager.dao.ParentPlatformMapper;
import com.genersoft.iot.vmp.storager.dao.PlatformCatalogMapper;
import com.genersoft.iot.vmp.storager.dao.PlatformChannelMapper;
import com.genersoft.iot.vmp.vmanager.gb28181.platform.bean.ChannelReduce;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.util.ObjectUtils;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @author lin
 */
@Service
public class PlatformChannelServiceImpl implements IPlatformChannelService {
 
    private final static Logger logger = LoggerFactory.getLogger(PlatformChannelServiceImpl.class);
 
    @Autowired
    private PlatformChannelMapper platformChannelMapper;
 
    @Autowired
    TransactionDefinition transactionDefinition;
 
    @Autowired
    DataSourceTransactionManager dataSourceTransactionManager;
 
    @Autowired
    private SubscribeHolder subscribeHolder;
 
 
    @Autowired
    private DeviceChannelMapper deviceChannelMapper;
 
    @Autowired
    private PlatformCatalogMapper catalogManager;
 
    @Autowired
    private ParentPlatformMapper platformMapper;
 
    @Autowired
    EventPublisher eventPublisher;
 
    @Override
    public int updateChannelForGB(String platformId, List<ChannelReduce> channelReduces, String catalogId) {
        ParentPlatform platform = platformMapper.getParentPlatByServerGBId(platformId);
        if (platform == null) {
            logger.warn("更新级联通道信息时未找到平台{}的信息", platformId);
            return 0;
        }
        Map<Integer, ChannelReduce> deviceAndChannels = new HashMap<>();
        for (ChannelReduce channelReduce : channelReduces) {
            channelReduce.setCatalogId(catalogId);
            deviceAndChannels.put(channelReduce.getId(), channelReduce);
        }
        List<Integer> deviceAndChannelList = new ArrayList<>(deviceAndChannels.keySet());
        // 查询当前已经存在的
        List<Integer> channelIds = platformChannelMapper.findChannelRelatedPlatform(platformId, channelReduces);
        if (deviceAndChannelList != null) {
            deviceAndChannelList.removeAll(channelIds);
        }
        for (Integer channelId : channelIds) {
            deviceAndChannels.remove(channelId);
        }
        List<ChannelReduce> channelReducesToAdd = new ArrayList<>(deviceAndChannels.values());
        // 对剩下的数据进行存储
        int allCount = 0;
        boolean result = false;
        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
        int limitCount = 300;
        if (channelReducesToAdd.size() > 0) {
            if (channelReducesToAdd.size() > limitCount) {
                for (int i = 0; i < channelReducesToAdd.size(); i += limitCount) {
                    int toIndex = i + limitCount;
                    if (i + limitCount > channelReducesToAdd.size()) {
                        toIndex = channelReducesToAdd.size();
                    }
                    int count = platformChannelMapper.addChannels(platformId, channelReducesToAdd.subList(i, toIndex));
                    result = result || count < 0;
                    allCount += count;
                    logger.info("[关联通道]国标通道 平台:{}, 共需关联通道数:{}, 已关联:{}", platformId, channelReducesToAdd.size(), toIndex);
                }
            }else {
                allCount = platformChannelMapper.addChannels(platformId, channelReducesToAdd);
                result = result || allCount < 0;
                logger.info("[关联通道]国标通道 平台:{}, 关联通道数:{}", platformId, channelReducesToAdd.size());
            }
 
            if (result) {
                //事务回滚
                dataSourceTransactionManager.rollback(transactionStatus);
                allCount = 0;
            }else {
                logger.info("[关联通道]国标通道 平台:{}, 正在存入数据库", platformId);
                dataSourceTransactionManager.commit(transactionStatus);
 
            }
            SubscribeInfo catalogSubscribe = subscribeHolder.getCatalogSubscribe(platformId);
            if (catalogSubscribe != null) {
                List<DeviceChannel> deviceChannelList = getDeviceChannelListByChannelReduceList(channelReducesToAdd, catalogId, platform);
                if (deviceChannelList != null) {
                    eventPublisher.catalogEventPublish(platformId, deviceChannelList, CatalogEvent.ADD);
                }
            }
            logger.info("[关联通道]国标通道 平台:{}, 存入数据库成功", platformId);
        }
        return allCount;
    }
 
    private List<DeviceChannel> getDeviceChannelListByChannelReduceList(List<ChannelReduce> channelReduces, String catalogId, ParentPlatform platform) {
        List<DeviceChannel> deviceChannelList = new ArrayList<>();
        if (channelReduces.size() > 0){
            PlatformCatalog catalog = catalogManager.select(catalogId);
            if (catalog == null && !catalogId.equals(platform.getDeviceGBId())) {
                logger.warn("未查询到目录{}的信息", catalogId);
                return null;
            }
            for (ChannelReduce channelReduce : channelReduces) {
                DeviceChannel deviceChannel = deviceChannelMapper.queryChannel(channelReduce.getDeviceId(), channelReduce.getChannelId());
                deviceChannel.setParental(0);
                deviceChannelList.add(deviceChannel);
                if (platform.getTreeType().equals(TreeType.CIVIL_CODE)){
                    deviceChannel.setCivilCode(catalogId);
                }else if (platform.getTreeType().equals(TreeType.BUSINESS_GROUP)){
                    deviceChannel.setParentId(catalogId);
                    if (catalog != null) {
                        deviceChannel.setBusinessGroupId(catalog.getBusinessGroupId());
                    }
                }
            }
        }
        return deviceChannelList;
    }
 
    @Override
    public int delAllChannelForGB(String platformId, String catalogId) {
 
        int result;
        if (platformId == null) {
            return 0;
        }
        ParentPlatform platform = platformMapper.getParentPlatByServerGBId(platformId);
        if (platform == null) {
            return 0;
        }
        if (ObjectUtils.isEmpty(catalogId)) {
           catalogId = platform.getDeviceGBId();
        }
 
        if ((result = platformChannelMapper.delChannelForGBByCatalogId(platformId, catalogId)) > 0) {
            List<DeviceChannel> deviceChannels = platformChannelMapper.queryAllChannelInCatalog(platformId, catalogId);
            eventPublisher.catalogEventPublish(platformId, deviceChannels, CatalogEvent.DEL);
        }
        return result;
    }
}