LedgerWalletModel.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. //0支付 1转让支付 2 转让收款 3 充值 4 提现 5扣除书续费
  21. const Popular = 0;
  22. const Payment = 1;
  23. const Receive = 2;
  24. const Recharge = 3;
  25. const Withdraw = 4;
  26. const Share = 5;
  27. const Return = 6;
  28. const Giveaway = 7;
  29. const Direct = 8;
  30. const System = 9;
  31. const GiftPay = 10; //赠送支出
  32. const GiftReceipt = 11;
  33. const Community = 12; //社区津贴
  34. const Service = 13; //服务津贴
  35. const Together = 14; //共创津贴
  36. const Freight = 15; //物流运费
  37. const Super = 16; //茶宝标记激活
  38. /*
  39. * 支付状态
  40. * 0未支付 100支付中 200支付成功 400支付失败
  41. */
  42. public $pay_status = [
  43. '-1' => '全部',
  44. self::Popular => '热销支付',
  45. self::Payment => '转让支付',
  46. self::Receive => '转让收款',
  47. self::Recharge => '充值',
  48. self::Withdraw => '提现',
  49. self::Share => '分享',
  50. self::Return => '退回',
  51. self::Giveaway => '赠送手续费',
  52. self::Direct => '布道津贴', //直推
  53. self::System => '系统调整',
  54. self::GiftPay => '转账支出',
  55. self::GiftReceipt => '转账收款',
  56. self::Community => '社区津贴',
  57. self::Service => '服务津贴',
  58. self::Together => '共创津贴',
  59. self::Freight => '物流运费',
  60. self::Super => '茶宝标记激活',
  61. ];
  62. public static function getWalletChaBao($userID)
  63. {
  64. return self::where('user_id', $userID)->value('token');
  65. }
  66. public static function getWalletTotalChaBao($userID)
  67. {
  68. return self::where('user_id', $userID)->value('token + frozen');
  69. }
  70. public static function getWallet($userID)
  71. {
  72. return self::where('user_id', $userID)->find();
  73. }
  74. /**
  75. * 仅事务内使用,会附带"for update"
  76. * @param $userID
  77. * @throws Exception
  78. */
  79. public function getWalletTX($userID)
  80. {
  81. return $this->lock(true)->where('user_id', $userID)->find();
  82. }
  83. public static function getStatusList()
  84. {
  85. return [
  86. self::Popular => __('热销支付'),
  87. self::Payment => __('转让支付'),
  88. self::Receive => __('转让收款'),
  89. self::Recharge => __('充值'),
  90. self::Withdraw => __('提现'),
  91. self::Share => __('分享'),
  92. self::Return => __('退回'),
  93. self::Giveaway => __('赠送'),
  94. self::Direct => __('布道津贴'),
  95. self::System => __('系统调整'),
  96. self::GiftPay => __('转账支出'),
  97. self::GiftReceipt => __('转账收款'),
  98. self::Community => __('社区津贴'),
  99. self::Service => __('服务津贴'),
  100. self::Together => __('共创津贴'),
  101. self::Freight => __('物流运费'),
  102. self::Super => __('茶宝标记激活')];
  103. }
  104. /**
  105. * 更新钱包余额并添加账变记录
  106. * @param int $uid 用户ID
  107. * @param string $asset 资产类型
  108. * @param string $amount 金额 正:表示加 负:表示减
  109. * @param int $action 账变类型
  110. * @return void
  111. * @throws Exception
  112. */
  113. public function changeWalletAccount(int $uid, string $asset, string $amount, int $action, int $from_id = 0)
  114. {
  115. $available = $this->getWallet($uid);
  116. if (empty($available)) (new LedgerWalletModel())->insertGetId(['user_id' => $uid]); // 创建钱包
  117. $frozen = 0; //冻结金额
  118. // 账变资产模型
  119. switch ($asset) {
  120. case Asset::POWER:
  121. $changeModel = new LedgerPowerChangeModel();
  122. break;
  123. case Asset::USDT:
  124. $changeModel = new LedgerUsdtChangeModel();
  125. break;
  126. case Asset::TOKEN:
  127. $changeModel = new LedgerTokenChangeModel();
  128. if($action == self::Giveaway || $action == self::Freight) $frozen = $available->frozen;
  129. break;
  130. case Asset::TEAC:
  131. $changeModel = new LedgerTeacChangeModel();
  132. break;
  133. case Asset::SMH:
  134. $changeModel = new LedgerSmhChangeModel();
  135. break;
  136. case Asset::QUBIC:
  137. $changeModel = new LedgerQubicChangeModel();
  138. break;
  139. default:
  140. throw new Exception('币种错误:' . $asset);
  141. }
  142. // 更新钱包余额
  143. $newAmount = $this->changeWalletOnly($uid, $asset, $amount, $frozen);
  144. // 创建账变记录
  145. $insertRs = $changeModel->insert([
  146. 'user_id' => $uid,
  147. 'from_id' => $from_id,
  148. 'change_amount' => $amount,
  149. 'present_amount' => $newAmount,
  150. 'create_time' => time(),
  151. 'action' => $action
  152. ]);
  153. if (empty($insertRs)) {
  154. throw new Exception('创建' . $asset . '账变记录失败');
  155. }
  156. }
  157. /**
  158. * 冻结茶宝
  159. * @param int $uid 服务器算力的用户ID
  160. * @param string $usdt 报单金额
  161. * @return void
  162. * @throws Exception
  163. */
  164. public function sendUserSubFrozen(int $uid, string $amount, int $action, string $icn)
  165. {
  166. $available = $this->getWallet($uid);
  167. $ledgerWalletModel = new LedgerWalletModel();
  168. if (empty($available)) $ledgerWalletModel->insertGetId(['user_id' => $uid]); // 创建钱包
  169. $newAmount = $available->token;
  170. if($newAmount < $amount) throw new Exception('茶宝不足');
  171. // 创建账变记录
  172. $insertRs = $ledgerWalletModel->where('user_id', $uid)->update(['token' => bcsub($newAmount, $amount, 6), 'frozen' => bcadd($available->frozen, $amount, 6)]);
  173. // 创建账变记录
  174. $insertRs = (new LedgerTokenChangeModel)->insert([
  175. 'user_id' => $uid,
  176. 'from_id' => 0,
  177. 'change_amount' => $icn.$amount,
  178. 'present_amount' => $newAmount,
  179. 'create_time' => time(),
  180. 'action' => $action
  181. ]);
  182. if (empty($insertRs)) {
  183. throw new Exception('创建账变记录失败');
  184. }
  185. }
  186. /**
  187. * 仅更新钱包余额,没有账变
  188. * @param int $uid 用户ID
  189. * @param string $asset 资产类型
  190. * @param string $amount 金额 正:表示加 负:表示减
  191. * @return string 变动后的金额
  192. * @throws Exception
  193. */
  194. public function changeWalletOnly(int $uid, string $asset, string $amount, string $freeze): string
  195. {
  196. $available = $this->getWalletTX($uid);
  197. if (empty($available)) {
  198. throw new Exception('用户资产信息不存在');
  199. }
  200. // 金额为0的判断
  201. $amount = bcadd($amount, 0, 6);
  202. if (bccomp($amount, '0', 6) == 0) {
  203. throw new Exception('金额变动为0');
  204. }
  205. //总金额+冻结金额
  206. $totalAmount = bcadd($available[$asset], $freeze, 6);
  207. // 余额不足的判断
  208. if ($amount < 0 && $totalAmount < -$amount) {
  209. throw new Exception($asset . '余额不足');
  210. }
  211. //扣除冻结金额
  212. if($freeze > 0){
  213. if($freeze > -$amount) $walletUpdate['frozen'] = bcsub($freeze, -$amount, 6);
  214. if($freeze <= -$amount) $walletUpdate = [$asset => bcadd($totalAmount, $amount, 6), 'frozen' => 0];
  215. }else{
  216. $newAmount = bcadd($totalAmount, $amount, 6); // 新余额
  217. $walletUpdate = [$asset => $newAmount, 'update_time' => time()];
  218. }
  219. // 更新余额
  220. $changeRs = $this->save($walletUpdate, ['user_id' => $uid]);
  221. if (empty($changeRs)) {
  222. throw new Exception('更新' . $asset . '余额失败');
  223. }
  224. return $totalAmount;
  225. }
  226. /**
  227. * 发放直推USDT收益
  228. * @param int $uid
  229. * @param string $power
  230. * @return void
  231. * @throws DbException
  232. */
  233. public function sendUsdtProfit(int $uid, string $amount)
  234. {
  235. $ratio = (new Config())->getValue('direct_income');//直推奖励
  236. $user = (new UserModel())->get($uid);
  237. if (empty($user) || $user['parent_id'] == 0) {
  238. return;
  239. }
  240. $parent = (new UserModel())->get($user['parent_id']);
  241. if (empty($parent)) {
  242. return;
  243. }
  244. $temp = ['拦截提醒', $uid, $amount, $amount * $ratio];
  245. //dump($temp);
  246. // 上级算力的账变
  247. $this->changeWalletAccount($user['parent_id'], Asset::USDT, $amount * $ratio, Action::UsdtShareBonus, $uid);
  248. }
  249. /**
  250. * 发放直推收益
  251. * @param int $uid
  252. * @param string $power
  253. * @return void
  254. * @throws DbException
  255. */
  256. public function sendDirectProfit(int $uid, string $power)
  257. {
  258. $user = (new UserModel())->get($uid);
  259. if (empty($user) || $user['parent_id'] == 0) {
  260. return;
  261. }
  262. $parent = (new UserModel())->get($user['parent_id']);
  263. if (empty($parent)) {
  264. return;
  265. }
  266. // 查询直推奖励比例
  267. $directProfit = (new Config())->getValue('direct_income');
  268. $directProfitFloat = floatval($directProfit);
  269. if (is_null($directProfit) || $directProfitFloat <= 0) {
  270. return;
  271. }
  272. $directPower = $power * $directProfitFloat; // 可获得的直推奖励(算力)
  273. if($directPower > 0){
  274. $this->changeWalletAccount($user['parent_id'], Asset::POWER, $directPower, Action::PowerDirectAward, $uid);
  275. }
  276. // 上级算力的账变
  277. }
  278. /**
  279. * 发放见点奖
  280. * @param int $uid 服务器算力的用户ID
  281. * @param string $power 该租赁得到的算力
  282. * @return void
  283. * @throws Exception
  284. */
  285. public function sendRegBonus(int $uid)
  286. {
  287. $parentIDs = (new UserPathModel())->getAllParentIDs($uid);
  288. if (count($parentIDs) == 0) {
  289. return;
  290. }
  291. $userList = (new UserModel())
  292. ->field('id,team_level_id')
  293. ->where('id', 'in', $parentIDs)
  294. ->where('team_level_id', '>', 0)
  295. ->order('id desc')
  296. ->select();
  297. if (empty($userList)) {
  298. //没有符合条件的上级
  299. return;
  300. }
  301. $team_level_config = DB::table('team_level')
  302. ->field('level_id, reg_bonus')
  303. ->select();
  304. $level_reg_bonus = [];
  305. foreach ($team_level_config as $item) {
  306. $level_reg_bonus[$item['level_id']] = $item['reg_bonus'];
  307. }
  308. $now_level = 0;
  309. foreach ($userList as $item) {
  310. if ($item['team_level_id'] > $now_level && isset($level_reg_bonus[$item['team_level_id']])) {
  311. (new LedgerWalletModel())->changeWalletAccount($item['id'], Asset::USDT, $level_reg_bonus[$item['team_level_id']], Action::UsdtRegBonus, $uid);
  312. $now_level = $item['team_level_id'];
  313. }
  314. }
  315. }
  316. /**
  317. * 发放服务器市场推荐相关收益
  318. * @param int $uid
  319. * @param string $power
  320. * @return void
  321. * @throws DbException
  322. */
  323. public function sendMarketBonus(int $uid, array $server_info)
  324. {
  325. //直推奖
  326. $user = (new UserModel())->get($uid);
  327. if (empty($user) || $user['parent_id'] == 0) {
  328. return;
  329. }
  330. $parent = (new UserModel())->get($user['parent_id']);
  331. if (empty($parent) || $parent['effective_time'] == 0) {//必须参加过算力租赁才能领取该收益
  332. return;
  333. }
  334. $this->changeWalletAccount($user['parent_id'], Asset::USDT, $server_info['referral_bonus'], Action::ServerReferralBonus, $uid);
  335. //间推荐奖,向上级的上级发放佣金
  336. if ($parent['parent_id'] == 0) {
  337. return;
  338. }
  339. $indirect = (new UserModel())->get($parent['parent_id']);
  340. if (empty($indirect) || $indirect['effective_time'] == 0) {//必须参加过算力租赁才能领取该收益
  341. return;
  342. }
  343. $this->changeWalletAccount($parent['parent_id'], Asset::USDT, $server_info['indirect_bonus'], Action::ServerIndirectBonus, $uid);
  344. //社长费用
  345. $user_parent_ids = (new UserPathModel())->where('user_id', $uid)->column('parent_id');
  346. $parent_users = (new UserModel())
  347. ->field('id,parent_id')
  348. ->where('id', 'in', $user_parent_ids)
  349. ->where('effective_time', '>', 0)//必须参加过算力租赁才能领取该收益
  350. ->order('id desc')
  351. ->select();
  352. if (empty($parent_users)) {
  353. return;
  354. }
  355. //发放社区长费用
  356. $info = $parent_users[0];//取第一个
  357. $this->changeWalletAccount($info['id'], Asset::USDT, $server_info['community_bonus'], Action::ServerCommunityBonus, $uid);
  358. //发放社区长推荐奖
  359. if ($info['parent_id'] > 0) {
  360. $community_info = (new UserModel())->get($info['parent_id']);
  361. if ($community_info['effective_time'] > 0) {
  362. $this->changeWalletAccount($info['id'], Asset::USDT, $server_info['community_referral_bonus'], Action::ServerCommunityReferralBonus, $uid);
  363. }
  364. }
  365. //系统领导人费用
  366. $sys_leader_info_id = 0;//定义待发放收益的系统领导人ID
  367. if ($sys_leader_info_id > 0) {
  368. $this->changeWalletAccount($sys_leader_info_id, Asset::USDT, $server_info['sys_leader_bonus'], Action::ServerSysLeaderBonus, $uid);
  369. }
  370. }
  371. public function users()
  372. {
  373. return $this->hasOne(UserModel::class, 'id', 'user_id');
  374. }
  375. public function getCreateTimeTextAttr($value, $data)
  376. {
  377. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  378. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  379. }
  380. public function getUpdateTimeTextAttr($value, $data)
  381. {
  382. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  383. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  384. }
  385. protected function setCreateTimeAttr($value)
  386. {
  387. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  388. }
  389. protected function setUpdateTimeAttr($value)
  390. {
  391. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  392. }
  393. }