package com.fdkankan.fusion.service.impl;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fdkankan.fusion.common.FilePath;
import com.fdkankan.fusion.common.ResultCode;
import com.fdkankan.fusion.common.ResultData;
import com.fdkankan.fusion.common.util.*;
import com.fdkankan.fusion.entity.CaseNumEntity;
import com.fdkankan.fusion.entity.FusionNum;
import com.fdkankan.fusion.entity.Model;
import com.fdkankan.fusion.exception.BusinessException;
import com.fdkankan.fusion.httpClient.client.FdKKClient;
import com.fdkankan.fusion.mapper.ICaseNumMapper;
import com.fdkankan.fusion.request.SceneNumParam;
import com.fdkankan.fusion.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.fusion.websocket.SessionService;
import com.fdkankan.fusion.websocket.enums.CommonEnum;
import com.fdkankan.fusion.websocket.vo.WsMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.File;
import java.sql.BatchUpdateException;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* 服务实现类
*
*
* @author
* @since 2022-07-27
*/
@Service
@Slf4j
public class CaseNumServiceImpl extends ServiceImpl implements ICaseNumService {
@Autowired
UploadToOssUtil uploadToOssUtil;
@Value("${upload.query-path}")
private String queryPath;
@Value("${spring.profiles.active}")
private String environment;
@Autowired
IModelService modelService;
@Autowired
IFusionNumService fusionNumService;
@Autowired
ICaseViewService caseViewService;
@Autowired
IFusionMeterService fusionMeterService;
@Autowired
IFusionGuidePathService fusionGuidePathService;
@Autowired
ICaseTagService caseTagService;
@Autowired
ICaseTagPointService caseTagPointService;
@Autowired
ThreadService threadService;
@Autowired
SessionService sessionService;
@Override
public List getByCaseId(Integer caseId) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CaseNumEntity::getCaseId,caseId);
return this.list(wrapper);
}
@Override
public void addBatch(Integer caseId, List sceneNumParam) {
List addNumList = this.updateByNumList(caseId, sceneNumParam);
if(addNumList == null || addNumList.size()<=0){
return;
}
List newCaseNums = new ArrayList<>();
List modelList = new ArrayList<>();
HashMap> map = new HashMap<>();
for (SceneNumParam numParam : sceneNumParam) {
if(map.get(numParam.getType()) == null){
HashSet set = new HashSet<>(numParam.getNumList());
map.put(numParam.getType(),set);
}else {
map.get(numParam.getType()).addAll(numParam.getNumList());
}
}
for (Integer type : map.keySet()) {
HashSet nulList = map.get(type);
List numList = new ArrayList<>(nulList);
HashSet setNum = new HashSet<>(numList);
for (String num : setNum) {
if(!addNumList.contains(num)){
continue;
}
CaseNumEntity caseNumEntity = new CaseNumEntity();
caseNumEntity.setCaseId(caseId);
caseNumEntity.setNumType(type);
caseNumEntity.setNum(num);
newCaseNums.add(caseNumEntity);
if(type == 3){ //用户上传三维模型跳过
continue;
}
Model model = modelService.getIsNullNewByNum(num,type,1);
modelList.add(model);
}
}
if(newCaseNums.size() >0){
this.saveBatch(newCaseNums);
}
if(modelList.size() >0){
modelService.saveOrUpdateBatch(modelList);
}
}
private List updateByNumList(Integer caseId, List sceneNumParam) {
List addList = new ArrayList<>();
HashMap fusionNumHashMap = fusionNumService.getByCaseId(caseId);
HashMap> delMap = new HashMap<>();
for (SceneNumParam param : sceneNumParam) {
Integer type = param.getType();
List numList = param.getNumList();
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CaseNumEntity::getCaseId,caseId);
wrapper.eq(CaseNumEntity::getNumType,type);
List list = this.list(wrapper);
List hanNumList = list.parallelStream().map(CaseNumEntity::getNum).collect(Collectors.toList());
List delList = new ArrayList<>();
for (String num : hanNumList) {
if(!numList.contains(num)){
delList.add(num);
}
}
for (String num : numList) {
if(!hanNumList.contains(num)){
addList.add(num);
}
}
if(!delList.isEmpty()){
HashMap mapByNum = modelService.getMapByNum(delList);
for (String key : mapByNum.keySet()) {
Model model = mapByNum.get(key);
if(model != null && model.getType().equals(param.getType()) && fusionNumHashMap.containsKey(model.getModelId())){
throw new BusinessException(ResultCode.DEL_NUM_ERROR);
}
}
delMap.put(param.getType(),delList);
}
}
if(!delMap.isEmpty()){
for (Integer type : delMap.keySet()) {
this.deleteByNum(caseId,delMap.get(type),type);
}
}
return addList;
}
@Override
public void deleteByNum(Integer caseId, List delList,Integer type) {
if(delList.size() >0){
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CaseNumEntity::getCaseId,caseId);
wrapper.eq(CaseNumEntity::getNumType,type);
wrapper.in(CaseNumEntity::getNum,delList);
this.remove(wrapper);
fusionNumService.deleteByNumList(caseId,delList,true,type);
}
}
@Override
public void deleteByCaseId(Integer caseId) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CaseNumEntity::getCaseId,caseId);
this.remove(wrapper);
fusionNumService.deleteByCaseId(caseId);
caseViewService.deleteByCaseId(caseId);
fusionGuidePathService.deleteByCaseId(caseId);
fusionMeterService.deleteByCaseId(caseId);
caseTagService.deletePointByCaseId(caseId);
}
@Override
public HashMap> getTypeMap(Integer caseId) {
List caseNumList = this.getByCaseId(caseId);
HashMap> typeMap = new HashMap<>();
for (CaseNumEntity caseNumEntity : caseNumList) {
List numList ;
if(typeMap.get(caseNumEntity.getNumType()) == null){
numList = new ArrayList<>();
}else {
numList = typeMap.get(caseNumEntity.getNumType());
}
numList.add(caseNumEntity.getNum());
typeMap.put(caseNumEntity.getNumType(),numList);
}
return typeMap;
}
@Override
public List getByNum(String num) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CaseNumEntity::getNum,num);
return this.list(wrapper);
}
@Override
public void addModeByCaseId(Integer caseId, Integer modelId) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CaseNumEntity::getCaseId,caseId);
wrapper.eq(CaseNumEntity::getNum,modelId);
List list = this.list(wrapper);
if(list.isEmpty()){
CaseNumEntity caseNumEntity = new CaseNumEntity();
caseNumEntity.setCaseId(caseId);
caseNumEntity.setNumType(3);
caseNumEntity.setNum(modelId.toString());
this.save(caseNumEntity);
}
}
@Override
public void deleteByModel(Integer modelId) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CaseNumEntity::getNum,modelId.toString());
this.remove(wrapper);
modelService.removeById(modelId);
}
}