AccountService.php 5.4 KB

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