MemberCouponService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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\MemberAddressModel;
  13. use App\Models\MemberCouponModel;
  14. use App\Services\BaseService;
  15. use App\Services\RedisService;
  16. use Illuminate\Support\Facades\DB;
  17. /**
  18. * 用户优惠券管理-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * @package App\Services\Api
  22. */
  23. class MemberCouponService extends BaseService
  24. {
  25. // 静态对象
  26. protected static $instance = null;
  27. /**
  28. * 构造函数
  29. * @author laravel开发员
  30. * @since 2020/11/11
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new MemberCouponModel();
  35. }
  36. /**
  37. * 静态入口
  38. * @return static|null
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = (new static());
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * 列表数据
  49. * @param $params
  50. * @param int $pageSize
  51. * @return array
  52. */
  53. public function getDataList($params, $pageSize = 15)
  54. {
  55. $where = ['a.mark' => 1,'a.status'=>1];
  56. $status = isset($params['status']) ? $params['status'] : 0;
  57. if ($status > 0) {
  58. $where['a.status'] = $status;
  59. }
  60. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  61. if ($userId > 0) {
  62. $where['a.user_id'] = $userId;
  63. }
  64. $list = $this->model->from('member_coupons as a')
  65. ->where(function($query) use($params){
  66. $ids = isset($params['ids'])?$params['ids']:[];
  67. if($ids){
  68. $query->where(function($query) use($ids){
  69. $query->where('a.goods_ids','');
  70. foreach ($ids as $id){
  71. $query->orWhere('a.goods_ids','like', "%{$id},%");
  72. }
  73. });
  74. }else{
  75. $query->where('goods_ids','=','');
  76. }
  77. $total = isset($params['total'])?$params['total']:0;
  78. if($total){
  79. $query->where(function($query) use($total){
  80. $query->whereIn('a.coupon_type',[20,30])
  81. ->orWhere(function($query) use($total){
  82. $query->where('a.coupon_type','=',10)
  83. ->where('a.min_price','<=', $total);
  84. });
  85. });
  86. }
  87. })
  88. ->where($where)
  89. ->select(['a.*'])
  90. ->orderBy('a.reduce_price', 'desc')
  91. ->orderBy('a.id', 'desc')
  92. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  93. $list = $list ? $list->toArray() : [];
  94. return [
  95. 'pageSize' => $pageSize,
  96. 'total' => isset($list['total']) ? $list['total'] : 0,
  97. 'list' => isset($list['data']) ? $list['data'] : []
  98. ];
  99. }
  100. /**
  101. * 可领取列表
  102. * @param $params
  103. * @param int $pageSize
  104. * @return array
  105. */
  106. public function getCouponList($params, $pageSize = 15)
  107. {
  108. $where = ['a.mark' => 1,'a.status'=>1];
  109. $status = isset($params['status']) ? $params['status'] : 0;
  110. if ($status > 0) {
  111. $where['a.status'] = $status;
  112. }
  113. $list = $this->model->from('coupons as a')
  114. ->where($where)
  115. ->where(function($query) use($params){
  116. $query->whereIn('a.coupon_type',[10,30])
  117. ->whereRaw('num > received_num')
  118. ->where('receive_at','<=', date('Y-m-d H:i:s'));
  119. })
  120. ->select(['a.*'])
  121. ->orderBy('a.id', 'desc')
  122. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  123. $list = $list ? $list->toArray() : [];
  124. return [
  125. 'pageSize' => $pageSize,
  126. 'total' => isset($list['total']) ? $list['total'] : 0,
  127. 'list' => isset($list['data']) ? $list['data'] : []
  128. ];
  129. }
  130. }