diff --git a/ghy-admin/src/main/java/com/ghy/web/controller/order/OrderMasterController.java b/ghy-admin/src/main/java/com/ghy/web/controller/order/OrderMasterController.java index 44e1328d..241ceeb5 100644 --- a/ghy-admin/src/main/java/com/ghy/web/controller/order/OrderMasterController.java +++ b/ghy-admin/src/main/java/com/ghy/web/controller/order/OrderMasterController.java @@ -987,6 +987,48 @@ public class OrderMasterController extends BaseController { // orderMaster.setOrderStatuses(); // } List list = orderMasterService.selectOrderMasterList(orderMaster); + + if (CollectionUtils.isEmpty(list)) { + return voDataTable(orderListResponses, new ArrayList<>()); + } + + // 优化1:收集所有需要查询的ID,避免重复查询 + Set workerIds = new HashSet<>(); + Set addressIds = new HashSet<>(); + Set goodsIds = new HashSet<>(); + + list.forEach(master -> { + if (master.getWorkerId() != null) { + workerIds.add(master.getWorkerId()); + } + if (master.getAddressId() != null) { + addressIds.add(master.getAddressId()); + } + if (master.getGoodsId() != null) { + goodsIds.add(master.getGoodsId()); + } + }); + + // 优化2:批量查询常用数据,避免循环中的重复查询 + Map workerMap = new HashMap<>(); + Map addressMap = new HashMap<>(); + Map goodsMap = new HashMap<>(); + + if (!CollectionUtils.isEmpty(workerIds)) { + List workers = workerService.selectByIds(workerIds); + workers.forEach(worker -> workerMap.put(worker.getId(), worker)); + } + + if (!CollectionUtils.isEmpty(addressIds)) { + List addresses = addressService.selectByCustomerAddressIds(addressIds); + addresses.forEach(address -> addressMap.put(address.getCustomerAddressId(), address)); + } + + if (!CollectionUtils.isEmpty(goodsIds)) { + List goodsList = goodsService.selectByIds(goodsIds); + goodsList.forEach(goods -> goodsMap.put(goods.getGoodsId(), goods)); + } + list.forEach(master -> { //子单 List detailList = orderDetailService.selectByOrderMasterId(master.getId()); @@ -995,41 +1037,48 @@ public class OrderMasterController extends BaseController { OrderListResponse orderListResponse = new OrderListResponse(); List standardList = new ArrayList<>(); - // 师傅信息 - Worker worker = master.getWorkerId() != null ? workerService.selectById(master.getWorkerId()) : null; + // 优化3:使用预查询的师傅信息 + Worker worker = workerMap.get(master.getWorkerId()); // 消费者信息 // Customer customer = customerService.selectByCustomerId(master.getCustomerId()); // 商品规格及信息 List orderStandardList = orderGoodsService.selectByOrderMasterId(master.getId()); + Goods goods = new Goods(); + if (orderStandardList.size() > 0) { + // 商品信息 + GoodsStandard goodsStandard = goodsStandardService.selectById(orderStandardList.get(0).getGoodsStandardId()); + if (goodsStandard != null) { + goods = goodsService.selectById(goodsStandard.getGoodsId()); + } + } else { + // 优化4:使用预查询的商品信息 + goods = goodsMap.getOrDefault(master.getGoodsId(), new Goods()); + } - // 商品信息 - GoodsStandard goodsStandard = goodsStandardService.selectById(orderStandardList.get(0).getGoodsStandardId()); - - Goods goods = goodsService.selectById(goodsStandard.getGoodsId()); // 填充商品三级类目 - if(goods.getDeptGoodsCategoryId() != null){ + if (goods.getDeptGoodsCategoryId() != null) { Long categoryId = null; // 前端发单和后台派单 - if(com.ghy.common.utils.StringUtils.isEmpty(master.getOrderMode())){ + if (com.ghy.common.utils.StringUtils.isEmpty(master.getOrderMode())) { DeptGoodsCategory deptGoodsCategory = deptGoodsCategoryService.selectOneByGoodsCategoryId(goods.getDeptGoodsCategoryId()); - if(deptGoodsCategory != null){ + if (deptGoodsCategory != null) { categoryId = deptGoodsCategory.getGoodsCategoryId(); } - }else { + } else { DeptGoodsCategory deptGoodsCategory = deptGoodsCategoryService.get(goods.getDeptGoodsCategoryId()); - if(deptGoodsCategory != null){ + if (deptGoodsCategory != null) { categoryId = deptGoodsCategory.getGoodsCategoryId(); } } - if(categoryId != null){ + if (categoryId != null) { GoodsCategory one = goodsCategoryService.selectById(categoryId); - if(one != null && one.getParentCategoryId() != null){ + if (one != null && one.getParentCategoryId() != null) { GoodsCategory two = goodsCategoryService.selectById(one.getParentCategoryId()); - if(two != null && two.getParentCategoryId() != null){ + if (two != null && two.getParentCategoryId() != null) { GoodsCategory three = goodsCategoryService.selectById(two.getParentCategoryId()); - if(three != null){ + if (three != null) { master.setConsoleGoodsName(three.getGoodsCategoryName() + "-" + two.getGoodsCategoryName() + "-" + one.getGoodsCategoryName()); @@ -1044,16 +1093,15 @@ public class OrderMasterController extends BaseController { BigDecimal totalPayMoney = financialMaster.getPayMoney(); BigDecimal totalChangeMoney = new BigDecimal(0); BigDecimal paymentMoney = BigDecimal.ZERO; - if (financialMaster.getPayStatus()==1){ - paymentMoney=paymentMoney.add(financialMaster.getTotalMoney()); + if (financialMaster.getPayStatus() == 1) { + paymentMoney = paymentMoney.add(financialMaster.getTotalMoney()); } - List financialChangeRecords=financialChangeRecordService.selectByMasterId(master.getId()); - BigDecimal changePaymentMoney =financialChangeRecords.stream() - .filter(record->record.getPayStatus()==1) + List financialChangeRecords = financialChangeRecordService.selectByMasterId(master.getId()); + BigDecimal changePaymentMoney = financialChangeRecords.stream() + .filter(record -> record.getPayStatus() == 1) .map(FinancialChangeRecord::getChangeMoney) .reduce(BigDecimal.ZERO, BigDecimal::add); - paymentMoney=paymentMoney.add(changePaymentMoney); - logger.info("加价费已支付{}加价列表{}主单id为{}",changePaymentMoney,financialChangeRecords,master.getId()); + paymentMoney = paymentMoney.add(changePaymentMoney); for (OrderDetail detail : detailList) { // 查询子单加价记录 @@ -1065,29 +1113,34 @@ public class OrderMasterController extends BaseController { OrderAttachmentRecord param = new OrderAttachmentRecord(); param.setOrderDetailId(detail.getId()); List orderAttachmentRecordList = orderAttachmentRecordService.selectOrderAttachmentRecordList(param); - for (OrderAttachmentRecord orderAttachmentRecord:orderAttachmentRecordList){ - if (record!=null&&record.getPayStatus()==0){ + for (OrderAttachmentRecord orderAttachmentRecord : orderAttachmentRecordList) { + if (record != null && record.getPayStatus() == 0) { totalChangeMoney = totalChangeMoney.add(orderAttachmentRecord.getAttachMoney()); - }else{ + } else { paymentMoney = paymentMoney.add(orderAttachmentRecord.getAttachMoney()); } } - logger.info("未支付的加价订单{}加价配件订单列表{}",record,orderAttachmentRecordList); } - // 地址信息 - CustomerAddress customerAddress = addressService.selectByCustomerAddressId(master.getAddressId()); + // 优化5:使用预查询的地址信息 + CustomerAddress customerAddress = addressMap.get(master.getAddressId()); + if (customerAddress == null) { + customerAddress = new CustomerAddress(); + } SysArea sysArea; - if(customerAddress.getStreetId()!=null){ + if (customerAddress.getStreetId() != null) { sysArea = sysAreaService.selectById(customerAddress.getStreetId()); - }else { + } else { sysArea = sysAreaService.selectById(customerAddress.getCountryId()); } - String completeAddress = sysArea.getMergerName().replaceAll(",", "") + customerAddress.getAddress(); + String completeAddress = ""; + if (sysArea != null) { + completeAddress = sysArea.getMergerName().replaceAll(",", "") + customerAddress.getAddress(); + } - // 查询售后记录 + // 优化6:避免重复查询订单详情 List afterServiceRecords = new ArrayList<>(); - List detailOrderList = orderDetailService.selectByOrderMasterId(master.getId()); - detailOrderList.forEach(detail -> { + // 直接使用已经查询的detailList,避免重复查询 + detailList.forEach(detail -> { AfterServiceRecord afterServiceRecordQry = new AfterServiceRecord(); afterServiceRecordQry.setOrderDetailId(detail.getId()); List records = afterServiceRecordService.selectAfterServiceRecordList(afterServiceRecordQry); @@ -1122,8 +1175,8 @@ public class OrderMasterController extends BaseController { orderListResponse.setChangeMoney(totalChangeMoney); orderListResponse.setWorkerName(worker == null ? "" : worker.getName()); orderListResponse.setWorkerPhone(worker == null ? "" : worker.getPhone()); - orderListResponse.setCustomerName(org.apache.commons.lang3.StringUtils.isNotEmpty(master.getName())?master.getName():customerAddress.getName()); - orderListResponse.setCustomerPhone(org.apache.commons.lang3.StringUtils.isNotEmpty(master.getPhone())?master.getPhone():customerAddress.getPhone()); + orderListResponse.setCustomerName(org.apache.commons.lang3.StringUtils.isNotEmpty(master.getName()) ? master.getName() : customerAddress.getName()); + orderListResponse.setCustomerPhone(org.apache.commons.lang3.StringUtils.isNotEmpty(master.getPhone()) ? master.getPhone() : customerAddress.getPhone()); orderListResponse.setServerTime(master.getRevTime()); orderListResponse.setExpectTimeStart(master.getExpectTimeStart()); orderListResponse.setExpectTimeEnd(master.getExpectTimeEnd()); @@ -1142,7 +1195,7 @@ public class OrderMasterController extends BaseController { orderListResponse.setAfterServiceRecordList(afterServiceRecords); orderListResponse.setServerMoney(master.getServerMoney()); orderListResponse.setIsCall(master.getIsCall()); - if (StringUtils.isNotEmpty(master.getProvinceName())){ + if (StringUtils.isNotEmpty(master.getProvinceName())) { orderListResponse.setProvinceId(master.getProvinceId()); orderListResponse.setCityId(master.getCityId()); orderListResponse.setCountryId(master.getCountryId()); @@ -1154,14 +1207,22 @@ public class OrderMasterController extends BaseController { orderListResponse.setStreetId(master.getStreetId()); orderListResponse.setStreetName(master.getStreetName()); orderListResponse.setAddress(master.getAddress()); - }else{ - if (!StringUtils.isEmpty(customerAddress.getStreetId()+"")){ - String addressSysArea=sysArea.getMergerName(); + } else { + if (!StringUtils.isEmpty(customerAddress.getStreetId() + "")) { + String addressSysArea = sysArea != null ? sysArea.getMergerName() : ""; String[] array = addressSysArea.split(","); - orderListResponse.setProvinceName(array[0]); - orderListResponse.setCityName(array[1]); - orderListResponse.setCountryName(array[2]); - orderListResponse.setStreetName(array[3]); + if (array.length > 0) { + orderListResponse.setProvinceName(array[0]); + } + if (array.length > 1) { + orderListResponse.setCityName(array[1]); + } + if (array.length == 3) { + orderListResponse.setCountryName(array[2]); + } + if (array.length == 4) { + orderListResponse.setStreetName(array[3]); + } } orderListResponse.setProvinceId(customerAddress.getProvinceId()); orderListResponse.setCityId(customerAddress.getCityId()); @@ -1170,24 +1231,9 @@ public class OrderMasterController extends BaseController { orderListResponse.setAddress(customerAddress.getAddress()); orderListResponse.setPhone(customerAddress.getPhone()); orderListResponse.setName(customerAddress.getName()); - } orderListResponses.add(orderListResponse); - }); - -// PageDomain pageDomain = TableSupport.buildPageRequest(); -// if (null == pageDomain.getPageNum() || null == pageDomain.getPageSize()) { -// rspData.setRows(orderListResponses); -// rspData.setTotal(orderListResponses.size()); -// return rspData; -// } -// Integer pageSize = pageDomain.getPageNum(); -// if (pageSize > orderListResponses.size()) { -// pageSize = orderListResponses.size(); -// } -// rspData.setRows(orderListResponses.subList(0, pageSize)); -// rspData.setTotal(orderListResponses.size()); return voDataTable(orderListResponses, list); } @@ -1661,8 +1707,13 @@ public class OrderMasterController extends BaseController { String[] array = addressSysArea.split(","); orderListResponse.setProvinceName(array[0]); orderListResponse.setCityName(array[1]); - orderListResponse.setCountryName(array[2]); - orderListResponse.setStreetName(array[3]); + if (array.length==3){ + orderListResponse.setCountryName(array[2]); + } + if (array.length==4){ + orderListResponse.setStreetName(array[3]); + } + } orderListResponse.setProvinceId(customerAddress.getProvinceId()); orderListResponse.setCityId(customerAddress.getCityId());