find(); if ($data) { return self::where($where)->find(); } else { return self::create(array_merge($where, ['money' => $money])); } } /** * @param $uid * @param $to_uid * @param $money_type * @param $money * @return bool * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public static function tradeMoney($uid, $to_uid, $money_type, $money) { $user = self::initialMoney($uid, $money_type); $user_info = Site::get($uid); $to_user = UserMoney::initialUserMoney($to_uid, $money_type); $to_user_info = User::getUserInfo($to_uid); if ($user['money'] < $money) { return self::setErrorInfo('奖池余额不足'); } $balance = bcsub($user['money'], $money, 8); $to_balance = bcadd($to_user['money'], $money, 8); $mark = '转至' . $to_user_info['nickname'] . "({$to_user_info['uid']})" . $money . ' ' . $money_type; $to_mark = '收到' . $user_info['name'] . "平台奖池转入" . $money . ' ' . $money_type; self::beginTrans(); try { $res2 = SystemBill::expend('奖池转出', $uid, $money_type, 'expend', $money, 0, $balance, $mark, 1); $res3 = UserBill::income('奖池转入', $to_uid, $money_type, 'system_income', $money, $res2->id, $to_balance, $to_mark, 1); $res4 = self::where('site_id', $uid)->where('money_type', $money_type)->update(['money' => $balance]); $res5 = UserMoney::where('uid', $to_uid)->where('money_type', $money_type)->update(['money' => $to_balance]); $res = $res2 && $res3 && $res4 && $res5; if ($res) { self::commitTrans(); return true; } else { self::rollbackTrans(); return self::setErrorInfo('转账失败'); } } catch (\Exception $e) { self::rollbackTrans(); return self::setErrorInfo($e->getMessage()); } } /** * @param $uid * @param $money_type * @param $money * @param $type * @param string $title * @param string $mark * @param int $from_vote * @param int $from_sub_vote * @return bool * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public static function expendMoney($uid, $money_type, $money, $type, $title = '奖池转出', $mark = '', $link_id = 0) { $user = self::initialMoney($uid, $money_type); if ($user['money'] < $money) { return self::setErrorInfo('余额不足'); } $balance = bcsub($user['money'], $money, 8); try { $res2 = SystemBill::expend($title, $uid, $money_type, $type, $money, $link_id, $balance, $mark, 1); $res4 = self::where('site_uid', $uid)->where('money_type', $money_type)->update(['money' => $balance]); $res = $res2 && $res4; if ($res) { return true; } else { return self::setErrorInfo('交易失败'); } } catch (\Exception $e) { return self::setErrorInfo($e->getMessage()); } } /** * @param $uid * @param $money_type * @param $money * @param $type * @param string $title * @param string $mark * @param int $from_vote * @param int $from_sub_vote * @return bool * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public static function incomeMoney($uid, $money_type, $money, $type, $title = '奖池转入', $mark = '', $link_id = 0) { $user = self::initialMoney($uid, $money_type); $balance = bcadd($user['money'], $money, 8); try { $res2 = SystemBill::income($title, $uid, $money_type, $type, $money, $link_id, $balance, $mark, 1); $res4 = self::where('site_id', $uid)->where('money_type', $money_type)->update(['money' => $balance]); $res = $res2 && $res4; if ($res) { return true; } else { return self::setErrorInfo('交易失败'); } } catch (\Exception $e) { return self::setErrorInfo($e->getMessage()); } } }