LedgerWalletModel.php 13 KB

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