liukangdong
2024-10-12 bf79a36667124868583fcbf47af0e709e088b5e9
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
package com.doumee.service.business.impl.hksync;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.haikang.model.HKConstants;
import com.doumee.core.haikang.model.param.BaseListPageResponse;
import com.doumee.core.haikang.model.param.BaseResponse;
import com.doumee.core.haikang.model.param.request.*;
import com.doumee.core.haikang.model.param.respose.*;
import com.doumee.core.haikang.service.HKService;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.DateUtil;
import com.doumee.dao.business.PlatformDeviceMapper;
import com.doumee.dao.business.PlatformJobMapper;
import com.doumee.dao.business.PlatformMapper;
import com.doumee.dao.business.model.Device;
import com.doumee.dao.business.model.Platform;
import com.doumee.dao.business.model.PlatformDevice;
import com.doumee.dao.business.model.PlatformJob;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * 设备信息表Service实现
 * @author 江蹄蹄
 * @date 2023/11/30 15:33
 */
@Service
public class HkSyncPlatformsServiceImpl extends HkSyncBaseServiceImpl {
    @Autowired
    private PlatformMapper platformMapper;
    @Autowired
    private PlatformJobMapper platformJobMapper;
    @Autowired
    private PlatformDeviceMapper platformDeviceMapper;
 
    /**
     * 同步海康月台数据
     * @param param
     * @return
     */
    @Override
    @Transactional
    public String syncPlatforms(PlatformsListRequest param){
        if(Constants.DEALING_HK_SYNCPLATFORM){
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "同步任务正在执行哦,请稍后查看结果!") ;
        }
        Constants.DEALING_HK_SYNCPLATFORM =true;
        try {
            List<Platform> deleteList = new ArrayList<>();
            List<Platform> addList = new ArrayList<>();
            List<Platform> editList = new ArrayList<>();
            List<PlatformDevice> deviceList = new ArrayList<>();
            Date date = new Date();
            //查询全部门禁设备数据
            List<Platform> allList = platformMapper.selectList(new QueryWrapper<Platform>().lambda().eq(Platform::getIsdeleted,Constants.ZERO));
            List<PlatformListInfoResponse> allHkList = getAllHkList(param);
            /**
             * 获取增删改数据集合
             */
            getDataChangeList(allList,allHkList,addList,deviceList,editList,deleteList,date);
            if(deleteList.size()>0){
                //逻辑删除
                List<Integer> platIds = new ArrayList<>();
                for(Platform d : deleteList){
                    platformMapper.updateById(d);
                    platIds.add(d.getId());
                }
                //更新删除的月台信息删正在执行的作业状态为【待签到】状态
                platformJobMapper.update(null,  new UpdateWrapper<PlatformJob>().lambda()
                        .eq(PlatformJob::getIsdeleted,Constants.ZERO)
                        .notIn(PlatformJob::getStatus,Constants.PlatformJobStatus.WART_SIGN_IN.getKey()
                                ,Constants.PlatformJobStatus.WAIT_CONFIRM.getKey()
                                ,Constants.PlatformJobStatus.DONE.getKey()
                                ,Constants.PlatformJobStatus.LEAVED.getKey()
                                ,Constants.PlatformJobStatus.CANCEL.getKey() )
                        .in(PlatformJob::getPlatformId,platIds)
                        .set(PlatformJob::getPlatformId,null)
                        .set(PlatformJob::getPlatforms,null)
                        .set(PlatformJob::getStatus,Constants.PlatformJobStatus.WART_SIGN_IN.getKey())
                );
            }
            if(deviceList.size()>0){
                platformDeviceMapper.insert(deviceList);
            }
            if(editList.size()>0){
                for(Platform d : editList){
                    platformMapper.updateById(d);
                }
            }
            return "同步数据:新增【"+addList.size()+"】条,更新【"+editList.size()+"】条,删除【"+deleteList.size()+"】条";
        }catch (Exception e){
            e.printStackTrace();
            throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "同步失败!");
        }finally {
            Constants.DEALING_HK_SYNCPLATFORM =false;
        }
    }
    @Override
