123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- <?php
- namespace addons\shopro\service\goods;
- use app\admin\model\shopro\goods\Goods;
- use app\admin\model\shopro\goods\SkuPrice;
- use app\admin\model\shopro\app\ScoreSkuPrice;
- use app\admin\model\shopro\Category;
- use think\Db;
- use addons\shopro\library\Tree;
- use addons\shopro\service\SearchHistory;
- use addons\shopro\facade\Activity as ActivityFacade;
- class GoodsService
- {
- protected $query = null;
- protected $order = [];
- protected $md5s = [];
- protected $format = null;
- protected $is_activity = false;
- protected $show_score_shop = false;
- public function __construct(\Closure $format = null, \think\db\Query $query = null)
- {
- $this->query = $query ?: new Goods();
- $this->format = $format;
- }
-
- public function with($with)
- {
- $this->query->with($with);
- return $this;
- }
-
- public function show()
- {
- $this->md5s[] = 'show';
- $this->query = $this->query->whereIn('status', ['up', 'hidden']);
- return $this;
- }
-
- public function up()
- {
- $this->md5s[] = 'up';
- $this->query = $this->query->where('status', 'up');
- return $this;
- }
-
- public function activity($activity = false)
- {
- $this->is_activity = $activity ? true : false;
- return $this;
- }
-
- public function score()
- {
- $this->show_score_shop = true;
-
- $scoreGoodsIds = ScoreSkuPrice::where('status', 'up')->group('goods_id')->cache(60)->column('goods_id');
- $this->query = $this->query->whereIn('id', $scoreGoodsIds);
- return $this;
- }
-
- public function stock()
- {
- $this->md5s[] = 'stock';
- $skuSql = SkuPrice::field('sum(stock) as stock, goods_id as sku_goods_id')->group('goods_id')->buildSql();
- $this->query = $this->query->join([$skuSql => 'sp'], 'id = sp.sku_goods_id', 'left');
- return $this;
- }
-
- public function search($keyword)
- {
- $keyword = is_string($keyword) ? $keyword : json_encode($keyword);
- $this->md5s[] = 'search:' . $keyword;
-
- $searchHistory = new SearchHistory();
- $searchHistory->save(['keyword' => $keyword]);
- $this->query = $this->query->where('title|subtitle', 'like', '%' . $keyword . '%');
- return $this;
- }
-
- public function whereIds($ids = '')
- {
- $ids = is_array($ids) ? join(',', $ids) : $ids;
- $this->md5s[] = 'ids:' . $ids;
- if ($ids) {
- $this->query = $this->query->orderRaw('field(id, ' . $ids . ')');
- $this->query = $this->query->whereIn('id', $ids);
- }
- return $this;
- }
-
- public function category($category_id, $is_category_deep = true)
- {
- $this->md5s[] = 'category_id:' . $category_id;
- $category_ids = [];
- if (isset($category_id) && $category_id != 0) {
- if ($is_category_deep) {
-
- $category_ids = (new Tree(function () {
-
- return (new Category)->normal();
- }))->getChildIds($category_id);
- } else {
- $category_ids = [$category_id];
- }
- }
- $this->query->where(function ($query) use ($category_ids) {
-
- foreach ($category_ids as $key => $category_id) {
- $query->whereOrRaw("find_in_set($category_id, category_ids)");
- }
- });
- return $this;
- }
-
- public function order($sort = 'weigh', $order = 'desc')
- {
- $sort = $sort == 'price' ? Db::raw('convert(`price`, DECIMAL(10, 2)) ' . $order) : $sort;
- $this->query->order($sort, $order);
- return $this;
- }
-
- public function select($is_cache = false)
- {
- $this->md5s[] = 'select';
-
- $this->order();
- $goods = $this->query->field('*,(sales + show_sales) as total_sales')
- ->select();
-
- foreach ($goods as $key => $gd) {
- $gd = $this->defaultFormat($gd);
- if ($this->format instanceof \Closure) {
- $gd = ($this->format)($gd, $this);
- }
- $goods[$key] = $gd;
- }
- return $goods;
- }
- public function select_autopage($is_cache = false)
- {
- $this->md5s[] = 'select';
-
- $this->order();
- $goods = $this->query->field('id,image,title,price,price as pricemin,is_sku,status,type,dispatch_type')
- ->autopage()
- ->select();
-
-
- return $goods;
- }
-
- public function paginate($is_cache = false)
- {
- $this->md5s[] = 'paginate';
-
- $this->order();
- $goods = $this->query->field('*,(sales + show_sales) as total_sales')
- ->paginate(request()->param('list_rows', 10));
-
- $goods->each(function($god) {
- $god = $this->defaultFormat($god);
- if ($this->format instanceof \Closure) {
- ($this->format)($god, $this);
- }
- });
- return $goods;
- }
-
- public function find($is_cache = false)
- {
- $this->md5s[] = 'find';
- $goods = $this->query
- ->find();
- if ($goods && $this->format instanceof \Closure) {
-
- $goods = $this->defaultFormat($goods);
- ($this->format)($goods, $this);
- }
- return $goods;
- }
-
- public function findOrFail($is_cache = false)
- {
- $this->md5s[] = 'find';
- $goods = $this->query->cache($this->getCacheKey($is_cache), (200 + mt_rand(0, 100)))->find();
- if (!$goods) {
- error_stop('商品不存在');
- }
- if ($this->format instanceof \Closure) {
-
- $goods = $this->defaultFormat($goods);
- ($this->format)($goods, $this);
- }
- return $goods;
- }
-
- public function defaultFormat($goods)
- {
- $skuPrices = $goods->sku_prices;
- $activity = $this->is_activity ? $goods->activity : null;
- if ($activity) {
- $skuPrices = ActivityFacade::recoverSkuPrices($goods, $activity);
-
- }else{
- foreach ($skuPrices as $key => $skuPrice) {
- $skuPrice->old_price = $skuPrice['price'];
- }
- unset($skuPrice);
- }
- if ($this->show_score_shop) {
- $scoreSkuPrices = $goods->all_score_sku_prices;
-
- foreach ($skuPrices as $key => &$skuPrice) {
- $stock = $skuPrice->stock;
- $skuPrice->stock = 0;
- $skuPrice->sales = 0;
- foreach ($scoreSkuPrices as $scoreSkuPrice) {
- if ($skuPrice->id == $scoreSkuPrice->goods_sku_price_id) {
- $skuPrice->stock = ($scoreSkuPrice->stock > $stock) ? $stock : $scoreSkuPrice->stock;
- $skuPrice->sales = $scoreSkuPrice->sales;
- $skuPrice->price = $scoreSkuPrice->price;
- $skuPrice->score = $scoreSkuPrice->score;
- $skuPrice->status = $scoreSkuPrice->status;
-
-
- $skuPrice->item_goods_sku_price = $scoreSkuPrice;
- break;
- }
- }
- }
- }
-
- foreach ($skuPrices as $key => $skuPrice) {
- if ($skuPrice['status'] != 'up') {
- unset($skuPrices[$key]);
- }
- }
- $skuPrices = $skuPrices instanceof \think\Collection ? $skuPrices->values() : array_values($skuPrices);
- if ($activity) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- } elseif ($this->show_score_shop) {
-
-
- } else {
-
-
- }
- if ($this->show_score_shop) {
-
- $goods['show_score_shop'] = $this->show_score_shop;
- }
-
- $goods['new_sku_prices'] = $skuPrices;
-
- $stocks = $skuPrices instanceof \think\Collection ? $skuPrices->column('stock') : array_column($skuPrices, 'stock');
- $stock = array_sum($stocks);
- $goods['stock'] = $stock;
-
- $goods['activity_type'] = $activity['type'] ?? null;
- return $goods;
- }
-
- protected function getCacheKey($is_cache = false)
- {
- if ($is_cache) {
- sort($this->md5s);
- $key = 'goods-service-' . md5(json_encode($this->md5s));
- }
- return $key ?? false;
- }
-
- public function __call($funcname, $arguments)
- {
- $this->query->{$funcname}(...$arguments);
- return $this;
- }
- }
|