StreamBusiness.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace app\business;
  3. use Illuminate\Support\Arr;
  4. use support\Db;
  5. use support\Redis;
  6. class StreamBusiness
  7. {
  8. /** 新增金额
  9. * @param $uid
  10. * @param $money
  11. * @param $type
  12. * @param $mold
  13. * @param $mold_filed
  14. * @param $source_id
  15. * @return void
  16. * @throws \Exception
  17. */
  18. static public function addStream($uid, $money, $type, $mold, $mold_filed, $source_id = 0)
  19. {
  20. try {
  21. $userData = Db::table('wa_users')->where('id', $uid)->first();
  22. Db::table('wa_users')->where('id', $uid)->increment($mold_filed, $money);
  23. $total_money = bcadd($userData->$mold_filed, $money, 2);
  24. Db::table('wa_stream')->insert([
  25. 'user_id' => $uid,
  26. 'money' => $money,
  27. 'total_money' => $total_money,
  28. 'type' => $type,
  29. 'mold' => $mold,
  30. 'source_id' => $source_id,
  31. 'add_time' => time(),
  32. 'created_at' => date('Y-m-d H:i:s'),
  33. 'updated_at' => date('Y-m-d H:i:s'),
  34. ]);
  35. } catch (\Throwable $exception) {
  36. throw new \Exception($exception->getMessage());
  37. }
  38. }
  39. /** 扣除金额
  40. * @param $uid
  41. * @param $money
  42. * @param $type
  43. * @param $mold
  44. * @param $mold_filed
  45. * @param $source_id
  46. * @return void
  47. * @throws \Exception
  48. */
  49. static public function delStream($uid, $money, $type, $mold, $mold_filed, $source_id = 0)
  50. {
  51. try {
  52. $userData = Db::table('wa_users')->where('id', $uid)->first();
  53. Db::table('wa_users')->where('id', $uid)->decrement($mold_filed, $money);
  54. $total_money = bcsub($userData->$mold_filed, $money, 2);
  55. if ($total_money < 0) {
  56. throw new \Exception('您余额不足!');
  57. }
  58. Db::table('wa_stream')->insert([
  59. 'user_id' => $uid,
  60. 'money' => '-' . $money,
  61. 'total_money' => $total_money,
  62. 'income' => 2,
  63. 'type' => $type,
  64. 'mold' => $mold,
  65. 'source_id' => $source_id,
  66. 'add_time' => time(),
  67. 'created_at' => date('Y-m-d H:i:s'),
  68. 'updated_at' => date('Y-m-d H:i:s'),
  69. ]);
  70. } catch (\Throwable $exception) {
  71. throw new \Exception($exception->getMessage());
  72. }
  73. }
  74. }