GoodsService.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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\GoodsModel;
  13. use App\Models\OrderModel;
  14. use App\Services\BaseService;
  15. use App\Services\RedisService;
  16. use wxkxklmyt\Scws;
  17. /**
  18. * 货物商品管理-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * @package App\Services\Api
  22. */
  23. class GoodsService 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 GoodsModel();
  35. }
  36. /**
  37. * 静态入口
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = new static();
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * 列表数据
  48. * @param $params
  49. * @param int $pageSize
  50. * @return array
  51. */
  52. public function getDataList($params, $pageSize = 15)
  53. {
  54. $cacheKey = "caches:goods:picker:{$pageSize}_" . ($params ? md5(json_encode($params)) : 0);
  55. $datas = RedisService::get($cacheKey);
  56. if (empty($datas)) {
  57. $query = $this->getQuery($params)
  58. ->orderBy('a.create_time', 'desc')
  59. ->orderBy('a.id', 'desc');
  60. $field = ["a.*"];
  61. $list = $query->select($field)
  62. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  63. $list = $list ? $list->toArray() : [];
  64. if ($list) {
  65. foreach ($list['data'] as &$item) {
  66. $item['shipper_phone_text'] = $item['shipper_phone'] ? format_mobile($item['shipper_phone']) : '';
  67. $item['receiver_phone_text'] = $item['receiver_phone'] ? format_mobile($item['receiver_phone']) : '';
  68. }
  69. $datas = [
  70. 'pageSize' => $pageSize,
  71. 'total' => isset($list['total']) ? $list['total'] : 0,
  72. 'list' => isset($list['data']) ? $list['data'] : []
  73. ];
  74. RedisService::set($cacheKey, $datas, rand(3, 5));
  75. }
  76. }
  77. return $datas;
  78. }
  79. /**
  80. * 查询条件
  81. * @param $params
  82. * @return mixed
  83. */
  84. public function getQuery($params)
  85. {
  86. $where = ['a.status' => 1, 'a.mark' => 1];
  87. $status = isset($params['status']) ? $params['status'] : 1;
  88. if ($status > 0) {
  89. $where['a.status'] = $status;
  90. } else {
  91. unset($where['a.status']);
  92. }
  93. $model = $this->model->from('goods as a')
  94. ->where($where)
  95. ->where(function ($query) use ($params) {
  96. $pickerStatus = isset($params['picker_status']) ? $params['picker_status'] : 1;
  97. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  98. if ($pickerStatus) {
  99. $query->where(function ($query) use ($pickerStatus, $userId) {
  100. $goodsIds = OrderModel::where(function ($query) use ($userId) {
  101. $query->whereIn('status', [2, 3]);
  102. if($userId){
  103. $query->orWhere(function($query) use($userId){
  104. $query->where(['user_id' => $userId, 'status' => 1]);
  105. });
  106. }
  107. })
  108. ->where('mark', 1)
  109. ->where('create_time', '>=', time() - 30 * 86400)
  110. ->pluck('goods_id');
  111. $goodsIds = $goodsIds ? array_unique($goodsIds->toArray()) : [];
  112. $query->where('a.picker_status', $pickerStatus);
  113. if ($goodsIds) {
  114. $query->whereNotIn('a.id', $goodsIds);
  115. }
  116. });
  117. }
  118. $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
  119. if ($keyword) {
  120. $scws = new Scws();
  121. $kws = $scws->scws($keyword, 3, false);
  122. if (count($kws) > 1) {
  123. $query->where(function ($query) use ($kws) {
  124. foreach ($kws as $kw) {
  125. $kw = trim($kw);
  126. $query->where('a.goods_name', 'like', "%{$kw}%")
  127. ->orWhere('a.car_type', 'like', "%{$kw}%");
  128. }
  129. });
  130. } else {
  131. $query->where(function ($query) use ($keyword) {
  132. $query->where('a.goods_name', 'like', "%{$keyword}%")
  133. ->orWhere('a.car_type', 'like', "%{$keyword}%");
  134. });
  135. }
  136. }
  137. // 取货人
  138. $shipper = isset($params['shipper']) ? trim($params['shipper']) : '';
  139. if ($shipper) {
  140. $query->where(function ($query) use ($shipper) {
  141. $query->where('a.shipper_name', 'like', "%{$shipper}%")
  142. ->orWhere('a.shipper_phone', 'like', "%{$shipper}%");
  143. });
  144. }
  145. // 收货人
  146. $receiver = isset($params['receiver']) ? trim($params['receiver']) : '';
  147. if ($receiver) {
  148. $query->where(function ($query) use ($receiver) {
  149. $query->where('a.receiver_name', 'like', "%{$receiver}%")
  150. ->orWhere('a.receiver_phone', 'like', "%{$receiver}%");
  151. });
  152. }
  153. });
  154. return $model;
  155. }
  156. /**
  157. * 详情信息
  158. * @param $id
  159. * @return mixed
  160. */
  161. public function getInfo($id)
  162. {
  163. $cacheKey = "caches:goods:info_{$id}";
  164. $info = RedisService::get($cacheKey);
  165. if ($info) {
  166. return $info;
  167. }
  168. $info = $this->model->where(['id' => $id])->first();
  169. $info = $info ? $info->toArray() : [];
  170. if ($info) {
  171. $info['bonus'] = isset($info['price']) ? $info['price'] : 0;
  172. $info['shipper_phone_text'] = isset($info['shipper_phone']) ? format_mobile($info['shipper_phone']) : '';
  173. $info['receiver_phone_text'] = isset($info['receiver_phone']) ? format_mobile($info['receiver_phone']) : '';
  174. RedisService::set($cacheKey, $info, rand(10, 20));
  175. }
  176. return $info;
  177. }
  178. }