ShopGoodLists.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace app\workerapi\lists\shops;
  3. use app\common\enum\YesNoEnum;
  4. use app\common\lists\ListsSearchInterface;
  5. use app\common\model\shops\ShopGoods;
  6. use app\workerapi\lists\BaseWorkerDataLists;
  7. use think\db\Query;
  8. class ShopGoodLists extends BaseWorkerDataLists implements ListsSearchInterface
  9. {
  10. /**
  11. * @notes 设置搜索条件
  12. * @return \string[][]
  13. * @author likeadmin
  14. * @date 2024/07/07 18:23
  15. */
  16. public function setSearch(): array
  17. {
  18. return [
  19. '=' => ['goods_category_id', 'goods_status','is_recommend'],
  20. '%like%' => ['goods_name'],
  21. ];
  22. }
  23. public function lists(): array
  24. {
  25. $lists = ShopGoods::with(['goodSpecsInventory'=>function(Query $query){
  26. $query->field(['id','shop_goods_id','remaining_inventory','service_fee','service_total']);
  27. }])
  28. ->where($this->searchWhere)
  29. ->field(['id', 'delivery_type' ,'shop_goods_type', 'goods_name','goods_category_id','company_name','goods_image','goods_banners'])
  30. ->when(isset($this->params['is_recommend']) && $this->params['is_recommend'],function (Query $query){
  31. $query->order('is_recommend','desc');
  32. })
  33. ->where('goods_status',YesNoEnum::YES)
  34. ->append(['delivery_type_text','shop_goods_type_text'])
  35. ->order('id','desc')
  36. ->select()
  37. ->each(function($item){
  38. $item['remaining_inventory'] =(int) $item->goodSpecsInventory()->sum('remaining_inventory');
  39. $item['service_fee'] = $item->goodSpecsInventory()->min('service_fee');
  40. unset($item['goodSpecsInventory']);
  41. })
  42. ->toArray();
  43. return $lists;
  44. }
  45. public function count(): int
  46. {
  47. return ShopGoods::where($this->searchWhere)->where('goods_status',YesNoEnum::YES)->count();
  48. }
  49. }