BalanceLogService.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\BalanceLogModel;
  13. use App\Models\PledgeOrderModel;
  14. use App\Models\PriceLogModel;
  15. use App\Services\BaseService;
  16. use App\Services\RedisService;
  17. /**
  18. * 余额账户-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * @package App\Services\Api
  22. */
  23. class BalanceLogService extends BaseService
  24. {
  25. protected static $instance;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new BalanceLogModel();
  34. }
  35. /**
  36. * 静态入口
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = new static();
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * 充值提现统计
  47. * @param int $userId 用户ID
  48. * @param $type 类型:1-充值,2-提现
  49. * @return array|mixed
  50. */
  51. public function getCountByType(int $userId=0, $type=1, $coinType=1, $status=2)
  52. {
  53. $cacheKey = "caches:balance:counts_{$userId}_{$type}_{$coinType}_{$status}";
  54. $counts = RedisService::get($cacheKey);
  55. if($counts){
  56. return $counts;
  57. }
  58. $where = ['user_id'=> $userId,'coin_type'=>0,'type'=>$type,'mark'=>1];
  59. if($coinType){
  60. $where['coin_type'] = $coinType;
  61. }else{
  62. unset($where['coin_type']);
  63. }
  64. $data = $this->model->where($where)->where(function ($query)use($status){
  65. if($status){
  66. $query->where('status', $status);
  67. }else{
  68. $query->whereIn('status', [1,2]);
  69. }
  70. })->sum('money');
  71. RedisService::set($cacheKey, $data, rand(3, 5));
  72. return $data;
  73. }
  74. }