package com.fdkankan.agent.service.impl;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
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.fdkankan.agent.common.OssPath;
import com.fdkankan.agent.common.PageInfo;
import com.fdkankan.agent.common.ResultCode;
import com.fdkankan.agent.entity.*;
import com.fdkankan.agent.exception.BusinessException;
import com.fdkankan.agent.httpClient.client.FdKKClient;
import com.fdkankan.agent.httpClient.service.LaserService;
import com.fdkankan.agent.mapper.ISceneProMapper;
import com.fdkankan.agent.request.SceneParam;
import com.fdkankan.agent.response.SceneVo;
import com.fdkankan.agent.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.agent.util.MangerUploadToOssUtil;
import com.fdkankan.agent.util.SceneStatusUtil;
import com.fdkankan.common.constant.SceneConstant;
import com.fdkankan.common.util.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
/**
*
* pro场景表 服务实现类
*
*
* @author
* @since 2022-11-09
*/
@Service
@Slf4j
public class SceneProServiceImpl extends ServiceImpl implements ISceneProService {
@Autowired
LaserService laserService;
@Autowired
IScenePlusService scenePlusService;
@Autowired
IScenePlusExtService scenePlusExtService;
@Resource
FdKKClient fdKKClient;
@Autowired
ICameraService cameraService;
@Autowired
ICameraDetailService cameraDetailService;
@Autowired
IUserIncrementService userIncrementService;
@Autowired
IIncrementTypeService incrementTypeService;
@Autowired
MangerUploadToOssUtil mangerUploadToOssUtil;
@Autowired
IFolderSceneService folderSceneService;
@Autowired
ISceneService sceneService;
@Autowired
ISceneCopyLogService sceneCopyLogService;
@Override
public Object pageList(SceneParam param) {
if(param.getType() == 2){ //深时
return laserService.pageList(param);
}
Page page = this.getBaseMapper().pageList(new Page<>(param.getPageNum(),param.getPageSize()),param);
HashMap map = null;
if(page.getRecords().size() >0){
List numList = page.getRecords().parallelStream().map(SceneVo::getNum).collect(Collectors.toList());
map = sceneCopyLogService.getByNewNumList(numList);
}
for (SceneVo record : page.getRecords()) {
record.setStatusString(SceneStatusUtil.getStatusString(record));
if(map !=null ){
SceneCopyLog sceneCopyLog = map.get(record.getNum());
if(sceneCopyLog != null){
record.setCopyTime(sceneCopyLog.getCreateTime());
record.setIsCopy(true);
}
}
}
return PageInfo.PageInfo(page);
}
@Override
public void copy(String sceneNum) throws Exception {
ScenePro scenePro = this.getByNum(sceneNum);
ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
if((scenePro == null || scenePro.getCameraId() == null ) && (scenePlus== null || scenePlus.getCameraId() == null)){
throw new BusinessException(ResultCode.SCENE_EMPTY);
}
Long cameraId = scenePro == null ? scenePlus.getCameraId() : scenePro.getCameraId();
Camera camera = cameraService.getById(cameraId);
if(camera == null){
throw new BusinessException(ResultCode.CAMERA_EMPTY);
}
CameraDetail detailEntity = cameraDetailService.getByCameraId(cameraId);
if(detailEntity == null){
throw new BusinessException(ResultCode.CAMERA_EMPTY);
}
Long needSpace = 0L;
if(scenePro != null){
needSpace = scenePro.getSpace();
}
if(scenePlus != null){
ScenePlusExt scenePlusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
if(scenePlusExt != null && scenePlusExt.getSpace() != null){
needSpace = scenePlusExt.getSpace();
}
}
Boolean checkSpace = cameraDetailService.checkSpace(detailEntity, needSpace);
if(!checkSpace){
throw new BusinessException(ResultCode.CAMERA_SPACE_ERROR);
}
HashMap param = new HashMap<>();
param.put("num",sceneNum);
JSONObject jsonObject = fdKKClient.copyScene(param, "m_a_n_a_g_e");
Integer code = jsonObject.getInteger("code");
if(code != 0){
throw new BusinessException(jsonObject.getInteger("code"),jsonObject.getString("msg"));
}
}
@Override
public ScenePro getByNum(String num) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(ScenePro::getNum,num);
wrapper.eq(ScenePro::getIsUpgrade,0);
List list = this.list(wrapper);
if(list == null || list.size() <=0){
return null;
}
return list.get(0);
}
@Override
public Long getCountByCameraId(Long cameraId) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(ScenePro::getIsUpgrade,0);
wrapper.eq(ScenePro::getCameraId,cameraId);
wrapper.in(ScenePro::getStatus,0,-2);
return this.count(wrapper);
}
}