AccountService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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\AccountLogModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * 交易管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class AccountService
  20. */
  21. class AccountService extends BaseService
  22. {
  23. public static $instance = null;
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. * AccountService constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new AccountLogModel();
  33. }
  34. /**
  35. * 静态入口
  36. * @return static|null
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = (new static());
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * @param $params
  47. * @param int $pageSize
  48. * @return array
  49. */
  50. public function getDataList($params, $pageSize = 15)
  51. {
  52. $query = $this->getQuery($params);
  53. $list = $query->select(['a.*'])
  54. ->orderBy('a.create_time','desc')
  55. ->orderBy('a.id','desc')
  56. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  57. $list = $list? $list->toArray() :[];
  58. return [
  59. 'pageSize'=> $pageSize,
  60. 'total'=>isset($list['total'])? $list['total'] : 0,
  61. 'list'=> isset($list['data'])? $list['data'] : []
  62. ];
  63. }
  64. public function getQuery($params)
  65. {
  66. $where = ['a.mark' => 1];
  67. $type = isset($params['type'])? $params['type'] : 0;
  68. if($type>0){
  69. $where['a.type'] = $type;
  70. }
  71. return $this->model->with(['member'])->from("account_logs as a")
  72. ->leftJoin('member as b','b.id','=','a.user_id')
  73. ->where($where)
  74. ->where(function ($query) use($params) {
  75. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  76. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  77. if($userId){
  78. $query->where('a.user_id',$userId);
  79. }
  80. $status = isset($params['status'])? $params['status'] : 0;
  81. if($status){
  82. $query->where('a.status',$status);
  83. }else{
  84. $query->whereNotIn('a.status',[2]);
  85. }
  86. $changeType = isset($params['change_type'])? $params['change_type'] : 0;
  87. if($changeType==1){
  88. $query->where('money','<', 0);
  89. }else if($changeType==2){
  90. $query->where('money','>', 0);
  91. }
  92. if ($keyword) {
  93. $query->where(function($query) use($keyword){
  94. $query->where('b.nickname','like',"%{$keyword}%")
  95. ->orWhere('b.mobile','like',"%{$keyword}%")
  96. ->orWhere('b.realname','like',"%{$keyword}%");
  97. });
  98. }
  99. $orderNo = isset($params['order_no'])? trim($params['order_no']) : '';
  100. if($orderNo){
  101. $query->where(function($query) use($orderNo){
  102. $query->where('a.source_order_no','like',"%{$orderNo}%");
  103. });
  104. }
  105. })
  106. ->where(function ($query) use($params){
  107. // 日期
  108. $date = isset($params['date']) ? $params['date'] : [];
  109. $start = isset($date[0])? $date[0] : '';
  110. $end = isset($date[1])? $date[1] : '';
  111. $end = $start>=$end? '' : $end;
  112. if ($start) {
  113. $query->where('a.create_time','>=', strtotime($start));
  114. }
  115. if($end){
  116. $query->where('a.create_time','<=', strtotime($end));
  117. }
  118. });
  119. }
  120. /**
  121. * 账单
  122. * @param $id
  123. * @return array|mixed
  124. */
  125. public function getInfo($id)
  126. {
  127. $cacheKey = "caches:accounts:info_{$id}";
  128. $info = RedisService::get($cacheKey);
  129. if($info){
  130. return $info;
  131. }
  132. $info = $this->model->where(['id'=> $id,'mark'=>1])
  133. ->first();
  134. $info = $info? $info->toArray() : [];
  135. if($info){
  136. RedisService::set($cacheKey, $info, rand(5,10));
  137. }
  138. return $info;
  139. }
  140. }