package com.fdkankan.fusion.service.impl;
import cn.dev33.satoken.stp.StpUtil;
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.fdkankan.fusion.common.FilePath;
import com.fdkankan.fusion.common.ResultCode;
import com.fdkankan.fusion.common.util.UploadToOssUtil;
import com.fdkankan.fusion.config.SecurityUtil;
import com.fdkankan.fusion.entity.CaseFiles;
import com.fdkankan.fusion.entity.CaseOverview;
import com.fdkankan.fusion.entity.CaseTabulation;
import com.fdkankan.fusion.exception.BusinessException;
import com.fdkankan.fusion.httpClient.FdService;
import com.fdkankan.fusion.mapper.ICaseOverviewMapper;
import com.fdkankan.fusion.request.ExportOverviewParam;
import com.fdkankan.fusion.service.ICaseFilesService;
import com.fdkankan.fusion.service.ICaseOverviewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.fusion.service.ICaseTabulationService;
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.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
*
* 服务实现类
*
*
* @author
* @since 2025-05-13
*/
@Service
@Slf4j
public class CaseOverviewServiceImpl extends ServiceImpl implements ICaseOverviewService {
@Autowired
ICaseFilesService caseFilesService;
@Autowired
ICaseTabulationService caseTabulationService;
@Override
public List getByCaseId(String caseId) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CaseOverview::getCaseId,caseId);
wrapper.orderByDesc(CaseOverview::getId);
return this.list(wrapper);
}
@Override
public void updateTitleById(Integer overviewId, String filesTitle) {
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(CaseOverview::getId,overviewId);
wrapper.set(CaseOverview::getTitle,filesTitle);
this.update(wrapper);
}
@Override
public void updateCaseByIds(List overviewIds, Integer caseId) {
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.in(CaseOverview::getId,overviewIds);
wrapper.set(CaseOverview::getCaseId,caseId);
this.update(wrapper);
}
@Override
public void updateListCoverById(Integer overviewId, String listCover) {
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(CaseOverview::getId,overviewId);
wrapper.set(CaseOverview::getListCover,listCover);
this.update(wrapper);
}
@Override
public CaseOverview getByNumAndSubGroup(String num, Integer subGroup) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CaseOverview::getNum,num);
wrapper.eq(CaseOverview::getSubGroup,subGroup);
List list = this.list(wrapper);
if(list ==null || list.isEmpty()){
return null;
}
return list.get(0);
}
@Override
public Long getNo(Long sysUserId) {
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(CaseOverview::getSysUserId,sysUserId);
return this.count(wrapper);
}
@Override
public void export(ExportOverviewParam param) {
if(!param.getOverviewIds().isEmpty()){
List caseOverviews = this.listByIds(param.getOverviewIds());
for (CaseOverview caseOverview : caseOverviews) {
Integer tabulationId = null;
List byOverviewId = caseTabulationService.getByOverviewId(String.valueOf(caseOverview.getId()));
for (CaseTabulation caseTabulation : byOverviewId) {
tabulationId = caseTabulation.getId();
}
CaseFiles caseFiles = new CaseFiles();
caseFiles.setCaseId(param.getCaseId());
caseFiles.setFilesUrl(caseOverview.getListCover());
caseFiles.setFilesTypeId(41);
caseFiles.setFilesTitle(caseOverview.getTitle());
caseFiles.setOverviewId(caseOverview.getId());
caseFiles.setTabulationId(tabulationId);
caseFilesService.saveOrUpdate(caseFiles);
}
}
if(!param.getTabulationIds().isEmpty()){
List caseTabulationList = caseTabulationService.listByIds(param.getTabulationIds());
for (CaseTabulation caseTabulation : caseTabulationList) {
CaseFiles caseFiles = new CaseFiles();
caseFiles.setCaseId(param.getCaseId());
caseFiles.setFilesUrl(caseTabulation.getListCover());
caseFiles.setFilesTypeId(42);
caseFiles.setFilesTitle(caseTabulation.getTitle());
caseFiles.setTabulationId(caseTabulation.getId());
caseFilesService.saveOrUpdate(caseFiles);
}
}
}
@Autowired
UploadToOssUtil uploadToOssUtil;
@Override
public void toMeshEdit(String num, Integer subGroup, String listCover) {
String filePath = String.format(FilePath.MESH_SCENE_FLOOR_PNG_PATH,num);
String fileName = String.format(FilePath.MESH_SCENE_FLOOR_PNG_PATH_NAME,subGroup);
String jsonPathSource = String.format(FilePath.MESH_SCENE_FLOOR_JSON_PATH_SOURCE,num);
String jsonPath = String.format(FilePath.MESH_SCENE_FLOOR_JSON_PATH,num);
String localPath = String.format(FilePath.MESH_SCENE_FLOOR_JSON_PATH_LOCAL,num);
try {
uploadToOssUtil.copyFile(uploadToOssUtil.getOssPath(listCover),filePath + fileName);
String content = uploadToOssUtil.readFile(jsonPathSource);
JSONObject jsonObject = JSONObject.parseObject(content);
JSONArray jsonArray = jsonObject.getJSONArray("floors");
for (Object object : jsonArray) {
JSONObject obj = (JSONObject) object;
Integer subgroupjs = obj.getInteger("subgroup");
if(subgroupjs.equals(subGroup)){
obj.put("filename",fileName);
}
}
FileUtil.writeString(jsonObject.toJSONString(),new File(localPath), StandardCharsets.UTF_8);
uploadToOssUtil.uploadOss(localPath,jsonPath);
}catch (Exception e){
log.info("同步mesh编辑器失败:{}",e);
}finally {
FileUtil.del(localPath);
}
}
@Autowired
FdService fdService;
@Override
public void checkLoginUser(CaseOverview caseOverview, CaseTabulation caseTabulation,String pageType) {
if(caseOverview == null && caseTabulation == null){
throw new BusinessException(ResultCode.RECORD_NOT_EXIST);
}
List caseIds = new ArrayList<>();
String num = null;
if(caseOverview != null){
caseIds = caseFilesService.getByOverviewId(caseOverview.getId()).stream().map(CaseFiles::getCaseId).collect(Collectors.toList());
if(caseOverview.getCaseId() != null){
caseIds.add(caseOverview.getCaseId());
}
num = caseOverview.getNum();
}
if(caseTabulation != null){
caseIds = caseFilesService.getByTabulation(caseTabulation.getId()).stream().map(CaseFiles::getCaseId).collect(Collectors.toList());
if(caseTabulation.getCaseId() != null){
caseIds.add(caseTabulation.getCaseId());
}
}
Long sysUserId = caseOverview != null ? caseOverview.getSysUserId() : caseTabulation.getSysUserId();
if(fdService.cheUserAuth(sysUserId)){
return;
}
if(StringUtils.isNotBlank(num) && fdService.checkNum(num,pageType,StpUtil.getTokenValue())){
return;
}
if(!caseIds.isEmpty()){
Boolean flag = false;
for (Integer caseId : caseIds) {
try {
fdService.checkCaseAuth(caseId.toString(),null,pageType,StpUtil.getTokenValue());
flag = true;
}catch (Exception e){
}
}
if(flag){
return;
}
}
throw new BusinessException(ResultCode.NOT_PER);
}
}