LedgerWalletModel.php 15 KB

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