//生成6位验证码
String code = RandomStringUtils.randomNumeric(6);
//遍历list,用函数式编程进行业务逻辑处理
parkStaff.getEnterpriseIds().forEach(enterpriseId ->{
// 判断同一企业下手机号是否存在
ParkStaff phoneCheck = new ParkStaff();
phoneCheck.setEnterpriseId(enterpriseId);
phoneCheck.setPhone(parkStaff.getPhone());
phoneCheck.setState("1");
List<ParkStaff> parkStaffs = selectParkStaffList(phoneCheck);
if(parkStaffs.size() == 0){
// 手机号不存在
// 添加数据
ParkStaff ps = new ParkStaff();
parkStaff.setEnterpriseId(enterpriseId);
List<ParkStaff> staffs = selectParkStaffList(ps);
List<String> collect = staffs.stream().map(ParkStaff::getCode).collect(Collectors.toList());
String code = null;
while (true){
int rado= (int)((Math.random()*9+1)*10000);
code = "800" + String.valueOf(rado);
if(!collect.contains(code)){
break;
}
}
parkStaff.setEnterpriseId(enterpriseId);
parkStaff.setCode(code);
parkStaff.setCreateTime(DateUtils.getNowDate());
parkStaffMapper.insertParkStaff(parkStaff);
}
});
//上传图片压缩
<dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version> </dependency>
/**
* 通用上传请求
*/
@PostMapping("/common/upload")
public AjaxResult uploadFile(MultipartFile file) throws Exception
{
try
{
MultipartFile oldMultipartFile = file;
// 上传文件路径
String filePath = RuoYiConfig.getUploadPath();
//上传图片的话就压缩
String oldfileName = file.getName();
String originalFilename = file.getOriginalFilename();
String fileType = originalFilename.substring(originalFilename.lastIndexOf("."));
if (".jpg".equalsIgnoreCase(fileType) || ".png".equalsIgnoreCase(fileType)) {
//计算缩放比例保留四位小数
double filesize=file.getSize();
double num=1;
if(filesize>1060000) {
num = 1024 * 1024 / filesize;
}
BigDecimal b1 = new BigDecimal(num);
double quality=b1.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
LocalDateTime now = LocalDateTime.now();
String absolutePath = filePath+"/"+oldfileName+"-tmp"+fileType;
File tempFile = new File(absolutePath);
Thumbnails.of(file.getInputStream())
.scale(1f) //图片大小(长宽)压缩比例 从0-1,1表示原图
.outputQuality(quality) //图片质量压缩比例 从0-1,越接近1质量越好
.toFile(tempFile);
FileInputStream fileInputStream = new FileInputStream(tempFile);
file = new MockMultipartFile(oldfileName, originalFilename, fileType, fileInputStream);
fileInputStream.close();
boolean success = tempFile.delete();
log.info("删除临时file success:{}", success);
}
//上传文件
String fileName = FileUploadUtils.upload(filePath, file);
// 上传并返回新文件名称
String url = serverConfig.getUrl() + fileName;
AjaxResult ajax = AjaxResult.success();
ajax.put("fileName", fileName);
ajax.put("url", url);
return ajax;
}
catch (Exception e)
{
return AjaxResult.error(e.getMessage());
}
}