|
@@ -0,0 +1,269 @@
|
|
|
+package com.fdkankan.external.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.date.TimeInterval;
|
|
|
+import cn.hutool.core.thread.ExecutorBuilder;
|
|
|
+import cn.hutool.core.thread.ThreadUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.fdkankan.common.constant.CommonStatus;
|
|
|
+import com.fdkankan.common.constant.CommonSuccessStatus;
|
|
|
+import com.fdkankan.common.constant.SceneSource;
|
|
|
+import com.fdkankan.external.callback.ErrorCallback;
|
|
|
+import com.fdkankan.external.callback.SuccessCallback;
|
|
|
+import com.fdkankan.external.entity.*;
|
|
|
+import com.fdkankan.external.httpclient.HttpClient;
|
|
|
+import com.fdkankan.external.service.*;
|
|
|
+import com.fdkankan.web.response.ResultData;
|
|
|
+import com.mybatisflex.annotation.UseDataSource;
|
|
|
+import com.mybatisflex.core.query.QueryWrapper;
|
|
|
+import com.mybatisflex.spring.service.impl.ServiceImpl;
|
|
|
+import com.fdkankan.external.mapper.SceneOfflinePackagePushMapper;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.math.BigInteger;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.Callable;
|
|
|
+import java.util.concurrent.ThreadPoolExecutor;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 场景离线包推送表 服务层实现。
|
|
|
+ *
|
|
|
+ * @author dsx
|
|
|
+ * @since 2023-12-07
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class SceneOfflinePackagePushServiceImpl extends ServiceImpl<SceneOfflinePackagePushMapper, SceneOfflinePackagePush> implements ISceneOfflinePackagePushService {
|
|
|
+
|
|
|
+ @Value("${host.4dkk.manage}")
|
|
|
+ private String fdkkManageHost;
|
|
|
+ @Value("${host.4dkk.scene}")
|
|
|
+ private String fdkkSceneHost;
|
|
|
+
|
|
|
+ @Value("${api.4dkk.manage.checkDownLoad}")
|
|
|
+ private String checkDownLoadUrl;
|
|
|
+ @Value("${api.4dkk.manage.downScene}")
|
|
|
+ private String downSceneUrl;
|
|
|
+ @Value("${api.4dkk.manage.downloadProcess}")
|
|
|
+ private String downloadProcessUrl;
|
|
|
+ @Value("${api.4dkk.scene.getInfo}")
|
|
|
+ private String getInfoUrl;
|
|
|
+
|
|
|
+ private final static ThreadPoolExecutor threadPoolExecutor = ExecutorBuilder.create().setCorePoolSize(1).setMaxPoolSize(3).build();
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IDepartmentService departmentService;
|
|
|
+ @Autowired
|
|
|
+ private IDepartmentCameraService departmentCameraService;
|
|
|
+ @Autowired
|
|
|
+ private IScenePlusService scenePlusService;
|
|
|
+ @Autowired
|
|
|
+ private ISsoService ssoService;
|
|
|
+ @Autowired
|
|
|
+ private IScenePlusExtService scenePlusExtService;
|
|
|
+ @Autowired
|
|
|
+ private ISceneOfflinePackagePushService sceneOfflinePackagePushService;
|
|
|
+ @Autowired
|
|
|
+ private ISceneService sceneService;
|
|
|
+ @Autowired
|
|
|
+ private HttpClient httpClient;
|
|
|
+ @Autowired
|
|
|
+ private ICameraService cameraService;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void scenePushScheduleHandler(String departmentCode) {
|
|
|
+
|
|
|
+ Department department = departmentService.getByCode(departmentCode);
|
|
|
+ if(Objects.isNull(department)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<DepartmentCamera> departmentCameraList = departmentCameraService.listByDepartmentId(department.getId());
|
|
|
+ if(CollUtil.isEmpty(departmentCameraList)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<String> snCodeList = departmentCameraList.stream().map(DepartmentCamera::getSnCode).collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<Camera> cameras = cameraService.listBySnCodeList(snCodeList);
|
|
|
+ if(CollUtil.isEmpty(cameras)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Map<Long, String> snCodeMap = cameras.stream().collect(Collectors.toMap(Camera::getId, Camera::getSnCode));
|
|
|
+ List<Long> cameraIdList = cameras.stream().map(Camera::getId).collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<ScenePlus> scenePlusList = scenePlusService.listByCameraIdList(cameraIdList);
|
|
|
+ if(CollUtil.isEmpty(scenePlusList)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //模拟管理后台登录
|
|
|
+ String token = ssoService.manageLogin(department.getManageUserName(), department.getManagePassword());
|
|
|
+ Map<String, Object> headers = new HashMap<>();
|
|
|
+ headers.put("token", token);
|
|
|
+
|
|
|
+ SceneOfflinePackagePush condition =
|
|
|
+ SceneOfflinePackagePush.builder()
|
|
|
+ .departmentId(department.getId())
|
|
|
+ .pushStatus(CommonSuccessStatus.SUCCESS.code())
|
|
|
+ .build();
|
|
|
+ for (ScenePlus scenePlus : scenePlusList) {
|
|
|
+ try {
|
|
|
+ condition.setNum(scenePlus.getNum());
|
|
|
+
|
|
|
+ SceneOfflinePackagePush commonPush =
|
|
|
+ SceneOfflinePackagePush.builder()
|
|
|
+ .departmentId(department.getId())
|
|
|
+ .destUrl(department.getDestUrl())
|
|
|
+ .storageType("oss")
|
|
|
+ .snCode(snCodeMap.get(scenePlus.getCameraId()))
|
|
|
+ .num(scenePlus.getNum()).build();
|
|
|
+ ScenePlusExt scenePlusExt = null;
|
|
|
+
|
|
|
+ //点云场景推送
|
|
|
+ int isObj = CommonStatus.NO.code();
|
|
|
+ if(scenePlus.getSceneSource() == SceneSource.JG.code() || scenePlus.getSceneSource() == SceneSource.SG.code()){
|
|
|
+ condition.setZipType("laser");
|
|
|
+ SceneOfflinePackagePush lastPush = sceneOfflinePackagePushService.getLastByCondition(condition);
|
|
|
+ Scene scene = sceneService.getBySceneCode(scenePlus.getNum());
|
|
|
+ if(Objects.isNull(scene)){
|
|
|
+ throw new RuntimeException("未查询到激光系统场景信息,场景码:" + scenePlus.getNum());
|
|
|
+ }
|
|
|
+ //如果没有推送过或者推送过但是版本号不一致,就需要推送
|
|
|
+ if(Objects.isNull(lastPush) || lastPush.getVersion() != scene.getOfflineVerForPush()){
|
|
|
+ threadPoolExecutor.submit(()->{
|
|
|
+ SceneOfflinePackagePush push = BeanUtil.copyProperties(commonPush, SceneOfflinePackagePush.class);
|
|
|
+ push.setZipType("laser");
|
|
|
+ push.setVersion(scene.getOfflineVerForPush());
|
|
|
+ try {
|
|
|
+ sceneOfflinePackagePushService.scenePushHandler(push, headers);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("场景推送失败,num:{}",scenePlus.getNum(), e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ scenePlusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
|
|
|
+ isObj = scenePlusExt.getIsObj();
|
|
|
+ }
|
|
|
+
|
|
|
+ //看看场景推送
|
|
|
+ if(scenePlus.getSceneSource() == SceneSource.BM.code()
|
|
|
+ || scenePlus.getSceneSource() == SceneSource.ZT.code()
|
|
|
+ || isObj == CommonStatus.YES.code()){
|
|
|
+
|
|
|
+ condition.setZipType("kankan");
|
|
|
+ SceneOfflinePackagePush lastPush = sceneOfflinePackagePushService.getLastByCondition(condition);
|
|
|
+
|
|
|
+ //查询版本号
|
|
|
+ String getInfo = fdkkSceneHost.concat(String.format(getInfoUrl, scenePlus.getNum()));
|
|
|
+ ResultData<Map<String, Object>> mapResultData = httpClient.get(getInfo, new HashMap<>(), new SuccessCallback(), new ErrorCallback());
|
|
|
+ int version = (int)mapResultData.getData().get("version");
|
|
|
+
|
|
|
+ //如果没有推送过或者推送过但是版本号不一致,就需要推送
|
|
|
+ if(Objects.isNull(lastPush) || lastPush.getVersion() != version){
|
|
|
+ threadPoolExecutor.submit(()->{
|
|
|
+ SceneOfflinePackagePush push = BeanUtil.copyProperties(commonPush, SceneOfflinePackagePush.class);
|
|
|
+ push.setZipType("kankan");
|
|
|
+ push.setVersion(version);
|
|
|
+ try {
|
|
|
+ sceneOfflinePackagePushService.scenePushHandler(push, headers);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("场景推送失败,num:{}",scenePlus.getNum(), e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("场景推送失败,num:{}",scenePlus.getNum(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public SceneOfflinePackagePush getLastByCondition(SceneOfflinePackagePush condition) {
|
|
|
+
|
|
|
+ QueryWrapper wrapper = new QueryWrapper();
|
|
|
+ wrapper.eq(SceneOfflinePackagePush::getDepartmentId, condition.getDepartmentId());
|
|
|
+ wrapper.eq(SceneOfflinePackagePush::getNum, condition.getNum());
|
|
|
+ wrapper.orderBy(SceneOfflinePackagePush::getId, false);
|
|
|
+ wrapper.limit(1);
|
|
|
+ if(StrUtil.isNotEmpty(condition.getZipType())){
|
|
|
+ wrapper.eq(SceneOfflinePackagePush::getZipType, condition.getZipType());
|
|
|
+ }
|
|
|
+ if(Objects.nonNull(condition.getPushStatus())){
|
|
|
+ wrapper.eq(SceneOfflinePackagePush::getPushStatus, condition.getPushStatus());
|
|
|
+ }
|
|
|
+
|
|
|
+ return this.getOne(wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void scenePushHandler(SceneOfflinePackagePush push, Map<String, Object> headers){
|
|
|
+ String num = push.getNum();
|
|
|
+ String zipType = push.getZipType();
|
|
|
+ String downloadUrl = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ //校验场景下载
|
|
|
+ String url = fdkkManageHost.concat(String.format(checkDownLoadUrl, num));
|
|
|
+ ResultData<Map<String, Object>> resultData = httpClient.get(url, headers, new SuccessCallback(), new ErrorCallback());
|
|
|
+ Map<String, Object> data = resultData.getData();
|
|
|
+ int downloadStatus = (int) data.get("downloadStatus");
|
|
|
+ if (downloadStatus == 3) {
|
|
|
+ downloadUrl = (String) data.get("downloadUrl");
|
|
|
+ } else {
|
|
|
+ if (downloadStatus == 0 || downloadStatus == 2) {
|
|
|
+ url = this.fdkkManageHost.concat(String.format(downSceneUrl, num));
|
|
|
+ httpClient.get(url, headers, new SuccessCallback(), new ErrorCallback());
|
|
|
+ }
|
|
|
+
|
|
|
+ //计时
|
|
|
+ TimeInterval timer = DateUtil.timer();
|
|
|
+ url = this.fdkkManageHost.concat(String.format(this.downloadProcessUrl, num));
|
|
|
+ boolean exit = false;
|
|
|
+ do {
|
|
|
+ ResultData<Map<String, Object>> mapResultData = httpClient.get(url, headers, new SuccessCallback(), new ErrorCallback());
|
|
|
+ data = mapResultData.getData();
|
|
|
+ int status = (int) data.get("status");
|
|
|
+ if (status == 1002) {
|
|
|
+ downloadUrl = (String) data.get("url");
|
|
|
+ exit = true;
|
|
|
+ }
|
|
|
+ if (status == 1003) {
|
|
|
+ exit = true;
|
|
|
+ log.error("下载失败,num:{}", num);
|
|
|
+ throw new RuntimeException("下载失败,num:" + num);
|
|
|
+ }
|
|
|
+ if (timer.intervalMinute() > 8 * 60) {
|
|
|
+ exit = true;
|
|
|
+ log.error("下载超时,num:{}", num);
|
|
|
+ throw new RuntimeException("下载超时,num:" + num);
|
|
|
+ }
|
|
|
+ } while (!exit);
|
|
|
+ }
|
|
|
+
|
|
|
+ //开始推送到第三方服务
|
|
|
+ if(StrUtil.isNotEmpty(downloadUrl)){
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("num", num);
|
|
|
+ params.put("zipType", zipType);
|
|
|
+ params.put("downloadUrl", downloadUrl);
|
|
|
+ ResultData pushRes = httpClient.post2(push.getDestUrl(), params, new SuccessCallback(), new ErrorCallback());
|
|
|
+ }
|
|
|
+ push.setPushStatus(CommonSuccessStatus.SUCCESS.code());
|
|
|
+ push.setZipPath(downloadUrl);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("场景推送失败,num:{}", num, e);
|
|
|
+ push.setPushStatus(CommonSuccessStatus.FAIL.code());
|
|
|
+ }
|
|
|
+
|
|
|
+ this.saveOrUpdate(push);
|
|
|
+ }
|
|
|
+}
|