LedgerWalletModel.php 15 KB

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