StoreCoupon.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\outapi\controller;
  12. use app\services\activity\coupon\StoreCouponIssueServices;
  13. use app\services\product\product\StoreProductCouponServices;
  14. use think\facade\App;
  15. /**
  16. * 优惠券控制器
  17. * Class StoreCoupon
  18. * @package app\outapi\controller
  19. */
  20. class StoreCoupon extends AuthController
  21. {
  22. /**
  23. * StoreCoupon constructor.
  24. * @param App $app
  25. * @param StoreCouponIssueServices $service
  26. * @method temp
  27. */
  28. public function __construct(App $app, StoreCouponIssueServices $services)
  29. {
  30. parent::__construct($app);
  31. $this->services = $services;
  32. }
  33. /**
  34. * 获取优惠券列表
  35. * @return mixed
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function lst()
  41. {
  42. $where = $this->request->getMore([
  43. ['status', 1],
  44. ['coupon_title', ''],
  45. ['receive_type', ''],
  46. ['type', ''],
  47. ]);
  48. $list = $this->services->getCouponList($where);
  49. return app('json')->success($list);
  50. }
  51. /**
  52. * 新增优惠券
  53. * @return void
  54. */
  55. public function save()
  56. {
  57. $data = $this->request->postMore([
  58. ['coupon_title', ''],
  59. ['coupon_price', 0.00],
  60. ['use_min_price', 0.00],
  61. ['coupon_time', 0],
  62. ['start_use_time', 0],
  63. ['end_use_time', 0],
  64. ['start_time', 0],
  65. ['end_time', 0],
  66. ['receive_type', 0],
  67. ['total_count', 0],
  68. ['status', 0],
  69. ]);
  70. $data['type'] = 0;
  71. $data['product_id'] = '';
  72. $data['category_id'] = 0;
  73. $data['is_permanent'] = 0;
  74. if ((int)$data['total_count'] == 0) {
  75. $data['is_permanent'] = 1;
  76. }
  77. $id = $this->services->saveCoupon($data);
  78. return app('json')->success(100000, ['id' => $id]);
  79. }
  80. /**
  81. * 修改优惠券状态
  82. * @param $id
  83. * @param $status
  84. * @return mixed
  85. */
  86. public function status($id, $status)
  87. {
  88. if ($id < 1 || !in_array((int)$status, [0, 1])) {
  89. return app('json')->fail(100100);
  90. }
  91. $this->services->update($id, ['status' => $status]);
  92. return app('json')->success(100001);
  93. }
  94. /**
  95. * 删除
  96. * @param string $id
  97. * @return mixed
  98. */
  99. public function delete($id)
  100. {
  101. if (!$id) return app('json')->fail(100100);
  102. $this->services->update($id, ['is_del' => 1]);
  103. /** @var StoreProductCouponServices $storeProductService */
  104. $storeProductService = app()->make(StoreProductCouponServices::class);
  105. //删除商品关联这个优惠券
  106. $storeProductService->delete(['issue_coupon_id' => $id]);
  107. return app('json')->success(100002);
  108. }
  109. }