LedgerWalletModel.php 13 KB

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