LedgerWalletModel.php 13 KB

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