db; $transaction = $connection->beginTransaction(); try { $num = 0; $allHbAmount = 0; foreach ($rulesData as $item) { $num += $item['hbNum']; $allHbAmount += $item['hbAmount'] * $item['hbNum']; $createRows[] = [ 'hbAmount' => $item['hbAmount'], 'hbNum' => $item['hbNum'], 'miniCost' => $item['miniCost'] ?? 0, //最低消费金额,0表示没有要求 'duration' => $item['duration'] == 0 ? 0 : $item['duration'], //0表示永不过期 'effectiveType' => $item['effectiveType'], 'effectiveDate' => $item['effectiveType'] == 1 ? ($item['effectiveDate'] ?? date('Y-m-d')) : '0000-00-00', 'effectiveDays' => $item['effectiveDays'] ?? 0 ]; } $createRowsStr = json_encode($createRows); if ($id == '') { self::add([ 'shopId' => $shopId, 'mainId' => $mainId, 'amount' => $amount, 'num' => $num, 'allHbAmount' => $allHbAmount, 'rules' => $createRowsStr ]); } else { self::updateById($id, [ 'amount' => $amount, 'num' => $num, 'allHbAmount' => $allHbAmount, 'rules' => $createRowsStr ]); } $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); YII::error('RechargeShbClass::setRules error: ' . $e->getMessage()); throw $e; } return ['createRows' => $createRows]; } }