StoreCouponIssue.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. ]);
  44. $list = $this->services->getCouponIssueList($where);
  45. return app('json')->success($list);
  46. }
  47. /**
  48. * 添加优惠券
  49. * @return mixed
  50. */
  51. public function saveCoupon()
  52. {
  53. $data = $this->request->postMore([
  54. ['coupon_title', ''],
  55. ['coupon_price', 0.00],
  56. ['use_min_price', 0.00],
  57. ['coupon_time', 0],
  58. ['start_use_time', 0],
  59. ['end_use_time', 0],
  60. ['start_time', 0],
  61. ['end_time', 0],
  62. ['receive_type', 0],
  63. ['is_permanent', 0],
  64. ['total_count', 0],
  65. ['product_id', ''],
  66. ['category_id', []],
  67. ['type', 0],
  68. ['sort', 0],
  69. ['status', 0],
  70. ['receive_limit', 1],
  71. ]);
  72. $res = $this->services->saveCoupon($data);
  73. if ($res) return app('json')->success(100000);
  74. }
  75. /**
  76. * 修改优惠券状态
  77. * @param $id
  78. * @param $status
  79. * @return mixed
  80. */
  81. public function status($id, $status)
  82. {
  83. $this->services->update($id, ['status' => $status]);
  84. return app('json')->success(100001);
  85. }
  86. /**
  87. * 复制优惠券获取优惠券详情
  88. * @param int $id
  89. * @return mixed
  90. */
  91. public function copy($id = 0)
  92. {
  93. if (!$id) return app('json')->fail(100100);
  94. $info = $this->services->get($id);
  95. if ($info) $info = $info->toArray();
  96. if ($info['product_id'] != '') {
  97. $productIds = explode(',', $info['product_id']);
  98. /** @var StoreProductServices $product */
  99. $product = app()->make(StoreProductServices::class);
  100. $productImages = $product->getColumn([['id', 'in', $productIds]], 'image', 'id');
  101. foreach ($productIds as $item) {
  102. $info['productInfo'][] = [
  103. 'product_id' => $item,
  104. 'image' => $productImages[$item]
  105. ];
  106. }
  107. }
  108. if ($info['category_id'] != '') {
  109. $info['category_id'] = explode(',', $info['category_id']);
  110. foreach ($info['category_id'] as &$category_id) {
  111. $category_id = (int)$category_id;
  112. }
  113. }
  114. return app('json')->success($info);
  115. }
  116. /**
  117. * 删除
  118. * @param string $id
  119. * @return mixed
  120. */
  121. public function delete($id)
  122. {
  123. $this->services->update($id, ['is_del' => 1]);
  124. /** @var StoreProductCouponServices $storeProductService */
  125. $storeProductService = app()->make(StoreProductCouponServices::class);
  126. //删除商品关联这个优惠券
  127. $storeProductService->delete(['issue_coupon_id' => $id]);
  128. return app('json')->success(100002);
  129. }
  130. /**
  131. * 修改状态
  132. * @param $id
  133. * @return mixed
  134. * @throws \FormBuilder\Exception\FormBuilderException
  135. */
  136. public function edit($id)
  137. {
  138. return app('json')->success($this->services->createForm($id));
  139. }
  140. /**
  141. * 领取记录
  142. * @param string $id
  143. * @return mixed|string
  144. */
  145. public function issue_log($id)
  146. {
  147. $list = $this->services->issueLog($id);
  148. return app('json')->success($list);
  149. }
  150. }