StoreCouponIssue.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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', '', '', 'receive_types'],
  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. ['user_type', 1],
  74. ]);
  75. $res = $this->services->saveCoupon($data);
  76. if ($res) return app('json')->success(100000);
  77. }
  78. /**
  79. * 修改优惠券状态
  80. * @param $id
  81. * @param $status
  82. * @return mixed
  83. */
  84. public function status($id, $status)
  85. {
  86. $this->services->update($id, ['status' => $status]);
  87. return app('json')->success(100001);
  88. }
  89. /**
  90. * 复制优惠券获取优惠券详情
  91. * @param int $id
  92. * @return mixed
  93. */
  94. public function copy($id = 0)
  95. {
  96. if (!$id) return app('json')->fail(100100);
  97. $info = $this->services->get($id);
  98. if ($info) $info = $info->toArray();
  99. if ($info['receive_type'] == 1 || $info['receive_type'] == 3) {
  100. $info['user_type'] = 1;
  101. }
  102. if ($info['receive_type'] == 4) {
  103. $info['user_type'] = 2;
  104. $info['receive_type'] = 1;
  105. }
  106. if ($info['product_id'] != '') {
  107. $productIds = explode(',', $info['product_id']);
  108. /** @var StoreProductServices $product */
  109. $product = app()->make(StoreProductServices::class);
  110. $productImages = $product->getColumn([['id', 'in', $productIds]], 'image', 'id');
  111. foreach ($productIds as $item) {
  112. $info['productInfo'][] = [
  113. 'product_id' => $item,
  114. 'image' => $productImages[$item]
  115. ];
  116. }
  117. }
  118. if ($info['category_id'] != '') {
  119. $info['category_id'] = explode(',', $info['category_id']);
  120. foreach ($info['category_id'] as &$category_id) {
  121. $category_id = (int)$category_id;
  122. }
  123. }
  124. return app('json')->success($info);
  125. }
  126. /**
  127. * 删除
  128. * @param string $id
  129. * @return mixed
  130. */
  131. public function delete($id)
  132. {
  133. $this->services->update($id, ['is_del' => 1]);
  134. /** @var StoreProductCouponServices $storeProductService */
  135. $storeProductService = app()->make(StoreProductCouponServices::class);
  136. //删除商品关联这个优惠券
  137. $storeProductService->delete(['issue_coupon_id' => $id]);
  138. return app('json')->success(100002);
  139. }
  140. /**
  141. * 修改状态
  142. * @param $id
  143. * @return mixed
  144. * @throws \FormBuilder\Exception\FormBuilderException
  145. */
  146. public function edit($id)
  147. {
  148. return app('json')->success($this->services->createForm($id));
  149. }
  150. /**
  151. * 领取记录
  152. * @param string $id
  153. * @return mixed|string
  154. */
  155. public function issue_log($id)
  156. {
  157. $list = $this->services->issueLog($id);
  158. return app('json')->success($list);
  159. }
  160. }