load->view('welcome_message'); } public function test() { $this->load->model('biz/biz_account_model', 'account_model'); $this->load->model('biz/biz_accountLog_model', 'accountlog_model'); $this->load->library('bizAccount'); $this->load->helper('string'); $uid = 1; $bizId = 10; $brokerage1 = 10; $brokerage2 = 20; $brokerage3 = 30; $brokerage4 = 40; $totalMoney = $brokerage1 + $brokerage2 + $brokerage3 + $brokerage4; $ck = md5(time() . random_string() . $uid); // 16 字节 = 32 字符 try { $bizAccount = new BizAccount(); $account = $bizAccount->getAccountBizId($bizId, true); if (!$account) { throw new Exception('账号不存在', 0); } $leftMoney = $account['money_left']; if ($leftMoney < $totalMoney) { throw new Exception('余额不足', 0); } $this->account_model->db->trans_begin(); $upData = [ "money_left = money_left-$totalMoney" => null ]; $where = [ "money_left >= $totalMoney" => null ]; $upAccount = $this->account_model->update($upData, $where); if (!(!is_bool($upAccount) && $upAccount)) { // debug_log("[error]# ". $this->coupon_model->db->last_query(), __FUNCTION__, $this->log_dir); throw new Exception('余额不足', 0); } $logData = []; if ($brokerage1 > 0) { $leftMoney = $leftMoney - $brokerage1; $logData[] = [ 'account_id' => $account['id'], 'trade_type' => BizAccount::TRADE_TYPE_USE, 'money_out' => $brokerage1, 'money_left' => $leftMoney, 'ck' => $ck, 'descrip' => '订单支付', 'c_time' => time(), ]; } if ($brokerage2 > 0) { $leftMoney = $leftMoney - $brokerage2; $logData[] = [ 'account_id' => $account['id'], 'trade_type' => BizAccount::TRADE_TYPE_USE, 'money_out' => $brokerage2, 'money_left' => $leftMoney, 'ck' => $ck, 'descrip' => '订单支付', 'c_time' => time(), ]; } if ($brokerage3 > 0) { $leftMoney = $leftMoney - $brokerage3; $logData[] = [ 'account_id' => $account['id'], 'trade_type' => BizAccount::TRADE_TYPE_USE, 'money_out' => $brokerage3, 'money_left' => $leftMoney, 'ck' => $ck, 'descrip' => '订单支付', 'c_time' => time(), ]; } if ($brokerage4 > 0) { $leftMoney = $leftMoney - $brokerage4; $logData[] = [ 'account_id' => $account['id'], 'trade_type' => BizAccount::TRADE_TYPE_USE, 'money_out' => $brokerage4, 'money_left' => $leftMoney, 'ck' => $ck, 'descrip' => '订单支付', 'c_time' => time(), ]; } $ret = $this->accountlog_model->add_batch($logData); if (!$ret) { throw new Exception('写入日志失败', 0); } $this->account_model->db->trans_commit(); print_r('保存成功'); } catch (Exception $e) { $this->account_model->db->trans_rollback(); print_r($e->getMessage()); } } }