ShopGoodLists.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. record_sql();
  26. $lists = ShopGoods::with(['goodSpecsInventory'=>function(Query $query){
  27. $query->field(['id','shop_goods_id','remaining_inventory','service_fee','service_total']);
  28. }])
  29. ->where($this->searchWhere)
  30. ->field(['id', 'delivery_type' ,'shop_goods_type', 'goods_name','goods_category_id','company_name','goods_image','goods_banners'])
  31. ->when(isset($this->params['is_recommend']) && $this->params['is_recommend'],function (Query $query){
  32. $query->order('is_recommend','desc');
  33. })
  34. ->where('goods_status',YesNoEnum::YES)
  35. ->append(['delivery_type_text','shop_goods_type_text'])
  36. ->order('id','desc')
  37. ->select()
  38. ->each(function($item){
  39. $item['remaining_inventory'] =(int) $item->goodSpecsInventory()->sum('remaining_inventory');
  40. $item['service_fee'] = $item->goodSpecsInventory()->min('service_fee');
  41. $serviceItem = $item->goodSpecsInventory()->where('service_fee',$item['service_fee'])->find()->toArray();
  42. $item['service_total'] = floatval($serviceItem['service_total'] ?? 0);
  43. unset($item['goodSpecsInventory']);
  44. })
  45. ->toArray();
  46. return $lists;
  47. }
  48. public function count(): int
  49. {
  50. return ShopGoods::where($this->searchWhere)->where('goods_status',YesNoEnum::YES)->count();
  51. }
  52. }