searchActiveInSuk($suk); if ($flag == 20) { $left[] = $p; $left_spent = bcadd($left_spent, $ppaid, 2); } else if ($flag == 21) { $right[] = $p; $right_spent = bcadd($right_spent, $ppaid, 2); } else { Log::error('cant find suk in lucky field. id=' . $oid); continue; } }// foreach $products = []; $diff_money = bcsub($left_spent, $right_spent, 2); if ($diff_money < 0.0) { $diff_money = -$diff_money; } $open = 20; $winners = $left; $losers = $right; if ($left_spent > $right_spent) { $open = 21; $winners = $right; $losers = $left; } // 结果处置 foreach($winners as $p) { $single = $orders[$p['id']] == 1; $this->execute($p, $single, true); } foreach($losers as $p) { $single = $orders[$p['id']] == 1; $this->execute($p, $single, false); } } // calc /** * 奖励分配 * * @param $product 获奖订单中的商品 * @param bool $single 是否是单种商品构成的订单 * @param bool $win 是否中奖 */ protected function execute($product, $single=true, $win=true) { BaseModel::beginTrans(); $result = 0; $reparation = 0.0; if ($win) { $result = 1; $reparation = bcmul(bcsub($product['paid'], $product['cost'], 2), 0.1, 2); } // 标记商品为已参与活动 $r1 = StoreOrderCartInfo::where('oid', $product['id']) ->where('product_id', $product['product_id']) ->update([ 'activity' => self::$NAME, 'result' => $result, 'reparation' => $reparation, ]); if (!$win) { return; } $refund_price = bcadd($product['paid'], $reparation, 2); // 订单全部商品都中奖的退单 $r2 = true; if ($single) { $r2 = StoreOrder::where('id', $product['id'])->update([ 'refund_price' => $refund_price, 'refund_status' => 2, ]); } // 中奖的 退款并赔款返回到佣金 $r3 = UserBill::income('參加活动', $product['uid'], 'now_money', 'brokerage', $refund_price, $product['id'], bcadd($product['now_money'], $refund_price, 2), '订单退款' . $product['paid'] . '+' . $reparation . '元'); $r4 = User::bcInc($product['uid'], 'brokerage_price', $refund_price, 'uid'); $ok = $r1 && $r2 && $r3 && $r4; BaseModel::checkTrans($ok); if ($ok) { // 中奖用户发送中奖消息. UserNotice::sendNoticeTo($product['uid'], '您的订单已退款', '您好,由于活动商品库存有限,您的订单 ' . $product['store_name'] . ' 未能成功分配. 该商品付款' . $product['paid'] . '元已如数退还, 并赔付您' . $reparation . '元作为补偿, 对您造成的不便我们深感抱歉. 同时感谢您对美天旺的喜爱和信任, 美天旺活动场将永远为您守候. 顺祝生活愉快.'); } } }