order('id', 'asc')->find(); if (self::where($where)->count() > 1) { self::where($where)->where('id', '<>', $data['id'])->delete(); } if ($data) { // $money_types = sys_data('money_type'); // $update = []; // foreach ($money_types as $v) { // if ($v['code'] == $money_type) { // if ($v['charge']) { // //TODO 接入链上充值 // if (!$data['address']) { // $update = BlockChianService::createAddress($v['code']); // } // } // break; // } // } // if ($update) // self::where($where)->update($update); return self::where($where)->find(); } else { // $money_types = sys_data('money_type'); // foreach ($money_types as $v) { // if ($v['code'] == $money_type) { // if ($v['charge']) { // //TODO 接入链上充值 // $address = BlockChianService::createAddress($v['code']); // $where['address'] = $address['address']; // $where['privateKey'] = $address['privateKey']; // $where['hexAddress'] = $address['hexAddress']; // } // break; // } // } 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::initialUserMoney($uid, $money_type); $user_info = User::getUserInfo($uid); $to_user = self::initialUserMoney($to_uid, $money_type); $to_user_info = User::getUserInfo($to_uid); if ($to_uid == $uid) { return self::setErrorInfo('不能转账给自己'); } if (bcsub($user['money'], $user['only_pink'], 8) < $money) { return self::setErrorInfo('可转账' . $money_type . '不足'); } $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['nickname'] . "({$user_info['uid']})支付" . $money . ' ' . $money_type; self::beginTrans(); try { $res2 = UserBill::expend('会员转出', $uid, $money_type, 'expend', $money, 0, $balance, $mark, 1); $res3 = UserBill::income('会员转入', $to_uid, $money_type, 'income', $money, $res2->id, $to_balance, $to_mark, 1); $res4 = self::where('uid', $uid)->where('money_type', $money_type)->update(['money' => $balance]); $res5 = self::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 $aid * @param $uid * @param $to_uid * @param $layer * @param $money_type * @param $money * @param int $status * @return bool * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public static function activityTradeMoney($aid, $uid, $to_uid, $link_id, $layer, $money_type, $money, int $status = 1) { $activity = Activity::get($aid); if (!$activity) return self::setErrorInfo('找不到活动'); $user = self::initialUserMoney($uid, $money_type); $user_info = User::getUserInfo($uid); if (bcsub($user['money'], $user['only_pink'], 8) < $money) { return self::setErrorInfo('可转账' . $money_type . '不足'); } $balance = bcsub($user['money'], $money, 8); if ($to_uid > 0) { $to_user = self::initialUserMoney($to_uid, $money_type); $to_user_info = User::getUserInfo($to_uid); $to_balance = bcadd($to_user['money'], $money, 8); $to_mark = '收到' . $user_info['nickname'] . "({$user_info['uid']})" . $activity['name'] . '升级V' . $layer . "支付" . $money . ' ' . $money_type; } $mark = $activity['name'] . '升级V' . $layer . ('付款给' . ($to_user_info['nickname'] ?? '系统')) . "({$to_uid})" . $money . ' ' . $money_type; self::beginTrans(); try { $res2 = UserBill::expend('互助升级转出', $uid, $money_type, 'activity_expend', $money, $link_id, $balance, $mark, 1, '', $aid, $layer); if ($to_uid > 0) $res3 = UserBill::income('互助升级转入', $to_uid, $money_type, 'activity_income', $money, $link_id, $to_balance ?? 0, $to_mark ?? '', $status, '', $aid, $layer); else $res3 = true; $res4 = self::where('uid', $uid)->where('money_type', $money_type)->update(['money' => $balance]); if ($status && $to_uid > 0) $res5 = self::where('uid', $to_uid)->where('money_type', $money_type)->update(['money' => $to_balance ?? 0]); else $res5 = true; $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, $not_pink = true, $admin_mark = '') { $user = self::initialUserMoney($uid, $money_type); if ($not_pink) { if (bcsub($user['money'], $user['only_pink'], 8) < $money) { return self::setErrorInfo('可用余额不足'); } } else { if ($user['money'] < $money) { return self::setErrorInfo('余额不足'); } } $balance = bcsub($user['money'], $money, 8); try { $res2 = UserBill::expend($title, $uid, $money_type, $type, $money, $link_id, $balance, $mark, 1, $admin_mark); $res4 = self::where('uid', $uid)->where('money_type', $money_type)->update(['money' => $balance]); if (!$not_pink && $user['only_pink'] > 0) { if ($user['only_pink'] >= $money) { $res4 = $res4 && self::where('uid', $uid)->where('money_type', $money_type)->update(['only_pink' => bcsub($user['only_pink'], $money, 8)]); } else { $res4 = $res4 && self::where('uid', $uid)->where('money_type', $money_type)->update(['only_pink' => 0]); } } $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, $status = 1, $not_pink = true, $admin_mark = '') { $user = self::initialUserMoney($uid, $money_type); $balance = bcadd($user['money'], $money, 8); try { $res2 = UserBill::income($title, $uid, $money_type, $type, $money, $link_id, $balance, $mark, $status, $admin_mark); if ($status) { $res4 = self::where('uid', $uid)->where('money_type', $money_type)->update(['money' => $balance]); if (!$not_pink) { $res4 = $res4 && self::where('uid', $uid)->where('money_type', $money_type)->update(['only_pink' => bcadd($user['only_pink'], $money, 8)]); } } else $res4 = true; $res = $res2 && $res4; if ($res) { return true; } else { return self::setErrorInfo('交易失败'); } } catch (\Exception $e) { return self::setErrorInfo($e->getMessage()); } } public static function sendMoney($id) { $info = UserBill::get($id); if (!$info || $info['status'] != 0) { return self::setErrorInfo('状态异常'); } $user = self::initialUserMoney($info['uid'], $info['category']); $balance = bcadd($user['money'], $info['number'], 8); try { $res2 = UserBill::edit(['status' => 1, 'balance' => $balance, 'add_time' => time()], $id); $res4 = self::where('uid', $info['uid'])->where('money_type', $info['category'])->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 $origin_money_type * @param $target_money_type * @param $money * @return bool * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public static function exchangeMoney($uid, $origin_money_type, $target_money_type, $money) { if ($origin_money_type == $target_money_type) return self::setErrorInfo('相同币种不可兑换'); $origin_money = self::initialUserMoney($uid, $origin_money_type); $target_money = self::initialUserMoney($uid, $target_money_type); if (bcsub($origin_money['money'], $origin_money['only_pink'], 8) < $money) { return self::setErrorInfo('可用' . $origin_money_type . '不足'); } $money_types = sys_data('money_type'); $origin_ratio = 0; $target_ratio = 0; foreach ($money_types as $v) { if ($v['code'] == $origin_ratio) { $origin_ratio = $v['usdt_price']; if ($origin_money_type == 'LALA') { $origin_ratio = get_lala_ratio(); } } if ($v['code'] == $target_money_type) { $target_ratio = $v['usdt_price']; if ($target_money_type == 'LALA') { $target_ratio = get_lala_ratio(); } } } $ratio = bcdiv($origin_ratio, $target_ratio, 14); $add_money = bcmul($money, $ratio, 8); $balance = bcsub($origin_money['money'], $money, 8); $to_balance = bcadd($target_money['money'], $add_money, 8); $mark = '使用' . $money . ' ' . $origin_money_type . '兑换' . $add_money . ' ' . $target_money_type; self::beginTrans(); try { $res2 = UserBill::expend('闪兑', $uid, $origin_money_type, 'exchange', $money, 0, $balance, $mark, 1); $res3 = UserBill::income('闪兑', $uid, $target_money_type, 'exchange', $add_money, $res2->id, $to_balance, $mark, 1); $res4 = self::where('uid', $uid)->where('money_type', $origin_money_type)->update(['money' => $balance]); $res5 = self::where('uid', $uid)->where('money_type', $target_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()); } } }