Browse Source

角果识别后台

master
zhangyeguang 2 weeks ago
parent
commit
a720652ebc
  1. 6
      pom.xml
  2. 9
      src/main/java/com/jiagutech/common/exception/BizCode.java
  3. 2
      src/main/java/com/jiagutech/common/exception/GlobalExceptionHandler.java
  4. 8
      src/main/java/com/jiagutech/controller/RecordController.java
  5. 2
      src/main/java/com/jiagutech/entity/UserEntity.java
  6. 4
      src/main/java/com/jiagutech/record/RecordTaskManager.java
  7. 31
      src/main/java/com/jiagutech/record/RecordTaskProcessor.java
  8. 61
      src/main/java/com/jiagutech/service/RecordServiceImpl.java
  9. 1
      src/main/java/com/jiagutech/service/UserServiceImpl.java
  10. 25
      src/main/java/com/jiagutech/utils/RecognitionUtil.java

6
pom.xml

@ -84,6 +84,12 @@
<artifactId>mapstruct</artifactId> <artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version> <version>${mapstruct.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>com.alibaba.fastjson2</groupId> <groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId> <artifactId>fastjson2</artifactId>

9
src/main/java/com/jiagutech/common/exception/BizCode.java

@ -24,6 +24,10 @@ public enum BizCode {
FILE_UPLOAD_ERROR(90002, "文件上传失败"), FILE_UPLOAD_ERROR(90002, "文件上传失败"),
/**
* 服务器繁忙
*/
SERVER_BUSY(500, "服务器繁忙,请稍后再试"),
USER_NOT_FOUND(40004, "用户不存在"), USER_NOT_FOUND(40004, "用户不存在"),
@ -35,7 +39,7 @@ public enum BizCode {
TOKEN_TIMEOUT_ERROR(40006, "token已过期"), TOKEN_TIMEOUT_ERROR(40006, "token已过期"),
USER_STOP_FOUND(40008, "用户被禁用"), USER_STOP_FOUND(40008, "用户被禁用"),
USER_STATUS_ERROR(40009,"用户未审核通过"),
USER_STATUS_ERROR(40009, "用户未审核通过"),
USER_NOT_LOGIN(40011, "用户未登录,请登录后再操作"), USER_NOT_LOGIN(40011, "用户未登录,请登录后再操作"),
@ -43,12 +47,11 @@ public enum BizCode {
ACCESS_NOT_ALLOWABLE(50001, "服务不允许直接访问"), ACCESS_NOT_ALLOWABLE(50001, "服务不允许直接访问"),
PERMISSION_NOT_FOUND(40001,"该用户无操作权限"),
PERMISSION_NOT_FOUND(40001, "该用户无操作权限"),
USER_PHONE_EXIST(40012, "用户手机号已存在"), USER_PHONE_EXIST(40012, "用户手机号已存在"),
; ;

2
src/main/java/com/jiagutech/common/exception/GlobalExceptionHandler.java

@ -45,7 +45,7 @@ public class GlobalExceptionHandler {
@ExceptionHandler(BusinessException.class) @ExceptionHandler(BusinessException.class)
public ResponseEntity<R> handleBusinessException(BusinessException ex) { public ResponseEntity<R> handleBusinessException(BusinessException ex) {
return new ResponseEntity<>(R.fail(ex.getCode(), ex.getMessage()), HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(R.fail(ex.getCode(), ex.getMessage()), HttpStatus.OK);
} }
@ExceptionHandler(MethodArgumentTypeMismatchException.class) @ExceptionHandler(MethodArgumentTypeMismatchException.class)

8
src/main/java/com/jiagutech/controller/RecordController.java

@ -67,8 +67,9 @@ public class RecordController {
@ApiOperation(value = "更新识别记录内容") @ApiOperation(value = "更新识别记录内容")
@PutMapping("/updateRecordContent") @PutMapping("/updateRecordContent")
public void updateRecord(@RequestBody RecordContentEntity recordContentEntity) {
public R<Void> updateRecord(@RequestBody RecordContentEntity recordContentEntity) {
recordService.updateRecordContent(recordContentEntity); recordService.updateRecordContent(recordContentEntity);
return R.ok("操作成功");
} }
@ -80,13 +81,14 @@ public class RecordController {
@ApiOperation(value = "比例尺设置") @ApiOperation(value = "比例尺设置")
@PutMapping("/scale") @PutMapping("/scale")
public void updateScale(@RequestBody UpdateScaleRequest request) {
public R<Void> updateScale(@RequestBody UpdateScaleRequest request) {
recordService.updateScale(request); recordService.updateScale(request);
return R.ok("操作成功");
} }
@ApiOperation(value = "删除记录") @ApiOperation(value = "删除记录")
@DeleteMapping(value = "/delete/{recordId}") @DeleteMapping(value = "/delete/{recordId}")
public R<Void> deleteRecord(Long recordId) {
public R<Void> deleteRecord(@PathVariable Long recordId) {
boolean res = recordService.deleteRecord(recordId); boolean res = recordService.deleteRecord(recordId);
return res ? R.ok() : R.fail(); return res ? R.ok() : R.fail();
} }

2
src/main/java/com/jiagutech/entity/UserEntity.java

@ -24,7 +24,7 @@ public class UserEntity {
private int status; private int status;
private int role;
private Integer role;
} }

4
src/main/java/com/jiagutech/record/RecordTaskManager.java

@ -38,4 +38,8 @@ public class RecordTaskManager {
return taskItems; return taskItems;
} }
public static int taskCount() {
return recordTasks.size();
}
} }

31
src/main/java/com/jiagutech/record/RecordTaskProcessor.java

@ -7,6 +7,7 @@ import com.jiagutech.entity.RecordEntity;
import com.jiagutech.mapper.RecordContentMapper; import com.jiagutech.mapper.RecordContentMapper;
import com.jiagutech.mapper.RecordMapper; import com.jiagutech.mapper.RecordMapper;
import com.jiagutech.utils.HuaweiObs; import com.jiagutech.utils.HuaweiObs;
import com.jiagutech.utils.RecognitionUtil;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -15,8 +16,6 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.io.File; import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
@ -50,7 +49,7 @@ public class RecordTaskProcessor {
int rapeNum = 0; int rapeNum = 0;
int beakNum = 0; int beakNum = 0;
for (ModelPredictResponse item : predict) { for (ModelPredictResponse item : predict) {
item.setLength(getMaxLength(item.getPoints()));
item.setLength(RecognitionUtil.getMaxLength(item.getPoints()));
switch (item.getClassify()) { switch (item.getClassify()) {
case 0: case 0:
rapeNum++; rapeNum++;
@ -64,12 +63,12 @@ public class RecordTaskProcessor {
} }
progress += (float) (100 / recordTask.getFiles().length - 10); progress += (float) (100 / recordTask.getFiles().length - 10);
recordTask.setProgress(progress); recordTask.setProgress(progress);
if (!CollectionUtils.isEmpty(predict)) {
// if (!CollectionUtils.isEmpty(predict)) {
String imageUrl; String imageUrl;
try { try {
log.info("存储图片到oss,file:{}", file.getName()); log.info("存储图片到oss,file:{}", file.getName());
// imageUrl = huaweiObs.uploadFile(file);
imageUrl = "";
imageUrl = huaweiObs.uploadFile(file);
// imageUrl = "";
progress += 2f; progress += 2f;
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -88,29 +87,11 @@ public class RecordTaskProcessor {
recordContentMapper.insert(recordContentEntity); recordContentMapper.insert(recordContentEntity);
log.info("保存处理结果,file:{}", file.getName()); log.info("保存处理结果,file:{}", file.getName());
progress += 3f; progress += 3f;
}
}
// }
} }
private static double calculateDistance(int x1, int y1, int x2, int y2) {
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
} }
private float getMaxLength(int[][] points) {
// 计算四条边的长度
double ab = calculateDistance(points[0][0], points[0][1], points[1][0], points[1][1]); // AB
double bc = calculateDistance(points[1][0], points[1][1], points[2][0], points[2][1]); // BC
double cd = calculateDistance(points[2][0], points[2][1], points[3][0], points[3][1]); // CD
double da = calculateDistance(points[3][0], points[3][1], points[0][0], points[0][1]); // DA
double max = Math.max(Math.max(ab, bc), Math.max(cd, da));
BigDecimal b = new BigDecimal(max);
// 找出最大边长
return b.setScale(2, RoundingMode.HALF_UP).floatValue();
}
} }

61
src/main/java/com/jiagutech/service/RecordServiceImpl.java

@ -1,6 +1,7 @@
package com.jiagutech.service; package com.jiagutech.service;
import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.util.MapUtils;
@ -16,6 +17,8 @@ import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiagutech.client.RecognitionModelClient; import com.jiagutech.client.RecognitionModelClient;
import com.jiagutech.common.exception.BizCode;
import com.jiagutech.common.exception.BusinessException;
import com.jiagutech.dto.*; import com.jiagutech.dto.*;
import com.jiagutech.dto.common.LoginUser; import com.jiagutech.dto.common.LoginUser;
import com.jiagutech.dto.common.PageRequest; import com.jiagutech.dto.common.PageRequest;
@ -35,6 +38,8 @@ import com.jiagutech.record.RecordTaskHandler;
import com.jiagutech.record.RecordTaskManager; import com.jiagutech.record.RecordTaskManager;
import com.jiagutech.record.RecordTaskProcessor; import com.jiagutech.record.RecordTaskProcessor;
import com.jiagutech.utils.LoginUtil; import com.jiagutech.utils.LoginUtil;
import com.jiagutech.utils.RecognitionUtil;
import jdk.nashorn.internal.ir.IfNode;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.HorizontalAlignment;
@ -76,8 +81,17 @@ public class RecordServiceImpl implements RecordService {
return null; return null;
} }
/**
* 同时处理的任务数小于等于8个
*
* @param request
* @return
*/
@Override @Override
public String createRecordAsync(CreateRecordRequest request) { public String createRecordAsync(CreateRecordRequest request) {
if (RecordTaskManager.taskCount() >= 9) {
throw new BusinessException(BizCode.SERVER_BUSY);
}
String taskId = UUID.randomUUID().toString(); String taskId = UUID.randomUUID().toString();
RecordTaskHandler recordTaskHandler = new RecordTaskHandler(customThreadPool, taskProcessor, tmpPath); RecordTaskHandler recordTaskHandler = new RecordTaskHandler(customThreadPool, taskProcessor, tmpPath);
recordTaskHandler.createTask(request, taskId); recordTaskHandler.createTask(request, taskId);
@ -142,8 +156,13 @@ public class RecordServiceImpl implements RecordService {
List<ModelPredictResponse> recognitionData = record.getRecognitionData(); List<ModelPredictResponse> recognitionData = record.getRecognitionData();
Scale scaleDto = record.getScale(); Scale scaleDto = record.getScale();
//把识别数据进行分类 //把识别数据进行分类
float size = scaleDto.getSize();
int scale = scaleDto.getScale();
float size = 0;
int scale = 0;
if (scaleDto != null) {
size = scaleDto.getSize();
scale = scaleDto.getScale();
}
for (ModelPredictResponse item : recognitionData) { for (ModelPredictResponse item : recognitionData) {
float len; float len;
if (size > 0 && scale > 0) { if (size > 0 && scale > 0) {
@ -191,12 +210,14 @@ public class RecordServiceImpl implements RecordService {
} }
//创建单元格的合并策略, 前7列数据需要合并 //创建单元格的合并策略, 前7列数据需要合并
for (int i = 0; i <= 6; i++) { for (int i = 0; i <= 6; i++) {
if (endRowIndex > startRowIndex) {
OnceAbsoluteMergeStrategy mergeStrategy = new OnceAbsoluteMergeStrategy( OnceAbsoluteMergeStrategy mergeStrategy = new OnceAbsoluteMergeStrategy(
startRowIndex, startRowIndex,
endRowIndex, endRowIndex,
i, i); i, i);
mergeStrategies.add(mergeStrategy); mergeStrategies.add(mergeStrategy);
} }
}
startRowIndex += len; startRowIndex += len;
avgHandleLen.add(handleLenList.stream().mapToDouble(Float::doubleValue).average().orElse(0.0)); avgHandleLen.add(handleLenList.stream().mapToDouble(Float::doubleValue).average().orElse(0.0));
@ -294,6 +315,30 @@ public class RecordServiceImpl implements RecordService {
@Override @Override
public void updateRecordContent(RecordContentEntity recordContentEntity) { public void updateRecordContent(RecordContentEntity recordContentEntity) {
if (CollectionUtil.isNotEmpty(recordContentEntity.getRecognitionData())) {
int handleNum = 0;
int rapeNum = 0;
int beakNum = 0;
for (ModelPredictResponse item : recordContentEntity.getRecognitionData()) {
if (item.getLength() == null || item.getLength() == 0) {
item.setLength(RecognitionUtil.getMaxLength(item.getPoints()));
}
switch (item.getClassify()) {
case 0:
rapeNum++;
break;
case 1:
beakNum++;
break;
default:
handleNum++;
}
}
recordContentEntity.setHandleNum(handleNum)
.setBeakNum(beakNum)
.setSiliquaNum(rapeNum);
}
recordContentMapper.updateById(recordContentEntity); recordContentMapper.updateById(recordContentEntity);
} }
@ -314,9 +359,10 @@ public class RecordServiceImpl implements RecordService {
own.setScale(request.getScale()); own.setScale(request.getScale());
own.setScaleType(request.getScaleType()); own.setScaleType(request.getScaleType());
own.setId(request.getRecordContentId()); own.setId(request.getRecordContentId());
other = new RecordContentEntity().setScale(request.getScale());
other = new RecordContentEntity().setScaleType(3).setScale(request.getScale());
queryWrapper.eq("record_id", request.getRecordId()); queryWrapper.eq("record_id", request.getRecordId());
queryWrapper.eq("scale_type", 3);
queryWrapper.in("scale_type", 0, 1, 3);
queryWrapper.ne("id", request.getRecordContentId());
break; break;
case 2: case 2:
own = recordContentMapper.selectById(request.getRecordContentId()); own = recordContentMapper.selectById(request.getRecordContentId());
@ -330,14 +376,16 @@ public class RecordServiceImpl implements RecordService {
break; break;
case 3: case 3:
default: default:
own = new RecordContentEntity().setScaleType(3);
queryWrapper.eq("record_id", request.getRecordId()); queryWrapper.eq("record_id", request.getRecordId());
queryWrapper.eq("scale_type", 1); queryWrapper.eq("scale_type", 1);
RecordContentEntity recordContentEntity = recordContentMapper.selectOne(queryWrapper); RecordContentEntity recordContentEntity = recordContentMapper.selectOne(queryWrapper);
if (recordContentEntity != null) { if (recordContentEntity != null) {
own = new RecordContentEntity().setScaleType(3);
own.setScale(recordContentEntity.getScale()); own.setScale(recordContentEntity.getScale());
}
own.setId(request.getRecordContentId()); own.setId(request.getRecordContentId());
} else {
throw new BusinessException(BizCode.General_Failure, "当前无统一的比例尺");
}
} }
recordContentMapper.updateById(own); recordContentMapper.updateById(own);
@ -380,4 +428,5 @@ public class RecordServiceImpl implements RecordService {
} }
} }

1
src/main/java/com/jiagutech/service/UserServiceImpl.java

@ -61,6 +61,7 @@ public class UserServiceImpl implements UserService {
.setRealName(user.getRealName()) .setRealName(user.getRealName())
.setUnitName(user.getUnitName()) .setUnitName(user.getUnitName())
.setPhone(user.getPhone()) .setPhone(user.getPhone())
.setRole(0)
.setPassword(BCrypt.hashpw(user.getPassword())); .setPassword(BCrypt.hashpw(user.getPassword()));
userMapper.insert(userEntity); userMapper.insert(userEntity);
emailUtil.sendEmail(null, user.getPhone(), user.getUnitName(), user.getRealName()); emailUtil.sendEmail(null, user.getPhone(), user.getUnitName(), user.getRealName());

25
src/main/java/com/jiagutech/utils/RecognitionUtil.java

@ -0,0 +1,25 @@
package com.jiagutech.utils;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class RecognitionUtil {
private static double calculateDistance(int x1, int y1, int x2, int y2) {
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
}
public static float getMaxLength(int[][] points) {
// 计算四条边的长度
double ab = calculateDistance(points[0][0], points[0][1], points[1][0], points[1][1]); // AB
double bc = calculateDistance(points[1][0], points[1][1], points[2][0], points[2][1]); // BC
double cd = calculateDistance(points[2][0], points[2][1], points[3][0], points[3][1]); // CD
double da = calculateDistance(points[3][0], points[3][1], points[0][0], points[0][1]); // DA
double max = Math.max(Math.max(ab, bc), Math.max(cd, da));
BigDecimal b = new BigDecimal(max);
// 找出最大边长
return b.setScale(2, RoundingMode.HALF_UP).floatValue();
}
}
Loading…
Cancel
Save