package com.fdkankan.fusion.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fdkankan.fusion.entity.CaseFiles;
import com.fdkankan.fusion.entity.CaseOverview;
import com.fdkankan.fusion.entity.CaseTabulation;
import com.fdkankan.fusion.mapper.ICaseTabulationMapper;
import com.fdkankan.fusion.service.ICaseFilesService;
import com.fdkankan.fusion.service.ICaseOverviewService;
import com.fdkankan.fusion.service.ICaseTabulationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.fyun.face.FYunFileServiceInterface;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.List;
/**
*
* 服务实现类
*
*
* @author
* @since 2025-05-13
*/
@Service
public class CaseTabulationServiceImpl extends ServiceImpl implements ICaseTabulationService {
@Autowired
ICaseFilesService caseFilesService;
@Autowired
ICaseOverviewService caseOverviewService;
@Autowired
FYunFileServiceInterface fYunFileServiceInterface;
@Override
public List getByCaseId(Integer caseId) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CaseTabulation::getCaseId,caseId);
wrapper.isNull(CaseTabulation::getOverviewId);
wrapper.orderByDesc(CaseTabulation::getId);
return this.list(wrapper);
}
@Override
public List getByOverviewId(String overviewId) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CaseTabulation::getOverviewId,overviewId);
wrapper.orderByDesc(CaseTabulation::getId);
return this.list(wrapper);
}
@Override
public List getByOverId(Integer overId) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CaseTabulation::getOverviewId,overId);
return this.list(wrapper);
}
@Override
public void delByIds(List ids) {
if(!ids.isEmpty()){
List caseTabulations = this.listByIds(ids);
this.removeByIds(ids);
for (CaseTabulation caseTabulation : caseTabulations) {
delFile(caseTabulation);
}
}
}
private void delFile(CaseTabulation caseTabulation) {
List list = this.getByCover(caseTabulation.getListCover());
if(list.isEmpty()){
try {
fYunFileServiceInterface.deleteFile(caseTabulation.getListCover());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
List list2 = this.getByCover(caseTabulation.getMapUrl());
if(list2.isEmpty()){
try {
fYunFileServiceInterface.deleteFile(caseTabulation.getMapUrl());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
List list3 = this.getByCover(caseTabulation.getCover());
if(list3.isEmpty()){
try {
fYunFileServiceInterface.deleteFile(caseTabulation.getCover());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
private List getByCover(String listCover) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.and(e -> e.eq(CaseTabulation::getListCover,listCover).or().eq(CaseTabulation::getMapUrl,listCover).or().like(CaseTabulation::getCover,listCover));
return this.list(wrapper);
}
@Override
public void addOrUpdate(CaseTabulation caseTabulation) {
this.saveOrUpdate(caseTabulation);
if(StringUtils.isNotBlank(caseTabulation.getListCover()) || caseTabulation.getOverviewId() != null){
CaseFiles caseFiles = new CaseFiles();
if(caseTabulation.getId()!=null){
CaseTabulation db = this.getById(caseTabulation.getId());
if(caseTabulation.getOverviewId() == null){
caseTabulation.setOverviewId(db.getOverviewId());
}
CaseFiles dbCaseFile = caseFilesService.getByTabulation(caseTabulation.getId());
if(dbCaseFile != null){
caseFiles.setFilesId(dbCaseFile.getFilesId());
}
}
if(caseTabulation.getOverviewId()!=null){
CaseFiles dbCaseFile = caseFilesService.getByOverviewId(caseTabulation.getOverviewId());
if(dbCaseFile != null){
caseFiles.setFilesId(dbCaseFile.getFilesId());
}
}
caseFiles.setCaseId(caseTabulation.getCaseId());
caseFiles.setFilesUrl(caseTabulation.getListCover());
if(StringUtils.isBlank(caseTabulation.getListCover())){
CaseOverview caseOverview = caseOverviewService.getById(caseTabulation.getOverviewId());
caseFiles.setFilesUrl(caseOverview.getListCover());
}
if(caseFiles.getFilesId() == null){
if(caseTabulation.getOverviewId() != null){ //平面图
caseFiles.setFilesTitle("平面图");
caseFiles.setFilesTypeId(1);
caseFiles.setOverviewId(caseTabulation.getOverviewId() );
caseFiles.setTabulationId(caseTabulation.getId() );
}else {
caseFiles.setFilesTypeId(1);
caseFiles.setFilesTitle("方位图");
caseFiles.setTabulationId(caseTabulation.getId() );
}
}
caseFilesService.saveOrUpdate(caseFiles);
}
}
@Override
public void updateTitleById(Integer tabulationId, String filesTitle) {
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(CaseTabulation::getId,tabulationId);
wrapper.set(CaseTabulation::getTitle,filesTitle);
this.update(wrapper);
}
}