ProductService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\Product;
  5. class ProductService extends BaseService
  6. {
  7. public static $MODEL= Product::class;
  8. const STATUS_YES = 1;
  9. const STATUS_NOT = 0;
  10. /**
  11. * @description: 获取查询条件
  12. * @param {array} $search 查询内容
  13. * @return {array}
  14. */
  15. public static function getWhere(array $search = []): array
  16. {
  17. $where = [];
  18. if (isset($search['id']) && $search['id'] !== '') {
  19. $where[] = ['id', '=', $search['id']];
  20. }
  21. if (isset($search['is_top']) && $search['is_top'] !== '') {
  22. $where[] = ['is_top', '=', $search['is_top']];
  23. }
  24. if (isset($search['status']) && $search['status'] !== '') {
  25. $where[] = ['status', '=', $search['status']];
  26. }
  27. if (isset($search['min_price']) && $search['min_price'] !== '') {
  28. $where[] = ['price', '>', $search['min_price']];
  29. }
  30. if (isset($search['max_price']) && $search['max_price'] !== '') {
  31. $where[] = ['price', '<', $search['max_price']];
  32. }
  33. if (isset($search['name']) && $search['name'] !== '') {
  34. $where[] = ['name', 'like', '%' . $search['name'] . '%'];
  35. }
  36. return $where;
  37. }
  38. /**
  39. * @description: 查询单条数据
  40. * @param array $search
  41. * @return
  42. */
  43. public static function findOne(array $search)
  44. {
  45. return static::model()::where(static::getWhere($search))->first();
  46. }
  47. /**
  48. * @description: 查询所有数据
  49. * @param array $search
  50. * @return \Illuminate\Database\Eloquent\Collection
  51. */
  52. public static function findAll(array $search = [])
  53. {
  54. return static::model()::where(static::getWhere($search))->get();
  55. }
  56. /**
  57. * @description: 分页查询
  58. * @param array $search
  59. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  60. */
  61. public static function paginate(array $search = [])
  62. {
  63. $limit = isset($search['limit']) ? $search['limit'] : 15;
  64. $paginator = static::model()::where(static::getWhere($search))
  65. ->orderBy('id', 'desc')
  66. ->paginate($limit);
  67. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  68. }
  69. /**
  70. * @description:
  71. * @param {*} $params
  72. * @return {*}
  73. */
  74. public static function submit($params = [])
  75. {
  76. $result = false;
  77. $msg['code'] = self::NOT;
  78. $msg['msg'] = '';
  79. // 2. 判断是否是更新
  80. if (!empty($params['id'])) {
  81. // 更新
  82. $info = self::findOne(['id'=>$params['id']] );
  83. if (!$info) {
  84. $msg['msg'] = '数据不存在!';
  85. }else{
  86. $result = $info->update($params);
  87. $id = $params['id'];
  88. }
  89. } else {
  90. // 创建
  91. $result = $info = self::model()::create($params);
  92. $id = $result->id;
  93. }
  94. if($result){
  95. $msg['code'] = self::YES;
  96. $msg['msg'] = '设置成功';
  97. }else{
  98. $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
  99. }
  100. return $msg;
  101. }
  102. /**
  103. * @description: 获取随机的商品
  104. * @param {*} $notInIds 排除的商品
  105. * @param {*} $maxPrice 最高金额
  106. * @param {*} $minPrice 最低金额
  107. * @return {*}
  108. */
  109. public static function getRandom($notInIds = [] ,$maxPrice = 0 ,$minPrice = 0)
  110. {
  111. // // 有最低消费 优先找大于最低消费的
  112. // if($minPrice > 0){
  113. // $info = static::model()::where(self::getWhere(['min_price' => $minPrice ,'status' => self::STATUS_YES]))->whereNotIn('id',$notInIds)->orderBy('price','asc')->first();
  114. // return $info;
  115. // }
  116. // 先找适合自己余额的商品
  117. $info = static::model()::where(self::getWhere(['max_price' => $maxPrice ,'status' => self::STATUS_YES]))->whereNotIn('id',$notInIds)->inRandomOrder()->first();
  118. if(!$info){
  119. // 随机商品
  120. $info = static::model()::where(self::getWhere(['min_price' => $minPrice ,'status' => self::STATUS_YES]))->whereNotIn('id',$notInIds)->orderBy('price','asc')->first();
  121. return $info;
  122. }
  123. return $info;
  124. }
  125. }