| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- <?php
- namespace app\common\model;
- use app\admin\model\LedgerDeclarationChange;
- use Exception;
- use fast\Action;
- use fast\Asset;
- use think\Db;
- use think\exception\DbException;
- use think\Model;
- use think\Log;
- class LedgerWalletModel extends Model
- {
- protected $name = "ledger_wallet";
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'create_time';
- protected $updateTime = 'update_time';
- protected $deleteTime = false;
-
- public static function getWalletChaBao($userID)
- {
- return round(self::where('user_id', $userID)->value('token'), 2);
- }
- public static function getWalletFrozen($userID)
- {
- return self::where('user_id', $userID)->value('frozen');
- }
- public static function getWalletBuying($userID)
- {
- return self::where('user_id', $userID)->value('buying');
- }
- public static function getWalletTeac(int $userID)
- {
- return self::where('user_id', $userID)->value('teac');
- }
- public static function getTeacAngel(int $userID)
- {
- return self::where('user_id', $userID)->value('teac_angel');
- }
- public static function getWalletTotalChaBao($userID)
- {
- return self::where('user_id', $userID)->value('token + frozen');
- }
- public static function getWallet($userID)
- {
- return self::where('user_id', $userID)->find();
- }
- /**
- * 仅事务内使用,会附带"for update"
- * @param $userID
- * @throws Exception
- */
- public function getWalletTX($userID)
- {
- return $this->lock(true)->where('user_id', $userID)->find();
- }
- /**
- * 更新钱包余额并添加账变记录
- * @param int $uid 用户ID
- * @param string $asset 资产类型
- * @param string $amount 金额 正:表示加 负:表示减
- * @param int $action 账变类型
- * @param bool $team_level 层级账变
- * @return void
- * @throws Exception
- */
- public function changeWalletAccount(int $uid, string $asset, string $amount, int $action, int $from_id = 0, bool $team_level = false)
- {
- $available = $this->getWallet($uid);
- if($team_level && empty($available['team_level_id'])) return;
- if (empty($available)) (new LedgerWalletModel())->insertGetId(['user_id' => $uid]); // 创建钱包
- // 账变资产模型
- switch ($asset) {
- case Asset::TOKEN:
- $changeModel = new LedgerTokenChangeModel();
- break;
- case Asset::TEAC:
- $changeModel = new LedgerTeacChangeModel();
- break;
- case Asset::FROZEN:
- //扣掉赠送手续费
- $changeModel = new LedgerFrozenChangeModel();
- break;
- case Asset::TEAC_ANGEL:
- $changeModel = new LedgerTeacAngelChangeModel();
- break;
- case Asset::TEAC_ECOLY:
- $changeModel = new LedgerTeacEcolyChangeModel();
- break;
- default:
- throw new Exception('币种错误:' . $asset);
- }
- // 更新钱包余额
- $newAmount = $this->changeWalletOnly($uid, $asset, $amount);
-
- // 创建账变记录
- $insertRs = $changeModel->insert([
- 'user_id' => $uid,
- 'from_id' => $from_id,
- 'change_amount' => $amount,
- 'present_amount' => $newAmount,
- 'create_time' => time(),
- 'action' => $action
- ]);
- if (empty($insertRs)) throw new Exception('创建' . $asset . '账变记录失败');
- return $newAmount;
- }
-
- /**
- * 仅更新钱包余额,没有账变
- * @param int $uid 用户ID
- * @param string $asset 资产类型
- * @param string $amount 金额 正:表示加 负:表示减
- * @return string 变动后的金额
- * @throws Exception
- */
- public function changeWalletOnly(int $uid, string $asset, string $amount): string
- {
- $available = $this->getWalletTX($uid);
- if (empty($available)) {
- throw new Exception('用户资产信息不存在');
- }
- // 金额为0的判断
- $amount = bcadd($amount, 0, 6);
- if (bccomp($amount, '0', 6) == 0) {
- throw new Exception('金额变动为0');
- }
- // 余额不足的判断
- if ($amount < 0 && $available[$asset] < -$amount) {
- throw new Exception(Asset::getAssetName($asset) . '余额不足');
- }
- $newAmount = bcadd($available[$asset], $amount, 6); // 新余额
- $walletUpdate = [$asset => $newAmount, 'update_time' => time()];
- // 更新余额
- $changeRs = $this->update($walletUpdate, ['user_id' => $uid]);
- if (empty($changeRs)) {
- throw new Exception('更新' . $asset . '余额失败');
- }
- return $newAmount;
- }
- /**
- * 冻结茶宝
- * @param int $uid 用户ID
- * @param string $amount 报单金额
- * @return void
- * @throws Exception
- */
- public function setChangeFrozen(int $uid, string $amount, int $action, int $type, string $icn, int $from_id = 0)
- {
- $available = $this->getWallet($uid);
- $ledgerWalletModel = new LedgerWalletModel();
- if (empty($available)) throw new Exception('用户资产信息不存在');
- $walletUpdate = [];
- $result = false;
- $token = 0; //要扣除的茶宝
- $frozen = 0; //要扣除的标记茶宝
- //运费/手续费:扣除
- if($icn == '-'){
- $totalAmount = bcadd($available['token'], $available['frozen'], 6);
- //扣除冻结金额
- if($available['frozen'] >= $amount) {
- $walletUpdate['frozen'] = $frozen = bcsub($available['frozen'], $amount, 6);
- }
- if($available['frozen'] < $amount) {
- $frozen = $available['frozen'];
- $walletUpdate = ['token'=> bcadd($totalAmount, -$amount, 6), 'frozen' => 0];
- //添加茶宝账变记录
- $subAmount = $token = bcsub($amount, $available['frozen'], 6);
- $this->changeWalletAccount($uid, Asset::TOKEN, -$subAmount, $type, $from_id);
- }
- //添加冻结
- $newAmount = $walletUpdate['frozen'];
- if($available['frozen'] > 0) $result = true;
- }
- //手续费
- if($icn == '+' && $available['frozen'] < $amount){
- //添加账变记录
- if($available->token < $amount) throw new Exception('茶宝不足');
- $sunAmount = bcsub($available->frozen, $amount, 6); //扣除的冻结金额
- $chabao = bcadd($available['token'], $sunAmount, 6);
- $newAmount = $amount; //剩余的冻结金额
- //添加冻结
- $walletUpdate =['token' => $chabao, 'frozen' =>$newAmount];
- $this->changeWalletAccount($uid, Asset::TOKEN, $sunAmount, $type, $from_id);
- $frozen = $available['frozen'];
- $result = true;
- }
-
- // 创建账变记录
- if (count($walletUpdate) > 0) $ledgerWalletModel->where('user_id', $uid)->update($walletUpdate);
- if($result){
- (new LedgerFrozenChangeModel())->insert([
- 'user_id' => $uid,
- 'from_id' => $from_id,
- 'change_amount' => $icn.$frozen,
- 'present_amount' => $newAmount,
- 'create_time' => time(),
- 'action' => $action
- ]);
- }
- return [$token, $frozen];
- }
- /**
- * 发放直推USDT收益
- * @param int $uid
- * @param string $power
- * @return void
- * @throws DbException
- */
- public function sendUsdtProfit(int $uid, string $amount)
- {
- $ratio = (new Config())->getValue('direct_income');//直推奖励
- $user = (new UserModel())->get($uid);
- if (empty($user) || $user['parent_id'] == 0) {
- return;
- }
- $parent = (new UserModel())->get($user['parent_id']);
- if (empty($parent)) {
- return;
- }
- $temp = ['拦截提醒', $uid, $amount, $amount * $ratio];
- //dump($temp);
- // 上级算力的账变
- $this->changeWalletAccount($user['parent_id'], Asset::USDT, $amount * $ratio, Action::UsdtShareBonus, $uid);
- }
- /**
- * 发放直推收益
- * @param int $uid
- * @param string $power
- * @return void
- * @throws DbException
- */
- public function sendDirectProfit(int $uid, string $power)
- {
- $user = (new UserModel())->get($uid);
- if (empty($user) || $user['parent_id'] == 0) {
- return;
- }
- $parent = (new UserModel())->get($user['parent_id']);
- if (empty($parent)) {
- return;
- }
- // 查询直推奖励比例
- $directProfit = (new Config())->getValue('direct_income');
- $directProfitFloat = floatval($directProfit);
- if (is_null($directProfit) || $directProfitFloat <= 0) {
- return;
- }
- $directPower = $power * $directProfitFloat; // 可获得的直推奖励(算力)
- if($directPower > 0){
- $this->changeWalletAccount($user['parent_id'], Asset::POWER, $directPower, Action::PowerDirectAward, $uid);
- }
- // 上级算力的账变
- }
-
- public function users()
- {
- return $this->hasOne(UserModel::class, 'id', 'user_id');
- }
- public function getCreateTimeTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
- return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
- }
- public function getUpdateTimeTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
- return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
- }
- protected function setCreateTimeAttr($value)
- {
- return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
- }
- protected function setUpdateTimeAttr($value)
- {
- return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
- }
- }
|