GoodsService.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace app\common\service;
  3. use app\common\constant\CommonConstant;
  4. use app\common\model\Goods;
  5. /**
  6. * 商品服务类
  7. */
  8. class GoodsService
  9. {
  10. /**
  11. * 商品列表
  12. *
  13. * @param integer $category_id 二级分类ID
  14. * @param string $search 搜索 商品名称
  15. * @return array
  16. **/
  17. public static function get_list($category_id, $search)
  18. {
  19. if (!$category_id && empty($search)) {
  20. return [];
  21. }
  22. $list = Goods::field('status,is_deleted,create_at', true)
  23. // ->where('status', CommonConstant::IS_WHO_1)
  24. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  25. ->when($category_id > 0, function ($query) use ($category_id) {
  26. $query->where('goods_category_id', $category_id);
  27. })
  28. ->when(!empty($search), function ($query) use ($search) {
  29. $query->where('goods_name', 'LIKE', '%' . $search . '%');
  30. })
  31. ->with([
  32. 'goodsStock'
  33. ])
  34. ->order('sort desc,id desc')
  35. ->select();
  36. return $list;
  37. }
  38. }