//    @Async
    public String getPlatformStatus(PlatformStatusRequest param){
        if(Constants.DEALING_HK_SYNCPLATFORM_STATUS){
            return null;
        }
        Constants.DEALING_HK_SYNCPLATFORM_STATUS =true;
        try {
            List<Device> editList = new ArrayList<>();
            Date date = new Date();
            //查询全部门禁设备数据
            List<PlatformStatusInfoResponse> allHkList = getAllHKStatusList(param);
 
            if(allHkList.size()>0){
                //更新状态 (月台状态 0-无车 1-有车 2-超时停靠 3-错误停靠)
                for(PlatformStatusInfoResponse d : allHkList){
                    platformMapper.update(null,new UpdateWrapper<Platform>().lambda()
                            .set(Platform::getPlatformStatus,d.getStatus())
                            .set(Platform::getEditDate,date)
                            .eq(Platform::getHkId,d.getPlatformId()));
                }
            }
        }catch (Exception e){
            throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "同步设备状态失败!");
        }finally {
            Constants.DEALING_HK_SYNCPLATFORM_STATUS =false;
        }
        return "同步成功";
    }
 
    private List<PlatformListInfoResponse> getAllHkList(PlatformsListRequest param) {
        BaseResponse<BaseListPageResponse<PlatformListInfoResponse>> response = HKService.platformsList(param);
        if(response == null || !StringUtils.equals(response.getCode(), HKConstants.RESPONSE_SUCCEE) || response.getData() ==null){
            throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "对不起,海康同步数据失败~");
        }
 
        return response.getData().getList();
    }
 
    public  List<PlatformStatusInfoResponse>  getAllHKStatusList(PlatformStatusRequest param){
            //分页遍历循环查询所有门禁设备数据
            BaseResponse<BaseListPageResponse<PlatformStatusInfoResponse>> response = HKService.platformStatus(param);
            if(response == null || !StringUtils.equals(response.getCode(), HKConstants.RESPONSE_SUCCEE) || response.getData() ==null){
                throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "对不起,海康同步数据失败~");
            }
          return  response.getData().getList();
 
    }
 
 
    private AcsDeviceInfoResponse getDeviceByDoorid(String indexCode, List<AcsDeviceInfoResponse> allHkList ) {
        if(allHkList!=null && allHkList.size()>0){
            for(AcsDeviceInfoResponse info : allHkList){
                if(StringUtils.equals(indexCode,info.getIndexCode())){
                 return info;
                }
            }
        }
 
        return new AcsDeviceInfoResponse();
    }
    private void getDataChangeList(List<Platform> allList,
                                   List<PlatformListInfoResponse> allHkList,
                                   List<Platform> addList ,
                                   List<PlatformDevice> deviceList,
                                   List<Platform> editList,
                                   List<Platform> deleteList, Date date) {
        if(allHkList!=null && allHkList.size()>0){
            //获取海康全部门禁组数据
            for(PlatformListInfoResponse device : allHkList){
                Platform model = getExistedDevice(device,allList);
                if(model !=null){
                    //如果已存在,则更新数据
                    model =  initDataByHkData(model,device,date);
                    editList.add(model);
                    //清空监控点数据
                    platformDeviceMapper.delete(new UpdateWrapper<PlatformDevice>().lambda()
                            .eq(PlatformDevice::getPlatformId,model.getId())
                            .eq(PlatformDevice::getType,Constants.ONE)
                    );
                    deviceList.addAll(getCameraList(device.getCameras(),model));
                }else{
                    //如果不存在,则新增数据
                    model = new Platform();
                    model =  initDataByHkData(model,device,date);
                    platformMapper.insert(model);
                    addList.add(model);
                    deviceList.addAll(getCameraList(device.getCameras(),model));
                }
            }
        }
        //判断获取删除的门禁设备,逻辑删除
        getDeleteList(allList,allHkList,deleteList,date);
    }
 
    private List<PlatformDevice> getCameraList(List<PlatformCameraInfoResponse> cameras, Platform model) {
        List<PlatformDevice> list = new ArrayList<>();
        if(cameras!=null && cameras.size()>0){
            for(PlatformCameraInfoResponse param :cameras){
                PlatformDevice d = new PlatformDevice();
                d.setCreateDate(model.getCreateDate());
                d.setPlatformId(model.getId());
                d.setIsdeleted(Constants.ZERO);
                d.setDeviceId(param.getCameraId());
                d.setName(param.getCameraName());
                d.setType(Constants.ONE);
                list.add(d);
            }
        }
        return list;
    }
 
    /**
     * 初始化海康入库数据
     * @param model
     * @param device
     * @param date
     * @return
     */
    private Platform initDataByHkData(Platform model, PlatformListInfoResponse device,Date date ) {
        model.setIsdeleted(Constants.ZERO);
        model.setCreateDate(date);
        model.setHkDate(date);
        model.setHkId(device.getPlatformId());
        model.setCreateDate(DateUtil.getISO8601DateByStr(device.getCreateTime()));
        model.setName(device.getPlatformName());
        model.setCompanys(device.getCompanyNames());
        model.setAngle(device.getAngle());
        model.setXpos(device.getX());
        model.setYpos(device.getY());
        model.setWidth(device.getWidth());
        model.setHeight(device.getHeight());
        model.setRemark(JSONObject.toJSONString(device));
        return  model;
    }
 
    private Platform getExistedDevice(PlatformListInfoResponse device, List<Platform> allList) {
        if(allList.size()>0){
            for(Platform r : allList){
                if(StringUtils.equals(r.getHkId(), device.getPlatformId())){
                    //表示未删除
                    return  r;
                }
            }
        }
        return  null;
    }
 
    private void getDeleteList(List<Platform> allList, List<PlatformListInfoResponse> allHkList,List<Platform> deleteList ,Date date) {
        if(allList!=null && allList.size()>0){
            for(Platform device : allList){
                if(isDeletedData(device,allHkList)){
                    device.setIsdeleted(Constants.ONE);
                    device.setEditDate(date);
                    deleteList.add(device);
                }
            }
        }
    }
    private boolean isDeletedData(Platform device, List<PlatformListInfoResponse> allHkList) {
        if(allHkList.size()>0){
            for(PlatformListInfoResponse r : allHkList){
                if(StringUtils.equals(device.getHkId(), r.getPlatformId())){
                    //表示未删除
                    return  false;
                }
            }
        }
        return  true;
    }
 
}