PledgeOrderService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. /**
  15. * 质押订单管理-服务类
  16. * @author laravel开发员
  17. * @since 2020/11/11
  18. * @package App\Services\Common
  19. */
  20. class PledgeOrderService extends BaseService
  21. {
  22. // 静态对象
  23. protected static $instance = null;
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new PledgeOrderModel();
  32. }
  33. /**
  34. * 静态入口
  35. * @return static|null
  36. */
  37. public static function make()
  38. {
  39. if (!self::$instance) {
  40. self::$instance = (new static());
  41. }
  42. return self::$instance;
  43. }
  44. /**
  45. * 获取列表
  46. * @param $params 参数
  47. * @param int $pageSize 分页大小:默认 15
  48. * @return array
  49. */
  50. public function getDataList($params, $pageSize = 10, $field = [])
  51. {
  52. $query = $this->getQuery($params);
  53. $list = $query->select($field ? $field : ['a.*', 'b.nickname','b.wallet_url'])
  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. foreach ($list['data'] as &$item) {
  60. $item['create_time'] = datetime($item['create_time'],'Y-m-d H:i:s');
  61. if($item['user_type'] == 1){
  62. $item['uid'] = $item['user_id'];
  63. $item['account'] = isset($item['nickname'])&& $item['nickname']? $item['nickname'] : $item['user_id'];
  64. }
  65. }
  66. }
  67. return [
  68. 'pageSize' => $pageSize,
  69. 'total' => isset($list['total']) ? $list['total'] : 0,
  70. 'list' => isset($list['data']) ? $list['data'] : []
  71. ];
  72. }
  73. /**
  74. * 查询构造
  75. * @param $params
  76. * @return \Illuminate\Database\Eloquent\Builder
  77. */
  78. public function getQuery($params)
  79. {
  80. $where = ['a.mark' => 1];
  81. return $this->model->with(['member'])
  82. ->from('pledge_orders as a')
  83. ->leftJoin('member as b', function($join) {
  84. $join->on('b.id','=', 'a.user_id');
  85. })
  86. ->where($where)
  87. ->where(function ($query) use ($params) {
  88. $kw = isset($params['keyword']) ? trim($params['keyword']) : '';
  89. if ($kw) {
  90. $query->where('b.id', '=', $params['keyword'])
  91. ->orWhere('b.nickname', 'like', "%{$params['keyword']}%")
  92. ->orWhere('b.wallet_url', '=', trim($params['keyword']));
  93. }
  94. })
  95. ->where(function ($query) use($params){
  96. // 日期
  97. $date = isset($params['date']) ? $params['date'] : [];
  98. $start = isset($date[0])? $date[0] : '';
  99. $end = isset($date[1])? $date[1] : '';
  100. $end = $start>$end? '' : $end;
  101. if ($start) {
  102. $query->where('a.create_time','>=', strtotime($start));
  103. }
  104. if($end){
  105. $query->where('a.create_time','<=', strtotime($end));
  106. }
  107. $orderNo = isset($params['order_no'])? trim($params['order_no']) : '';
  108. if($orderNo){
  109. $query->where('a.order_no', 'like', "%{$orderNo}%");
  110. }
  111. $status = isset($params['status'])? $params['status'] : 0;
  112. if (is_array($status)) {
  113. $query->whereIn('a.status', $status);
  114. } else if($status){
  115. $query->where('a.status', $status);
  116. }
  117. });
  118. }
  119. /**
  120. * 统计
  121. * @param $params
  122. * @return array
  123. */
  124. public function count($params)
  125. {
  126. $query = $this->getQuery($params);
  127. $count = $query->count('a.id');
  128. $total = $query->sum('a.money');
  129. $profit = $query->sum('a.profit');
  130. return [
  131. 'count' => $count,
  132. 'total' => $total,
  133. 'profit' => $profit,
  134. ];
  135. }
  136. }