@Override public Double processSoItem(Tuple input, JumpMQOrderVo order) { double orderAmt = 0.0D; logger.info("Message successfully received: " + order.baseInfo()); if (order.getChildOrderList() != null && !order.getChildOrderList().isEmpty()) { for (JumpMQOrderVo childOrder : order.getChildOrderList()) { logger.info("Order successfully received: " + childOrder.baseInfo()); List<JumpMQOrderItemVo> soItemList = childOrder.getSoItemList(); for (JumpMQOrderItemVo soItem : soItemList) { if (soItem.getSubSoItemList() != null && !soItem.getSubSoItemList().isEmpty()) { List<JumpMQOrderItemVo> subSoItemList = soItem.getSubSoItemList(); for (JumpMQOrderItemVo subSoItem : subSoItemList) { logger.info("SoItem successfully received: " + subSoItem.baseInfo()); orderAmt += handleSoItem(input, order, childOrder, subSoItem); logger.info("SoItem successfully processed: " + subSoItem.baseInfo()); } } else { logger.info("SoItem successfully received: " + soItem.baseInfo()); orderAmt += handleSoItem(input, order, childOrder, soItem); logger.info("SoItem successfully processed: " + soItem.baseInfo()); } } logger.info("Order successfully processed: " + childOrder.baseInfo()); } } else { logger.info("Order successfully received: " + order.baseInfo()); List<JumpMQOrderItemVo> soItemList = order.getSoItemList(); for (JumpMQOrderItemVo soItem : soItemList) { if (soItem.getSubSoItemList() != null && !soItem.getSubSoItemList().isEmpty()) { List<JumpMQOrderItemVo> subSoItemList = soItem.getSubSoItemList(); for (JumpMQOrderItemVo subSoItem : subSoItemList) { logger.info("SoItem successfully received: " + subSoItem.baseInfo()); orderAmt += handleSoItem(input, null, order, subSoItem); logger.info("SoItem successfully processed: " + subSoItem.baseInfo()); } } else { logger.info("SoItem successfully received: " + soItem.baseInfo()); orderAmt += handleSoItem(input, null, order, soItem); logger.info("SoItem successfully processed: " + soItem.baseInfo()); } } logger.info("Order successfully processed: " + order.baseInfo()); } return orderAmt; }
@Override public void execute(Tuple input) { try { // 当前坐标点信息 strLastXValue = CommonUtil.getCurFullXValueStr(); jumpMQOrderVo = (JumpMQOrderVo) input.getValue(0); // 多线程下,input.getValue(0)的某个值不会被多个线程重复获取,即线程安全 Double orderAmount = 0.0; // 获取订单创建时间 if (jumpMQOrderVo == null) { logger.info("====空消息体==="); throw new Exception(); } // 跨天判断 msgDate = jumpMQOrderVo.getMsgSendTime(); if (msgDate.getDate() != curDate.getDate()) { if (msgDate.after(curDate)) { // 跨天 logger.info("===跨天处理===" + orderCreateDate); curDate = msgDate; /** 跨天处理 */ curTime = DateUtil.getFmtDate(msgDate, DateUtil.YYYYMMDD_STR); curHour = msgDate.getHours() + 1; /** 清空小时数据 */ orderAmountHourTotal = 0.0; orderNumHourTotal = 0; orderAmountTotal = 0.0; rowkey_amount_last = "last_amount_" + curTime; rowkey_amount_col_data = "col_data_" + curTime; sFinal = "[]"; orderNumTotal = 0; rowkey_ordr_num_last = "last_ordr_num_" + curTime; rowkey_ordr_num_col_data = "col_data_ordr_num_" + curTime; sFinalOrdrNum = "[]"; discountItemTotal.set(0); discountPriceTotal.set(0); dealHourFlg = 0; } else if (msgDate.before(curDate)) { // 非当天的历史订单 return; } } // 处理订单 orderAmount = processSoItem(input, jumpMQOrderVo); Date orderDate = BusinessLogic.orderDate(jumpMQOrderVo); if (orderDate == null) throw new IllegalArgumentException("Order Date is NULL"); orderCreateDate = DateUtil.getFmtDate(orderDate, DateUtil.YYYYMMDD_STR); orderCreateHour = orderDate.getHours() + 1; // 进行订单净额累加操作 logger.info("orderAmount:" + orderAmount); orderAmountTotal += orderAmount; logger.info("orderAmountTotal:" + orderAmountTotal); orderNumTotal++; // 订单创建小时数和当前系统小时数相同则进行小时订单净额累加操作,否则进行跨小时处理 if (curHour == orderCreateHour) { // 如果sFinal为空,则初始化 if ("[]".equals(sFinal)) { sFinal = CommonUtil.appendNextHourStr(curHour, sFinal, "0"); } orderAmountHourTotal += orderAmount; orderNumHourTotal++; // 柱状图当前小时数据更新,之后需要重新设值 sFinal = CommonUtil.replaceNowHourStr(sFinal, orderAmountHourTotal.toString()); colData = sFinal; if ("[]".equals(sFinalOrdrNum)) { sFinalOrdrNum = CommonUtil.appendNextHourStr(curHour, sFinalOrdrNum, "0"); } sFinalOrdrNum = CommonUtil.replaceNowHourStr(sFinalOrdrNum, String.valueOf(orderNumHourTotal)); colDataOrdrNum = sFinalOrdrNum; } else if (curHour < orderCreateHour) { // 需要强制入库,否则会丢失至多30秒的数据 logger.info("===跨小时处理===" + orderCreateHour); /** 跨小时处理 */ curHour = orderCreateHour; orderAmountHourTotal = 0.0; // 柱状图拼接新的小时数据 sFinal = CommonUtil.appendNextHourStr(curHour, sFinal, orderAmountHourTotal.toString()); colData = sFinal; orderNumHourTotal = 0; sFinalOrdrNum = CommonUtil.appendNextHourStr(curHour, sFinalOrdrNum, String.valueOf(orderNumHourTotal)); colDataOrdrNum = sFinalOrdrNum; } else { // 当前时间大于订单创建时间,表示mq里面有之前阻塞的订单,需要特殊处理或忽略 logger.info("===处理阻塞订单===订单id:" + jumpMQOrderVo.baseInfo()); } /** 写habse */ // 插入小时级最后累加值记录,30秒一次入库,如果期间有很多订单过来,30秒期间刚好碰到跨小时的情况的话,那部分数据就会丢失 synchronized (this) { insertDB_last_point_endTime = System.currentTimeMillis(); // 5秒写入一次最后的点,前端5秒读取一次刷新最新的值insertDB_last_point_startTime if (insertDB_last_point_endTime - insertDB_last_point_startTime >= Constant.SECOND_5) { /** 订单净额 */ logger.info("orderAmountTotal : " + orderAmountTotal); logger.info("orderNumHourTotal:" + orderNumHourTotal); // 插入最后累加值记录,重启bolt的时候需要获取最后一次插入的金额,在此基础上再进行累加 dao.insertGosOrder( Constant.TABLE_ORDER_INFO_RESULT, rowkey_amount_last, Constant.COMMON_FAMILY, new String[] {"order_amount", "xValue", "xTitle"}, new String[] {orderAmountTotal + "", strLastXValue[1], strLastXValue[0]}); // 插入拼接的柱状图数据,单条记录,后端重启和前端展示获取历史都从这里获取 dao.insertGosOrder( Constant.TABLE_ORDER_INFO_RESULT, rowkey_amount_col_data, Constant.COMMON_FAMILY, "col_data", colData); /** 订单数 */ dao.insertGosOrder( Constant.TABLE_ORDER_INFO_RESULT, rowkey_ordr_num_last, Constant.COMMON_FAMILY, new String[] {"order_num", "xValue", "xTitle"}, new String[] {orderNumTotal + "", strLastXValue[1], strLastXValue[0]}); dao.insertGosOrder( Constant.TABLE_ORDER_INFO_RESULT, rowkey_ordr_num_col_data, Constant.COMMON_FAMILY, "col_data_ordr_num", colDataOrdrNum); insertDB_last_point_startTime = System.currentTimeMillis(); } } synchronized (this) { insertDB_endTime = System.currentTimeMillis(); if (insertDB_endTime - insertDB_startTime >= Constant.SECOND_50) { /** 线图数据处理 */ rowkey_amount = curTime + "_amount_" + strLastXValue[0].replace(".", ""); // 插入点的历史数据,线图中用来拼接历史数据线条 dao.insertGosOrder( Constant.TABLE_ORDER_INFO_RESULT, rowkey_amount, Constant.COMMON_FAMILY, new String[] {"order_amount", "xValue", "xTitle"}, new String[] {orderAmountTotal + "", strLastXValue[1], strLastXValue[0]}); rowkey_ordr_num = curTime + "_ordr_num_" + strLastXValue[0].replace(".", ""); dao.insertGosOrder( Constant.TABLE_ORDER_INFO_RESULT, rowkey_ordr_num, Constant.COMMON_FAMILY, new String[] {"order_num", "xValue", "xTitle"}, new String[] {orderNumTotal + "", strLastXValue[1], strLastXValue[0]}); logger.info("===50秒入库成功==="); insertDB_startTime = System.currentTimeMillis(); // 写Oracle ,推送数据到手机端报表 // insertOracle(con, orderAmountTotal, orderNumTotal); } } } catch (Exception e) { logger.info("ERROR. Caused by:", e); } }