123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- package com.fdkankan.ucenter.service.impl;
- import cn.hutool.core.bean.BeanUtil;
- import cn.hutool.core.io.FileUtil;
- import cn.hutool.core.thread.ThreadUtil;
- import cn.hutool.core.util.StrUtil;
- import cn.hutool.extra.qrcode.QrCodeUtil;
- import cn.hutool.extra.qrcode.QrConfig;
- import cn.hutool.http.HttpUtil;
- import com.alibaba.fastjson.JSON;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.fdkankan.common.constant.CommonStatus;
- import com.fdkankan.common.constant.ErrorCode;
- import com.fdkankan.common.constant.SceneVersionType;
- import com.fdkankan.common.util.FileUtils;
- import com.fdkankan.fyun.face.FYunFileServiceInterface;
- import com.fdkankan.image.MatrixToImageWriterUtil;
- import com.fdkankan.model.utils.CreateObjUtil;
- import com.fdkankan.ucenter.common.OssPath;
- import com.fdkankan.ucenter.common.PageInfo;
- import com.fdkankan.ucenter.common.Result;
- import com.fdkankan.ucenter.common.constants.ConstantFilePath;
- import com.fdkankan.ucenter.common.constants.NacosProperty;
- import com.fdkankan.ucenter.common.constants.UploadFilePath;
- import com.fdkankan.redis.constant.RedisKey;
- import com.fdkankan.redis.util.RedisUtil;
- import com.fdkankan.ucenter.bean.SceneJsonBean;
- import com.fdkankan.ucenter.entity.*;
- import com.fdkankan.ucenter.httpClient.service.LaserService;
- import com.fdkankan.ucenter.mapper.IScenePlusMapper;
- import com.fdkankan.ucenter.service.*;
- import java.io.File;
- import java.nio.charset.StandardCharsets;
- import java.util.*;
- import java.util.concurrent.CompletableFuture;
- import java.util.concurrent.ExecutorService;
- import java.util.stream.Collectors;
- import com.fdkankan.ucenter.util.RoamingPointUtil;
- import com.fdkankan.ucenter.vo.ResponseScene;
- import com.fdkankan.ucenter.vo.SceneEditControlsVO;
- import com.fdkankan.ucenter.vo.request.RequestScene;
- import com.fdkankan.ucenter.vo.response.SceneVo;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.ObjectUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- /**
- * <p>
- * 场景主表 服务实现类
- * </p>
- *
- * @author
- * @since 2022-07-04
- */
- @Service
- @Slf4j
- public class ScenePlusServiceImpl extends ServiceImpl<IScenePlusMapper, ScenePlus> implements IScenePlusService {
- @Value("${fyun.host}")
- private String fyunHost;
- @Autowired
- private IScenePlusExtService scenePlusExtService;
- @Autowired
- private ISceneEditInfoService sceneEditInfoService;
- @Autowired
- private ISceneEditInfoExtService sceneEditInfoExtService;
- @Autowired
- private ISceneEditControlsService sceneEditControlsService;
- @Autowired
- private ICameraDetailService cameraDetailService;
- @Autowired
- private ISurveillanceService surveillanceService;
- @Autowired
- private ISceneProService sceneProService;
- @Autowired
- FYunFileServiceInterface fYunFileServiceInterface;
- @Autowired
- RedisUtil redisUtil;
- @Autowired
- LaserService laserService;
- @Autowired
- ISceneCopyLogService sceneCopyLogService;
- @Autowired
- private IUserService userService;
- @Autowired
- private ICompanyService companyService;
- @Override
- public Long getCountByUserId(Long userId, List<?> sceneSourceList) {
- LambdaQueryWrapper<ScenePlus> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(ScenePlus::getUserId, userId);
- wrapper.in(ScenePlus::getSceneSource, sceneSourceList);
- return this.count(wrapper);
- }
- @Override
- public Long getCountByUserId(Long userId, Integer cameraType) {
- return this.getBaseMapper().getCountByUserId(userId,cameraType);
- }
- @Override
- public List<ScenePlus> getListByCameraId(Long cameraId) {
- LambdaQueryWrapper<ScenePlus> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(ScenePlus::getCameraId,cameraId);
- return this.list(wrapper);
- }
- @Override
- public void bindOrUnCamera(List<Long> cameraIds, Long userId) {
- if(cameraIds.size() >0){
- LambdaUpdateWrapper<ScenePlus> wrapper = new LambdaUpdateWrapper<>();
- wrapper.set(ScenePlus::getUserId,userId)
- .in(ScenePlus::getCameraId,cameraIds);
- this.update(wrapper);
- }
- }
- @Override
- public List<ScenePlus> getListByCameraIds(List<Long> cameraIds) {
- if(cameraIds.size() >0){
- LambdaQueryWrapper<ScenePlus> wrapper = new LambdaQueryWrapper<>();
- wrapper.in(ScenePlus::getCameraId,cameraIds);
- return this.list(wrapper);
- }
- return new ArrayList<>();
- }
- @Override
- public List<ScenePlus> getListByNums(List<String> numList) {
- LambdaQueryWrapper<ScenePlus> wrapper = new LambdaQueryWrapper<>();
- wrapper.in(ScenePlus::getNum,numList);
- return this.list(wrapper);
- }
- @Override
- public HashMap<Long, ScenePlus> getMapByIds(List<Long> sceneIds) {
- LambdaQueryWrapper<ScenePlus> wrapper = new LambdaQueryWrapper<>();
- wrapper.in(ScenePlus::getId,sceneIds);
- List<ScenePlus> list = this.list(wrapper);
- HashMap<Long,ScenePlus> map = new HashMap<>();
- list.forEach(entity -> map.put(entity.getId(),entity));
- return map;
- }
- @Override
- public ScenePlus getByNum(String sceneNum) {
- LambdaQueryWrapper<ScenePlus> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(ScenePlus::getNum,sceneNum);
- List<ScenePlus> list = this.list(wrapper);
- if(list!=null && list.size() >0){
- return list.get(0);
- }
- return null;
- }
- @Override
- public List<ScenePlus> getByIds(List<Long> plusIds) {
- LambdaQueryWrapper<ScenePlus> wrapper = new LambdaQueryWrapper<>();
- wrapper.in(ScenePlus::getId,plusIds);
- return this.list(wrapper);
- }
- @Override
- public Long copyV4Scene(ScenePlus scenePlus,String newNum ,CameraDetail cameraDetail,String time) throws Exception {
- String num = scenePlus.getNum();
- Long plusId = scenePlus.getId();
- ScenePlusExt plusExt = scenePlusExtService.getByPlusId(plusId);
- // 拷贝数据
- scenePlus.setNum(newNum);
- scenePlus.setId(null);
- scenePlus.setSceneStatus(0);
- this.save(scenePlus);
- ExecutorService executor = ThreadUtil.newSingleExecutor();
- try {
- CompletableFuture.runAsync(() -> {
- try {
- // 拷贝场景编辑资源
- String oldEditPath = String.format(UploadFilePath.EDIT_PATH, num);
- String newEditPath = String.format(UploadFilePath.EDIT_PATH, newNum);
- fYunFileServiceInterface.copyFileInBucket(oldEditPath, newEditPath);
- // 拷贝场景展示资源
- String oldViewPath = String.format(UploadFilePath.VIEW_PATH, num);
- String newViewPath = String.format(UploadFilePath.VIEW_PATH, newNum);
- fYunFileServiceInterface.copyFileInBucket(oldViewPath, newViewPath);
- //复制计算结果文件
- String oldResultPath = String.format(UploadFilePath.SCENE_RESULT_DATA_PATH, num);
- String newResultPath = String.format(UploadFilePath.SCENE_RESULT_DATA_PATH, newNum);
- fYunFileServiceInterface.copyFileInBucket(oldResultPath, newResultPath);
- // 拷贝本地资源
- String oldPath = String.format("/mnt/4Dkankan/scene/%s/caches/images", num);
- String newPath = String.format("/mnt/4Dkankan/scene/%s/caches/images", newNum);
- if(new File(oldPath).exists()){
- FileUtils.copyDirectiory(oldPath, newPath);
- }
- String scenePath = ConstantFilePath.SCENE_V4_PATH + num;
- File file = new File(scenePath);
- if(file.exists()){
- String newScenePath = ConstantFilePath.SCENE_V4_PATH + newNum;
- FileUtils.copyDirectiory(scenePath, newScenePath);
- }
- String newVideos = plusExt.getVideos();
- if(StrUtil.isNotEmpty(newVideos)){
- newVideos = plusExt.getVideos().replaceAll("/data/data" + num, "/scene_view_data/" + newNum + "/data").replaceAll(num, newNum);
- }
- String oldDataSource = plusExt.getDataSource();
- String newDataSource = sceneProService.setDataSource(plusExt.getDataSource(),time);
- String buildModelPath = ConstantFilePath.BUILD_MODEL_PATH;
- if(scenePlus.getSceneSource() == 4 || scenePlus.getSceneSource() == 5){
- buildModelPath = ConstantFilePath.BUILD_MODEL_LASER_PATH;
- }
- sceneProService.copyFdage(oldDataSource,newDataSource,buildModelPath,time);
- plusExt.setId(null);
- plusExt.setPlusId(scenePlus.getId());
- plusExt.setDataSource(newDataSource);
- plusExt.setWebSite(plusExt.getWebSite().replace(num, newNum));
- plusExt.setThumb(plusExt.getThumb().replace(num, newNum));
- plusExt.setVideos(newVideos);
- plusExt.setViewCount(0);
- scenePlusExtService.save(plusExt);
- SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(plusId);
- Long sceneEditInfoId = sceneEditInfo.getId();
- sceneEditInfo.setId(null);
- sceneEditInfo.setScenePlusId(scenePlus.getId());
- sceneEditInfo.setSceneProId(null);
- sceneEditInfo.setTitle(scenePlus.getTitle());
- sceneEditInfoService.save(sceneEditInfo);
- SceneEditInfoExt sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfoId);
- sceneEditInfoExt.setId(null);
- sceneEditInfoExt.setEditInfoId(sceneEditInfo.getId());
- sceneEditInfoExt.setScenePlusId(scenePlus.getId());
- sceneEditInfoExt.setSceneProId(null);
- sceneEditInfoExtService.save(sceneEditInfoExt);
- SceneEditControls sceneEditControls = sceneEditControlsService.getBySceneEditId(sceneEditInfoId);
- sceneEditControls.setId(null);
- sceneEditControls.setEditInfoId(sceneEditInfo.getId());
- sceneEditControlsService.save(sceneEditControls);
- if(scenePlus.getSceneSource() == 4 || scenePlus.getSceneSource() == 5){ //深时复制
- laserService.copy(num,scenePlus.getCameraId(),scenePlus.getCreateTime(),newNum,0,null,
- sceneEditInfo.getScenePassword(),scenePlus.getTitle(),scenePlus.getUserId(),"V4",plusExt.getIsObj(),scenePlus.getSceneSource());
- }
- List<Surveillance> list = surveillanceService.list(new LambdaQueryWrapper<Surveillance>().eq(Surveillance::getNum, num));
- if (!Objects.isNull(list)) {
- list.stream().forEach(item -> {
- item.setNum(newNum);
- item.setId(null);
- surveillanceService.save(item);
- });
- }
- //复制马赛克数据
- String key = String.format(RedisKey.SCENE_MOSAIC_DATA, num);
- Map<String, String> map = redisUtil.hmget(key);
- redisUtil.hmset(String.format(RedisKey.SCENE_MOSAIC_DATA, newNum), map);
- // 生成scene.json
- SceneJsonBean sceneJson = new SceneJsonBean();
- BeanUtil.copyProperties(sceneEditInfoExt, sceneJson);
- BeanUtil.copyProperties(sceneEditInfo, sceneJson);
- SceneEditControlsVO sceneEditControlsVO = BeanUtil.copyProperties(sceneEditControls, SceneEditControlsVO.class);
- sceneJson.setControls(sceneEditControlsVO);
- sceneJson.setNum(newNum);
- sceneJson.setCreateTime(scenePlus.getCreateTime());
- sceneJson.setSceneResolution(plusExt.getSceneResolution());
- sceneJson.setSceneFrom(plusExt.getSceneFrom());
- sceneJson.setSceneKind(plusExt.getSceneKind());
- sceneJson.setModelKind(plusExt.getModelKind());
- if(StrUtil.isNotEmpty(plusExt.getVideos())){
- sceneJson.setVideos(plusExt.getVideos());
- }
- sceneJson.setMosaicList(sceneEditInfoService.getMosaicList(num));
- log.info("开始生成本地json文件……");
- String sceneJsonLocalPath = ConstantFilePath.SCENE_PATH + "data" + File.separator + "data" + newNum + File.separator + "scene.json";
- FileUtils.writeFile(sceneJsonLocalPath, JSON.toJSONString(sceneJson));
- String sceneJsonPath = String.format(UploadFilePath.DATA_VIEW_PATH+"scene.json", newNum);
- fYunFileServiceInterface.uploadFile(JSON.toJSONBytes(sceneJson), sceneJsonPath);
- //删除scenejson缓存
- redisUtil.del(String.format(RedisKey.SCENE_JSON, num));
- // 生成二维码
- this.createQrCode(newNum, scenePlus.getCameraId(), plusExt.getWebSite(), sceneEditInfoExt.getShareLogoImg(), SceneVersionType.V4.code());
- scenePlus.setSceneStatus(-2);
- this.updateById(scenePlus);
- cameraDetail.setUsedSpace(cameraDetail.getUsedSpace() + plusExt.getSpace());
- cameraDetailService.updateById(cameraDetail);
- if(scenePlus.getPayStatus() == -2){
- sceneProService.updateOssStatus(String.format(OssPath.v4_statusPath,scenePlus.getNum()),-2);
- }
- if(scenePlus.getSceneSource() == 4 || scenePlus.getSceneSource() == 5){ //深时复制
- String laserPath = laserService.copyDataSource(oldDataSource,plusExt.getDataSource());
- laserService.copy(num,scenePlus.getCameraId(),scenePlus.getCreateTime(),newNum,2,laserPath,
- sceneEditInfo.getScenePassword(),scenePlus.getTitle(),scenePlus.getUserId(),"V4",plusExt.getIsObj(),
- scenePlus.getSceneSource());
- } else if(!"aws".equals(NacosProperty.uploadType)){
- laserService.cloudPointBuild(num,newNum);
- }
- sceneCopyLogService.saveByNum(num,newNum,scenePlus.getUserId());
- }catch (Exception e){
- this.removeById(scenePlus.getId());
- log.error("复制场景异常", e);
- }
- }, executor).whenComplete((reslut, e) -> {
- log.info("复制场景oldNum:{},newNum:{}结束-{}",num,newNum, new Date());
- });
- }catch (Exception e){
- log.error("线程错误:{}",e);
- }finally {
- executor.shutdown();
- }
- return scenePlus.getId();
- }
- public static void main(String[] args) {
- String ext = FileUtil.extName("downloads/scene/KK-gz55kk3T/QRcode/shareLogo.png");
- System.out.println(ext);
- }
- public void createQrCode(String num, Long cameraId, String website, String shareLogoImg, String sceneVersion) {
- String localLogoPath = null;
- if(StrUtil.isNotEmpty(shareLogoImg)){
- if(sceneVersion.equals(SceneVersionType.V3.code())){
- localLogoPath = ConstantFilePath.SCENE_PATH + shareLogoImg;
- }else{
- localLogoPath = ConstantFilePath.SCENE_PATH + "qrLogo/" + UUID.randomUUID().toString() + "." + FileUtil.extName(shareLogoImg);
- fYunFileServiceInterface.downloadFile(shareLogoImg, localLogoPath);
- }
- if(!FileUtil.exist(localLogoPath)){
- localLogoPath = null;
- }
- }
- if(StrUtil.isEmpty(localLogoPath)){
- CameraDetail cameraDetail = null;
- Company company = null;
- if(Objects.nonNull(cameraId)){
- cameraDetail = cameraDetailService.getByCameraId(cameraId);
- if(Objects.nonNull(cameraDetail)){
- company = !ObjectUtils.isEmpty(cameraDetail.getCompanyId()) ? companyService.getById(cameraDetail.getCompanyId()) : null;
- }
- }
- shareLogoImg = !ObjectUtils.isEmpty(company) && !ObjectUtils.isEmpty(company.getQrLogo()) ? company.getQrLogo() : null;
- if (StrUtil.isNotEmpty(shareLogoImg)) {
- try {
- localLogoPath = ConstantFilePath.SCENE_PATH + "qrLogo/" + shareLogoImg.substring(shareLogoImg.lastIndexOf("//") + 1);
- HttpUtil.downloadFile(shareLogoImg, localLogoPath);
- } catch (Exception e) {
- log.error("logo下载失败:{}", shareLogoImg);
- localLogoPath = null;
- }
- }
- }
- //生成二维码
- String outPathZh = com.fdkankan.model.constants.ConstantFilePath.BASE_PATH + File.separator + "sceneQRcode/"+ num +".png";
- String outPathEn = com.fdkankan.model.constants.ConstantFilePath.BASE_PATH + File.separator + "sceneQRcode/"+ num +"_en.png";
- QrConfig qrConfig = QrConfig.create();
- qrConfig.setWidth(512);
- qrConfig.setHeight(512);
- if(!ObjectUtils.isEmpty(localLogoPath)){
- qrConfig.setImg(localLogoPath);
- }
- QrCodeUtil.generate(website, qrConfig, FileUtil.file(outPathZh));
- QrCodeUtil.generate(website + "&lang=en", qrConfig, FileUtil.file(outPathEn));
- //上传二维码
- if(sceneVersion.equals(SceneVersionType.V4.code())){
- fYunFileServiceInterface.uploadFile(outPathZh, String.format(com.fdkankan.model.constants.UploadFilePath.DOWNLOADS_QRCODE, num) + num + ".png");
- fYunFileServiceInterface.uploadFile(outPathEn, String.format(com.fdkankan.model.constants.UploadFilePath.DOWNLOADS_QRCODE, num) + num + "_en.png");
- }
- if(!ObjectUtils.isEmpty(localLogoPath)){
- FileUtils.deleteFile(localLogoPath);
- }
- }
- @Override
- public Result pageSceneWithHouseType(RequestScene param, String token) {
- User ssoUser = userService.getByToken(token);
- Page<ScenePlus> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()),
- new LambdaQueryWrapper<ScenePlus>().eq(ScenePlus::getUserId, ssoUser.getId()).eq(ScenePlus::getHouseType, CommonStatus.YES.code().intValue()));
- List<SceneVo> records = null;
- if(page.getRecords().size() > 0){
- records = page.getRecords().parallelStream().map(plus -> {
- return SceneVo.builder().createTime(plus.getCreateTime())
- .id(plus.getId()).roamingPointUrl(this.createRoamingPoint(plus.getNum())).sceneName(plus.getTitle())
- .num(plus.getNum()).updateTime(plus.getUpdateTime()).userId(plus.getUserId()).build();
- }).collect(Collectors.toList());
- }
- PageInfo pageInfo = PageInfo.PageInfo(page);
- pageInfo.setList(records);
- return Result.success(pageInfo);
- }
- @Override
- public Result getArPathByNum(String num) {
- ScenePro scenePro = sceneProService.getByNum(num);
- if(Objects.nonNull(scenePro)){
- return Result.success("成功", String.format("images/images%s/ar", num));
- }
- ScenePlus scenePlus = this.getByNum(num);
- if(Objects.nonNull(scenePlus)){
- return Result.success("成功", String.format(UploadFilePath.IMG_VIEW_PATH, num) + "ar");
- }
- return Result.failure(ErrorCode.FAILURE_CODE_5005.code(), ErrorCode.FAILURE_CODE_5005.message());
- }
- private String createRoamingPoint(String num){
- String roamingPointOssPath = String.format(UploadFilePath.DATA_VIEW_PATH, num) + "roamingPoint.json";
- String url = null;
- try {
- if(!fYunFileServiceInterface.fileExist(roamingPointOssPath)){
- String visionModeldataPath = ConstantFilePath.SCENE_V4_PATH + num + File.separator + "vision.modeldata";
- String visionModeldataOssPath = String.format(UploadFilePath.IMG_VIEW_PATH, num) + "vision.modeldata";
- fYunFileServiceInterface.downloadFile(visionModeldataOssPath, visionModeldataPath);
- String visionTxtLocalPath = ConstantFilePath.SCENE_V4_PATH + num + File.separator + "vision.txt";
- CreateObjUtil.convertVisionmodeldataToTxt(visionModeldataPath, visionTxtLocalPath);
- String roamingPoint = RoamingPointUtil.createRoamingPoint(num, visionTxtLocalPath);
- if (StrUtil.isNotEmpty(roamingPoint)) {
- fYunFileServiceInterface.uploadFile(roamingPoint.getBytes(StandardCharsets.UTF_8), roamingPointOssPath);
- url = fyunHost + roamingPointOssPath;
- }
- }else{
- url = fyunHost + roamingPointOssPath;
- }
- }catch (Exception e){
- log.warn("生成roamingPointJson出错,num:" + num, e);
- }
- return url;
- }
- }
|