StoreCouponIssue.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\adminapi\controller\v1\marketing;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\activity\coupon\StoreCouponIssueServices;
  14. use app\services\product\product\StoreProductCouponServices;
  15. use app\services\product\product\StoreProductServices;
  16. use think\facade\App;
  17. /**
  18. * 已发布优惠券管理
  19. * Class StoreCouponIssue
  20. * @package app\adminapi\controller\v1\marketing
  21. */
  22. class StoreCouponIssue extends AuthController
  23. {
  24. public function __construct(App $app, StoreCouponIssueServices $services)
  25. {
  26. parent::__construct($app);
  27. $this->services = $services;
  28. }
  29. /**
  30. * 获取优惠券列表
  31. * @return mixed
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public function index()
  37. {
  38. $where = $this->request->getMore([
  39. ['status', 1],
  40. ['coupon_title', ''],
  41. ['receive_type', ''],
  42. ['type', ''],
  43. ['coupon_type', ''],
  44. ]);
  45. $list = $this->services->getCouponIssueList($where);
  46. return app('json')->success($list);
  47. }
  48. /**
  49. * 添加优惠券
  50. * @return mixed
  51. */
  52. public function saveCoupon()
  53. {
  54. $data = $this->request->postMore([
  55. ['id', 0],
  56. ['coupon_title', ''],
  57. ['coupon_price', 0.00],
  58. ['use_min_price', 0.00],
  59. ['coupon_time', 0],
  60. ['start_use_time', 0],
  61. ['end_use_time', 0],
  62. ['start_time', 0],
  63. ['end_time', 0],
  64. ['receive_type', 0],
  65. ['is_permanent', 0],
  66. ['total_count', 0],
  67. ['product_id', ''],
  68. ['category_id', []],
  69. ['type', 0],
  70. ['sort', 0],
  71. ['status', 0],
  72. ['receive_limit', 1],
  73. ]);
  74. $res = $this->services->saveCoupon($data);
  75. if ($res) return app('json')->success(100000);
  76. }
  77. /**
  78. * 修改优惠券状态
  79. * @param $id
  80. * @param $status
  81. * @return mixed
  82. */
  83. public function status($id, $status)
  84. {
  85. $this->services->update($id, ['status' => $status]);
  86. return app('json')->success(100001);
  87. }
  88. /**
  89. * 复制优惠券获取优惠券详情
  90. * @param int $id
  91. * @return mixed
  92. */
  93. public function copy($id = 0)
  94. {
  95. if (!$id) return app('json')->fail(100100);
  96. $info = $this->services->get($id);
  97. if ($info) $info = $info->toArray();
  98. if ($info['product_id'] != '') {
  99. $productIds = explode(',', $info['product_id']);
  100. /** @var StoreProductServices $product */
  101. $product = app()->make(StoreProductServices::class);
  102. $productImages = $product->getColumn([['id', 'in', $productIds]], 'image', 'id');
  103. foreach ($productIds as $item) {
  104. $info['productInfo'][] = [
  105. 'product_id' => $item,
  106. 'image' => $productImages[$item]
  107. ];
  108. }
  109. }
  110. if ($info['category_id'] != '') {
  111. $info['category_id'] = explode(',', $info['category_id']);
  112. foreach ($info['category_id'] as &$category_id) {
  113. $category_id = (int)$category_id;
  114. }
  115. }
  116. return app('json')->success($info);
  117. }
  118. /**
  119. * 删除
  120. * @param string $id
  121. * @return mixed
  122. */
  123. public function delete($id)
  124. {
  125. $this->services->update($id, ['is_del' => 1]);
  126. /** @var StoreProductCouponServices $storeProductService */
  127. $storeProductService = app()->make(StoreProductCouponServices::class);
  128. //删除商品关联这个优惠券
  129. $storeProductService->delete(['issue_coupon_id' => $id]);
  130. return app('json')->success(100002);
  131. }
  132. /**
  133. * 修改状态
  134. * @param $id
  135. * @return mixed
  136. * @throws \FormBuilder\Exception\FormBuilderException
  137. */
  138. public function edit($id)
  139. {
  140. return app('json')->success($this->services->createForm($id));
  141. }
  142. /**
  143. * 领取记录
  144. * @param string $id
  145. * @return mixed|string
  146. */
  147. public function issue_log($id)
  148. {
  149. $list = $this->services->issueLog($id);
  150. return app('json')->success($list);
  151. }
  152. }