AccountService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. if($list){
  59. $accountTypes = config('payment.accountTypes');
  60. foreach($list['data'] as &$item){
  61. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  62. $item['time_text'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H:i') : '';
  63. $type = isset($item['type'])? intval($item['type']) : 0;
  64. $item['type_text'] = isset($item['remark'])? trim($item['remark']) : '';
  65. if(empty($item['type_text'])){
  66. $item['type_text'] = isset($accountTypes[$type])? $accountTypes[$type] : '支付订单';
  67. }
  68. }
  69. }
  70. return [
  71. 'pageSize'=> $pageSize,
  72. 'total'=>isset($list['total'])? $list['total'] : 0,
  73. 'list'=> isset($list['data'])? $list['data'] : []
  74. ];
  75. }
  76. public function getQuery($params)
  77. {
  78. $where = ['a.mark' => 1];
  79. $status = isset($params['status'])? $params['status'] : 0;
  80. $type = isset($params['type'])? $params['type'] : 0;
  81. if($status>0){
  82. $where['a.status'] = $status;
  83. }
  84. if($type>0){
  85. $where['a.type'] = $type;
  86. }
  87. return $this->model->with(['member'])->from("account_logs as a")
  88. ->leftJoin('member as b','b.id','=','a.user_id')
  89. ->where($where)
  90. ->where(function ($query) use($params) {
  91. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  92. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  93. if($userId){
  94. $query->where('a.user_id',$userId);
  95. }
  96. if ($keyword) {
  97. $query->where(function($query) use($keyword){
  98. $query->where('b.nickname','like',"%{$keyword}%")
  99. ->orWhere('b.mobile','like',"%{$keyword}%")
  100. ->orWhere('b.realname','like',"%{$keyword}%");
  101. });
  102. }
  103. $orderNo = isset($params['order_no'])? trim($params['order_no']) : '';
  104. if($orderNo){
  105. $query->where(function($query) use($orderNo){
  106. $query->where('a.source_order_no','like',"%{$orderNo}%");
  107. });
  108. }
  109. })
  110. ->where(function ($query) use($params){
  111. // 日期
  112. $date = isset($params['date']) ? $params['date'] : [];
  113. $start = isset($date[0])? $date[0] : '';
  114. $end = isset($date[1])? $date[1] : '';
  115. $end = $start>=$end? '' : $end;
  116. if ($start) {
  117. $query->where('a.create_time','>=', strtotime($start));
  118. }
  119. if($end){
  120. $query->where('a.create_time','<=', strtotime($end));
  121. }
  122. });
  123. }
  124. /**
  125. * 今日金额统计数据
  126. * @param $userId
  127. * @param int $type
  128. * @return array|mixed
  129. */
  130. public function getTotalByDay($userId, $type=1)
  131. {
  132. $cacheKey = "caches:accounts:total_{$userId}_{$type}";
  133. $data = RedisService::get($cacheKey);
  134. if($data){
  135. return $data;
  136. }
  137. $data = $this->model->where(['user_id'=> $userId,'type'=>$type,'status'=>1,'mark'=>1])
  138. ->where('create_time','>=', strtotime(date('Y-m-d')))
  139. ->sum('money');
  140. if($data){
  141. RedisService::set($cacheKey, $data, rand(5,10));
  142. }
  143. return $data;
  144. }
  145. }