数据库插入
This commit is contained in:
parent
3d4314e0dd
commit
02612ac95b
|
@ -0,0 +1,39 @@
|
|||
package com.wxjw.controller.openapi;
|
||||
|
||||
|
||||
import com.wxjw.common.BaseResponse;
|
||||
import com.wxjw.common.ResultUtil;
|
||||
import com.wxjw.dal.dao.ExcelInfoMapper;
|
||||
import com.wxjw.dal.pojo.ErrorCode;
|
||||
import com.wxjw.dal.pojo.data.insertTable.InsertTableData;
|
||||
import com.wxjw.service.InsertTableService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* Controller 插入数据内容
|
||||
*
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/openapi/files")
|
||||
public class InsertTableController {
|
||||
@Resource
|
||||
private ExcelInfoMapper excelInfoMapper;
|
||||
|
||||
@PostMapping("/insert")
|
||||
public ResponseEntity<BaseResponse<Object>> insertFunction(@RequestBody @NotNull InsertTableData resultBody) {
|
||||
if ("insert".equals(resultBody.getAction())) {
|
||||
InsertTableService insertTableService = new InsertTableService();
|
||||
insertTableService.insertLogic(resultBody, excelInfoMapper);
|
||||
return insertTableService.getOperationStatus();
|
||||
} else {
|
||||
return ResultUtil.error(ErrorCode.PARAMETER_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.wxjw.dal.pojo.data.insertTable;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author lfeng
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class InsertTableData {
|
||||
private String action;
|
||||
private long id;
|
||||
private long paren_id;
|
||||
private String file_name;
|
||||
private String sheet_name;
|
||||
private String table_name;
|
||||
private int type;
|
||||
private String create_by;
|
||||
private String create_time;
|
||||
private String update_by;
|
||||
private String update_time;
|
||||
}
|
65
src/main/java/com/wxjw/service/InsertTableService.java
Normal file
65
src/main/java/com/wxjw/service/InsertTableService.java
Normal file
|
@ -0,0 +1,65 @@
|
|||
package com.wxjw.service;
|
||||
|
||||
import com.wxjw.common.BaseResponse;
|
||||
import com.wxjw.common.ResultUtil;
|
||||
import com.wxjw.dal.dao.ExcelInfoMapper;
|
||||
import com.wxjw.dal.pojo.ErrorCode;
|
||||
import com.wxjw.dal.pojo.data.insertTable.InsertTableData;
|
||||
import com.wxjw.dal.pojo.entity.ExcelInfoEntity;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Service 插入数据内容
|
||||
*
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@Getter
|
||||
@Service
|
||||
public class InsertTableService {
|
||||
private ResponseEntity<BaseResponse<Object>> operationStatus;
|
||||
// 暂未设计
|
||||
@Getter
|
||||
private String validationErrorMessage = null;
|
||||
|
||||
public void insertLogic(InsertTableData resultBody, ExcelInfoMapper excelInfoMapper) {
|
||||
// 搜索数据是否存在
|
||||
ArrayList<ExcelInfoEntity> excelInfoList = (ArrayList<ExcelInfoEntity>) excelInfoMapper.getAllExcelFilesNameNoRepetition(resultBody.getFile_name());
|
||||
if (!excelInfoList.isEmpty()) {
|
||||
// 检查数据是否匹配
|
||||
excelInfoList.forEach(excelInfoEntity -> {
|
||||
if (Objects.equals(excelInfoEntity.getSheetName(), resultBody.getSheet_name())) {
|
||||
validationErrorMessage = "数据重复";
|
||||
operationStatus = ResultUtil.error(ErrorCode.DATA_DUPLICATION);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
// 执行插入
|
||||
if (validationErrorMessage == null) {
|
||||
// 插入数据
|
||||
ExcelInfoEntity excelInfoEntity = new ExcelInfoEntity();
|
||||
excelInfoEntity
|
||||
.setFileName(resultBody.getFile_name())
|
||||
.setSheetName(resultBody.getSheet_name())
|
||||
.setTableName(resultBody.getTable_name())
|
||||
.setType(resultBody.getType())
|
||||
.setCreateBy(resultBody.getCreate_by())
|
||||
.setUpdateBy(resultBody.getUpdate_by())
|
||||
.setCreateTime(resultBody.getCreate_time())
|
||||
.setUpdateTime(resultBody.getUpdate_time());
|
||||
if (excelInfoMapper.insertExcelInfo(excelInfoEntity)) {
|
||||
operationStatus = ResultUtil.success();
|
||||
} else {
|
||||
operationStatus = ResultUtil.error(ErrorCode.DATA_WRITE_FAILURE);
|
||||
}
|
||||
} else {
|
||||
operationStatus = ResultUtil.error(ErrorCode.DATA_DUPLICATION);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user