package com.fdkankan.manage.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONArray;
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.fyun.face.FYunFileServiceInterface;
import com.fdkankan.manage.common.CommandEnum;
import com.fdkankan.manage.common.OssPath;
import com.fdkankan.manage.common.PageInfo;
import com.fdkankan.manage.common.ResultCode;
import com.fdkankan.manage.config.ManageConfig;
import com.fdkankan.manage.constant.FileTypeEnum;
import com.fdkankan.manage.entity.*;
import com.fdkankan.manage.exception.BusinessException;
import com.fdkankan.manage.httpClient.client.HaixinClient;
import com.fdkankan.manage.httpClient.client.OtherClient;
import com.fdkankan.manage.httpClient.param.HaixinParam;
import com.fdkankan.manage.mapper.IDictFileMapper;
import com.fdkankan.manage.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.manage.util.MD5Checksum;
import com.fdkankan.manage.util.ShellUtil;
import com.fdkankan.manage.vo.EvidenceVo;
import com.fdkankan.manage.vo.request.DictFileParam;
import com.fdkankan.manage.vo.request.SubmitPhotoHaixinVo;
import com.fdkankan.manage.vo.request.addMediaLibraryParam;
import com.fdkankan.manage.vo.response.DictFileVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* 服务实现类
*
*
* @author
* @since 2024-12-02
*/
@Service
@Slf4j
public class DictFileServiceImpl extends ServiceImpl implements IDictFileService {
@Autowired
ICommonUploadService commonUploadService;
@Autowired
IDictService dictService;
@Autowired
IScenePlusService scenePlusService;
@Autowired
ICaseService caseService;
@Autowired
IDictIconService dictIconService;
@Autowired
IDictFileCaseService dictFileCaseService;
@Override
public Object pageList(DictFileParam param) {
if(StringUtils.isBlank(param.getTypeKey())){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
if(StpUtil.isLogin()){
param.setSysUserId( Long.valueOf( StpUtil.getLoginId().toString()));
}
Page pageVo = this.getBaseMapper().pageList(new Page<>(param.getPageNum(),param.getPageSize()),param);
List dictIds = pageVo.getRecords().stream().map(DictFile::getDictId).collect(Collectors.toList());
HashMap> map = dictIconService.getMapGroupDict(dictIds);
List ids = pageVo.getRecords().stream().map(DictFile::getId).collect(Collectors.toList());
HashMap> byDictIds = dictFileCaseService.getByDictIds(ids);
for (DictFileVo dictFileVo : pageVo.getRecords()) {
dictFileVo.setDictIconList(map.get(dictFileVo.getDictId()));
dictFileVo.setCaseList(byDictIds.get(dictFileVo.getId()));
}
return PageInfo.PageInfo(pageVo);
}
@Override
public Object traceEvidenceInfoList(DictFileParam param) {
if(StringUtils.isBlank(param.getTypeKey())){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
if(!StpUtil.isLogin()){
throw new BusinessException(ResultCode.USER_NOT_LOGIN);
}
param.setSysUserId( Long.valueOf( StpUtil.getLoginId().toString()));
if(StringUtils.isNotBlank(param.getNum())){
List caseList = caseService.getCaseByNum(param.getNum());
List caseIds = caseList.stream().map(Case::getCaseId).collect(Collectors.toList());
List dictFileCases = dictFileCaseService.getByCaseIds(caseIds);
Set ids = dictFileCases.stream().map(DictFileCase::getDictFileId).collect(Collectors.toSet());
if(!ids.isEmpty()){
param.setDictFileIds(ids);
}
}
if(StringUtils.isNotBlank(param.getKno())){
List dictFileCases = dictFileCaseService.getByCaseId(param.getKno());
Set ids = dictFileCases.stream().map(DictFileCase::getDictFileId).collect(Collectors.toSet());
if(ids.isEmpty()){
return PageInfo.PageInfoEmpty(param.getPageNum(),param.getPageSize());
}
param.setDictFileIds(ids);
}
Page pageVo = this.getBaseMapper().traceEvidenceInfoList(new Page<>(param.getPageNum(),param.getPageSize()),param);
List dictIds = pageVo.getRecords().stream().map(DictFile::getDictId).collect(Collectors.toList());
HashMap> map = dictIconService.getMapGroupDict(dictIds);
List ids = pageVo.getRecords().stream().map(DictFile::getId).collect(Collectors.toList());
HashMap> byDictIds = dictFileCaseService.getByDictIds(ids);
for (DictFileVo dictFileVo : pageVo.getRecords()) {
dictFileVo.setDictIconList(map.get(dictFileVo.getDictId()));
dictFileVo.setCaseList(byDictIds.get(dictFileVo.getId()));
}
return PageInfo.PageInfo(pageVo);
}
@Override
public void addOrUpdate(DictFile dictFile) {
if(StringUtils.isBlank(dictFile.getTypeKey())
|| dictFile.getDictId() == null ){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
this.saveOrUpdate(dictFile);
}
@Override
public void del(DictFile dictFile) {
if(dictFile.getId() == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
DictFile byId = this.getById(dictFile.getId());
if(byId != null && StringUtils.isNotBlank(byId.getUseType()) && !"ordinary".equals(byId.getUseType())){
throw new BusinessException(ResultCode.DEL_ERROR);
}
this.removeById(dictFile.getId());
}
@Override
public void updateDictId(Integer dictId, Integer UpDictId) {
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(DictFile::getDictId,dictId);
wrapper.set(DictFile::getDictId,UpDictId);
this.update(wrapper);
}
@Override
public DictFile getByEvidenceNo(String evidenceNo) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(DictFile::getEvidenceNo,evidenceNo);
List list = this.list(wrapper);
if(list.isEmpty()){
return null;
}
return list.get(0);
}
@Autowired
private FYunFileServiceInterface fYunFileServiceInterface;
@Autowired
ManageConfig manageConfig;
@Autowired
HaixinClient haixinClient;
@Autowired
OtherClient otherClient;
@Override
public void checkMediaLibrary(addMediaLibraryParam param) {
if(StringUtils.isBlank(param.getFilePath())){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
String extName = param.getFilePath().substring(param.getFilePath().lastIndexOf(".")).toLowerCase();
FileTypeEnum fileTypeEnum = FileTypeEnum.getByType(extName.replace(".", ""));
if(fileTypeEnum == null){
throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
}
}
@Override
public DictFile addMediaLibrary(addMediaLibraryParam param) {
if(StringUtils.isBlank(param.getFilePath())){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
if(param.getToHaixin() == 1 && StringUtils.isBlank(param.getNum())){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
CommonUpload commonUpload = null;
if(StringUtils.isNotBlank(param.getFileMd5())){
//param.setFileMd5( MD5Checksum.getMD5( "/oss/4dkankan/" + param.getFilePath()));
commonUpload = commonUploadService.getByFileMd5(param.getFileMd5());
}
if(commonUpload == null){
String uuid = UUID.randomUUID().toString().replace("-","");
String extName = param.getFilePath().substring(param.getFilePath().lastIndexOf(".")).toLowerCase();
String formart = extName.replace(".", "");
FileTypeEnum fileTypeEnum = FileTypeEnum.getByType(formart);
if(fileTypeEnum == null){
throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
}
String ossPath = String.format(OssPath.MANAGE_MODEL_FILE_PATH, uuid+ extName);
fYunFileServiceInterface.copyFileInBucket(param.getFilePath(),ossPath);
commonUpload = new CommonUpload();
commonUpload.setFileName(param.getFileName());
commonUpload.setFileUrl(manageConfig.getQueryPath() + ossPath);
commonUpload.setFileSize(param.getFileSize());
commonUpload.setFileType(fileTypeEnum.getCode());
commonUpload.setFileTypeStr(fileTypeEnum.getMsg());
commonUpload.setFileFormat(formart);
commonUpload.setResultFileFormat(formart);
commonUpload.setStatus(1);
commonUpload.setUseType("ordinary");
commonUpload.setFileMd5(param.getFileMd5());
commonUploadService.saveOrUpdate(commonUpload);
}
DictFile dictFile = null;
if(StpUtil.isLogin()){
dictFile = this.getByUploadIdAndSysId(commonUpload.getId(),Long.valueOf(StpUtil.getLoginId().toString()));
}
if(dictFile == null){
dictFile = new DictFile();
dictFile.setName(param.getFileName());
dictFile.setTypeKey("media-library");
if(StpUtil.isLogin()){
dictFile.setSysUserId(Long.valueOf(StpUtil.getLoginId().toString()));
}
dictFile.setDictId(param.getDictId());
dictFile.setUseType("ordinary");
dictFile.setUploadId(commonUpload.getId());
dictFile.setKno(param.getKno());
dictFile.setSid(param.getSid());
dictFile.setContent(JSONObject.toJSONString(param.getContent()));
this.saveOrUpdate(dictFile);
dictFileCaseService.add(dictFile.getId(),param.getNum());
}
try {
if(param.getDelSource() == 1){
fYunFileServiceInterface.deleteFile(param.getFilePath());
}
} catch (IOException e) {
log.info("删除原文件失败");
}
if(param.getToHaixin() ==1){
String pushUrl = manageConfig.getHaixinApiBasePath();
ScenePlus scenePlus = scenePlusService.getByNum(param.getNum());
if(scenePlus == null){
return dictFile;
}
if(StringUtils.isNotBlank(scenePlus.getPushAddress())){
pushUrl = scenePlus.getPushAddress();
}
Case caseEntity = caseService.getByKno(scenePlus.getKNo());
if(caseEntity == null){
return dictFile;
}
if(StringUtils.isBlank(pushUrl)){
return dictFile;
}
SubmitPhotoHaixinVo vo = new SubmitPhotoHaixinVo();
vo.setKno(scenePlus.getKNo());
vo.setCaseId(caseEntity.getCaseId().toString());
vo.setProjectId(scenePlus.getTaskId());
vo.setPhotoId(dictFile.getId().toString());
vo.setFileUrl( commonUpload.getFileUrl());
if(StringUtils.isBlank(param.getFileMd5())){
param.setFileMd5( MD5Checksum.getMD5( "/oss/4dkankan/" + param.getFilePath()));
}
vo.setMd5(param.getFileMd5());
vo.setCategory(param.getCategory());
vo.setServerUrl(manageConfig.getServerUrl());
// String jsonObject = haixinClient.submitPhoto(vo);
String jsonObject = otherClient.postJson(pushUrl +"/ecs/api/panoramicImageService/submitPhoto", BeanUtil.beanToMap(vo));
log.info("submitPhoto-tohaixin-result:{}",jsonObject);
}
return dictFile;
}
private DictFile getByUploadIdAndSysId(Integer uploadId, Long sysId) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(DictFile::getUploadId,uploadId);
wrapper.eq(DictFile::getSysUserId,sysId);
List list = this.list(wrapper);
if(list== null || list.isEmpty()){
return null;
}
return list.get(0);
}
@Override
public void updateByKno(String kno) {
try {
JSONObject jsonObject = haixinClient.queryEvidence(new HaixinParam(kno));
JSONObject data = jsonObject.getJSONObject("data");
JSONArray jsonArray = data.getJSONArray("sceneMaterialEvidence");
for (Object object : jsonArray) {
JSONObject obj = (JSONObject) object;
String evidenceNo = obj.getString("materialEvidenceNo"); //痕迹物证唯一编码
String category = obj.getString("category");
String fileServerPath = obj.getString("fileServerPath");
String materialEvidenceName = obj.getString("materialEvidenceName");
String leftPosition = obj.getString("leftPosition");
String feature = obj.getString("feature");
String collectionModeName = obj.getString("collectionModeName");
if(StringUtils.isBlank(collectionModeName)){
collectionModeName =obj.getString("collectionMode");
}
String collectedTime = obj.getString("collectedTime");
String createAccount = obj.getString("createAccount");
JSONArray jsonArray1 = obj.getJSONArray("sceneCollectedPerson");
StringBuilder collectedPerson = new StringBuilder();
for (Object o : jsonArray1) {
JSONObject jsonObject1 = (JSONObject) o;
collectedPerson.append(jsonObject1.getString("collectedPerson")).append(",");
}
if(collectedPerson.toString().contains(",")){
collectedPerson.delete(collectedPerson.lastIndexOf(","),collectedPerson.length() -1);
}
EvidenceVo vo = new EvidenceVo(materialEvidenceName,leftPosition,feature,collectionModeName,collectedTime,collectedPerson.toString(),0,null);
DictFile dictFile = this.getByEvidenceNo(evidenceNo);
if(dictFile == null){
dictFile = new DictFile();
}
CommonUpload commonUpload = commonUploadService.getById(dictFile.getUploadId());
if(commonUpload == null){
commonUpload = new CommonUpload();
}
commonUpload.setFileName(materialEvidenceName);
String formart = FileUtil.extName(fileServerPath);
commonUpload.setFileUrl(manageConfig.getHaixinApiBasePath() + "/"+ fileServerPath);
commonUpload.setFileSize("0");
commonUpload.setFileType(0);
commonUpload.setFileTypeStr("图片");
commonUpload.setFileFormat(formart);
commonUpload.setResultFileFormat(formart);
commonUpload.setStatus(1);
commonUpload.setUseType("trace_evidence");
commonUpload.setUpdateTime(new Date());
commonUploadService.saveOrUpdate(commonUpload);
Dict dict = dictService.getByResourceId(category);
dictFile.setName(materialEvidenceName);
dictFile.setTypeKey("media-library");
dictFile.setDictId(dict != null?dict.getId() : null);
if(StpUtil.isLogin()){
dictFile.setSysUserId(Long.valueOf(StpUtil.getLoginId().toString()));
}
dictFile.setUseType("trace_evidence");
dictFile.setUploadId(commonUpload.getId());
dictFile.setKno(kno);
dictFile.setContent(JSONObject.toJSONString(vo));
dictFile.setEvidenceNo(evidenceNo);
dictFile.setUpdateTime(new Date());
this.saveOrUpdate(dictFile);
Case byKno = caseService.getByKno(kno);
if(byKno !=null){
dictFileCaseService.add(dictFile.getId(),byKno.getCaseId());
}
}
}catch (Exception e){
}
}
}