LedgerWalletModel.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace app\common\model;
  3. use app\admin\model\LedgerDeclarationChange;
  4. use Exception;
  5. use fast\Action;
  6. use fast\Asset;
  7. use think\Db;
  8. use think\exception\DbException;
  9. use think\Model;
  10. use think\Log;
  11. class LedgerWalletModel extends Model
  12. {
  13. protected $name = "ledger_wallet";
  14. // 自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'int';
  16. // 定义时间戳字段名
  17. protected $createTime = 'create_time';
  18. protected $updateTime = 'update_time';
  19. protected $deleteTime = false;
  20. public static function getWalletChaBao($userID)
  21. {
  22. return self::where('user_id', $userID)->value('token');
  23. }
  24. public static function getWalletFrozen($userID)
  25. {
  26. return self::where('user_id', $userID)->value('frozen');
  27. }
  28. public static function getWalletTotalChaBao($userID)
  29. {
  30. return self::where('user_id', $userID)->value('token + frozen');
  31. }
  32. public static function getWallet($userID)
  33. {
  34. return self::where('user_id', $userID)->find();
  35. }
  36. /**
  37. * 仅事务内使用,会附带"for update"
  38. * @param $userID
  39. * @throws Exception
  40. */
  41. public function getWalletTX($userID)
  42. {
  43. return $this->lock(true)->where('user_id', $userID)->find();
  44. }
  45. /**
  46. * 更新钱包余额并添加账变记录
  47. * @param int $uid 用户ID
  48. * @param string $asset 资产类型
  49. * @param string $amount 金额 正:表示加 负:表示减
  50. * @param int $action 账变类型
  51. * @return void
  52. * @throws Exception
  53. */
  54. public function changeWalletAccount(int $uid, string $asset, string $amount, int $action, int $from_id = 0)
  55. {
  56. $available = $this->getWallet($uid);
  57. if (empty($available)) (new LedgerWalletModel())->insertGetId(['user_id' => $uid]); // 创建钱包
  58. $frozen = ''; //冻结金额
  59. // 账变资产模型
  60. switch ($asset) {
  61. case Asset::USDT:
  62. $changeModel = new LedgerUsdtChangeModel();
  63. break;
  64. case Asset::TOKEN:
  65. $changeModel = new LedgerTokenChangeModel();
  66. break;
  67. case Asset::TEAC:
  68. $changeModel = new LedgerTeacChangeModel();
  69. break;
  70. case Asset::SMH:
  71. $changeModel = new LedgerSmhChangeModel();
  72. break;
  73. case Asset::FROZEN:
  74. //扣掉赠送手续费
  75. $changeModel = new LedgerFrozenChangeModel();
  76. break;
  77. default:
  78. throw new Exception('币种错误:' . $asset);
  79. }
  80. // 更新钱包余额
  81. $newAmount = $this->changeWalletOnly($uid, $asset, $amount);
  82. // 创建账变记录
  83. $insertRs = $changeModel->insert([
  84. 'user_id' => $uid,
  85. 'from_id' => $from_id,
  86. 'change_amount' => $amount,
  87. 'present_amount' => $newAmount,
  88. 'create_time' => time(),
  89. 'action' => $action
  90. ]);
  91. if (empty($insertRs)) {
  92. throw new Exception('创建' . $asset . '账变记录失败');
  93. }
  94. }
  95. /**
  96. * 仅更新钱包余额,没有账变
  97. * @param int $uid 用户ID
  98. * @param string $asset 资产类型
  99. * @param string $amount 金额 正:表示加 负:表示减
  100. * @return string 变动后的金额
  101. * @throws Exception
  102. */
  103. public function changeWalletOnly(int $uid, string $asset, string $amount): string
  104. {
  105. $available = $this->getWalletTX($uid);
  106. if (empty($available)) {
  107. throw new Exception('用户资产信息不存在');
  108. }
  109. // 金额为0的判断
  110. $amount = bcadd($amount, 0, 6);
  111. if (bccomp($amount, '0', 6) == 0) {
  112. throw new Exception('金额变动为0');
  113. }
  114. // 余额不足的判断
  115. if ($amount < 0 && $available[$asset] < -$amount) {
  116. throw new Exception($asset . '余额不足');
  117. }
  118. $newAmount = bcadd($available[$asset], $amount, 6); // 新余额
  119. $walletUpdate = [$asset => $newAmount, 'update_time' => time()];
  120. // 更新余额
  121. $changeRs = $this->save($walletUpdate, ['user_id' => $uid]);
  122. if (empty($changeRs)) {
  123. throw new Exception('更新' . $asset . '余额失败');
  124. }
  125. return $newAmount;
  126. }
  127. /**
  128. * 冻结茶宝
  129. * @param int $uid 用户ID
  130. * @param string $amount 报单金额
  131. * @return void
  132. * @throws Exception
  133. */
  134. public function setChangeFrozen(int $uid, string $amount, int $action, string $icn, int $from_id = 0)
  135. {
  136. $available = $this->getWallet($uid);
  137. $ledgerWalletModel = new LedgerWalletModel();
  138. if (empty($available)) $ledgerWalletModel->insertGetId(['user_id' => $uid]); // 创建钱包
  139. $walletUpdate = [];
  140. $result = false;
  141. //运费/手续费:扣除
  142. if($icn == '-'){
  143. $totalAmount = bcadd($available['token'], $available['frozen'], 6);
  144. //扣除冻结金额
  145. if($available['frozen'] > $amount) $walletUpdate['frozen'] = bcsub($available['frozen'], $amount, 6);
  146. if($available['frozen'] <= $amount) {
  147. $walletUpdate = ['token'=> bcadd($totalAmount, -$amount, 6), 'frozen' => 0];
  148. //添加账变记录
  149. $subAmount = bcsub($amount, $available['frozen'], 6);
  150. $this->changeWalletAccount($uid, Asset::TOKEN, -$subAmount, LedgerTokenChangeModel::Giveaway, $from_id);
  151. }
  152. //添加冻结
  153. $newAmount = $walletUpdate['frozen'];
  154. if($available['frozen'] > 0) $result = true;
  155. }
  156. //手续费
  157. if($icn == '+' && $available['frozen'] < $amount){
  158. //添加账变记录
  159. if($available->token < $amount) throw new Exception('茶宝不足');
  160. $sunAmount = bcsub($available->frozen, $amount, 6); //扣除的冻结金额
  161. $chabao = bcadd($available['token'], $sunAmount, 6);
  162. $newAmount = $amount; //剩余的冻结金额
  163. //添加冻结
  164. $walletUpdate =['token' => $chabao, 'frozen' =>$newAmount];
  165. $this->changeWalletAccount($uid, Asset::TOKEN, $sunAmount, LedgerTokenChangeModel::Super, $from_id);
  166. $result = true;
  167. }
  168. // 创建账变记录
  169. if (count($walletUpdate) > 0) $ledgerWalletModel->where('user_id', $uid)->update($walletUpdate);
  170. if($result){
  171. return (new LedgerFrozenChangeModel())->insert([
  172. 'user_id' => $uid,
  173. 'from_id' => $from_id,
  174. 'change_amount' => $icn.$amount,
  175. 'present_amount' => $newAmount,
  176. 'create_time' => time(),
  177. 'action' => $action
  178. ]);
  179. }
  180. }
  181. /**
  182. * 发放直推USDT收益
  183. * @param int $uid
  184. * @param string $power
  185. * @return void
  186. * @throws DbException
  187. */
  188. public function sendUsdtProfit(int $uid, string $amount)
  189. {
  190. $ratio = (new Config())->getValue('direct_income');//直推奖励
  191. $user = (new UserModel())->get($uid);
  192. if (empty($user) || $user['parent_id'] == 0) {
  193. return;
  194. }
  195. $parent = (new UserModel())->get($user['parent_id']);
  196. if (empty($parent)) {
  197. return;
  198. }
  199. $temp = ['拦截提醒', $uid, $amount, $amount * $ratio];
  200. //dump($temp);
  201. // 上级算力的账变
  202. $this->changeWalletAccount($user['parent_id'], Asset::USDT, $amount * $ratio, Action::UsdtShareBonus, $uid);
  203. }
  204. /**
  205. * 发放直推收益
  206. * @param int $uid
  207. * @param string $power
  208. * @return void
  209. * @throws DbException
  210. */
  211. public function sendDirectProfit(int $uid, string $power)
  212. {
  213. $user = (new UserModel())->get($uid);
  214. if (empty($user) || $user['parent_id'] == 0) {
  215. return;
  216. }
  217. $parent = (new UserModel())->get($user['parent_id']);
  218. if (empty($parent)) {
  219. return;
  220. }
  221. // 查询直推奖励比例
  222. $directProfit = (new Config())->getValue('direct_income');
  223. $directProfitFloat = floatval($directProfit);
  224. if (is_null($directProfit) || $directProfitFloat <= 0) {
  225. return;
  226. }
  227. $directPower = $power * $directProfitFloat; // 可获得的直推奖励(算力)
  228. if($directPower > 0){
  229. $this->changeWalletAccount($user['parent_id'], Asset::POWER, $directPower, Action::PowerDirectAward, $uid);
  230. }
  231. // 上级算力的账变
  232. }
  233. public function users()
  234. {
  235. return $this->hasOne(UserModel::class, 'id', 'user_id');
  236. }
  237. public function getCreateTimeTextAttr($value, $data)
  238. {
  239. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  240. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  241. }
  242. public function getUpdateTimeTextAttr($value, $data)
  243. {
  244. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  245. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  246. }
  247. protected function setCreateTimeAttr($value)
  248. {
  249. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  250. }
  251. protected function setUpdateTimeAttr($value)
  252. {
  253. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  254. }
  255. }