| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\GoodsModel;
- use App\Models\OrderModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- use wxkxklmyt\Scws;
- /**
- * 货物商品管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Api
- */
- class GoodsService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- */
- public function __construct()
- {
- $this->model = new GoodsModel();
- }
- /**
- * 静态入口
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 列表数据
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 15)
- {
- $cacheKey = "caches:goods:picker:{$pageSize}_" . ($params ? md5(json_encode($params)) : 0);
- $datas = RedisService::get($cacheKey);
- if (empty($datas)) {
- $query = $this->getQuery($params)
- ->orderBy('a.create_time', 'desc')
- ->orderBy('a.id', 'desc');
- $field = ["a.*"];
- $list = $query->select($field)
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- if ($list) {
- foreach ($list['data'] as &$item) {
- $item['shipper_phone_text'] = $item['shipper_phone'] ? format_mobile($item['shipper_phone']) : '';
- $item['receiver_phone_text'] = $item['receiver_phone'] ? format_mobile($item['receiver_phone']) : '';
- }
- $datas = [
- 'pageSize' => $pageSize,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- RedisService::set($cacheKey, $datas, rand(3, 5));
- }
- }
- return $datas;
- }
- /**
- * 查询条件
- * @param $params
- * @return mixed
- */
- public function getQuery($params)
- {
- $where = ['a.status' => 1, 'a.mark' => 1];
- $status = isset($params['status']) ? $params['status'] : 1;
- if ($status > 0) {
- $where['a.status'] = $status;
- } else {
- unset($where['a.status']);
- }
- $model = $this->model->from('goods as a')
- ->where($where)
- ->where(function ($query) use ($params) {
- $pickerStatus = isset($params['picker_status']) ? $params['picker_status'] : 1;
- $userId = isset($params['user_id']) ? $params['user_id'] : 0;
- if ($pickerStatus) {
- $query->where(function ($query) use ($pickerStatus, $userId) {
- $goodsIds = OrderModel::where(function ($query) use ($userId) {
- $query->whereIn('status', [2, 3]);
- if($userId){
- $query->orWhere(function($query) use($userId){
- $query->where(['user_id' => $userId, 'status' => 1]);
- });
- }
- })
- ->where('mark', 1)
- ->where('create_time', '>=', time() - 30 * 86400)
- ->pluck('goods_id');
- $goodsIds = $goodsIds ? array_unique($goodsIds->toArray()) : [];
- $query->where('a.picker_status', $pickerStatus);
- if ($goodsIds) {
- $query->whereNotIn('a.id', $goodsIds);
- }
- });
- }
- $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
- if ($keyword) {
- $scws = new Scws();
- $kws = $scws->scws($keyword, 3, false);
- if (count($kws) > 1) {
- $query->where(function ($query) use ($kws) {
- foreach ($kws as $kw) {
- $kw = trim($kw);
- $query->where('a.goods_name', 'like', "%{$kw}%")
- ->orWhere('a.car_type', 'like', "%{$kw}%");
- }
- });
- } else {
- $query->where(function ($query) use ($keyword) {
- $query->where('a.goods_name', 'like', "%{$keyword}%")
- ->orWhere('a.car_type', 'like', "%{$keyword}%");
- });
- }
- }
- // 取货人
- $shipper = isset($params['shipper']) ? trim($params['shipper']) : '';
- if ($shipper) {
- $query->where(function ($query) use ($shipper) {
- $query->where('a.shipper_name', 'like', "%{$shipper}%")
- ->orWhere('a.shipper_phone', 'like', "%{$shipper}%");
- });
- }
- // 收货人
- $receiver = isset($params['receiver']) ? trim($params['receiver']) : '';
- if ($receiver) {
- $query->where(function ($query) use ($receiver) {
- $query->where('a.receiver_name', 'like', "%{$receiver}%")
- ->orWhere('a.receiver_phone', 'like', "%{$receiver}%");
- });
- }
- });
- return $model;
- }
- /**
- * 详情信息
- * @param $id
- * @return mixed
- */
- public function getInfo($id)
- {
- $cacheKey = "caches:goods:info_{$id}";
- $info = RedisService::get($cacheKey);
- if ($info) {
- return $info;
- }
- $info = $this->model->where(['id' => $id])->first();
- $info = $info ? $info->toArray() : [];
- if ($info) {
- $info['bonus'] = isset($info['price']) ? $info['price'] : 0;
- $info['shipper_phone_text'] = isset($info['shipper_phone']) ? format_mobile($info['shipper_phone']) : '';
- $info['receiver_phone_text'] = isset($info['receiver_phone']) ? format_mobile($info['receiver_phone']) : '';
- RedisService::set($cacheKey, $info, rand(10, 20));
- }
- return $info;
- }
- }
|