LedgerWalletModel.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 = 0; //冻结金额
  59. // 账变资产模型
  60. switch ($asset) {
  61. case Asset::USDT:
  62. $changeModel = new LedgerUsdtChangeModel();
  63. break;
  64. case Asset::TOKEN:
  65. $changeModel = new LedgerTokenChangeModel();
  66. if($action == $changeModel::Giveaway || $action == $changeModel::Freight) $frozen = $available->frozen; //冻结
  67. break;
  68. case Asset::TEAC:
  69. $changeModel = new LedgerTokenChangeModel();
  70. break;
  71. case Asset::SMH:
  72. $changeModel = new LedgerSmhChangeModel();
  73. break;
  74. case Asset::FROZEN:
  75. $changeModel = new LedgerTokenChangeModel();
  76. break;
  77. default:
  78. throw new Exception('币种错误:' . $asset);
  79. }
  80. // 更新钱包余额
  81. $newAmount = $this->changeWalletOnly($uid, $asset, $amount, $frozen);
  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 $usdt 报单金额
  99. * @return void
  100. * @throws Exception
  101. */
  102. public function sendUserSubFrozen(int $uid, string $amount, int $action, string $icn)
  103. {
  104. $available = $this->getWallet($uid);
  105. $ledgerWalletModel = new LedgerWalletModel();
  106. if (empty($available)) $ledgerWalletModel->insertGetId(['user_id' => $uid]); // 创建钱包
  107. $newAmount = $available->token;
  108. if($newAmount < $amount) throw new Exception('茶宝不足');
  109. // 创建账变记录
  110. $insertRs = $ledgerWalletModel->where('user_id', $uid)->update(['token' => bcsub($newAmount, $amount, 6), 'frozen' => bcadd($available->frozen, $amount, 6)]);
  111. // 创建账变记录
  112. $insertRs = (new LedgerTokenChangeModel)->insert([
  113. 'user_id' => $uid,
  114. 'from_id' => 0,
  115. 'change_amount' => $icn.$amount,
  116. 'present_amount' => $newAmount,
  117. 'create_time' => time(),
  118. 'action' => $action
  119. ]);
  120. if (empty($insertRs)) {
  121. throw new Exception('创建账变记录失败');
  122. }
  123. }
  124. /**
  125. * 仅更新钱包余额,没有账变
  126. * @param int $uid 用户ID
  127. * @param string $asset 资产类型
  128. * @param string $amount 金额 正:表示加 负:表示减
  129. * @return string 变动后的金额
  130. * @throws Exception
  131. */
  132. public function changeWalletOnly(int $uid, string $asset, string $amount, string $freeze): string
  133. {
  134. $available = $this->getWalletTX($uid);
  135. if (empty($available)) {
  136. throw new Exception('用户资产信息不存在');
  137. }
  138. // 金额为0的判断
  139. $amount = bcadd($amount, 0, 6);
  140. if (bccomp($amount, '0', 6) == 0) {
  141. throw new Exception('金额变动为0');
  142. }
  143. //总金额+冻结金额
  144. $totalAmount = bcadd($available[$asset], $freeze, 6);
  145. // 余额不足的判断
  146. if ($amount < 0 && $totalAmount < -$amount) {
  147. throw new Exception($asset . '余额不足');
  148. }
  149. //扣除冻结金额
  150. if($freeze > 0){
  151. if($freeze > -$amount) $walletUpdate['frozen'] = bcsub($freeze, -$amount, 6);
  152. if($freeze <= -$amount) $walletUpdate = [$asset => bcadd($totalAmount, $amount, 6), 'frozen' => 0];
  153. }else{
  154. $newAmount = bcadd($totalAmount, $amount, 6); // 新余额
  155. $walletUpdate = [$asset => $newAmount, 'update_time' => time()];
  156. }
  157. // 更新余额
  158. $changeRs = $this->save($walletUpdate, ['user_id' => $uid]);
  159. if (empty($changeRs)) {
  160. throw new Exception('更新' . $asset . '余额失败');
  161. }
  162. return $totalAmount;
  163. }
  164. /**
  165. * 发放直推USDT收益
  166. * @param int $uid
  167. * @param string $power
  168. * @return void
  169. * @throws DbException
  170. */
  171. public function sendUsdtProfit(int $uid, string $amount)
  172. {
  173. $ratio = (new Config())->getValue('direct_income');//直推奖励
  174. $user = (new UserModel())->get($uid);
  175. if (empty($user) || $user['parent_id'] == 0) {
  176. return;
  177. }
  178. $parent = (new UserModel())->get($user['parent_id']);
  179. if (empty($parent)) {
  180. return;
  181. }
  182. $temp = ['拦截提醒', $uid, $amount, $amount * $ratio];
  183. //dump($temp);
  184. // 上级算力的账变
  185. $this->changeWalletAccount($user['parent_id'], Asset::USDT, $amount * $ratio, Action::UsdtShareBonus, $uid);
  186. }
  187. /**
  188. * 发放直推收益
  189. * @param int $uid
  190. * @param string $power
  191. * @return void
  192. * @throws DbException
  193. */
  194. public function sendDirectProfit(int $uid, string $power)
  195. {
  196. $user = (new UserModel())->get($uid);
  197. if (empty($user) || $user['parent_id'] == 0) {
  198. return;
  199. }
  200. $parent = (new UserModel())->get($user['parent_id']);
  201. if (empty($parent)) {
  202. return;
  203. }
  204. // 查询直推奖励比例
  205. $directProfit = (new Config())->getValue('direct_income');
  206. $directProfitFloat = floatval($directProfit);
  207. if (is_null($directProfit) || $directProfitFloat <= 0) {
  208. return;
  209. }
  210. $directPower = $power * $directProfitFloat; // 可获得的直推奖励(算力)
  211. if($directPower > 0){
  212. $this->changeWalletAccount($user['parent_id'], Asset::POWER, $directPower, Action::PowerDirectAward, $uid);
  213. }
  214. // 上级算力的账变
  215. }
  216. /**
  217. * 发放见点奖
  218. * @param int $uid 服务器算力的用户ID
  219. * @param string $power 该租赁得到的算力
  220. * @return void
  221. * @throws Exception
  222. */
  223. public function sendRegBonus(int $uid)
  224. {
  225. $parentIDs = (new UserPathModel())->getAllParentIDs($uid);
  226. if (count($parentIDs) == 0) {
  227. return;
  228. }
  229. $userList = (new UserModel())
  230. ->field('id,team_level_id')
  231. ->where('id', 'in', $parentIDs)
  232. ->where('team_level_id', '>', 0)
  233. ->order('id desc')
  234. ->select();
  235. if (empty($userList)) {
  236. //没有符合条件的上级
  237. return;
  238. }
  239. $team_level_config = DB::table('team_level')
  240. ->field('level_id, reg_bonus')
  241. ->select();
  242. $level_reg_bonus = [];
  243. foreach ($team_level_config as $item) {
  244. $level_reg_bonus[$item['level_id']] = $item['reg_bonus'];
  245. }
  246. $now_level = 0;
  247. foreach ($userList as $item) {
  248. if ($item['team_level_id'] > $now_level && isset($level_reg_bonus[$item['team_level_id']])) {
  249. (new LedgerWalletModel())->changeWalletAccount($item['id'], Asset::USDT, $level_reg_bonus[$item['team_level_id']], Action::UsdtRegBonus, $uid);
  250. $now_level = $item['team_level_id'];
  251. }
  252. }
  253. }
  254. /**
  255. * 发放服务器市场推荐相关收益
  256. * @param int $uid
  257. * @param string $power
  258. * @return void
  259. * @throws DbException
  260. */
  261. public function sendMarketBonus(int $uid, array $server_info)
  262. {
  263. //直推奖
  264. $user = (new UserModel())->get($uid);
  265. if (empty($user) || $user['parent_id'] == 0) {
  266. return;
  267. }
  268. $parent = (new UserModel())->get($user['parent_id']);
  269. if (empty($parent) || $parent['effective_time'] == 0) {//必须参加过算力租赁才能领取该收益
  270. return;
  271. }
  272. $this->changeWalletAccount($user['parent_id'], Asset::USDT, $server_info['referral_bonus'], Action::ServerReferralBonus, $uid);
  273. //间推荐奖,向上级的上级发放佣金
  274. if ($parent['parent_id'] == 0) {
  275. return;
  276. }
  277. $indirect = (new UserModel())->get($parent['parent_id']);
  278. if (empty($indirect) || $indirect['effective_time'] == 0) {//必须参加过算力租赁才能领取该收益
  279. return;
  280. }
  281. $this->changeWalletAccount($parent['parent_id'], Asset::USDT, $server_info['indirect_bonus'], Action::ServerIndirectBonus, $uid);
  282. //社长费用
  283. $user_parent_ids = (new UserPathModel())->where('user_id', $uid)->column('parent_id');
  284. $parent_users = (new UserModel())
  285. ->field('id,parent_id')
  286. ->where('id', 'in', $user_parent_ids)
  287. ->where('effective_time', '>', 0)//必须参加过算力租赁才能领取该收益
  288. ->order('id desc')
  289. ->select();
  290. if (empty($parent_users)) {
  291. return;
  292. }
  293. //发放社区长费用
  294. $info = $parent_users[0];//取第一个
  295. $this->changeWalletAccount($info['id'], Asset::USDT, $server_info['community_bonus'], Action::ServerCommunityBonus, $uid);
  296. //发放社区长推荐奖
  297. if ($info['parent_id'] > 0) {
  298. $community_info = (new UserModel())->get($info['parent_id']);
  299. if ($community_info['effective_time'] > 0) {
  300. $this->changeWalletAccount($info['id'], Asset::USDT, $server_info['community_referral_bonus'], Action::ServerCommunityReferralBonus, $uid);
  301. }
  302. }
  303. //系统领导人费用
  304. $sys_leader_info_id = 0;//定义待发放收益的系统领导人ID
  305. if ($sys_leader_info_id > 0) {
  306. $this->changeWalletAccount($sys_leader_info_id, Asset::USDT, $server_info['sys_leader_bonus'], Action::ServerSysLeaderBonus, $uid);
  307. }
  308. }
  309. public function users()
  310. {
  311. return $this->hasOne(UserModel::class, 'id', 'user_id');
  312. }
  313. public function getCreateTimeTextAttr($value, $data)
  314. {
  315. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  316. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  317. }
  318. public function getUpdateTimeTextAttr($value, $data)
  319. {
  320. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  321. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  322. }
  323. protected function setCreateTimeAttr($value)
  324. {
  325. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  326. }
  327. protected function setUpdateTimeAttr($value)
  328. {
  329. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  330. }
  331. }