package com.example.demo.database.fdfsTjCopy1;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
@RestController
@RequestMapping("FdfsTjCopy1")
@Slf4j
public class FdfsTjCopy1Controller {
@Resource
FdfsTjCopy1ServiceImpl service;
@Resource
FastFileStorageClient fastFileStorageClient;
/**
* 已删除
*/
private static final Integer deleted = 1;
/**
* 未删除
*/
private static final Integer notDelete = 2;
@GetMapping("test")
public String test(){
LambdaQueryWrapper<FdfsTjCopy1> wrapper = new LambdaQueryWrapper<>();
wrapper.notExists("select 1 from temp_fastdfs_url where fdfs_tj_copy1.sort_path=temp_fastdfs_url.img_url");
wrapper.eq(FdfsTjCopy1::getIsDelete,notDelete);
wrapper.orderByDesc(FdfsTjCopy1::getCreateTime);
wrapper.last("limit 1");
// long count = service.count(wrapper);
List<FdfsTjCopy1> list = service.list(wrapper);
Integer sNum = 0;
Integer fNum = 0;
for (FdfsTjCopy1 fdfsTjCopy1 : list) {
try {
fastFileStorageClient.deleteFile(fdfsTjCopy1.getFdfsPath());
}catch (Exception e){
log.info("失败原因:{},失败路径:{},失败条目ID:{}",e.getMessage(),fdfsTjCopy1.getFdfsPath(),fdfsTjCopy1.getId());
fNum++;
continue;
}
fdfsTjCopy1.setIsDelete(deleted);
service.updateById(fdfsTjCopy1);
sNum++;
}
BigDecimal div = NumberUtil.mul(NumberUtil.div(new BigDecimal(sNum), new BigDecimal(list.size()), 4, RoundingMode.HALF_UP),new BigDecimal(100));
String result = StrUtil.format("本次删除结果:总数:{},成功数:{},失败数:{},成功率:{}%",list.size(),sNum,fNum,div.toString());
log.info(result);
return result;
}
}
评论0