List<DTO>排序和JSONArray 排序

1.List<DTO>排序方法1

    	//组装pair数据
        List<CardChannelInfoDTO> clientInfoDto = cardChannelInfoClientDTOLists;
        //排序正序
        clientInfoDto.forEach(clientInfo -> clientInfo.setPortNumber(clientInfo.getPortNumber()));
        clientInfoDto.sort(Comparator.comparingInt(CardChannelInfoDTO::getPortNumber));
		
		
		//反序
		clientInfoDto.forEach(clientInfo -> clientInfo.setPortNumber(
我代码能力很差,你解释一下package com.foonsu.efenxiao.biz.handler; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; import cn.hutool.core.text.csv.CsvReadConfig; import cn.hutool.core.text.csv.CsvReader; import cn.hutool.core.text.csv.CsvRow; import cn.hutool.core.util.ObjectUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.foonsu.efenxiao.biz.config.CidReadListener; import com.foonsu.efenxiao.biz.feign.OrderClientWrapper; import com.foonsu.efenxiao.biz.util.OrderUtil; import com.foonsu.efenxiao.order.dto.CidOrder; import com.foonsu.efenxiao.biz.vo.ImportCidAccountVO; import com.foonsu.efenxiao.biz.vo.ProgressVo; import com.foonsu.efenxiao.common.utils.CollectionsUtil; import com.foonsu.efenxiao.order.dto.OrderDTO; import com.foonsu.efenxiao.order.dto.OrderExtendDto; import com.foonsu.efenxiao.order.dto.OrderGoodsDto; import com.foonsu.efenxiao.order.dto.OrderTradeDto; import com.foonsu.efenxiao.order.dubbo.CidClient; import com.foonsu.efenxiao.order.dubbo.OrderClient; import com.foonsu.efenxiao.order.entity.CidAccount; import com.foonsu.efenxiao.order.entity.CidAccountingDetail; import com.foonsu.efenxiao.order.entity.CidPlan; import com.foonsu.efenxiao.order.entity.Order; import com.foonsu.efenxiao.order.vo.CidAccountSearchVO; import com.foonsu.efenxiao.order.vo.CidOrderConsumption; import com.foonsu.efenxiao.order.vo.CidPlanSearchResp; import com.foonsu.efenxiao.order.vo.CidPlanSearchVo; import com.foonsu.efenxiao.order.vo.req.AdvertiserAccount; import com.foonsu.efenxiao.order.vo.req.HistoryPushAccountReq; import com.foonsu.efenxiao.platform.dubbo.GoodsClient; import com.foonsu.efenxiao.platform.entity.CidGoodsPriceHistory; import com.foonsu.efenxiao.platform.entity.PlatformGoods; import com.foonsu.efenxiao.platform.vo.PlatformGoodsSkuVO; import com.foonsu.efenxiao.platform.vo.PlatformGoodsVO; import com.foonsu.efenxiao.search.dubbo.OrderSearchClient; import com.foonsu.efenxiao.search.vo.ordersearch.*; import com.foonsu.efenxiao.webutil.progress.impl.CidImportAccountProgress; import com.foonsu.efenxiao.webutil.progress.impl.CidImportStatementProgress; import com.foonsu.framework.boot.common.utils.MD5Utils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.apache.dubbo.config.annotation.DubboReference; import org.springblade.core.tool.api.R; import org.springblade.core.tool.utils.BeanUtil; import org.springblade.core.tool.utils.StringUtil; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigDecimal; import java.math.RoundingMode; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Function; import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @Component @Slf4j public class CidShopBillHandler { private static final Pattern PATTERN = Pattern.compile("\\d{6}-\\d{15}"); @Resource @Qualifier("cidHandleThreadPoolTaskExecutor") ThreadPoolTaskExecutor cidHandleThreadPoolTaskExecutor; @DubboReference private OrderClient orderClient; @DubboReference private CidClient cidClient; @DubboReference private OrderSearchClient orderSearchClient; @DubboReference private GoodsClient goodsClient; @Resource private OrderClientWrapper orderClientWrapper; @Resource private CidImportStatementProgress cidImportStatementProgress; @Resource private CidImportAccountProgress cidImportAccountProgress; @Value("${cid.tenant-id}") private String tenantId; public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); public CidSearchOrderResp searchOrder(SearchOrderReq req, String tenantId) { SearchOrderBackendParam backendParam = new SearchOrderBackendParam(); backendParam.setTenantId(tenantId); EsSearchOrderResp resp = orderSearchClient.searchCidOrder(req, backendParam); CidSearchOrderResp cidSearchOrderResp = new CidSearchOrderResp(); if (!resp.getOrderNos().isEmpty()) { List<String> allOrderNos = resp.getOrderNos().stream().flatMap(Collection::stream).collect(Collectors.toList()); List<Order> ordersData = orderClientWrapper.queryByOrderNos(allOrderNos); List<CidOrderVO> cidOrderVOS = this.convertData(resp, ordersData); cidSearchOrderResp.setData(cidOrderVOS); } cidSearchOrderResp.setTotal(resp.getTotalCount().intValue()); cidSearchOrderResp.setCurrent(req.getPageNo()); cidSearchOrderResp.setSize(req.getPageSize()); cidSearchOrderResp.setPages(cidSearchOrderResp.getTotal() / req.getPageSize() + (cidSearchOrderResp.getTotal() % req.getPageSize() > 0 ? 1 : 0)); return cidSearchOrderResp; } private List<CidOrderVO> convertData(EsSearchOrderResp esSearchOrderResp, List<Order> orders) { List<CidOrderVO> cidOrderVOS = new ArrayList<>(); for (List<String> orderNos : esSearchOrderResp.getOrderNos()) { List<Order> onelineOrders = orders.stream().filter(t -> orderNos.contains(t.getOrderNo())).collect(Collectors.toList()); if (!onelineOrders.isEmpty()) { Map<String, List<Order>> tradeNoOrders = onelineOrders.stream().collect(Collectors.groupingBy(Order::getTradeNo)); for (String tradeNo : tradeNoOrders.keySet()) { List<Order> splitOrders = tradeNoOrders.get(tradeNo); List<OrderDTO> orderDTOS = OrderUtil.convertToDto(splitOrders); // cid默认单商品订单 OrderDTO orderDTO = orderDTOS.get(0); CidOrderVO cidOrderVO = new CidOrderVO(); cidOrderVO.setShopName(orderDTO.getShopName()); cidOrderVO.setJlId(orderDTO.getOrderGoodsDtoList().get(0).getBindJlId()); cidOrderVO.setTradeNo(orderDTO.getTradeNo()); cidOrderVO.setOrderNo(orderDTO.getOrderNo()); cidOrderVO.setPayAmount(orderDTO.getOrderTradeDto().getPayAmount()); CidOrderExtendVO cidOrderExtendVO = JSON.parseObject(orderDTO.getExtendInfo(), CidOrderExtendVO.class); cidOrderVO.setCidOrderExtendVO(cidOrderExtendVO); List<CidGoodsVo> goodsVos = JSON.parseArray(orderDTO.getGoods(), CidGoodsVo.class); cidOrderVO.setGoods(goodsVos); cidOrderVOS.add(cidOrderVO); } } } return cidOrderVOS; } public void compute(String tenantId, List<String> tradeNos) { Map<String, List<CidAccountingDetail>> accountDetailGroupByTradeNo = cidClient.queryAllImportCidTradeNo(tenantId, null, tradeNos) .stream().collect(Collectors.groupingBy(CidAccountingDetail::getTradeNo)); List<String> importCidTradeNos = new ArrayList<>(accountDetailGroupByTradeNo.keySet()); List<List<String>> reqsList = CollectionsUtil.splitListToThreads(importCidTradeNos, 1500, 20); for (List<String> orders : reqsList) { cidHandleThreadPoolTaskExecutor.submit(() -> { for (String order : orders) { this.handleOrder(order, tenantId, accountDetailGroupByTradeNo.get(order)); } }); } } public void compute(List<CidAccountingDetail> accountingDetails) { accountingDetails = accountingDetails.stream().sorted(Comparator.comparing(CidAccountingDetail::getTradeNo)).collect(Collectors.toList()); CollectionsUtil.batchDeal(accountingDetails, 500, accountingDetailList -> { List<CidOrder> cidOrders = new ArrayList<>(); Set<String> tradeNos = accountingDetailList.stream().map(CidAccountingDetail::getTradeNo).collect(Collectors.toSet()); Map<String, List<LocalDateTime>> occurTimeByTradeNos = cidClient.queryOccurentTimeByTradeNos(tradeNos); Map<String, List<CidAccountingDetail>> accountDetailGroupByTradeNo = accountingDetailList.stream().collect(Collectors.groupingBy(CidAccountingDetail::getTradeNo)); List<Order> orders = orderClient.queryOrderByTradeNos(new ArrayList<>(tradeNos)); if (CollectionsUtil.isEmpty(orders)) return; Map<String, Order> ordergroupbyTradeNo = orders.stream().collect(Collectors.toMap(Order::getTradeNo, Function.identity())); Map<String, String> ordersGroupByProductIdMap = orders.stream().collect(Collectors.toMap(Order::getTradeNo, order -> { List<OrderGoodsDto> orderGoodsDtos = JSONArray.parseArray(order.getGoods(), OrderGoodsDto.class); return orderGoodsDtos.get(0).getProductId(); })); List<String> skuIds = orders.stream().map(order -> { List<OrderGoodsDto> orderGoodsDtos = JSONArray.parseArray(order.getGoods(), OrderGoodsDto.class); return orderGoodsDtos.get(0).getSkuId(); }).distinct().collect(Collectors.toList()); Map<String, List<CidGoodsPriceHistory>> cidGoodsPriceGroupBySkuIdMap = goodsClient.selectCidGoodsPriceHistory(tenantId, null, skuIds) .stream().collect(Collectors.groupingBy(CidGoodsPriceHistory::getSkuId)); List<String> productIds = ordersGroupByProductIdMap.values().stream().distinct().collect(Collectors.toList()); Map<String, PlatformGoodsVO> goodsGroupByProductId = goodsClient.selectCidGoods(tenantId, productIds, null) .stream().collect(Collectors.toMap(PlatformGoodsVO::getProductId, Function.identity())); accountDetailGroupByTradeNo.forEach((tradeNo, accountDetails) -> { if (ordergroupbyTradeNo.containsKey(tradeNo)) { Order order = ordergroupbyTradeNo.get(tradeNo); String productId = ordersGroupByProductIdMap.get(tradeNo); if (goodsGroupByProductId.containsKey(productId)) { PlatformGoodsVO platformGoodsVO = goodsGroupByProductId.get(productId); OrderExtendDto orderExtendDto = computeGoodsProfit(platformGoodsVO, order, cidGoodsPriceGroupBySkuIdMap); cidOrders.addAll(covertCidOrders(accountDetails, orderExtendDto, tradeNo, platformGoodsVO.getBindJlId(), occurTimeByTradeNos.get(tradeNo))); } } }); if (cidOrders.isEmpty()) return; orderSearchClient.saveCidOrders(cidOrders); }, cidHandleThreadPoolTaskExecutor); } private CidOrder createEsCidOrder(String tenantId, String bindJlId, String tradeNO) { CidOrder esCidOrder = new CidOrder(); esCidOrder.setAccountId(bindJlId); esCidOrder.setTenantId(tenantId); esCidOrder.setTradeNo(tradeNO); return esCidOrder; } private List<CidOrder> covertCidOrders(List<CidAccountingDetail> accountDetails, OrderExtendDto orderExtendDto, String tradeNO, String bindJlId, List<LocalDateTime> occurTimes) { List<CidOrder> esCidOrders = accountDetails.stream().map(accountingDetail -> { CidOrder cidOrder = createEsCidOrder(tenantId, bindJlId, tradeNO); cidOrder.setId(accountingDetail.getUniqueId()); cidOrder.setOccurrenceTime(Collections.singletonList(LocalDateTime.parse(accountingDetail.getOccurrenceTime(), DATETIME_FORMATTER))); cidOrder.setRefundType(orderExtendDto.getRefundType()); BigDecimal incomeAmount = accountingDetail.getIncomeAmount() == null ? BigDecimal.ZERO : accountingDetail.getIncomeAmount(); BigDecimal expenditureAmount = accountingDetail.getExpenditureAmount() == null ? BigDecimal.ZERO : accountingDetail.getExpenditureAmount(); cidOrder.setOrderProfit(incomeAmount.add(expenditureAmount)); switch (accountingDetail.getAccountingType()) { case "扣款": cidOrder.setDeductionSum(accountingDetail.getExpenditureAmount()); switch (accountingDetail.getServiceType()) { case "0040002": cidOrder.setSalesCompensate(accountingDetail.getExpenditureAmount()); break; case "0040003": cidOrder.setExpressCompensate(accountingDetail.getExpenditureAmount()); break; case "0040004": cidOrder.setDelaySendGoods(accountingDetail.getExpenditureAmount()); break; case "0040005": cidOrder.setFalseSendGoods(accountingDetail.getExpenditureAmount()); break; case "0040006": cidOrder.setOutOfStock(accountingDetail.getExpenditureAmount()); } break; case "技术服务费": cidOrder.setTechnologyFee(accountingDetail.getExpenditureAmount()); break; case "多多进宝": cidOrder.setDdjb(accountingDetail.getExpenditureAmount()); } return cidOrder; }).collect(Collectors.toList()); CidOrder esCidOrder = createEsCidOrder(tenantId, bindJlId, tradeNO); esCidOrder.setOrderProfit(BigDecimal.ZERO.subtract(orderExtendDto.getPurchaseExpressFee())); esCidOrder.setId(tradeNO); esCidOrder.setOccurrenceTime(occurTimes.stream().distinct().collect(Collectors.toList())); esCidOrders.add(esCidOrder); return esCidOrders; } private OrderExtendDto computeGoodsProfit(PlatformGoodsVO platformGoods, Order order, Map<String, List<CidGoodsPriceHistory>> cidGoodsPriceGroupBySkuIdMap) { OrderTradeDto orderTradeDto = JSON.parseObject(order.getTrade(), OrderTradeDto.class); List<OrderGoodsDto> orderGoodsDtos = JSONArray.parseArray(order.getGoods(), OrderGoodsDto.class); OrderGoodsDto orderGoodsDto = orderGoodsDtos.get(0); OrderExtendDto orderExtendDto = new OrderExtendDto(); List<PlatformGoodsSkuVO> skuVOS = platformGoods.getGoodsSkuList().stream().filter(sku -> sku.getSkuId().equals(orderGoodsDto.getSkuId())).collect(Collectors.toList()); if (!skuVOS.isEmpty()) { PlatformGoodsSkuVO platformGoodsSkuVO = skuVOS.get(0); BigDecimal purchasePrice = platformGoodsSkuVO.getPurchaseUnitPrice(); BigDecimal expressFee = platformGoodsSkuVO.getExpressFee(); String skuId = platformGoodsSkuVO.getSkuId(); if (cidGoodsPriceGroupBySkuIdMap.containsKey(skuId)) { List<CidGoodsPriceHistory> cidGoodsPriceHistories = cidGoodsPriceGroupBySkuIdMap.get(skuId); for (CidGoodsPriceHistory cidGoodsPriceHistory : cidGoodsPriceHistories) { if (cidGoodsPriceHistory.getType() == 2 && DateUtil.toLocalDateTime(new Date(orderTradeDto.getSourceCreateDate())).isAfter(cidGoodsPriceHistory.getUpdateTime())) { purchasePrice = cidGoodsPriceHistory.getPrice(); } if (cidGoodsPriceHistory.getType() == 3 && DateUtil.toLocalDateTime(new Date(orderTradeDto.getSourceCreateDate())).isAfter(cidGoodsPriceHistory.getUpdateTime())) { expressFee = cidGoodsPriceHistory.getPrice(); } } } orderExtendDto.setPurchasePrice(purchasePrice == null ? BigDecimal.ZERO : purchasePrice); orderExtendDto.setExpressFee(expressFee == null ? BigDecimal.ZERO : expressFee); } if (orderGoodsDto.getRefundType() != null) { orderExtendDto.setPurchaseExpressFee(orderExtendDto.getExpressFee()); orderExtendDto.setRefundType(orderGoodsDto.getRefundType()); } else { if (orderExtendDto.getPurchasePrice() != null && orderExtendDto.getExpressFee() != null) { orderExtendDto.setPurchaseExpressFee(orderExtendDto.getPurchasePrice().add(orderExtendDto.getExpressFee())); } } return orderExtendDto; } private void handleOrder(String importCidTradeNo, String tenantId, List<CidAccountingDetail> cidAccountingDetails) { if (StringUtil.isBlank(importCidTradeNo)) { return; } List<Order> orders = orderClient.queryOrderByTradeNo(importCidTradeNo); if (orders.isEmpty()) { log.info("对账订单为空"); return; } // 默认都是单商品订单 Order order = orders.get(0); List<CidAccountingDetail> accountingDetails = cidClient.queryAccountingDetailByTradeNo(tenantId, importCidTradeNo); if (accountingDetails.isEmpty()) { log.info("对账明细为空"); return; } OrderTradeDto orderTradeDto = JSON.parseObject(order.getTrade(), OrderTradeDto.class); List<OrderGoodsDto> orderGoodsDtos = JSONArray.parseArray(order.getGoods(), OrderGoodsDto.class); OrderGoodsDto orderGoodsDto = orderGoodsDtos.get(0); List<PlatformGoodsVO> platformGoodsVOS = goodsClient.selectCidGoods(tenantId, null, null); if (platformGoodsVOS.isEmpty()) { return; } PlatformGoodsVO platformGoods = platformGoodsVOS.get(0); OrderExtendDto orderExtendDto = StringUtils.isBlank(order.getExtendInfo()) ? new OrderExtendDto() : JSON.parseObject(order.getExtendInfo(), OrderExtendDto.class); BigDecimal orderNum = BigDecimal.ZERO; BigDecimal deductionSum = BigDecimal.ZERO; BigDecimal technologyFee = BigDecimal.ZERO; BigDecimal ddjb = BigDecimal.ZERO; BigDecimal salesCompensate = BigDecimal.ZERO;// 售后补偿 BigDecimal expressCompensate = BigDecimal.ZERO;// 运费补偿 BigDecimal delaySendGoods = BigDecimal.ZERO;// 延迟发货 BigDecimal falseSendGoods = BigDecimal.ZERO;// 虚假发货 BigDecimal outOfStock = BigDecimal.ZERO;// 缺货 for (CidAccountingDetail accountingDetail : accountingDetails) { orderNum = orderNum.add(accountingDetail.getIncomeAmount()).add(accountingDetail.getExpenditureAmount()); boolean serviceFlag = false; switch (accountingDetail.getAccountingType()) { case "扣款": deductionSum = deductionSum.add(accountingDetail.getExpenditureAmount()); serviceFlag = true; break; case "技术服务费": technologyFee = technologyFee.add(accountingDetail.getExpenditureAmount()); break; case "多多进宝": ddjb = ddjb.add(accountingDetail.getExpenditureAmount()); } if (serviceFlag) { switch (accountingDetail.getServiceType()) { case "0040002": salesCompensate = salesCompensate.add(accountingDetail.getExpenditureAmount()); break; case "0040003": expressCompensate = expressCompensate.add(accountingDetail.getExpenditureAmount()); break; case "0040004": delaySendGoods = delaySendGoods.add(accountingDetail.getExpenditureAmount()); break; case "0040005": falseSendGoods = falseSendGoods.add(accountingDetail.getExpenditureAmount()); break; case "0040006": outOfStock = outOfStock.add(accountingDetail.getExpenditureAmount()); } } } orderExtendDto.setDeductionSum(deductionSum); orderExtendDto.setTechnologyFee(technologyFee); orderExtendDto.setDdjb(ddjb); orderExtendDto.setSalesCompensate(salesCompensate); orderExtendDto.setExpressCompensate(expressCompensate); orderExtendDto.setDelaySendGoods(delaySendGoods); orderExtendDto.setFalseSendGoods(falseSendGoods); orderExtendDto.setOutOfStock(outOfStock); orderExtendDto.setPurchasePrice(BigDecimal.ZERO); orderExtendDto.setExpressFee(BigDecimal.ZERO); List<PlatformGoodsSkuVO> skuVOS = platformGoods.getGoodsSkuList().stream().filter(sku -> sku.getSkuId().equals(orderGoodsDto.getSkuId())).collect(Collectors.toList()); if (!skuVOS.isEmpty()) { PlatformGoodsSkuVO platformGoodsSkuVO = skuVOS.get(0); BigDecimal purchasePrice = platformGoodsSkuVO.getPurchaseUnitPrice(); BigDecimal expressFee = platformGoodsSkuVO.getExpressFee(); List<CidGoodsPriceHistory> cidGoodsPriceHistories = goodsClient.selectCidGoodsPriceHistory(tenantId, null, null); if (!cidGoodsPriceHistories.isEmpty()) { for (CidGoodsPriceHistory cidGoodsPriceHistory : cidGoodsPriceHistories) { if (cidGoodsPriceHistory.getType() == 2 && DateUtil.toLocalDateTime(new Date(orderTradeDto.getSourceCreateDate())).isAfter(cidGoodsPriceHistory.getUpdateTime())) { purchasePrice = cidGoodsPriceHistory.getPrice(); } if (cidGoodsPriceHistory.getType() == 3 && DateUtil.toLocalDateTime(new Date(orderTradeDto.getSourceCreateDate())).isAfter(cidGoodsPriceHistory.getUpdateTime())) { expressFee = cidGoodsPriceHistory.getPrice(); } } } orderExtendDto.setPurchasePrice(purchasePrice == null ? BigDecimal.ZERO : purchasePrice); orderExtendDto.setExpressFee(expressFee == null ? BigDecimal.ZERO : expressFee); } if (orderGoodsDto.getRefundType() != null) { orderExtendDto.setPurchaseExpressFee(orderExtendDto.getExpressFee()); orderExtendDto.setRefundType(orderGoodsDto.getRefundType()); } else { if (orderExtendDto.getPurchasePrice() != null && orderExtendDto.getExpressFee() != null) { orderExtendDto.setPurchaseExpressFee(orderExtendDto.getPurchasePrice().add(orderExtendDto.getExpressFee())); } } orderExtendDto.setOrderSum(orderNum); orderExtendDto.setOrderProfit(orderExtendDto.getOrderSum().subtract(orderExtendDto.getPurchaseExpressFee())); orderGoodsDtos.get(0).setBindJlId(platformGoods.getBindJlId()); order.setGoods(JSON.toJSONString(orderGoodsDtos)); order.setExtendInfo(JSON.toJSONString(orderExtendDto)); orderClient.updateCidOrder(order); orderSearchClient.saveCidOrders(orderExtendDto, tenantId, accountingDetails, platformGoods.getBindJlId(), importCidTradeNo); } public void importFile(MultipartFile file, String tenantId, String taskId) { ProgressVo progressVo = new ProgressVo(); R<ProgressVo> result = R.data(progressVo); try { progressVo.setTotalCount(0); progressVo.setAllHasDone(false); cidImportStatementProgress.saveProgress(taskId, result); CsvReader csvReader = new CsvReader(new InputStreamReader(file.getInputStream(), "GBK"), new CsvReadConfig()); List<CsvRow> rows = csvReader.read().getRows(); int size = rows.size(); log.info("文件行数:{}", size); if (size == 0) { log.info("文件为空"); progressVo.setTotalCount(0); progressVo.setAllHasDone(true); cidImportStatementProgress.saveProgress(taskId, result); return; } this.handlerAccountingDetail(rows, tenantId, taskId); } catch (IOException e) { log.error("导入失败", e); throw new RuntimeException(e); } } private void handlerAccountingDetail(List<CsvRow> rows, String tenantId, String taskId) { LocalDateTime importFileTime = LocalDateTime.now(); log.info("执行处理:{}", rows.size()); AtomicInteger atomicInteger = new AtomicInteger(0); List<CidAccountingDetail> accountingDetails = new ArrayList<>(); try { for (CsvRow row : rows) { try { // log.info("row:{}", row); if (row.size() != 7) { continue; } if (StringUtil.isBlank(row.get(1))) { continue; } if ("商户订单号".equals(row.get(0))) { continue; } atomicInteger.getAndIncrement(); CidAccountingDetail accountingDetail = new CidAccountingDetail(); accountingDetail.setTenantId(tenantId); String tradeNo = row.get(0); if (StringUtil.isBlank(tradeNo)) { Matcher matcher = PATTERN.matcher(row.get(5)); if (matcher.find()) { tradeNo = matcher.group(); } else { continue; } } accountingDetail.setTradeNo(tradeNo); accountingDetail.setOccurrenceTime(row.get(1)); accountingDetail.setIncomeAmount(new BigDecimal(row.get(2))); accountingDetail.setExpenditureAmount(new BigDecimal(row.get(3))); accountingDetail.setAccountingType(row.get(4)); accountingDetail.setRemark(row.get(5)); String[] service = row.get(6).split("\\|"); accountingDetail.setServiceType(service[0]); accountingDetail.setServiceDesc(service[1]); accountingDetail.setCreateTime(importFileTime); String uniqueId = MD5Utils.encryptMD5(accountingDetail.getTradeNo() + accountingDetail.getOccurrenceTime() + accountingDetail.getAccountingType()); accountingDetail.setUniqueId(uniqueId); accountingDetails.add(accountingDetail); } catch (Exception e) { throw new RuntimeException(e); } } if (accountingDetails.isEmpty()) return; CountDownLatch countDownLatch = new CountDownLatch(accountingDetails.size() / 200 + 1); for (int j = 0; j < accountingDetails.size(); j += 200) { List<CidAccountingDetail> cidAccountList = accountingDetails.subList(j, Math.min(j + 200, accountingDetails.size())); cidHandleThreadPoolTaskExecutor.submit(() -> { cidClient.batchSaveAccountingDetail(cidAccountList); countDownLatch.countDown(); }); } countDownLatch.await(); compute(accountingDetails); } catch (Exception e) { throw new RuntimeException(e); } } public void importAccount(MultipartFile file, String tenantId, String taskId) { LocalDateTime importTime = LocalDateTime.now(); ProgressVo progressVo = new ProgressVo(); R<ProgressVo> result = R.data(progressVo); try { progressVo.setTotalCount(0); progressVo.setAllHasDone(false); cidImportAccountProgress.saveProgress(taskId, result); CidReadListener cidReadListener = new CidReadListener(); cidReadListener.setTenantId(tenantId); EasyExcel.read(file.getInputStream(), ImportCidAccountVO.class, cidReadListener).sheet().doRead(); int size = cidReadListener.getCachedDataList().size(); log.info("账户文件行数:{}", size); if (size == 0) { progressVo.setTotalCount(0); progressVo.setAllHasDone(true); cidImportAccountProgress.saveProgress(taskId, result); log.info("文件为空"); return; } int threadHandlerNum = size / 5 + (size % 5 > 0 ? 1 : 0); int fromIdx = 0; for (int i = 0; i < 5; i++) { List<CidAccount> cidAccounts = cidReadListener.getCachedDataList().subList(fromIdx, size - fromIdx >= threadHandlerNum ? fromIdx + threadHandlerNum : size); cidHandleThreadPoolTaskExecutor.submit(() -> { for (int j = 0; j < cidAccounts.size(); j += 100) { List<CidAccount> cidAccountList = cidAccounts.subList(j, Math.min(j + 100, cidAccounts.size())); cidClient.batchSaveCidAccount(cidAccountList); } log.info("处理账户完成:{}", cidAccounts.size()); return "任务完成"; }); fromIdx += cidAccounts.size(); } try { Thread.sleep(5000); } catch (InterruptedException e) { throw new RuntimeException(e); } log.info("处理账户完成, 调用生成报表:{}", importTime); this.generatePlan(tenantId, taskId, importTime, size); } catch (IOException e) { log.error("导入报表失败:{}", e.getMessage(), e); throw new RuntimeException(e); } } public void generatePlan(String tenantId, String taskId, LocalDateTime importTime, int insertCidAccountCount) { log.info("生成cid报表开始"); CidAccountSearchVO cidAccountSearchVO = new CidAccountSearchVO(); cidAccountSearchVO.setTenantId(tenantId); cidAccountSearchVO.setImportTime(importTime); Long size = 0L; for (int i = 1; i <= 10; i++) { size = cidClient.queryCidAccountCount(cidAccountSearchVO); log.info("计算cidAccount中,查询数据:{}", size); if (insertCidAccountCount == size) { break; } try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } if (size > 0L) { log.info("查询到{}条数据", size); AtomicInteger num = new AtomicInteger(0); ProgressVo progressVo = new ProgressVo(); progressVo.setTotalCount(size.intValue()); R<ProgressVo> result = R.data(progressVo); progressVo.setSuccessCount(num.get()); cidImportAccountProgress.saveProgress(taskId, result); Long finalSize = size; int pages = 1; int pageNo = 1; do { cidAccountSearchVO.setPageNo(pageNo); Page<CidAccount> cidAccountPage = cidClient.queryCidAccountPage(cidAccountSearchVO); pages = (int) cidAccountPage.getPages(); cidHandleThreadPoolTaskExecutor.submit(() -> { log.info("处理数量:{}", cidAccountPage.getRecords().size()); this.handleGroupPlan(cidAccountPage.getRecords(), taskId, finalSize.intValue(), num); }); pageNo++; } while (pageNo <= pages); } log.info("生成cid报表完成"); } private void handleGroupPlan(List<CidAccount> cidAccounts, String taskId, int accountListSize, AtomicInteger num) { ProgressVo progressVo = new ProgressVo(); progressVo.setTotalCount(accountListSize); R<ProgressVo> result = R.data(progressVo); for (CidAccount cidAccount : cidAccounts) { List<PlatformGoods> platformGoodsList = goodsClient.queryGoodsByAccountId(cidAccount.getTenantId(), cidAccount.getAccountId()); this.handlePlan(() -> { CidPlan cidPlan = new CidPlan(); cidPlan.setTenantId(cidAccount.getTenantId()); cidPlan.setAccountName(cidAccount.getAccount()); cidPlan.setChannel("巨量引擎"); cidPlan.setJlId(cidAccount.getAccountId()); cidPlan.setTotalConsumption(cidAccount.getConsume()); return cidPlan; }, platformGoodsList, null); num.getAndIncrement(); progressVo.setSuccessCount(num.get()); cidImportAccountProgress.saveProgress(taskId, result); } } private void handlePlan(Supplier<CidPlan> cidPlanSupplier, List<PlatformGoods> platformGoodsList, CidPlanSearchVo cidPlanSearchVo) { CidPlan cidPlan = cidPlanSupplier.get(); CidPlan result = orderSearchClient.statisticsCidOrder(cidPlan, DateUtil.toLocalDateTime(cidPlanSearchVo.getStartTime()), DateUtil.toLocalDateTime(cidPlanSearchVo.getEndTime())); BeanUtil.copyProperties(result, cidPlan); if (CollectionUtil.isNotEmpty(platformGoodsList)) { log.info("计划:{},没有找到商品", cidPlan.getJlId()); PlatformGoods platformGoods = platformGoodsList.get(0); cidPlan.setSalePrice(platformGoods.getSaleUnitPrice()); cidPlan.setPurchasePrice(platformGoods.getPurchaseUnitPrice()); cidPlan.setExpressFee(platformGoods.getExpressFee()); } } private List<CidOrderVO> filterOrderByDate(CidPlanSearchVo cidPlanSearchVo, List<CidOrderVO> orders) { if (cidPlanSearchVo == null) return orders; List<String> tradeNos = cidClient.selectOrderByTradeNoAndTime( orders.stream().map(CidOrderVO::getTradeNo).collect(Collectors.toList()), cidPlanSearchVo); //orderS过滤掉不包含tradeNo的订单 return orders.stream().filter(cidOrderVO -> tradeNos.contains(cidOrderVO.getTradeNo())).collect(Collectors.toList()); } private void handleOrder(List<CidOrderVO> data, Map<String, BigDecimal> param, Map<String, BigDecimal> deductionDetail, Map<String, Integer> orderParam) { int count = (int) data.stream().filter(cidOrderVO -> cidOrderVO.getCidOrderExtendVO().getRefundType() != null).count(); orderParam.put("refundOrderNum", orderParam.get("refundOrderNum") + count); orderParam.put("orderNum", orderParam.get("orderNum") + data.size()); for (CidOrderVO cidOrderVO : data) { if (cidOrderVO.getCidOrderExtendVO().getDeductionSum() != null) { param.put("deductionSum", param.get("deductionSum").add(cidOrderVO.getCidOrderExtendVO().getDeductionSum())); } if (cidOrderVO.getCidOrderExtendVO().getTechnologyFee() != null) { param.put("technologyFee", param.get("technologyFee").add(cidOrderVO.getCidOrderExtendVO().getTechnologyFee())); } if (cidOrderVO.getCidOrderExtendVO().getDdjb() != null) { param.put("ddjb", param.get("ddjb").add(cidOrderVO.getCidOrderExtendVO().getDdjb())); } if (cidOrderVO.getCidOrderExtendVO().getOrderProfit() != null) { param.put("profit", param.get("profit").add(cidOrderVO.getCidOrderExtendVO().getOrderProfit())); } if (cidOrderVO.getCidOrderExtendVO().getSalesCompensate() != null) { deductionDetail.put("salesCompensate", deductionDetail.get("salesCompensate").add(cidOrderVO.getCidOrderExtendVO().getSalesCompensate())); } if (cidOrderVO.getCidOrderExtendVO().getExpressCompensate() != null) { deductionDetail.put("expressCompensate", deductionDetail.get("expressCompensate").add(cidOrderVO.getCidOrderExtendVO().getExpressCompensate())); } if (cidOrderVO.getCidOrderExtendVO().getDelaySendGoods() != null) { deductionDetail.put("delaySendGoods", deductionDetail.get("delaySendGoods").add(cidOrderVO.getCidOrderExtendVO().getDelaySendGoods())); } if (cidOrderVO.getCidOrderExtendVO().getFalseSendGoods() != null) { deductionDetail.put("falseSendGoods", deductionDetail.get("falseSendGoods").add(cidOrderVO.getCidOrderExtendVO().getFalseSendGoods())); } if (cidOrderVO.getCidOrderExtendVO().getOutOfStock() != null) { deductionDetail.put("outOfStock", deductionDetail.get("outOfStock").add(cidOrderVO.getCidOrderExtendVO().getOutOfStock())); } } } public CidPlanSearchResp searchPlan(CidPlanSearchVo cidPlanSearchVo) { CidPlanSearchResp cidPlanSearchResp = new CidPlanSearchResp(); Page<CidPlan> cidPlanPage = cidClient.queryCidPlanPage(cidPlanSearchVo); List<CidPlan> cidPlans = cidPlanPage.getRecords(); if (ObjectUtil.isAllNotEmpty(cidPlanSearchVo.getStartTime(), cidPlanSearchVo.getEndTime())) { realTimeCompute(cidPlans, cidPlanSearchVo); } BigDecimal currentProfit = cidPlans.stream().map(CidPlan::getProfit).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, RoundingMode.HALF_UP); cidPlanSearchResp.setCurrentProfit(currentProfit); cidPlanSearchResp.setData(cidPlanPage); CidPlan result = orderSearchClient.statisticsCidOrder(new CidPlan(), DateUtil.toLocalDateTime(cidPlanSearchVo.getStartTime()), DateUtil.toLocalDateTime(cidPlanSearchVo.getEndTime())); cidPlanSearchResp.setTotalProfit(result.getProfit()); return cidPlanSearchResp; } private void realTimeCompute(List<CidPlan> cidPlans, CidPlanSearchVo cidPlanSearchVo) { if (CollectionsUtil.isEmpty(cidPlans)) return; computeCidPlan(cidPlans, cidPlanSearchVo, cidPlans.stream().map(CidPlan::getJlId).collect(Collectors.toList())); } private void computeCidPlan(List<CidPlan> cidPlans, CidPlanSearchVo cidPlanSearchVo, List<String> accountIds) { Map<String, List<PlatformGoods>> goodsMapGroupByBindJlId = goodsClient.queryGoodsByAccountIds(cidPlanSearchVo.getTenantId(), accountIds) .stream().collect(Collectors.groupingBy(PlatformGoods::getBindJlId)); CountDownLatch countDownLatch = new CountDownLatch(cidPlans.size()); cidPlans.forEach(cidPlan -> cidHandleThreadPoolTaskExecutor.execute(() -> { this.handlePlan(() -> cidPlan, goodsMapGroupByBindJlId.get(cidPlan.getJlId()), cidPlanSearchVo); countDownLatch.countDown(); })); try { countDownLatch.await(); } catch (InterruptedException e) { log.error("realTimeCompute error: {}", e.getMessage()); } } @Transactional(rollbackFor = Exception.class) public Boolean pushAccount(List<AdvertiserAccount> data) { List<String> accountIds = data.stream().map(AdvertiserAccount::getAdvertiserId).collect(Collectors.toList()); Map<String, List<PlatformGoods>> goodsMapGroupByBindJlId = goodsClient.queryGoodsByAccountIds(tenantId, accountIds) .stream().collect(Collectors.groupingBy(PlatformGoods::getBindJlId)); Map<String, CidPlan> cidPlanMap = cidClient.queryCidPlans(tenantId, accountIds).stream().collect(Collectors.toMap(CidPlan::getJlId, Function.identity())); CopyOnWriteArrayList<CidPlan> cidPlansList = new CopyOnWriteArrayList<>(); CountDownLatch count = new CountDownLatch(accountIds.size()); List<CidAccount> cidAccounts = data.stream().map(account -> { CidAccount cidAccount = account.mapToCidAccount(new HashMap<>(), tenantId); cidHandleThreadPoolTaskExecutor.execute(() -> { String accountId = cidAccount.getAccountId(); this.handlePlan(() -> dealHistoryPlan(cidAccount, cidPlanMap, cidPlansList), goodsMapGroupByBindJlId.get(accountId), null); count.countDown(); }); return cidAccount; }).collect(Collectors.toList()); cidClient.batchSaveCidPlan(cidPlansList); orderSearchClient.saveCidConsumption(convertEsCidOrderConsumption(data, new Date())); return cidClient.batchSaveCidAccount(cidAccounts); } private CidPlan dealHistoryPlan(CidAccount cidAccount, Map<String, CidPlan> cidPlanMap, CopyOnWriteArrayList<CidPlan> cidPlansList) { String accountId = cidAccount.getAccountId(); List<CidPlan.HistoryTotalConsumption> historyTotalConsumptions = cidPlanMap.containsKey(accountId) && CollectionsUtil.isNotEmpty(cidPlanMap.get(accountId).getHistoryTotalConsumptions()) ? cidPlanMap.get(accountId).getHistoryTotalConsumptions() : new ArrayList<>(); String today = DateUtil.formatDate(new Date()); BigDecimal consume = cidAccount.getConsume(); CidPlan.HistoryTotalConsumption todayData = new CidPlan.HistoryTotalConsumption(DateUtil.parseDate(today), consume); if (historyTotalConsumptions.isEmpty() || cidPlanMap.get(accountId).getHistoryTotalConsumptions().stream() .noneMatch(historyTotalConsumption -> today.equals(DateUtil.formatDate(historyTotalConsumption.getDate())))) { historyTotalConsumptions.add(todayData); } CidPlan cidPlan = new CidPlan(); cidPlan.setTenantId(cidAccount.getTenantId()); cidPlan.setAccountName(cidAccount.getAccount()); cidPlan.setChannel("巨量引擎"); cidPlan.setJlId(accountId); cidPlan.setTotalConsumption(consume); cidPlan.setHistoryTotalConsumption(JSON.toJSONString(historyTotalConsumptions)); cidPlansList.add(cidPlan); return cidPlan; } @Transactional(rollbackFor = Exception.class) public void pushHistoryData(HistoryPushAccountReq historyPushAccountReq) { String day = historyPushAccountReq.getDate(); DateTime time = DateUtil.parseDate(day); List<AdvertiserAccount> advertiserAccounts = historyPushAccountReq.getData(); List<String> accountIds = advertiserAccounts.stream().map(AdvertiserAccount::getAdvertiserId).collect(Collectors.toList()); Map<String, BigDecimal> historyDataMap = advertiserAccounts.stream().collect(Collectors.toMap(AdvertiserAccount::getAdvertiserId, AdvertiserAccount::getStatCost)); List<CidPlan> cidPlans = cidClient.queryCidPlans(tenantId, accountIds); cidPlans.forEach(cidPlan -> { BigDecimal consume = historyDataMap.get(cidPlan.getJlId()); CidPlan.HistoryTotalConsumption todayData = new CidPlan.HistoryTotalConsumption(time, consume); List<CidPlan.HistoryTotalConsumption> historyTotalConsumptions = cidPlan.getHistoryTotalConsumptions(); if (CollectionsUtil.isNotEmpty(historyTotalConsumptions)) { if (historyTotalConsumptions.stream() .noneMatch(consumption -> day.equals(DateUtil.formatDate(consumption.getDate())))) { historyTotalConsumptions.add(todayData); historyTotalConsumptions.sort(Comparator.comparing(CidPlan.HistoryTotalConsumption::getDate)); cidPlan.setHistoryTotalConsumption(JSON.toJSONString(historyTotalConsumptions)); } } else { cidPlan.setHistoryTotalConsumption(JSON.toJSONString(Collections.singleton(todayData))); } }); if (!cidPlans.isEmpty()) { cidClient.batchSaveCidPlan(cidPlans); orderSearchClient.saveCidConsumption(convertEsCidOrderConsumption(advertiserAccounts, time)); } } private List<CidOrderConsumption> convertEsCidOrderConsumption(List<AdvertiserAccount> advertiserAccounts, Date time) { return advertiserAccounts.stream().map(advertiserAccount -> new CidOrderConsumption(advertiserAccount, time.toInstant() .atZone(ZoneId.systemDefault()) .toLocalDate())).collect(Collectors.toList()); } public void syncData(String accountId) { CollectionsUtil.batchDealWholeData(pageNo -> { Page<CidPlan> cidPlanPage = cidClient.queryCidPlanPage(new CidPlanSearchVo(pageNo, 1000, tenantId, accountId)); List<CidPlan> cidPlans = cidPlanPage.getRecords(); log.info("cidPlans:{}", JSON.toJSONString(cidPlans)); List<CidOrderConsumption> cidOrderConsumptions = cidPlans.stream().flatMap(cidPlan -> cidPlan.getHistoryTotalConsumptions().stream() .filter(historyTotalConsumption -> BigDecimal.ZERO.compareTo(historyTotalConsumption.getTotalConsumption()) < 0) .map(historyTotalConsumption -> new CidOrderConsumption(cidPlan.getJlId(), historyTotalConsumption)) ).collect(Collectors.toList()); log.info("cidOrderConsumptions:{}", JSON.toJSONString(cidOrderConsumptions)); if (CollectionUtil.isNotEmpty(cidOrderConsumptions)) { orderSearchClient.saveCidConsumption(cidOrderConsumptions); } return cidPlans; }); } } 并且告诉我每一行为什么这样写
07-01
package com.boe.cim.teacher.luence; import java.io.StringReader; import java.nio.file.Paths; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TermRangeQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.WildcardQuery; import org.apache.lucene.search.highlight.Fragmenter; import org.apache.lucene.search.highlight.Highlighter; import org.apache.lucene.search.highlight.QueryScorer; import org.apache.lucene.search.highlight.SimpleHTMLFormatter; import org.apache.lucene.search.highlight.SimpleSpanFragmenter; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.search.NumericRangeQuery; import com.boe.cim.teacher.pojo.RequirementInfo; public class LuceneSearchRequirement { /** * @param indexreDir * 索引文件路径path * @param queryField * 被索引字段 Field * @param queryMsg * 索引值 * @return * @throws Exception */ public List<RequirementInfo> search(String indexreDir, String queryField, String queryMsg) throws Exception { // 得到读取索引文件的路径 Directory dir = FSDirectory.open(Paths.get(indexreDir)); // 通过dir得到的路径下的所有的文件 // 建立索引查询器 IndexReader reader = DirectoryReader.open(dir); IndexSearcher searcher = new IndexSearcher(reader); // 中文分词器 SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer(); // ======== 新增:日期范围查询处理 ======== Query dateQuery = null; // TermRangeQuery dateQuery= null; ; if ("requirementtime".equals(queryField)) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); long minTime = sdf.parse(queryMsg).getTime(); // dateQuery = LongPoint.newRangeQuery("time", minTime, Long.MAX_VALUE); // dateQuery = TermRangeQuery.newStringRange("requirementtime", queryMsg, null, true, true); dateQuery = NumericRangeQuery.newLongRange( "requirementtime", minTime, Long.MAX_VALUE, true, // 包含下界 true // 包含上界 ); } // 建立查询解析器 /** * 第一个参数是要查询的字段; 第二个参数是分析器Analyzer */ // QueryParser parser = new QueryParser(queryField, analyzer); // 根据传进来的par查找 // Query query = parser.parse(queryMsg); // Query query = new TermQuery(new Term("teacher",queryMsg)); // Query query = new WildcardQuery(new Term(queryField,"*"+queryMsg+"*")); Query query; //这四种类型需要特别匹配,不需要分词器进行搜索 if(queryField.equals("requirement") || queryField.equals("department") || queryField.equals("liaisonman") || queryField.equals("requirementtype") ) { query = new WildcardQuery(new Term(queryField,"*"+queryMsg+"*")); }else { QueryParser parser = new QueryParser(queryField, analyzer); query = parser.parse(queryMsg); } // 计算索引开始时间 long start = System.currentTimeMillis(); // ======== 新增:组合日期查询 ======== if (dateQuery != null) { BooleanQuery.Builder builder = new BooleanQuery.Builder(); builder.add(query, BooleanClause.Occur.MUST); builder.add(dateQuery, BooleanClause.Occur.MUST); query = builder.build(); } // // 计算索引开始时间 // long start = System.currentTimeMillis(); // 开始查询 /** * 第一个参数是通过传过来的参数来查找得到的query; 第二个参数是要出查询的行数 */ TopDocs topDocs = searcher.search(query, 104); // 索引结束时间 long end = System.currentTimeMillis(); System.out.println("匹配:["+queryField+"]," + queryMsg + ",总共花费了" + (end - start) + "毫秒,共查到" + topDocs.totalHits + "条记录。"); // 高亮显示start // 算分 QueryScorer scorer = new QueryScorer(query); // 显示得分高的片段 Fragmenter fragmenter = new SimpleSpanFragmenter(scorer); // 设置标签内部关键字的颜色 // 第一个参数:标签的前半部分;第二个参数:标签的后半部分。 SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<b><font color='red'>", "</font></b>"); // 第一个参数是对查到的结果进行实例化;第二个是片段得分(显示得分高的片段,即摘要) Highlighter highlighter = new Highlighter(simpleHTMLFormatter, scorer); // 设置片段 highlighter.setTextFragmenter(fragmenter); // 高亮显示end // 遍历topDocs /** * ScoreDoc:是代表一个结果的相关度得分与文档编号等信息的对象。 scoreDocs:代表文件的数组 * * @throws Exception */ List<RequirementInfo> listinfo = new ArrayList<>(); for (ScoreDoc scoreDoc : topDocs.scoreDocs) { RequirementInfo requirementinfo = new RequirementInfo(); // 获取文档 Document document = searcher.doc(scoreDoc.doc); // 输出全路径 String id = document.get("id"); String queryType = document.get(queryField); requirementinfo.setId(Integer.parseInt(id)); if (id != null) { // 把全部得分高的摘要给显示出来 // 第一个参数是对哪个参数进行设置;第二个是以流的方式读入 TokenStream tokenStream = analyzer.tokenStream(queryField, new StringReader(queryType)); // 获取最高的片段 String highlighterString; if(queryField.equals("requirement") || queryField.equals("department") || queryField.equals("liaisonman") || queryField.equals("requirementtype") ) { highlighterString = queryType.replaceAll(queryMsg, "<b><font color='red'>"+queryMsg+"</font></b>"); }else { highlighterString = highlighter.getBestFragment(tokenStream, queryType); } // String highlighterString = highlighter.getBestFragment(new SmartChineseAnalyzer(), queryField, queryType); //设置高亮字段 switch (queryField) { // 根据搜索条件进行赋值 case "requirement": //需求名称 requirementinfo.setRequirement(highlighterString); break; case "department": //需求组织 requirementinfo.setDepartment(highlighterString); break; case "liaisonman": //需求联系人 requirementinfo.setLiaisonman(highlighterString); break; case "requirementtype": //需求类型 requirementinfo.setRequirementtype(Integer.parseInt(highlighterString.replaceAll("<[^>]+>", ""))); break; case "requirementcontents": //需求内容 requirementinfo.setRequirementcontents(highlighterString); break; case "requirementbackground": //需求背景 requirementinfo.setRequirementbackground(highlighterString); break; } listinfo.add(requirementinfo); } } reader.close(); return listinfo; } } package com.boe.cim.teacher.controller; import java.util.ArrayList; import java.util.List; import java.util.ListIterator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.boe.cim.teacher.dto.CommonOutPutResult; import com.boe.cim.teacher.luence.LuceneSearchRequirement; import com.boe.cim.teacher.pojo.RequirementInfo; import com.boe.cim.teacher.service.RequirementInfoService; import io.swagger.annotations.ApiOperation; import net.sf.json.JSONObject; @RestController @RequestMapping("/requirement/select") public class RequirementInfoSelectFunctionController { @Value("${lucenere.indexre.dir}") private String IndexreDir; @Autowired private RequirementInfoService requirementInfoService; @GetMapping("/simpleselect") @ApiOperation("简单搜索") public CommonOutPutResult simpleselect(String FieldType, String queryMsg) { try { List<RequirementInfo> list = new LuceneSearchRequirement().search(IndexreDir, FieldType, queryMsg); List<RequirementInfo> listResult = requirementInfoService.requirementInfoSelectResult(list); List<List<RequirementInfo>> listTotal = new ArrayList<>(); JSONObject data = new JSONObject(); data.put("count", listResult.size()); data.put("page", Math.ceil((float) listResult.size() / 7)); ListIterator<RequirementInfo> lit = listResult.listIterator(); List<RequirementInfo> tempL = new ArrayList<>(10); int index = 0; // 把List中的需求信息分为 8 个一组 变成 List<List<RequirementInfo>> while (lit.hasNext()) { index++; RequirementInfo tempT = lit.next(); tempL.add(tempT); if (index == 7 || !lit.hasNext()) {// 7个 List<RequirementInfo> temp = new ArrayList<>(tempL); listTotal.add(temp); tempL.clear(); index = 0; } } CommonOutPutResult result = new CommonOutPutResult(true, "搜索成功", "200", data); result.setList(listTotal); return result; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return new CommonOutPutResult(false, "搜索失败", "200", null); } @GetMapping("/highselect") @ApiOperation("高级搜索功能") public CommonOutPutResult highselect(@RequestParam(value = "requirement") String requirement, @RequestParam(value = "department") String department, @RequestParam(value = "liaisonman") String liaisonman, @RequestParam(value = "requirementtype") String requirementtype, @RequestParam(value = "requirementcontents") String requirementcontents) throws Exception { // 不好判断是否是集合为空还是字段为空导致的结果 List<RequirementInfo> listRequirement = new ArrayList<>(); List<RequirementInfo> listDepartment = new ArrayList<>(); List<RequirementInfo> listLiaisonman = new ArrayList<>(); List<RequirementInfo> listRequirementtype = new ArrayList<>(); List<RequirementInfo> listRequirementcontents = new ArrayList<>(); // 需求列表 if (!requirement.equals("")) { listRequirement = new LuceneSearchRequirement().search(IndexreDir, "requirement", requirement); } // 组织系别 if (!department.equals("")) { listDepartment = new LuceneSearchRequirement().search(IndexreDir, "department", department); } // 联系人列表 if (!liaisonman.equals("")) { listLiaisonman = new LuceneSearchRequirement().search(IndexreDir, "liaisonman", liaisonman); } // 需求类型 if (!requirementtype.equals("")) { listRequirementtype = new LuceneSearchRequirement().search(IndexreDir, "requirementtype", requirementtype); } // 研究方向 if (!requirementcontents.equals("")) { listRequirementcontents = new LuceneSearchRequirement().search(IndexreDir, "requirementcontents", requirementcontents); } // 需求列表 boolean setFirstList = false; List<RequirementInfo> listTemp = new ArrayList<>(); if (!requirement.equals("")) { // 第一个只可能是赋值List listTemp = listRequirement; setFirstList = true; } // 组织系别 if (!department.equals("") && setFirstList) { listTemp.retainAll(listDepartment); //标红字段 for(int i=0;i<listTemp.size();i++) { RequirementInfo requirementTemp = listTemp.get(i); requirementTemp.setDepartment(listDepartment.get(listDepartment.indexOf(requirementTemp)).getDepartment()); listTemp.set(i, requirementTemp); } } else if (!department.equals("") && !setFirstList) { listTemp = listDepartment; } // 需求联系人 if (!liaisonman.equals("") && setFirstList) { listTemp.retainAll(listLiaisonman); //标红字段 for(int i=0;i<listTemp.size();i++) { RequirementInfo requirementTemp = listTemp.get(i); requirementTemp.setLiaisonman(listLiaisonman.get(listLiaisonman.indexOf(requirementTemp)).getLiaisonman()); listTemp.set(i, requirementTemp); } } else if (!liaisonman.equals("") && !setFirstList) { listTemp = listLiaisonman; } // 需求类型 if (!requirementtype.equals("") && setFirstList) { listTemp.retainAll(listRequirementtype); //标红字段 for(int i=0;i<listTemp.size();i++) { RequirementInfo requirementTemp = listTemp.get(i); requirementTemp.setRequirementtype(listRequirementtype.get(listRequirementtype.indexOf(requirementTemp)).getRequirementtype()); listTemp.set(i, requirementTemp); } } else if (!requirementtype.equals("") && !setFirstList) { listTemp = listRequirementtype; } // 研究方向 if (!requirementcontents.equals("") && setFirstList) { listTemp.retainAll(listRequirementcontents); //标红字段 for(int i=0;i<listTemp.size();i++) { RequirementInfo requirementTemp = listTemp.get(i); requirementTemp.setRequirementcontents(listRequirementcontents.get(listRequirementcontents.indexOf(requirementTemp)).getRequirementcontents()); listTemp.set(i, requirementTemp); } } else if (!requirementcontents.equals("") && !setFirstList) { listTemp = listRequirementcontents; } List<RequirementInfo> listResult = requirementInfoService.requirementInfoSelectResult(listTemp); List<List<RequirementInfo>> listTotal = new ArrayList<>(); JSONObject data = new JSONObject(); data.put("count", listResult.size()); data.put("page", Math.ceil((float) listResult.size() / 7)); ListIterator<RequirementInfo> lit = listResult.listIterator(); List<RequirementInfo> tempL = new ArrayList<>(10); int index = 0; // 把List中的教师信息分为 8 个一组 变成 List<List<TeacherInfo>> while (lit.hasNext()) { index++; RequirementInfo tempT = lit.next(); tempL.add(tempT); if (index == 7 || !lit.hasNext()) {// 7个 List<RequirementInfo> temp = new ArrayList<>(tempL); listTotal.add(temp); tempL.clear(); index = 0; } } CommonOutPutResult result = new CommonOutPutResult(true, "搜索成功", "200", data); result.setList(listTotal); return result; } @GetMapping("/allreqiurement") @ApiOperation("获取所有需求") public CommonOutPutResult getAllRequirements() { List<RequirementInfo> requirements = requirementInfoService.getAllRequirements(); List<List<RequirementInfo>> listTotal = new ArrayList<>(); JSONObject data = new JSONObject(); data.put("count", requirements.size()); data.put("page", Math.ceil((float) requirements.size() / 7)); ListIterator<RequirementInfo> lit = requirements.listIterator(); List<RequirementInfo> tempL = new ArrayList<>(10); int index = 0; // 把List中的教师信息分为 8 个一组 变成 List<List<TeacherInfo>> while (lit.hasNext()) { index++; RequirementInfo tempT = lit.next(); tempL.add(tempT); if (index == 7 || !lit.hasNext()) {// 7个 List<RequirementInfo> temp = new ArrayList<>(tempL); listTotal.add(temp); tempL.clear(); index = 0; } } CommonOutPutResult result = new CommonOutPutResult(true, "获取需求成功", "200", data); result.setList(listTotal); return result; } } package com.boe.cim.teacher.service; import java.util.List; import com.boe.cim.teacher.dto.RequirementInfoUpdate; import com.boe.cim.teacher.pojo.RequirementInfo; import net.sf.json.JSONArray; public interface RequirementInfoService { RequirementInfo requirementInfoInsert(RequirementInfo requirementinfo)throws Exception; JSONArray department(); String ChangeRequirementInfoFileList(String filename,String filetype,String id,String requirement)throws Exception; //需求信息更新 ( 1.基本需求信息更新 2.详细需求信息更新 3.技术寻源更新 ) void updaterequirementinfo(RequirementInfoUpdate requirementInfoUpdate); //返回一个经过处理后的需求List 符合搜索结果并且高亮字段 List<RequirementInfo> requirementInfoSelectResult(List<RequirementInfo> listOkRequirementInfo); //返回一个所有的需求List List<RequirementInfo> getAllRequirements() ; void insertNewDepartment(String department)throws Exception; } package com.boe.cim.teacher.service.impl; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; //import org.springframework.beans.factory.annotation.Value; import org.springframework.dao.DuplicateKeyException; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import com.boe.cim.teacher.dao.RequirementInfoMapper; import com.boe.cim.teacher.dto.RequirementInfoUpdate; import com.boe.cim.teacher.pojo.RequirementInfo; import com.boe.cim.teacher.service.RequirementInfoService; //import com.boe.cim.teacher.luence.LuceneIndexRequirement; import net.sf.json.JSONArray; import net.sf.json.JSONObject; @Service public class RequirementInfoServiceImpl implements RequirementInfoService { @Autowired private RequirementInfoMapper requirementInfoMapper; @Autowired private StringRedisTemplate redisTemplate; // @Value("${lucenere.indexre.dir}") // private String IndexreDir; // @Autowired // private LuceneIndexRequirement LuceneIndexrequirement; @Override public RequirementInfo requirementInfoInsert(RequirementInfo requirementinfo) throws Exception { // TODO Auto-generated method stub try { requirementInfoMapper.insertNewRequirementInfo(requirementinfo); } catch (DuplicateKeyException e) { e.printStackTrace(); throw new Exception("该需求已导入:" + e.getCause()); } catch (Exception e) { e.printStackTrace(); throw new Exception("导入时发生未知错误,错误原因如下:" + e.getCause()); } // System.out.println("requirementcontents = " + requirementinfo.getRequirementcontents()); // System.out.println("requirementcontents = " + requirementinfo.getTechnicalsource ()); // try { // new LuceneIndexRequirement().indexSingleRequirement(IndexreDir, requirementinfo); // 调用新增方法 // } catch (Exception e) { // e.printStackTrace(); // throw new Exception("新增需求索引更新失败" + e.getCause()); // } redisTemplate.convertAndSend("requirementinfo",String.valueOf(requirementinfo.getId()));//将ID发送到队列中用于更新需求索引信息 return requirementInfoMapper.getRequirementInfoByUniquekey(requirementinfo); } @Override public JSONArray department() { // TODO Auto-generated method stub JSONArray result = new JSONArray(); List<String> departments = requirementInfoMapper.distinctdepartment(); List<JSONObject> departmentResult = new ArrayList<>(); for (String department : departments) { JSONObject departmentObject = new JSONObject(); departmentObject.put("value", department); departmentResult.add(departmentObject); } result.add(departmentResult); return result; } @Override public String ChangeRequirementInfoFileList(String filename, String filetype, String id, String requirement) throws Exception { // TODO Auto-generated method stub RequirementInfo requirementinfo = requirementInfoMapper.getRequirementByIdAndName(Integer.parseInt(id), requirement); String updateFileString = ""; switch (filetype) { case "附件": String visitexcels = requirementinfo.getVisitexcel(); updateFileString = filenamedelete(filename, visitexcels); try { requirementInfoMapper.updateRequirementFileList(filetype, updateFileString, Integer.parseInt(id), requirement); } catch (Exception e) { e.printStackTrace(); throw new Exception("更新数据库文件列表失败,请联系管理员手动删除"); } break; } return updateFileString; } String filenamedelete(String filename, String filenames) { String[] files = filenames.split(";"); String result = ""; for (String fileSplitName : files) { if (!filename.equals(fileSplitName)) result += fileSplitName + ";"; } return result; } @Override public void updaterequirementinfo(RequirementInfoUpdate requirementInfoUpdate) { // TODO Auto-generated method stub String type = requirementInfoUpdate.getType(); switch (type) { case "基本信息": requirementInfoMapper.updatenormalinfo(requirementInfoUpdate.getId(), requirementInfoUpdate.getRequirement(), requirementInfoUpdate.getDepartment(),requirementInfoUpdate.getLiaisonman(), requirementInfoUpdate.getTelphone()); break; case "详细信息": requirementInfoMapper.updaterequirementdetail(requirementInfoUpdate.getId(), requirementInfoUpdate.getRequirement(), requirementInfoUpdate.getRequirementbackground(), requirementInfoUpdate.getRequirementdescribe(), requirementInfoUpdate.getRequirementcontents(), requirementInfoUpdate.getAcceptstandard(), requirementInfoUpdate.getBudget(),requirementInfoUpdate.getAssessment()); //只有科研能力才需要更新索引 // redisTemplate.convertAndSend("requirementinfoupdate",String.valueOf(requirementInfoUpdate.getId()));//将ID发送到队列中用于更新需求索引信息 break; case "技术寻源": requirementInfoMapper.updatecorporation(requirementInfoUpdate.getId(), requirementInfoUpdate.getRequirement(),requirementInfoUpdate.getTechnicalsource()); break; } redisTemplate.convertAndSend("requirementinfoupdate",String.valueOf(requirementInfoUpdate.getId()));//将ID发送到队列中用于更新需求索引信息 } @Override public List<RequirementInfo> requirementInfoSelectResult(List<RequirementInfo> listOkRequirementInfo) { // TODO Auto-generated method stub List<RequirementInfo> listAllrequirement =requirementInfoMapper.RequirementInfoCreateLuceneIndex(); List<RequirementInfo> listResultRequirementInfo = new ArrayList<>(); for(RequirementInfo requirementinfo : listOkRequirementInfo) { RequirementInfo resultRequirementInfo = new RequirementInfo(); int index = listAllrequirement.indexOf(requirementinfo); if(index < 0)continue; resultRequirementInfo = listAllrequirement.get(index); //高亮字段的赋值转移,处理的是能被搜索的字段 if(requirementinfo.getRequirement() != null) { resultRequirementInfo.setRequirement(requirementinfo.getRequirement()); } if(requirementinfo.getLiaisonman() != null) { resultRequirementInfo.setLiaisonman(requirementinfo.getLiaisonman()); } if(requirementinfo.getRequirementbackground() != null) { resultRequirementInfo.setRequirementbackground(requirementinfo.getRequirementbackground()); } if(requirementinfo.getRequirementdescribe() != null) { resultRequirementInfo.setRequirementdescribe(requirementinfo.getRequirementdescribe()); } if(requirementinfo.getDepartment() != null) { resultRequirementInfo.setDepartment(requirementinfo.getDepartment()); } if(requirementinfo.getRequirementcontents() != null) { resultRequirementInfo.setRequirementcontents(requirementinfo.getRequirementcontents()); } if(requirementinfo.getRequirementtype() != -1) { resultRequirementInfo.setRequirementtype(requirementinfo.getRequirementtype()); } listResultRequirementInfo.add(resultRequirementInfo); } return listResultRequirementInfo; } @Override public List<RequirementInfo> getAllRequirements() { // 假设LuceneSearchRequirement提供静态访问方法 List<RequirementInfo> listAllrequirement =requirementInfoMapper.RequirementInfoCreateLuceneIndex(); return listAllrequirement; } @Override public void insertNewDepartment(String department) throws Exception{ // TODO Auto-generated method stub List<String> listdepartment = requirementInfoMapper.selectSeeIfExits(department); //不存在,插入数据库 if(listdepartment.size() == 0) { requirementInfoMapper.insertNewDepartment(department); } } } 上面给出的是已有代码,请在此基础上修改,实现将MySQL 中requirement table中requirementtime记录的是需求时间string类型,格式为 YYYY-MM-DD,生成luenceindex检索文档,现在从luenceindex检索大于vue前端传来的日期(string类型)的需求并返回结果列表,请问具体实现代码:
最新发布
09-16
/** * 推送调度信息 */ @Component @Async @Slf4j @ConditionalOnProperty(prefix = "push.suining", name = "enable", havingValue = "true", matchIfMissing = false) public class DispatchTimeNodeEventListener implements ApplicationListener<DispatchTimeNodeEvent> { @Autowired private ICarOutMapper iCarOutMapper; @Autowired private IAmbMapper iAmbMapper; @Autowired private RedisUtil redisUtil; @Autowired private TaskEmergencyConfig taskEmergencyConfig; @Override public void onApplicationEvent(DispatchTimeNodeEvent event) { //获取调度id DispatchInfoDto dto = (DispatchInfoDto) event.getSource(); Long dispatchId = dto.getDispatchId(); log.info("接受到的参数:{}", dispatchId); //查询调度出车信息 List<CarOutPo> carOutPos = iCarOutMapper.selectList(Wrappers.<CarOutPo>lambdaQuery().eq(CarOutPo::getTaskDispatchId, dispatchId)); if (CollectionUtil.isEmpty(carOutPos)) { return; } List<Integer> carOutStatus = CarOutStatusEnum.pushSH(); //过滤满足条件的出车状态 carOutPos = carOutPos.stream().filter(carOut -> carOutStatus.contains(carOut.getStatus()) || CarOutStatusEnum.isCancel(carOut.getStatus())).collect(Collectors.toList()); //不过滤状态,保证当前状态下有值,时间有值,及时补偿,redis只保存推送成功的 List<CarOutPo> newCarOut = new ArrayList<>(); //判断该阶段是否已经推送过数据 for (CarOutPo carOutPo : carOutPos) { //组装redisKey String redisKey = String.format("PushNode:%s:%s", dispatchId, carOutPo.getAmbId()); List<Integer> status = (List<Integer>) redisUtil.get(redisKey); log.info("redis缓冲的数据:{}",JSONArray.toJSONString(status)); if (CollectionUtils.isEmpty(status) || !status.contains(carOutPo.getStatus())) { log.info("节点状态:{},{}",dispatchId,status); List<Integer> integers = status == null ? new ArrayList<>() : status; integers.add(carOutPo.getStatus()); redisUtil.set(redisKey, integers, 24 * 60 * 60 * 2); newCarOut.add(carOutPo); } // 判断是否是最后一个节点:做补偿机制 if (Objects.equals(carOutPo.getStatus(), CarOutStatusEnum.CAR_ARRIVED_HOSPITAL.getCode())) { log.info("是否进入补偿机制"); List<Integer> pushSH = CarOutStatusEnum.pushSH(); List<Integer> redisStatus = (List<Integer>) redisUtil.get(redisKey); log.info("进入缓冲机制的redis里面的数据:{}",JSONArray.toJSONString(redisStatus)); pushSH.removeAll(redisStatus); log.info("需要补偿的节点数据:{}",JSONArray.toJSONString(pushSH)); if(CollectionUtil.isNotEmpty(pushSH)){ for (Integer zt : pushSH) { CarOutPo carOutPo1 = BeanUtil.copyProperties(carOutPo, CarOutPo.class); log.info("补偿节点状态:{},{}",dispatchId,zt); List<Integer> integers = new ArrayList<>(redisStatus); integers.add(zt); redisUtil.set(redisKey, integers, 24 * 60 * 60 * 2); carOutPo1.setStatus(zt); newCarOut.add(carOutPo1); } } } } if (CollectionUtil.isEmpty(newCarOut)) { return; } List<CarOutPo> collect = newCarOut.stream().sorted(Comparator.comparing(CarOutPo::getStatus)).collect(Collectors.toList()); collect.forEach(carOutPo -> { CarOutInfoDto build = CarOutInfoDto.builder() .jz_dddh(dispatchId.toString()) .ztdm(Optional.ofNullable(carOutPo.getAmbId()).map(Object::toString).orElse(null)) .ztpz(Optional.ofNullable(carOutPo.getPlateNumber()).filter(StringUtils::isNotBlank).orElse(null)) .jdmc(Optional.ofNullable(carOutPo.getStatus()).map(CarOutStatusEnum::carOutStatusName).orElse(null)) .ycyy(Optional.ofNullable(carOutPo.getCancelReason()).filter(StringUtils::isNotBlank).orElse(null)).build(); if (carOutPo.getStatus() != null) { if (Objects.equals(carOutPo.getStatus(), CarOutStatusEnum.CAR_DEPARTED.getCode())) { build.setJlsj(Optional.ofNullable(carOutPo.getLeaveTime()).map(item -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item)).orElse(null)); } else if (Objects.equals(carOutPo.getStatus(), CarOutStatusEnum.CAR_ARRIVED_SITE.getCode())) { build.setJlsj(Optional.ofNullable(carOutPo.getArriveTime()).map(item -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item)).orElse(null)); } else if (Objects.equals(carOutPo.getStatus(), CarOutStatusEnum.CAR_RETURN.getCode())) { build.setJlsj(Optional.ofNullable(carOutPo.getReturnTime()).map(item -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item)).orElse(null)); } else if (Objects.equals(carOutPo.getStatus(), CarOutStatusEnum.CAR_ARRIVED_HOSPITAL.getCode())) { build.setJlsj(Optional.ofNullable(carOutPo.getCompletedTime()).map(item -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item)).orElse(null)); } else if (CarOutStatusEnum.isCancel(carOutPo.getStatus())) { build.setJlsj(Optional.ofNullable(carOutPo.getCancelTime()).map(item-> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item)).orElse(null)); } } String param = JSONObject.toJSONString(build); retryPush( param,dispatchId); }); } private static final int MAX_RETRIES = 3; private static final long RETRY_INTERVAL_MS = 1000; // 每次重试间隔1秒 public boolean retryPush( String param,Long dispatchId) { int attempt = 0; while (attempt < MAX_RETRIES) { try { if (executePush(param,dispatchId)) { return true; // 推送成功,直接返回 } } catch (Exception e) { log.warn("推送失败(第{}次尝试): {}", attempt + 1, e.getMessage()); } attempt++; if (attempt < MAX_RETRIES) { try { Thread.sleep(RETRY_INTERVAL_MS); // 等待一段时间后重试 } catch (InterruptedException ie) { Thread.currentThread().interrupt(); log.error("线程中断:{}", ie.getMessage()); } } } return false; // 所有重试均失败 } private boolean executePush(String param,Long dispatchId) { log.info("推送的参数:{}", param); log.info("推送地址:{}", taskEmergencyConfig.getBaseUrl() + taskEmergencyConfig.getPushDispatchInfoUrl()); log.info("推送header参数:{}", taskEmergencyConfig.getThirdInstitutionID()); String resultJson = HttpUtils.sendHttps(taskEmergencyConfig.getBaseUrl() + taskEmergencyConfig.getPushDispatchInfoUrl(), taskEmergencyConfig.getThirdInstitutionID(), param); log.info("响应数据:{}", resultJson); if (StringUtils.isNotBlank(resultJson)) { JSONObject jsonObject = JSONObject.parseObject(resultJson); if (!Objects.equals(jsonObject.getString("resultcode"), "0")) { log.error("响应错误提示:{}", jsonObject.getString("message")); //失败的要贴出并补偿 //并将发送数据存入redis中做备份 String key = String.format("TimeNode:%s", dispatchId); redisUtil.set(key, param); return false; } return true; } return false; } } 推送代码如上,根据以上代码优化逻辑,保证推送状态有序按照CAR_DEPARTED、CAR_ARRIVED_SITE、CAR_RETURNCAR_ARRIVED_HOSPITAL按照有序推送,retryPush失败后不保存推送当前状态,输出完整的代码
05-28
public List<SfaPhotoDTO> getSfaPhotoList2(String bcodeCode, String sfaStoreCode, String sfaActivityStartDate, String sfaActivityEndDate, String agreementCode, BigDecimal agreementAmount, String sfaStoreName,String prCode) throws Exception { List<SfaPhotoDTO> sfaPhotoDTOList =new ArrayList<>(); List<Map<String, String>> resultArraryList = new ArrayList(); String openid = "8593263660775352654"; String appkey = "iZ0vk77OluDhoc3bKh"; List<String> photoLink = new ArrayList(); String resp; JSONObject jsonObject; int number = 1; do { Long msgId = java.util.UUID.randomUUID().getLeastSignificantBits() * -1; String timestamp = new SimpleDateFormat("yyyyMMddHHmmSS").format(new Date()); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS"); StringBuffer stringBufferS = new StringBuffer(); StringBuffer stringBufferE = new StringBuffer(); stringBufferS.append(sfaActivityStartDate).append(" ").append("00:00:00"); stringBufferE.append(sfaActivityEndDate).append(" ").append("00:00:00"); Date parseS = dateFormat.parse(stringBufferS.toString()); Date parseE = dateFormat.parse(stringBufferE.toString()); String startTime = dateFormat.format(parseS); String endTime = dateFormat.format(parseE); String jsonData = "{\n" + " \"create_start\": \"" + startTime + "\",\n" + " \"create_end\": \"" + endTime + "\",\n" + " \"comment_time_start\": \"\",\n" + " \"comment_time_end\": \"\",\n" + " \"cm_code\": \"" + (sfaStoreCode != null ? sfaStoreCode : "") + "\",\n" + " \"page_number\": \"" + number + "\",\n" + " \"page_size\": \"20\"\n" + "}"; String digest = encodeMd5(jsonData + "|" + appkey + "|" + timestamp); String url = "https://openapi.waiqin365.com/api/kenvue/v1/queryCxActivity/" + openid + "/" + timestamp + "/" + digest + "/" + msgId; resp = postJsonString(url, jsonData); jsonObject = JSON.parseObject(resp); if (jsonObject.get("response_data")!=null){ List<SFAAiPhotoData> responseData1 = JSONArray.parseArray(jsonObject.get("response_data").toString(), SFAAiPhotoData.class); for (SFAAiPhotoData form : responseData1) { Map<String, String> resultMap = new HashMap<>(); List<FormField> formDataList = form.getFormDataList(); String imagePath = null; // 遍历表单字段 for (FormField field : formDataList) { String fieldValue = field.getFieldValue(); String fieldName = field.getFieldName(); if (fieldName.contains("水印图片")){ if (StringUtils.isNotBlank(fieldValue)) { if (fieldValue.contains(".jpg") || fieldValue.contains(".png")) { imagePath = parseImagePath(fieldValue); break; } } } } // 如果找到图片路径,则放入结果映射 if (imagePath != null) { resultMap.put("image_path", imagePath); String[] split = imagePath.split(","); for (int i = 0; i < split.length; i++) { System.out.println(split[i]); photoLink.add(split[i]); } } resultArraryList.add(resultMap); } }else { break; } number++; } while (!jsonObject.get("response_data").equals("[]")); try { //查询是否已经有关联过的照片 String tpAgreementPhotoLink = this.getTpAgreementPhotoLinkContainSos(bcodeCode, sfaStoreCode,sfaActivityStartDate,sfaActivityEndDate,prCode); for (int i = 0; i < photoLink.size(); i++) { if (tpAgreementPhotoLink!=null && !tpAgreementPhotoLink.equals("")){ if (tpAgreementPhotoLink.contains(photoLink.get(i))){ sfaPhotoDTOList.add(new SfaPhotoDTO(i+1L,"duitou",sfaStoreCode,photoLink.get(i),bcodeCode,photoLink.get(i),true,agreementAmount,agreementCode,sfaActivityStartDate,sfaActivityEndDate,sfaStoreName,prCode)); }else { sfaPhotoDTOList.add(new SfaPhotoDTO(i+1L,"duitou",sfaStoreCode,photoLink.get(i),bcodeCode,null,false,agreementAmount,agreementCode,sfaActivityStartDate,sfaActivityEndDate,sfaStoreName,prCode)); } }else { sfaPhotoDTOList.add(new SfaPhotoDTO(i+1L,"duitou",sfaStoreCode,photoLink.get(i),bcodeCode,null,agreementAmount,agreementCode,sfaActivityStartDate,sfaActivityEndDate,sfaStoreName,prCode)); } } //如果没有查询到照片只返回头信息 if (photoLink.size()==0){ sfaPhotoDTOList.add(new SfaPhotoDTO(sfaStoreCode,bcodeCode,agreementAmount,agreementCode,sfaActivityStartDate,sfaActivityEndDate,sfaStoreName,prCode)); } }catch (Exception e){ e.printStackTrace(); }finally { } // sfaPhotoDTOList.stream().sorted(Comparator.comparing((SfaPhotoDTO dto) -> dto.getAssociatePhotoLinks()!=null,Comparator.reverseOrder())).collect(Collectors.toList()); List<SfaPhotoDTO> sortedList = new ArrayList<>(); List<SfaPhotoDTO> nullAssociatePhotoLinksList = new ArrayList<>(); for (SfaPhotoDTO dto : sfaPhotoDTOList) { if (dto.getAssociatePhotoLinks()!= null) { sortedList.add(dto); } else { nullAssociatePhotoLinksList.add(dto); } } sortedList.addAll(nullAssociatePhotoLinksList); //如果传过来的门店编码时间段在mapping表中有,则进入下需要查询SOS接口 // 修改后的HQL(直接使用日期类型) String hql = "FROM com.erry.jnjhoco.payment.entity.SFATaskMapping mapping " + "WHERE mapping.sfaStoreCode = :storeCode AND mapping.sfaTaskDate BETWEEN :startDate AND :endDate"; Map<String, Object> params = new HashMap<>(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date parseStart = dateFormat.parse(sfaActivityStartDate+" 00:00:00"); Date parseEnd = dateFormat.parse(sfaActivityEndDate+" 00:00:00"); params.put("storeCode", sfaStoreCode); params.put("startDate", parseStart); params.put("endDate", parseEnd); List<SFATaskMapping> sfaTaskMappings = daoService.retrieve(hql, params); if (!sfaTaskMappings.isEmpty() && sfaTaskMappings.size()>0){ for (SFATaskMapping sfaTaskMapping : sfaTaskMappings) { List<SfaPhotoDTO> sfaPhotoListSos = new ArrayList<>(); sfaPhotoListSos = this.getSfaPhotoListSos(bcodeCode, sfaTaskMapping.getSfaStoreCode(), sfaTaskMapping.getSfaTaskDate(), sfaTaskMapping.getSfaTaskDate(), agreementCode, agreementAmount, sfaStoreName, prCode, sfaTaskMapping.getSonId(),sfaActivityStartDate,sfaActivityEndDate); if (!sfaPhotoListSos.isEmpty() && sfaPhotoListSos.size()>0){ sortedList.addAll(sfaPhotoListSos); } } } return sortedList; 这段就是我前端调用的接口,现在效率非常低,你能帮我优化一下吗
08-22
### 使用 Fastjson 将字符串转换为 `List<DTO>` Fastjson 提供了直接将 JSON 字符串转换为泛型集合的方法,可以使用 `JSON.parseArray` 方法实现将字符串转换为 `List<DTO>`。 示例代码如下: ```java import com.alibaba.fastjson.JSON; public class Example { public static void main(String[] args) { // JSON字符串 String jsonString = "[{\"name\":\"John\",\"age\":30},{\"name\":\"Alice\",\"age\":25}]"; // 转换为 List<DTO> 对象 List<UserDTO> userDTOList = JSON.parseArray(jsonString, UserDTO.class); // 输出转换后的对象属性 for (UserDTO userDTO : userDTOList) { System.out.println("Name: " + userDTO.getName() + ", Age: " + userDTO.getAge()); } } } class UserDTO { private String name; private int age; // Getter Setter 方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } ``` ### 使用 Jackson 将字符串转换为 `List<DTO>` Jackson 通过 `ObjectMapper` 支持将 JSON 字符串转换为泛型集合。可以使用 `readValue` 方法并传入 `TypeReference` 来实现。 示例代码如下: ```java import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.List; public class Example { public static void main(String[] args) throws Exception { // JSON字符串 String jsonString = "[{\"name\":\"John\",\"age\":30},{\"name\":\"Alice\",\"age\":25}]"; // 创建 ObjectMapper 实例 ObjectMapper objectMapper = new ObjectMapper(); // 转换为 List<DTO> 对象 List<UserDTO> userDTOList = objectMapper.readValue(jsonString, new TypeReference<List<UserDTO>>() {}); // 输出转换后的对象属性 for (UserDTO userDTO : userDTOList) { System.out.println("Name: " + userDTO.getName() + ", Age: " + userDTO.getAge()); } } } class UserDTO { private String name; private int age; // Getter Setter 方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } ``` ### 使用 Gson 将字符串转换为 `List<DTO>` Gson 通过 `fromJson` 方法并结合 `TypeToken` 实现将 JSON 字符串转换为泛型集合。 示例代码如下: ```java import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; import java.util.List; public class Example { public static void main(String[] args) { // JSON字符串 String jsonString = "[{\"name\":\"John\",\"age\":30},{\"name\":\"Alice\",\"age\":25}]"; // 创建 Gson 实例 Gson gson = new Gson(); // 转换为 List<DTO> 对象 Type listType = new TypeToken<List<UserDTO>>(){}.getType(); List<UserDTO> userDTOList = gson.fromJson(jsonString, listType); // 输出转换后的对象属性 for (UserDTO userDTO : userDTOList) { System.out.println("Name: " + userDTO.getName() + ", Age: " + userDTO.getAge()); } } } class UserDTO { private String name; private int age; // Getter Setter 方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } ``` 上述方法均支持将 JSON 格式的字符串转换为包含 DTO 对象的列表,并且可以根据实际需求选择不同的库进行处理[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值