package com.fdkankan.fusion.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fdkankan.fusion.common.ResultCode;
import com.fdkankan.fusion.common.util.UploadToOssUtil;
import com.fdkankan.fusion.exception.BusinessException;
import com.fdkankan.fusion.common.ResultData;
import com.fdkankan.fusion.entity.CaseFiles;
import com.fdkankan.fusion.mapper.ICaseFilesMapper;
import com.fdkankan.fusion.service.ICaseFilesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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 java.util.List;
/**
*
* 服务实现类
*
*
* @author
* @since 2022-08-02
*/
@Service
public class CaseFilesServiceImpl extends ServiceImpl implements ICaseFilesService {
@Autowired
UploadToOssUtil uploadToOssUtil;
@Value("${upload.query-path}")
private String queryPath;
@Override
public List allList(Integer caseId, Integer caseFilesTypeId) {
if(caseId == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CaseFiles::getCaseId,caseId);
if(caseFilesTypeId!=null){
wrapper.eq(CaseFiles::getFilesTypeId,caseFilesTypeId);
}
wrapper.orderByAsc(CaseFiles::getFilesTypeId);
wrapper.orderByDesc(CaseFiles::getCreateTime);
return this.list(wrapper);
}
@Override
public List getByCaseId(Integer caseId) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CaseFiles::getCaseId,caseId);
return this.list(wrapper);
}
@Override
public void deleteByCaseId(Integer caseId) {
List byCaseId = this.getByCaseId(caseId);
for (CaseFiles files : byCaseId) {
if(StringUtils.isNotBlank(files.getFilesUrl())){
String ossPath = files.getFilesUrl().replace(queryPath,"");
if(uploadToOssUtil.existKey(ossPath)){
uploadToOssUtil.delete(ossPath);
}
}
this.removeById(files.getFilesId());
}
}
}