LedgerWalletModel.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 SameLevel = 405;
  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::SameLevel => '平级奖励',
  39. ];
  40. public function getWallet($userID)
  41. {
  42. return $this->where('user_id', $userID)->find();
  43. }
  44. /**
  45. * 仅事务内使用,会附带"for update"
  46. * @param $userID
  47. * @throws Exception
  48. */
  49. public function getWalletTX($userID)
  50. {
  51. return $this->lock(true)->where('user_id', $userID)->find();
  52. }
  53. /**
  54. * 仅更新钱包余额,没有账变
  55. * @param int $uid 用户ID
  56. * @param string $asset 资产类型
  57. * @param string $amount 金额 正:表示加 负:表示减
  58. * @return string 变动后的金额
  59. * @throws Exception
  60. */
  61. public function changeWalletOnly(int $uid, string $asset, string $amount): string
  62. {
  63. $available = $this->getWalletTX($uid);
  64. if (empty($available)) {
  65. throw new Exception('用户资产信息不存在');
  66. }
  67. // 金额为0的判断
  68. $amount = bcadd($amount, 0, 6);
  69. if (bccomp($amount, '0', 6) == 0) {
  70. throw new Exception('金额变动为0');
  71. }
  72. // 余额不足的判断
  73. if ($amount < 0 && $available[$asset] < -$amount) {
  74. throw new Exception($asset . '余额不足');
  75. }
  76. $newAmount = bcadd($available[$asset], $amount, 6); // 新余额
  77. $walletUpdate = [
  78. $asset => $newAmount,
  79. 'update_time' => time(),
  80. ];
  81. // 更新余额
  82. $changeRs = $this->save($walletUpdate, ['user_id' => $uid]);
  83. if (empty($changeRs)) {
  84. throw new Exception('更新' . $asset . '余额失败');
  85. }
  86. return $newAmount;
  87. }
  88. /**
  89. * 发放直推USDT收益
  90. * @param int $uid
  91. * @param string $power
  92. * @return void
  93. * @throws DbException
  94. */
  95. public function sendUsdtProfit(int $uid, string $amount)
  96. {
  97. $ratio = (new Config())->getValue('direct_income');//直推奖励
  98. $user = (new UserModel())->get($uid);
  99. if (empty($user) || $user['parent_id'] == 0) {
  100. return;
  101. }
  102. $parent = (new UserModel())->get($user['parent_id']);
  103. if (empty($parent)) {
  104. return;
  105. }
  106. $temp = ['拦截提醒', $uid, $amount, $amount * $ratio];
  107. //dump($temp);
  108. // 上级算力的账变
  109. $this->changeWalletAccount($user['parent_id'], Asset::USDT, $amount * $ratio, Action::UsdtShareBonus, $uid);
  110. }
  111. /**
  112. * 发放直推收益
  113. * @param int $uid
  114. * @param string $power
  115. * @return void
  116. * @throws DbException
  117. */
  118. public function sendDirectProfit(int $uid, string $power)
  119. {
  120. $user = (new UserModel())->get($uid);
  121. if (empty($user) || $user['parent_id'] == 0) {
  122. return;
  123. }
  124. $parent = (new UserModel())->get($user['parent_id']);
  125. if (empty($parent)) {
  126. return;
  127. }
  128. // dump('参数打印');
  129. // dump($power);
  130. // 查询直推奖励比例
  131. $directProfit = (new Config())->getValue('direct_income');
  132. $directProfitFloat = floatval($directProfit);
  133. if (is_null($directProfit) || $directProfitFloat <= 0) {
  134. return;
  135. }
  136. // 每直推一个增加20%,封顶收益为100%
  137. // $directProfitFloat = $directProfitFloat * ($parent['direct_num']);
  138. // if ($directProfitFloat > 1) {
  139. // $directProfitFloat = 1;
  140. // }
  141. $directPower = $power * $directProfitFloat; // 可获得的直推奖励(算力)
  142. if($directPower > 0){
  143. $this->changeWalletAccount($user['parent_id'], Asset::POWER, $directPower, Action::PowerDirectAward, $uid);
  144. }
  145. // 上级算力的账变
  146. }
  147. /**
  148. * 发放代数收益
  149. * @param int $uid 服务器算力的用户ID
  150. * @param string $usdt 报单金额
  151. * @return void
  152. * @throws Exception
  153. */
  154. public function sendGenerateProfit(int $uid, string $usdt)
  155. {
  156. //上上级ID
  157. $parentID = (new UserPathModel())->getParentID($uid, 2);
  158. if(empty($parentID)){
  159. return;
  160. }
  161. // 查询代数收益比例
  162. $directProfit = (new ParametersModel)->getValue('generateProfitRate');
  163. $rateArr = explode(',', $directProfit);
  164. if (empty($rateArr) || count($rateArr) != 2) {
  165. throw new Exception('获取代数收益比例失败');
  166. }
  167. $genRate2 = explode(':', $rateArr[0]); // 上上级人数和比例
  168. $genRate3 = explode(':', $rateArr[1]); // 上上上级人数和比例
  169. // 上上级的发放
  170. $amount = bcmul($genRate2[1], $usdt, 6);
  171. if ($amount > 0) {
  172. $parent1 = (new UserModel())->getById($parentID);
  173. if (!empty($parent1) && $parent1['direct_num'] >= $genRate2[0]) { // 有效直推人数达标
  174. (new LedgerWalletModel())->changeWalletAccount($parentID, Asset::USDT, $amount, Action::UsdtGenerateProfit, $uid);
  175. }
  176. }
  177. }
  178. /**
  179. * 发放见点奖
  180. * @param int $uid 服务器算力的用户ID
  181. * @param string $power 该租赁得到的算力
  182. * @return void
  183. * @throws Exception
  184. */
  185. public function sendRegBonus(int $uid)
  186. {
  187. $parentIDs = (new UserPathModel())->getAllParentIDs($uid);
  188. if (count($parentIDs) == 0) {
  189. return;
  190. }
  191. $userList = (new UserModel())
  192. ->field('id,team_level_id')
  193. ->where('id', 'in', $parentIDs)
  194. ->where('team_level_id', '>', 0)
  195. ->order('id desc')
  196. ->select();
  197. if (empty($userList)) {
  198. //没有符合条件的上级
  199. return;
  200. }
  201. $team_level_config = DB::table('team_level')
  202. ->field('level_id, reg_bonus')
  203. ->select();
  204. $level_reg_bonus = [];
  205. foreach ($team_level_config as $item) {
  206. $level_reg_bonus[$item['level_id']] = $item['reg_bonus'];
  207. }
  208. $now_level = 0;
  209. foreach ($userList as $item) {
  210. if ($item['team_level_id'] > $now_level && isset($level_reg_bonus[$item['team_level_id']])) {
  211. (new LedgerWalletModel())->changeWalletAccount($item['id'], Asset::USDT, $level_reg_bonus[$item['team_level_id']], Action::UsdtRegBonus, $uid);
  212. $now_level = $item['team_level_id'];
  213. }
  214. }
  215. }
  216. /**
  217. * 发放服务器市场推荐相关收益
  218. * @param int $uid
  219. * @param string $power
  220. * @return void
  221. * @throws DbException
  222. */
  223. public function sendMarketBonus(int $uid, array $server_info)
  224. {
  225. //直推奖
  226. $user = (new UserModel())->get($uid);
  227. if (empty($user) || $user['parent_id'] == 0) {
  228. return;
  229. }
  230. $parent = (new UserModel())->get($user['parent_id']);
  231. if (empty($parent) || $parent['effective_time'] == 0) {//必须参加过算力租赁才能领取该收益
  232. return;
  233. }
  234. $this->changeWalletAccount($user['parent_id'], Asset::USDT, $server_info['referral_bonus'], Action::ServerReferralBonus, $uid);
  235. //间推荐奖,向上级的上级发放佣金
  236. if ($parent['parent_id'] == 0) {
  237. return;
  238. }
  239. $indirect = (new UserModel())->get($parent['parent_id']);
  240. if (empty($indirect) || $indirect['effective_time'] == 0) {//必须参加过算力租赁才能领取该收益
  241. return;
  242. }
  243. $this->changeWalletAccount($parent['parent_id'], Asset::USDT, $server_info['indirect_bonus'], Action::ServerIndirectBonus, $uid);
  244. //社长费用
  245. $user_parent_ids = (new UserPathModel())->where('user_id', $uid)->column('parent_id');
  246. $parent_users = (new UserModel())
  247. ->field('id,parent_id')
  248. ->where('id', 'in', $user_parent_ids)
  249. ->where('effective_time', '>', 0)//必须参加过算力租赁才能领取该收益
  250. ->order('id desc')
  251. ->select();
  252. if (empty($parent_users)) {
  253. return;
  254. }
  255. //发放社区长费用
  256. $info = $parent_users[0];//取第一个
  257. $this->changeWalletAccount($info['id'], Asset::USDT, $server_info['community_bonus'], Action::ServerCommunityBonus, $uid);
  258. //发放社区长推荐奖
  259. if ($info['parent_id'] > 0) {
  260. $community_info = (new UserModel())->get($info['parent_id']);
  261. if ($community_info['effective_time'] > 0) {
  262. $this->changeWalletAccount($info['id'], Asset::USDT, $server_info['community_referral_bonus'], Action::ServerCommunityReferralBonus, $uid);
  263. }
  264. }
  265. //系统领导人费用
  266. $sys_leader_info_id = 0;//定义待发放收益的系统领导人ID
  267. if ($sys_leader_info_id > 0) {
  268. $this->changeWalletAccount($sys_leader_info_id, Asset::USDT, $server_info['sys_leader_bonus'], Action::ServerSysLeaderBonus, $uid);
  269. }
  270. }
  271. public function getCreateTimeTextAttr($value, $data)
  272. {
  273. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  274. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  275. }
  276. public function getUpdateTimeTextAttr($value, $data)
  277. {
  278. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  279. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  280. }
  281. protected function setCreateTimeAttr($value)
  282. {
  283. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  284. }
  285. protected function setUpdateTimeAttr($value)
  286. {
  287. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  288. }
  289. }