package com.fdkankan.scene.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.util.ThreeDESUtil;
import com.fdkankan.redis.constant.RedisKey;
import com.fdkankan.redis.util.RedisClient;
import com.fdkankan.scene.bean.ResultData;
import com.fdkankan.scene.entity.Scene;
import com.fdkankan.scene.entity.SceneFileMapping;
import com.fdkankan.scene.httpclient.CustomHttpClient;
import com.fdkankan.scene.mapper.SceneMapper;
import com.fdkankan.scene.service.SceneFileMappingService;
import com.fdkankan.scene.service.SceneService;
import com.fdkankan.scene.vo.SceneEditControlsVO;
import com.fdkankan.scene.vo.SceneInfoParamVO;
import com.fdkankan.scene.vo.SceneInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.UrlResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
*
* 服务实现类
*
*
* @author dsx
* @since 2024-06-26
*/
@Slf4j
@Service
public class SceneServiceImpl extends ServiceImpl implements SceneService {
@Autowired
private SceneFileMappingService sceneFileMappingService;
@Autowired
private RedisClient redisClient;
@Resource
private CustomHttpClient customHttpClient;
@Override
public ResultData getSceneInfo(SceneInfoParamVO param) throws Exception {
String num = param.getNum();
SceneInfoVO sceneInfoVO = null;
List list = this.list(new LambdaQueryWrapper().eq(Scene::getNum, num).orderByDesc(Scene::getId));
if(CollUtil.isEmpty(list)){
return ResultData.error(ErrorCode.FAILURE_CODE_5005.code(), ErrorCode.FAILURE_CODE_5005.message());
}
Scene scene = list.get(0);
sceneInfoVO = new SceneInfoVO();
sceneInfoVO.setTitle(scene.getTitle());
sceneInfoVO.setNum(scene.getNum());
sceneInfoVO.setFloorLogoSize(Integer.valueOf(scene.getFloorlogosize()));
sceneInfoVO.setSceneKind(scene.getScenekind());
sceneInfoVO.setSceneResolution(scene.getSceneresolution());
sceneInfoVO.setSceneFrom(scene.getScenefrom());
sceneInfoVO.setModelKind(scene.getModelkind());
sceneInfoVO.setFloorPlanAngle(Float.valueOf(scene.getFloorplanangle()));
sceneInfoVO.setFloorLogo("2");
SceneEditControlsVO sceneEditControlsBean = SceneEditControlsVO.builder().showRule(1).showFloorplan(0).showDollhouse(0).showMap(1).showPanorama(1).showVR(1).showTitle(1).build();
sceneInfoVO.setControls(sceneEditControlsBean);
List mappingList = sceneFileMappingService.list(new LambdaQueryWrapper().eq(SceneFileMapping::getNum, num).orderByAsc(SceneFileMapping::getId));
Map keyMap = new HashMap<>();
for (SceneFileMapping sceneFileMapping : mappingList) {
keyMap.put(sceneFileMapping.getKey(), "service/scene/file?key=" + sceneFileMapping.getKey());
}
// sceneInfoVO.setMapping(keyMap);
return ResultData.ok(sceneInfoVO);
}
@Override
public ResponseEntity outFileByKey(String key, HttpServletResponse response) throws IOException {
SceneFileMapping one = sceneFileMappingService.getOne(new LambdaQueryWrapper().eq(SceneFileMapping::getKey, key));
// InputStream inputStream = customHttpClient.downloadFileToInputStream(one.getUrl());
org.springframework.core.io.Resource resource = null;
URL url = new URL(one.getUrl());
URLConnection connection = url.openConnection();
connection.setConnectTimeout(600000);
connection.setReadTimeout(600000);
resource = new UrlResource(url);
return ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM).body(resource);
}
@Override
public void outFileByKey2(String key, Integer subgroup, HttpServletResponse response) throws IOException {
SceneFileMapping one = sceneFileMappingService.getOne(new LambdaQueryWrapper().eq(SceneFileMapping::getKey, key).eq(SceneFileMapping::getSubgroup, subgroup));
if(Objects.isNull(one)){
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}else{
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader("Content-Disposition", "attachment; filename=\"" + FileUtil.getName(key) + "\"");
try(
InputStream inputStream = customHttpClient.downloadFileToInputStream(one.getUrl());
ServletOutputStream outputStream = response.getOutputStream())
{
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.flush();
}catch (Exception e){
log.info("读取文件失败:key:{},url:{}", one.getKey(), one.getUrl());
throw new RuntimeException(e);
}
}
}
public static void main(String[] args) {
JSONObject params = new JSONObject();
params.put("SYSCODE", "CHNTZCGL_ZCSZHGL");
params.put("ACCTID", "101269561");
params.put("TOKEN", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJSRVMiLCJpc3MiOiJXUzRBIiwiZXhwIjoxNzI3MjQxMTQwLCJOQU5PU0VDT05EIjozNTk0NjI1NTg5ODQ3MDU0Nn0.MIlnrl5X00_0QgaT1N9wYRwi4WQ86dGtQlD3EsA01I0");
String encode = ThreeDESUtil.encode(params.toString(), "6#dPz>3F");
System.out.println(encode);
JSONObject params2 = new JSONObject();
params2.put("SERVICEID", "CHNTZCGL_ZCSZHGL");
params2.put("LOGINACCOUNT", "yangqc");
params2.put("TOKEN", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJSRVMiLCJpc3MiOiJXUzRBIiwiZXhwIjoxNzI3NTc3Mzc2LCJOQU5PU0VDT05EIjozNjI4MjQ5MTc2NjM4MDAzMX0.fbTCdbjMTOD6NywkuKGtsOnQdmFI7PiU4g8mEiLQ5wQ");
encode = ThreeDESUtil.encode(params2.toString(), "6#dPz>3F");
System.out.println(encode);
JSONObject params3 = new JSONObject();
params3.put("SERVICEID", "CHNTZCGL_ZCSZHGL");
params3.put("QUERYMODE", "3");
params3.put("TOKEN", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJSRVMiLCJpc3MiOiJXUzRBIiwiZXhwIjoxNzI3MjQxMTQwLCJOQU5PU0VDT05EIjozNTk0NjI1NTg5ODQ3MDU0Nn0.MIlnrl5X00_0QgaT1N9wYRwi4WQ86dGtQlD3EsA01I0");
params3.put("ORGCODE", "100035");
encode = ThreeDESUtil.encode(params3.toString(), "6#dPz>3F");
System.out.println(encode);
}
@Override
public Scene getByNum(String num, Integer subgroup) {
return this.getOne(new LambdaQueryWrapper().eq(Scene::getNum, num).eq(Scene::getSubgroup, subgroup));
}
@Override
public List listByNum(String num, Integer subgroup) {
return this.list(new LambdaQueryWrapper().eq(Scene::getNum, num).eq(Scene::getSubgroup, subgroup));
}
}