Merge remote-tracking branch 'origin/main'

This commit is contained in:
liuzhaotian
2024-05-14 16:05:47 +08:00
12 changed files with 419 additions and 210 deletions
@@ -1,10 +1,10 @@
package com.cf.imes.module.executor.controller.admin.order;
import com.alibaba.excel.EasyExcel;
import com.cf.imes.module.executor.controller.admin.order.vo.order.*;
import com.cf.imes.module.executor.controller.admin.order.vo.product.OrderBodyRespVO;
import com.cf.imes.module.executor.controller.admin.orderParts.vo.OrderPartsRespVO;
import com.cf.imes.module.executor.util.fileConversion.admin.files.excel.OrderExcelUtil;
import com.cf.imes.module.executor.dal.dataobject.goods.GoodsDO;
import com.cf.imes.module.executor.util.fileConversion.admin.files.excel.ExcelReadUtil;
import com.cf.imes.module.executor.util.fileConversion.admin.files.excel.OrderPlateImportExcelVO;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.util.ResourceUtils;
@@ -58,7 +58,7 @@ public class OrderController {
private OrderService orderService;
@Resource
private OrderExcelUtil orderExcelUtil;
private ExcelReadUtil orderExcelUtil;
private static final Logger log = LoggerFactory.getLogger(OrderController.class);
@@ -271,7 +271,6 @@ public class OrderController {
return success(BeanUtils.toBean(orderService.getBody(orderId), OrderBodyRespVO.class));
}
@GetMapping("getApiData")
@Operation(summary = "生产单api数据导入新增")
@PreAuthorize("@ss.hasPermission('executor:order:getApiData')")
@@ -279,8 +278,6 @@ public class OrderController {
return success(orderService.importApiData(orderNo));
}
@PostMapping("/importFile")
@Operation(summary = "生产单文件导入新增")
@Parameters({
@@ -296,9 +293,13 @@ public class OrderController {
List<?> list = new ArrayList<>();
if (type == 0) { //excel
// 读取文件
orderExcelUtil.read(file, OrderPlateImportExcelVO.class,0);
// List<OrderPlateImportExcelVO> orderPlateImportExcelVOS = orderExcelUtil.read(file, OrderPlateImportExcelVO.class,0);
// list = orderPlateImportExcelVOS;
// OrderDO orderDO = orderExcelUtil.changeOrderDO(file).setOrderType(0);
List<OrderPlateImportExcelVO> orderPlateImportExcelVOS = orderExcelUtil.read(file, OrderPlateImportExcelVO.class,7);
list = orderPlateImportExcelVOS;
System.out.println(list);
} else if (type == 1) { // json
}else if (type == 2) { // xml
@@ -306,4 +307,12 @@ public class OrderController {
}
}
@GetMapping("/get-goodsList")
@Operation(summary = "生产单所用板材商品信息")
@Parameter(name = "orderId", description = "生产单编号", required = true, example = "1024")
@PreAuthorize("@ss.hasPermission('executor:order:query')")
public CommonResult<List<GoodsDO>> getGoodsList(@RequestParam("orderId") Long orderId) {
List<GoodsDO> goodsDOS = orderService.getGoodsList(orderId);
return success(goodsDOS);
}
}
@@ -1,6 +1,7 @@
package com.cf.imes.module.executor.dal.mysql.order;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.cf.imes.framework.common.pojo.PageResult;
@@ -89,4 +90,9 @@ public interface OrderMapper extends BaseMapperX<OrderDO> {
}
Map<String, Object> selectDynamicSqlString(@Param("sqlStr") String sqlStr);
default Long selectCountByOrderNo(@Param("CustomOrderNo") String CustomOrderNo){
return selectCount(new LambdaQueryWrapper<OrderDO>().eq(OrderDO::getCustomOrderNo, CustomOrderNo));
}
}
@@ -6,10 +6,12 @@ import com.cf.imes.module.executor.controller.admin.order.vo.order.*;
import com.cf.imes.module.executor.controller.admin.order.vo.product.OrderBodyRespVO;
import com.cf.imes.module.executor.controller.admin.orderParts.vo.OrderPartsRespVO;
import com.cf.imes.module.executor.controller.admin.plate.vo.PlateRespVO;
import com.cf.imes.module.executor.dal.dataobject.goods.GoodsDO;
import com.cf.imes.module.executor.dal.dataobject.order.OrderDO;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.module.executor.dal.dataobject.orderBody.OrderBodyDO;
import com.cf.imes.module.executor.dal.dataobject.orderModuleExtra.OrderModuleExtraDO;
import com.cf.imes.module.executor.dal.dataobject.plate.PlateDO;
import java.util.Collection;
import java.util.List;
@@ -125,4 +127,10 @@ public interface OrderService {
* @date 2024/3/30
*/
Long importApiData(String orderNo);
/**
* @param orderId: 生产单Id
* @return List<GoodsDO>
*/
List<GoodsDO> getGoodsList(Long orderId);
}
@@ -1,9 +1,12 @@
package com.cf.imes.module.executor.service.order;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import cn.smallbun.screw.core.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.cf.imes.framework.organ.core.context.OrganContextHolder;
import com.cf.imes.module.executor.controller.admin.order.vo.order.*;
import com.cf.imes.framework.datapermission.core.util.DataPermissionUtils;
import com.cf.imes.module.executor.controller.admin.order.vo.product.OrderBodyRespVO;
@@ -19,6 +22,7 @@ import com.cf.imes.module.executor.dal.dataobject.ordermodel.OrderModelDO;
import com.cf.imes.module.executor.dal.dataobject.plate.PlateDO;
import com.cf.imes.module.executor.dal.dataobject.plate.PlateGoodDO;
import com.cf.imes.module.executor.dal.dataobject.rawgoods.RawGoodsDO;
import com.cf.imes.module.executor.dal.mysql.goods.GoodsMapper;
import com.cf.imes.module.executor.dal.mysql.orderBody.OrderBodyMapper;
import com.cf.imes.module.executor.dal.mysql.orderGroup.OrderGroupMapper;
import com.cf.imes.module.executor.dal.mysql.orderItem.OrderItemMapper;
@@ -73,6 +77,9 @@ public class OrderServiceImpl implements OrderService {
@Resource
private RawGoodsMapper rawGoodsMapper;
@Resource
private GoodsMapper goodsMapper;
@Resource
private OrderPartsMapper orderPartsMapper;
@@ -114,7 +121,7 @@ public class OrderServiceImpl implements OrderService {
private static final int DEFAULT_DAYS_TO_ADD = 30;
private static final int PARTS_PAGE_MAX = 1000;
private static final int PARTS_PAGE_MAX = 10;
@Override
public Long createOrder(OrderSaveReqVO createReqVO) {
@@ -205,7 +212,7 @@ public class OrderServiceImpl implements OrderService {
}
@Override
public Map<String,List<?>> getModule(Long orderId) {
public Map<String, List<?>> getModule(Long orderId) {
// 校验存在
validateOrderExists(orderId);
// 模块信息
@@ -213,9 +220,9 @@ public class OrderServiceImpl implements OrderService {
// 配件
List<OrderPlatesDetailRespVO> orderItemDOS = orderItemMapper.selectPartsByOrderId(orderId);
Map<String,List<?>> map = new HashMap<>();
map.put("module",orderModuleRespVOList);
map.put("parts",orderItemDOS);
Map<String, List<?>> map = new HashMap<>();
map.put("module", orderModuleRespVOList);
map.put("parts", orderItemDOS);
return map;
}
@@ -226,8 +233,8 @@ public class OrderServiceImpl implements OrderService {
orderDetail.forEach(orderPlatesDetailRespVO -> {
OrderPlatesDetailReqVO orderPlatesDetailReqVO = BeanUtils.toBean(orderPlatesDetailRespVO, OrderPlatesDetailReqVO.class)
.setIsHole(false);
if (orderPlatesDetailRespVO.getUnregularPointCount()>0 || orderPlatesDetailRespVO.getFrontHoleCount()>0
|| orderPlatesDetailRespVO.getBackHoleCount()>0 || orderPlatesDetailRespVO.getSideHoleCount()>0)
if (orderPlatesDetailRespVO.getUnregularPointCount() > 0 || orderPlatesDetailRespVO.getFrontHoleCount() > 0
|| orderPlatesDetailRespVO.getBackHoleCount() > 0 || orderPlatesDetailRespVO.getSideHoleCount() > 0)
orderPlatesDetailReqVO.setIsHole(true);
platesDetailReqVOS.add(orderPlatesDetailReqVO);
});
@@ -295,7 +302,7 @@ public class OrderServiceImpl implements OrderService {
JSONObject dataParts = new JSONObject();
for (int i = 1; i <= parts.getJSONObject("value").getInteger("PageCount"); i++) {
jsonParts.put("curr_page", i);
JSONObject value = apiOrderService.getApiOrderPartsMessage(token, jsonParts).getJSONObject("value");
JSONObject value = apiOrderService.getApiOrderPartsMessage(token, jsonParts);
dataParts = mergeJSONObjects(dataParts, value);
}
// 柜体信息转换
@@ -310,7 +317,7 @@ public class OrderServiceImpl implements OrderService {
JSONObject dataBody = new JSONObject();
for (int i = 1; i <= body.getJSONObject("value").getInteger("PageCount"); i++) {
jsonParts.put("curr_page", i);
JSONObject value = apiOrderService.getApiOrderBodyMessage(token, jsonBody).getJSONObject("value");
JSONObject value = apiOrderService.getApiOrderBodyMessage(token, jsonBody);
dataBody = mergeJSONObjects(dataBody, value);
}
// 商品信息转换
@@ -319,14 +326,13 @@ public class OrderServiceImpl implements OrderService {
jsonGoods.put("page_count", PARTS_PAGE_MAX);
jsonGoods.put("order_no", orderNo);
JSONObject goods = apiOrderService.getApiOrderGoodsMessage(token, jsonGoods);
System.err.println("err_code " + goods.getString("err_code").equals("0"));
if (!goods.getString("err_code").equals("0")) {
throw exception(GOODS_DATA_ERROR);
}
JSONObject dataGoods = new JSONObject();
for (int i = 1; i <= goods.getJSONObject("value").getInteger("PageCount"); i++) {
jsonParts.put("curr_page", i);
JSONObject value = apiOrderService.getApiOrderGoodsMessage(token, jsonGoods).getJSONObject("value");
JSONObject value = apiOrderService.getApiOrderGoodsMessage(token, jsonGoods);
dataGoods = mergeJSONObjects(dataGoods, value);
}
// 板材信息转换
@@ -334,14 +340,14 @@ public class OrderServiceImpl implements OrderService {
jsonPlates.put("curr_page", 1);
jsonPlates.put("page_count", PARTS_PAGE_MAX);
jsonPlates.put("order_no", orderNo);
JSONObject plates = apiOrderService.getApiBlocksDataMessage(token, jsonGoods);
JSONObject plates = apiOrderService.getApiBlocksDataMessage(token, jsonPlates);
if (!plates.getString("err_code").equals("0")) {
throw exception(PLATE_DATA_ERROR);
}
JSONObject plate = new JSONObject();
for (int i = 1; i <= plates.getJSONObject("value").getInteger("PageCount"); i++) {
jsonParts.put("curr_page", i);
JSONObject value = apiOrderService.getApiBlocksDataMessage(token, jsonGoods).getJSONObject("value");
jsonPlates.put("curr_page", i);
JSONObject value = apiOrderService.getApiBlocksDataMessage(token, jsonPlates);
plate = mergeJSONObjects(plate, value);
}
// 加工组信息转换
@@ -349,12 +355,12 @@ public class OrderServiceImpl implements OrderService {
jsonModule.put("order_no", orderNo);
JSONObject dataModule = apiOrderService.getApiModuleTypeDataMessage(token, jsonGoods);
// 转换
// System.out.println(" jsonPlatesData " + dataPlates.getJSONObject("value")+ "\n"
// + " dataParts " + dataParts+ "\n"
// + " dataBody " + dataBody+ "\n"
// + " dataGoods " + dataGoods+ "\n"
// + " plate " + plate+ "\n"
// + " dataModule " + dataModule);
System.out.println(" jsonPlatesData " + dataPlates.getJSONObject("value")+ "\n"
+ " dataParts " + dataParts+ "\n"
+ " dataBody " + dataBody+ "\n"
+ " dataGoods " + dataGoods+ "\n"
+ " plate " + plate+ "\n"
+ " dataModule " + dataModule);
Map<String, List<?>> listMap = apiTypeRealize.apiPlateDataChange(
dataPlates.getJSONObject("value"),//板件生产信息
dataParts,// 配件信息
@@ -375,9 +381,21 @@ public class OrderServiceImpl implements OrderService {
Map<String, List<?>> map = getPlateGoods(goodsDO);
goodsDO = (List<GoodsDO>) map.get("goodsDO");
List<PlateGoodDO> plateGoodDOS = (List<PlateGoodDO>) map.get("plateGoodDOLists");
validateCustomOrderNoExists(order.get(0).getCustomOrderNo());
orderInputProcessor.batchInsert(rawGoodsDO, bodyDO, groupDO, partsDO, plateDO, null, itemDO, order.get(0),goodsDO,plateGoodDOS);
orderInputProcessor.batchSaveModel(BeanUtils.toBean(plateDetail, OrderModelDO.class));
List<OrderModelDO> orderModelDOs = BeanUtils.toBean(plateDetail, OrderModelDO.class);
orderModelDOs.forEach(orderModelDO -> orderModelDO.setOrderId(order.get(0).getId()));
orderInputProcessor.batchSaveModel(orderModelDOs);
// System.out.println("plateDO = " + plateDO+ " BeanUtils.toBean(plateDetail, OrderModelDO.class) = " + orderModelDOs);
return order.get(0).getId();
// return null;
}
@Override
public List<GoodsDO> getGoodsList(Long orderId) {
return goodsMapper.selectList(new LambdaQueryWrapper<GoodsDO>()
.eq(GoodsDO::getOrderId, orderId)
.eq(GoodsDO::getDeleted, false));
}
private Map<String, List<?>> getPlateGoods(List<GoodsDO> goodsLists) {
@@ -385,7 +403,7 @@ public class OrderServiceImpl implements OrderService {
List<PlateGoodDO> plateGoodLists = new ArrayList<>();
for (GoodsDO goodsDO : goodsLists) {
if (goodsDO.getGoodsId() != null){ // 有goodsId
if (goodsDO.getGoodsId() != null) { // 有goodsId
PlateGoodDO plateGoodDO = plateGoodMapper.selectOne(PlateGoodDO::getGoodsId, goodsDO.getGoodsId());
if (plateGoodDO != null) {//存在
goodsDO.setGoodsId(plateGoodDO.getId());
@@ -405,18 +423,54 @@ public class OrderServiceImpl implements OrderService {
}
// 合并两个 JSON 对象的方法
// public JSONObject mergeJSONObjects(JSONObject object1, JSONObject object2) {
// if (object1 == null) {
// object1.putAll( object2.getJSONObject("value"));
// } else {
//// System.out.println("object2 " + object2);
//// System.out.println(" object2.getJSONObject(value) " + object2.getJSONObject("value"));
// if (object2 != null)
// object1.putAll( object2.getJSONObject("value"));
// }
// System.out.println(" object1 " + object1);
// return object1;
// }
public JSONObject mergeJSONObjects(JSONObject object1, JSONObject object2) {
Object value2 = object2.get("List");
if (object1.getJSONArray("List") == null) {
object1.put("value", object2);
} else {
if (value2 != null)
object1.getJSONArray("List").add(value2);
// 创建新对象
JSONObject mergedObj = new JSONObject();
// 合并 List 属性
JSONArray mergedList = new JSONArray();
JSONArray list1 = null;
if (object1.getJSONObject("value") != null) {
list1 = object1.getJSONObject("value").getJSONArray("List");
if (list1 != null) {
for (int i = 0; i < list1.size(); i++) {
mergedList.add(list1.getJSONObject(i));
}
}
}
if (object1.getJSONArray("List") != null) {
list1 = object1.getJSONArray("List");
if (list1 != null) {
for (int i = 0; i < list1.size(); i++) {
mergedList.add(list1.getJSONObject(i));
}
}
}
JSONArray list2 = object2.getJSONObject("value").getJSONArray("List");
if (list2 != null) {
for (int i = 0; i < list2.size(); i++) {
mergedList.add(list2.getJSONObject(i));
}
}
return object1;
}
mergedObj.put("List", mergedList);
return mergedObj;
}
private void validateOrderBodyExists(Long orderId, Long bodyId) {
if (orderBodyMapper.selectCount(new LambdaQueryWrapper<OrderBodyDO>().eq(OrderBodyDO::getOrderId, orderId).eq(OrderBodyDO::getId, bodyId)) == 0) {
@@ -447,7 +501,10 @@ public class OrderServiceImpl implements OrderService {
// 验证自定义单号是否存在
private void validateCustomOrderNoExists(String customOrderNo) {
if (orderMapper.selectOne("custom_order_no", customOrderNo) != null) {
if (orderMapper.selectCount(new LambdaQueryWrapper<OrderDO>()
.eq(OrderDO::getCustomOrderNo, customOrderNo)
.eq(OrderDO::getDeleted, false)
.eq(OrderDO::getOrganId, OrganContextHolder.getOrganId())) > 0) {
throw exception(CUSTOM_ORDER_EXISTS);
}
}
@@ -242,9 +242,9 @@ public class ApiTypeRealize {
Map<Integer, RawGoodsDO> rawGoodsDOS = new HashMap<>();
Map<Integer, GoodsDO> goodsDOS = new HashMap<>();
if (dataRawGoods.getJSONObject("value").getJSONArray("List") != null &&
!dataRawGoods.getJSONObject("value").getJSONArray("List").isEmpty()) {
JSONArray goodsLists = dataRawGoods.getJSONObject("value").getJSONArray("List");
if (dataRawGoods.getJSONArray("List") != null &&
!dataRawGoods.getJSONArray("List").isEmpty()) {
JSONArray goodsLists = dataRawGoods.getJSONArray("List");
for (int i = 0; i < goodsLists.size(); i++) {
JSONObject goods = goodsLists.getJSONObject(i);
@@ -298,9 +298,9 @@ public class ApiTypeRealize {
private Map<Integer, OrderBodyDO> bodyInfoChange(JSONObject dataBody, Long orderId) {
Map<Integer, OrderBodyDO> bodyInfos = new HashMap<>();
if (dataBody.getJSONObject("value").getJSONArray("List") != null &&
!dataBody.getJSONObject("value").getJSONArray("List").isEmpty()) {
JSONArray bodyLists = dataBody.getJSONObject("value").getJSONArray("List");
if (dataBody.getJSONArray("List") != null &&
!dataBody.getJSONArray("List").isEmpty()) {
JSONArray bodyLists = dataBody.getJSONArray("List");
for (int i = 0; i < bodyLists.size(); i++) {
JSONObject body = bodyLists.getJSONObject(i);
OrderBodyDO bodyInfo = OrderBodyDO.builder()
@@ -354,7 +354,7 @@ public class ApiTypeRealize {
Map<Integer, OrderItemDO> partsItem = new HashMap<>();
if (value != null && !value.isEmpty()) {
JSONArray partsLists = value.getJSONObject("value").getJSONArray("List");
JSONArray partsLists = value.getJSONArray("List");
for (int i = 0; i < partsLists.size(); i++) {
JSONObject parts = partsLists.getJSONObject(i);
Long partsId = (Long) identifierGenerator.nextId(null);
@@ -411,9 +411,8 @@ public class ApiTypeRealize {
Map<Integer, PlateDO> platesInfos = new HashMap<>();
Map<Integer, OrderItemDO> platesItem = new HashMap<>();
if (dataPlates.getJSONObject("value") != null &&
!dataPlates.getJSONObject("value").isEmpty()) {
JSONArray plateLists = dataPlates.getJSONObject("value").getJSONArray("List");
if (dataPlates != null && !dataPlates.isEmpty()) {
JSONArray plateLists = dataPlates.getJSONArray("List");
for (int i = 0; i < plateLists.size(); i++) {
JSONObject plate = plateLists.getJSONObject(i);
@@ -41,10 +41,10 @@ public final class ExcelListener<T> extends AnalysisEventListener<OrderPlateImpo
flag = false;
}
if (data.getGoodType() == 1) { //板材
if (!data.isValidRawGoods()) {
data.setResult("板材信息填写有误");
flag = false;
}
// if (!data.isValidRawGoods()) {
// data.setResult("板材信息填写有误");
// flag = false;
// }
}
}
datas.add(data);
@@ -0,0 +1,212 @@
package com.cf.imes.module.executor.util.fileConversion.admin.files.excel;
import com.alibaba.excel.EasyExcel;
import com.alibaba.nacos.common.utils.DateFormatUtils;
import com.cf.imes.module.executor.dal.dataobject.order.OrderDO;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
@Slf4j
@Component
public class ExcelReadUtil { // 文件读取
private String value;
private ExcelListener<OrderPlateImportExcelVO> excelListener = new ExcelListener();
public List<OrderPlateImportExcelVO> read(MultipartFile file, Class<OrderPlateImportExcelVO> head, Integer index) throws IOException {
if (file.isEmpty()) {
value = "文件为空";
throw new IOException("文件为空");
} else if (!file.getOriginalFilename().endsWith(".xlsx")) {
value = "文件格式不正确";
throw new IOException("文件格式不正确");
} else if (file.getSize() > 1024 * 1024 * 10) {
value = "文件大小超过 10M";
throw new IOException("文件大小超过 10M");
} else if (file.getSize() == 0) {
value = "文件大小为 0";
throw new IOException("文件大小为 0");
} else {
List<OrderPlateImportExcelVO> list = new ArrayList<>();
EasyExcel.read(file.getInputStream(), excelListener).sheet().doRead();
return list;
}
}
// // 获取单元格数据
// public Map<Object, Object> changeValue(InputStream file) throws IOException {
// Map<Object, Object> map = new HashMap<>();
//
// //获取工作簿
// XSSFWorkbook book = new XSSFWorkbook(file);
// //获取工作表
// XSSFSheet sheet = book.getSheetAt(0);
// int lastRowNum = sheet.getLastRowNum();
//
// List<Object> objects = new ArrayList<>();
// Map<Object, Object> objectMap = new HashMap<>();
// List<List<Object>> objList = new ArrayList<>();
// boolean index = true;
//int a = 0;
// for (int i = 0; i < lastRowNum; i++) {
// //获取单元格
// XSSFRow row = sheet.getRow(i);
// if (row != null) {
// for (Cell cell : row) {
// if (cell != null && !"".equals(cell)) {
//
// Object result = getValue(cell);
//
// if (!result.equals("")) {
// objects.add(result);
// }
// }
// }
// if (objects.get(0).equals("###"))
// index = false;
// if (objects.get(0).equals("***"))
// break;
//
//// System.out.println(objects);
// if (index){
// for (int j = 0; j < objects.size() - 1; j++) {
// if (isEven(j)) {//偶数
// objectMap.put(objects.get(j), objects.get(j + 1));
// }
// }
// }else {
// System.out.println(objects.get(0) + ":" +objects);
// a++;
//// System.out.println(objects);
// objList.add(objects);
// System.err.println(objList);
// }
//
//
// objects.clear();
// }
// }
//// System.out.println(a);
//// System.out.println(objectMap);
//// System.err.println(objList);
//
// return map;
// }
public OrderDO changeOrderDO(MultipartFile file) throws IOException {
OrderDO orderDO = new OrderDO();
//获取工作簿
XSSFWorkbook book = new XSSFWorkbook(file.getInputStream());
//获取工作表
XSSFSheet sheet = book.getSheetAt(0);
int lastRowNum = sheet.getLastRowNum();
List<Object> objects = new ArrayList<>();
Map<Object, Object> objectMap = new HashMap<>();
for (int i = 0; i < lastRowNum; i++) {
//获取单元格
XSSFRow row = sheet.getRow(i);
if (row != null) {
for (Cell cell : row) {
if (cell != null && !"".equals(cell)) {
Object result = getValue(cell);
if (!result.equals(""))
objects.add(result);
}
}
for (int j = 0; j < objects.size() - 1; j++) {
if (isEven(j)) {//偶数
objectMap.put(objects.get(j), objects.get(j + 1));
}
}
if (objects.get(0).equals("###"))
break;
}
}
String s = (String) objectMap.get("出货日期");
orderDO.setCustomOrderNo((String) objectMap.get("自定义单号"))
.setCustomer((String) objectMap.get("客户"))
.setDealer((String) objectMap.get("经销商"))
.setDealerPhoneNumber((String) objectMap.get("经销商电话"))
.setAddress((String) objectMap.get("客户地址"))
.setPhoneNumber((String) objectMap.get("客户电话"))
.setSalesman((String) objectMap.get("业务员"))
.setDeliveryDate(changeTime(s))
.setSplitter((String) objectMap.get("拆单员"))
.setRemark((String) objectMap.get("备注"));
System.out.println(orderDO);
return orderDO;
}
// 数据判断
private Object getValue(Cell cell) {
Object result = null;
switch (cell.getCellType()) {
case NUMERIC: //数字
result = cell.getNumericCellValue();
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
result = DateFormatUtils.format(date, "yyyy-MM-dd'T'HH:mm:ss");
} else {
result = cell.getNumericCellValue();
DecimalFormat df = new DecimalFormat("0");
result = df.format(result);
}
break;
case BOOLEAN: //Boolean
result = cell.getBooleanCellValue();
break;
case ERROR: //故障
result = cell.getErrorCellValue();
break;
case FORMULA: //公式
result = cell.getCellFormula();
break;
case BLANK: //空值
result = "";
break;
default: //字符串
result = cell.getStringCellValue();
}
return result;
}
private LocalDateTime changeTime(String dateString) {
// 使用 DateTimeFormatter 解析字符串为 LocalDateTime 对象
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter);
// 打印解析后的 LocalDateTime 对象
System.out.println("Parsed LocalDateTime: " + dateTime);
return dateTime;
}
private boolean isEven(int num) {
return num % 2 == 0;
}
}
@@ -1,68 +0,0 @@
package com.cf.imes.module.executor.util.fileConversion.admin.files.excel;
import com.alibaba.excel.EasyExcel;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.module.executor.util.FileTypeChangeUtil;
import com.cf.imes.module.executor.util.deviseData.Detail;
import com.cf.imes.module.executor.util.deviseData.IBoardProdInfo;
import com.cf.imes.module.system.api.dict.DictDataApi;
import com.cf.imes.module.system.enums.DictTypeConstants;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 实现数据格式转换
*/
@Service
@Validated
public class FileTypeRealize{
@Resource
private DictDataApi dictDataApi;
@Resource
private OrderExcelUtil orderExcelUtil;
private Boolean flag = true;
public List<?> fileDataChange(MultipartFile file) throws IOException {
List<OrderPlateImportExcelVO> orderPlateImportExcelVOS = orderExcelUtil.read(file, OrderPlateImportExcelVO.class,7);
// 判断上传数据解析是否有误
if (!orderExcelUtil.getFlag()){
this.flag = false;
System.out.println(orderPlateImportExcelVOS);
orderExcelUtil.clear();
return orderPlateImportExcelVOS;
}
List<Detail> details = new ArrayList<>();
System.out.println(orderPlateImportExcelVOS);
for (OrderPlateImportExcelVO orderPlateImportExcelVO : orderPlateImportExcelVOS) {
orderPlateImportExcelVO.setRemark(orderPlateImportExcelVO.remarkJSON());
if(dictDataApi.getDictData(DictTypeConstants.SYNTHESIS_TYPE,String.valueOf(orderPlateImportExcelVO.getSynthesis())).getData() != null){
String synthesisTypeName = dictDataApi.getDictData(DictTypeConstants.SYNTHESIS_TYPE,String.valueOf(orderPlateImportExcelVO.getSynthesis())).getData().getLabel();
orderPlateImportExcelVO.setSynthesisTypeName(synthesisTypeName);
}
Detail detail = Detail.builder()
.iBoardProdInfo(BeanUtils.toBean(orderPlateImportExcelVO, IBoardProdInfo.class))
.build();
details.add(detail);
}
orderExcelUtil.clear();
return details;
}
public boolean getFlag() {
return this.flag;
}
}
@@ -1,58 +0,0 @@
package com.cf.imes.module.executor.util.fileConversion.admin.files.excel;
import com.alibaba.excel.EasyExcel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.*;
@Slf4j
@Component
public class OrderExcelUtil { // 文件读取
private String value;
private ExcelListener<OrderPlateImportExcelVO> excelListener = new ExcelListener<>();
public List<OrderPlateImportExcelVO> read(MultipartFile file, Class<OrderPlateImportExcelVO> head, Integer index) throws IOException {
if (file.isEmpty()) {
value = "文件为空";
throw new IOException("文件为空");
} else if (!file.getOriginalFilename().endsWith(".xlsx")) {
value = "文件格式不正确";
throw new IOException("文件格式不正确");
} else if (file.getSize() > 1024 * 1024 * 10) {
value = "文件大小超过 10M";
throw new IOException("文件大小超过 10M");
} else if (file.getSize() == 0) {
value = "文件大小为 0";
throw new IOException("文件大小为 0");
} else {
//读取文件内容
EasyExcel.read(file.getInputStream(), head, excelListener).
headRowNumber(index).sheet(0).doRead();
//获取读取的数据
List<OrderPlateImportExcelVO> list = excelListener.getDatas();
if (excelListener.getValueMap().size() > 0)
excelListener.getValueMap().forEach((k, v) -> {
list.get(k - (index + 1)).setResult(v);
});
return list;
}
}
public boolean getFlag() {
return excelListener.getFlag();
}
public void clear() {
excelListener.getDatas().clear();
}
}
@@ -1,5 +1,6 @@
package com.cf.imes.module.executor.util.fileConversion.admin.files.excel;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.cf.imes.framework.excel.core.annotations.DictFormat;
import com.cf.imes.framework.excel.core.convert.DictConvert;
@@ -13,6 +14,7 @@ import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
/**
* 生产单 Excel 导入 VO
@@ -82,6 +84,19 @@ public class OrderPlateImportExcelVO {
@JsonIgnore
private String cabinetsName;
@ExcelProperty(value = "加工组类型", converter = DictConvert.class)
@JsonIgnore
@DictFormat(DictTypeConstants.SYNTHESIS_TYPE)
private int synthesis;
@JsonIgnore
@ExcelIgnore
private String synthesisTypeName;//加工组类型的名称
@ExcelProperty(value = "加工组名称")
@JsonIgnore
private String combinationName;
@ExcelProperty(value = "板名称")
@JsonIgnore
private String name;
@@ -90,45 +105,41 @@ public class OrderPlateImportExcelVO {
@JsonIgnore
private String plateNo;
@ExcelProperty(value = "开料")
@ExcelProperty(value = "成品")
@JsonIgnore
private float splitHeight;
private BigDecimal height;
@ExcelProperty(value = "开料")
@ExcelProperty(value = "成品")
@JsonIgnore
private float splitWidth;
private BigDecimal width;
@ExcelProperty(value = "拆单长")
@JsonIgnore
private BigDecimal splitHeight;
@ExcelProperty(value = "拆单宽")
@JsonIgnore
private BigDecimal splitWidth;
@ExcelProperty(value = "")
@JsonIgnore
private float splitThickness;
private BigDecimal splitThickness;
@ExcelProperty(value = "左封边")
@JsonIgnore
private float sealLeft;
private BigDecimal sealLeft;
@ExcelProperty(value = "右封边")
@JsonIgnore
private float sealRight;
private BigDecimal sealRight;
@ExcelProperty(value = "上封边")
@JsonIgnore
private float sealUp;
private BigDecimal sealUp;
@ExcelProperty(value = "下封边")
@JsonIgnore
private float sealDown;
@ExcelProperty(value = "加工组类型", converter = DictConvert.class)
@JsonIgnore
@DictFormat(DictTypeConstants.SYNTHESIS_TYPE)
private int synthesis;
@JsonIgnore
private String synthesisTypeName;//加工组类型的名称
@ExcelProperty(value = "加工组名称")
@JsonIgnore
private String combinationName;
private BigDecimal sealDown;
@ExcelProperty(value = "排版面", converter = DictConvert.class)
@JsonIgnore
@@ -179,8 +190,8 @@ public class OrderPlateImportExcelVO {
@ExcelProperty(value = "备注10")
private String remark10;
@ExcelProperty(value = "备注")
@JsonIgnore
@ExcelIgnore
private String remark;
@@ -191,13 +202,6 @@ public class OrderPlateImportExcelVO {
&& goodsNumber > 0;
}
@JsonIgnore
public boolean isValidRawGoods() {
return goodsNumber > 0 &&
splitHeight > 0 && splitWidth > 0 &&
sealLeft >= 0 && sealRight >= 0 && sealUp >= 0 && sealDown >= 0;
}
private boolean isNonEmpty(String value) {
return value != null && !value.trim().isEmpty();
}
@@ -0,0 +1,40 @@
package com.cf.imes.module.executor.util.fileConversion.admin.files.excel;
import com.alibaba.excel.EasyExcel;
import com.cf.imes.framework.excel.core.util.ExcelUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* @projectName: cf_imes_server
* @author: 晨丰科技
* @date: 2024/5/14 9:23
*/
public class Test {
public static void main(String[] args) throws IOException {
String filePath = "C:/Users/Administrator/Desktop/生产单新增Excel模板.xlsx";
InputStream inputStream = new FileInputStream(filePath);
ExcelListener<OrderPlateImportExcelVO> excelListener = new ExcelListener();
// ExcelListenerTwo excelListener = new ExcelListenerTwo();
// List<OrderTest> list = read(inputStream, OrderTest.class,excelListener);
// System.out.println(list);
// EasyExcel.read(inputStream, new ExcelListener()).sheet().doRead();
ExcelReadUtil excelReadUtil = new ExcelReadUtil();
EasyExcel.read(inputStream, excelListener).sheet().doRead();
// excelReadUtil.changeValue(inputStream);
}
}