StoreCoupon.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\admin\v1\marketing\coupon;
  12. use app\controller\admin\AuthController;
  13. use app\services\activity\coupon\StoreCouponProductServices;
  14. use app\services\activity\coupon\StoreCouponService;
  15. use think\facade\App;
  16. /**
  17. * 优惠券制作
  18. * Class StoreCoupon
  19. * @package app\controller\admin\v1\marketing
  20. */
  21. class StoreCoupon extends AuthController
  22. {
  23. /**
  24. * StoreCoupon constructor.
  25. * @param App $app
  26. * @param StoreCouponService $service
  27. */
  28. public function __construct(App $app, StoreCouponService $service)
  29. {
  30. parent::__construct($app);
  31. $this->services = $service;
  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 index()
  41. {
  42. $where = $this->request->getMore([
  43. ['status', 1],
  44. ['title', ''],
  45. ]);
  46. $data = $this->services->getList($where);
  47. return $this->success($data);
  48. }
  49. /**
  50. * 创建添加模板表单
  51. * @return mixed
  52. * @throws \FormBuilder\Exception\FormBuilderException
  53. */
  54. public function create()
  55. {
  56. [$type] = $this->request->getMore(['type'], true);
  57. return $this->success($this->services->createForm($type));
  58. }
  59. /**
  60. * 保存优惠券模板
  61. * @return mixed
  62. */
  63. public function save(StoreCouponProductServices $services)
  64. {
  65. $data = $this->request->postMore([
  66. ['title', ''],
  67. ['image', ''],
  68. ['category_id', ''],
  69. ['coupon_price', ''],
  70. ['use_min_price', ''],
  71. ['coupon_time', ''],
  72. ['sort', ''],
  73. ['status', 0],
  74. ['type', 0]
  75. ]);
  76. $data['product_id'] = '';
  77. if ($data['type'] == 1) {
  78. $this->validate($data, \app\validate\admin\marketing\StoreCouponValidate::class, 'type');
  79. } elseif ($data['type'] == 2) {
  80. $this->validate($data, \app\validate\admin\marketing\StoreCouponValidate::class, 'product');
  81. $productIds = array_column($data['image'], 'product_id');
  82. $data['product_id'] = implode(',', $productIds);
  83. } else {
  84. $this->validate($data, \app\validate\admin\marketing\StoreCouponValidate::class, 'save');
  85. }
  86. $data['add_time'] = time();
  87. $this->services->save($data);
  88. return $this->success('添加优惠券成功!');
  89. }
  90. /**
  91. * 删除优惠券模板
  92. * @param $id
  93. * @return mixed
  94. */
  95. public function delete($id)
  96. {
  97. $data['is_del'] = 1;
  98. $this->services->update($id, $data);
  99. return $this->success('删除成功!');
  100. }
  101. /**
  102. * 立即失效优惠券模板
  103. * @param $id
  104. * @return mixed
  105. */
  106. public function status($id)
  107. {
  108. $this->services->invalid($id);
  109. return $this->success('修改成功!');
  110. }
  111. /**
  112. * 发布优惠券表单
  113. * @param $id
  114. * @return mixed
  115. * @throws \FormBuilder\Exception\FormBuilderException
  116. */
  117. public function issue($id)
  118. {
  119. return $this->success($this->services->createIssue($id));
  120. }
  121. /**
  122. * 保存发布优惠券信息
  123. * @param $id
  124. * @return mixed
  125. */
  126. public function update_issue($id)
  127. {
  128. [$_id, $coupon_title, $rangeTime, $count, $status, $is_permanent, $full_reduction, $is_give_subscribe, $is_full_give, $is_type] = $this->request->postMore([
  129. 'id',
  130. ['coupon_title', ''],
  131. ['range_date', ['', '']],
  132. ['count', 0],
  133. ['status', 0],
  134. ['is_permanent', 0],
  135. ['full_reduction', 0],
  136. ['is_give_subscribe', 0],
  137. ['is_full_give', 0],
  138. ['is_type', 0]
  139. ], true);
  140. $this->services->upIssue($id, $_id, $coupon_title, $rangeTime, $count, $status, $is_permanent, $full_reduction, $is_give_subscribe, $is_full_give, $is_type);
  141. return $this->success('发布优惠劵成功!');
  142. }
  143. }