Discounts.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\store\product;
  12. use app\common\repositories\store\product\StoreDiscountProductRepository;
  13. use app\common\repositories\store\product\StoreDiscountRepository;
  14. use crmeb\basic\BaseController;
  15. use think\App;
  16. class Discounts extends BaseController
  17. {
  18. protected $repository ;
  19. /**
  20. * Product constructor.
  21. * @param App $app
  22. * @param StoreDiscountRepository $repository
  23. */
  24. public function __construct(App $app ,StoreDiscountRepository $repository)
  25. {
  26. parent::__construct($app);
  27. $this->repository = $repository;
  28. }
  29. /**
  30. * 获取指定产品ID的促销列表
  31. *
  32. * 此方法用于根据请求中的产品ID,查询具有特定促销状态的促销活动列表。
  33. * 它首先尝试从请求中获取产品ID,然后根据该ID查询相关的促销活动ID列表。
  34. * 如果存在相关促销,它将这些促销ID作为查询条件之一,以获取最终的促销活动列表。
  35. *
  36. * @return json 返回查询到的促销活动列表数据
  37. */
  38. public function lst()
  39. {
  40. // 从请求中获取产品ID,默认为0
  41. $id = $this->request->param('product_id',0);
  42. $limit = $this->request->param('limit',5);
  43. // 定义查询条件,包括状态、展示、结束时间和未删除的促销
  44. $where = [
  45. 'status' => 1,
  46. 'is_show'=> 1,
  47. 'end_time' => 1,
  48. 'is_del' => 0,
  49. ];
  50. // 如果提供了产品ID
  51. if ($id){
  52. // 查询与产品ID相关的促销ID列表
  53. $discount_id = app()->make(StoreDiscountProductRepository::class)
  54. ->getSearch(['product_id' => $id])
  55. ->column('discount_id');
  56. // 将促销ID列表作为查询条件之一
  57. $where['discount_id'] = $discount_id;
  58. }
  59. // 根据所有的查询条件获取促销活动列表
  60. $data = $this->repository->getApilist($where,$limit);
  61. // 返回查询结果
  62. return app('json')->success($data);
  63. }
  64. }