PledgeOrderService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\Common;
  12. use App\Models\PledgeOrderModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * 质押订单管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * @package App\Services\Common
  20. */
  21. class PledgeOrderService extends BaseService
  22. {
  23. // 静态对象
  24. protected static $instance = null;
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new PledgeOrderModel();
  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. * 获取列表
  47. * @param $params 参数
  48. * @param int $pageSize 分页大小:默认 15
  49. * @return array
  50. */
  51. public function getDataList($params, $pageSize = 10, $field = [])
  52. {
  53. $query = $this->getQuery($params);
  54. $list = $query->select($field ? $field : ['a.*', 'b.nickname','b.wallet_url'])
  55. ->orderBy('a.create_time','desc')
  56. ->orderBy('a.id','desc')
  57. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  58. $list = $list ? $list->toArray() : [];
  59. if ($list) {
  60. foreach ($list['data'] as &$item) {
  61. $item['create_time'] = datetime($item['create_time'],'Y-m-d H:i:s');
  62. $item['uid'] = $item['user_id'];
  63. $item['account'] = isset($item['nickname'])&& $item['nickname']? $item['nickname'] : $item['user_id'];
  64. }
  65. }
  66. return [
  67. 'pageSize' => $pageSize,
  68. 'total' => isset($list['total']) ? $list['total'] : 0,
  69. 'list' => isset($list['data']) ? $list['data'] : []
  70. ];
  71. }
  72. /**
  73. * 查询构造
  74. * @param $params
  75. * @return \Illuminate\Database\Eloquent\Builder
  76. */
  77. public function getQuery($params)
  78. {
  79. $where = ['a.mark' => 1];
  80. return $this->model->with(['member'])
  81. ->from('pledge_orders as a')
  82. ->leftJoin('member as b', function($join) {
  83. $join->on('b.id','=', 'a.user_id');
  84. })
  85. ->where($where)
  86. ->where(function ($query) use ($params) {
  87. $kw = isset($params['keyword']) ? trim($params['keyword']) : '';
  88. if ($kw) {
  89. $query->where('b.id', '=', $params['keyword'])
  90. ->orWhere('b.nickname', 'like', "%{$params['keyword']}%");
  91. }
  92. })
  93. ->where(function ($query) use($params){
  94. // 日期
  95. $date = isset($params['date']) ? $params['date'] : [];
  96. $start = isset($date[0])? $date[0] : '';
  97. $end = isset($date[1])? $date[1] : '';
  98. $end = $start>$end? '' : $end;
  99. if ($start) {
  100. $query->where('a.create_time','>=', strtotime($start));
  101. }
  102. if($end){
  103. $query->where('a.create_time','<=', strtotime($end));
  104. }
  105. $orderNo = isset($params['order_no'])? trim($params['order_no']) : '';
  106. if($orderNo){
  107. $query->where('a.order_no', 'like', "%{$orderNo}%");
  108. }
  109. $walletUrl = isset($params['wallet_url'])? trim($params['wallet_url']) : '';
  110. if($walletUrl){
  111. $query->where('b.wallet_url', '=', $walletUrl);
  112. }
  113. $status = isset($params['status'])? $params['status'] : 0;
  114. if (is_array($status)) {
  115. $query->whereIn('a.status', $status);
  116. } else if($status){
  117. $query->where('a.status', $status);
  118. }
  119. });
  120. }
  121. /**
  122. * 统计
  123. * @param $params
  124. * @return array
  125. */
  126. public function count($params)
  127. {
  128. $query = $this->getQuery($params);
  129. $count = $query->count('a.id');
  130. $total = $query->sum('a.money');
  131. $profit = $query->sum('a.profit');
  132. return [
  133. 'count' => $count,
  134. 'total' => $total,
  135. 'profit' => $profit,
  136. ];
  137. }
  138. /**
  139. * 统计累计质押金额
  140. * @param $userId 用户ID
  141. * @return array|false|mixed
  142. */
  143. public function getTotalByUser($userId)
  144. {
  145. $cacheKey = "caches:pledgeOrder:total_{$userId}";
  146. $data = RedisService::get($cacheKey);
  147. if($data || RedisService::exists($cacheKey)){
  148. return $data;
  149. }
  150. $data = $this->model->where(['user_id'=> $userId,'mark'=>1])->sum('money');
  151. RedisService::set($cacheKey, $data, rand(5,10));
  152. return $data;
  153. }
  154. }