Coupon.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\merchant\store\coupon;
  12. use app\common\repositories\store\coupon\StoreCouponSendRepository;
  13. use app\validate\merchant\StoreCouponSendValidate;
  14. use crmeb\basic\BaseController;
  15. use app\common\repositories\store\coupon\StoreCouponRepository;
  16. use app\common\repositories\store\coupon\StoreCouponUserRepository;
  17. use app\common\repositories\user\UserRepository;
  18. use app\validate\merchant\StoreCouponValidate;
  19. use FormBuilder\Exception\FormBuilderException;
  20. use think\App;
  21. use think\db\exception\DataNotFoundException;
  22. use think\db\exception\DbException;
  23. use think\db\exception\ModelNotFoundException;
  24. use think\exception\ValidateException;
  25. /**
  26. * Class CouponIssue
  27. * @package app\controller\merchant\store\coupon
  28. * @author xaboy
  29. * @day 2020-05-13
  30. */
  31. class Coupon extends BaseController
  32. {
  33. /**
  34. * @var StoreCouponRepository
  35. */
  36. protected $repository;
  37. /**
  38. * CouponIssue constructor.
  39. * @param App $app
  40. * @param StoreCouponRepository $repository
  41. */
  42. public function __construct(App $app, StoreCouponRepository $repository)
  43. {
  44. parent::__construct($app);
  45. $this->repository = $repository;
  46. }
  47. /**
  48. * @return mixed
  49. * @throws DbException
  50. * @throws DataNotFoundException
  51. * @throws ModelNotFoundException
  52. * @author xaboy
  53. * @day 2020-05-14
  54. */
  55. public function lst()
  56. {
  57. $where = $this->request->params(['is_full_give', 'status', 'is_give_subscribe', 'coupon_name', 'send_type', 'type']);
  58. [$page, $limit] = $this->getPage();
  59. return app('json')->success($this->repository->getList($this->request->merId(), $where, $page, $limit));
  60. }
  61. public function detail($id)
  62. {
  63. if (!$this->repository->merExists($this->request->merId(), $id))
  64. return app('json')->fail('数据不存在');
  65. $coupon = $this->repository->get($id)->append(['used_num', 'send_num']);
  66. return app('json')->success($coupon->toArray());
  67. }
  68. /**
  69. * @return mixed
  70. * @throws FormBuilderException
  71. * @author xaboy
  72. * @day 2020-05-13
  73. */
  74. public function createForm()
  75. {
  76. return app('json')->success(formToData($this->repository->form()));
  77. }
  78. /**
  79. * @param StoreCouponValidate $validate
  80. * @return mixed
  81. * @author xaboy
  82. * @day 2020/5/30
  83. */
  84. public function create(StoreCouponValidate $validate)
  85. {
  86. $merId = $this->request->merId();
  87. $data = $this->checkParams($validate);
  88. $data['mer_id'] = $merId;
  89. $this->repository->create($data);
  90. return app('json')->success('发布成功');
  91. }
  92. /**
  93. * @param StoreCouponValidate $validate
  94. * @return array
  95. * @author xaboy
  96. * @day 2020/5/20
  97. */
  98. public function checkParams(StoreCouponValidate $validate)
  99. {
  100. $data = $this->request->params(['use_type', 'title', 'coupon_price', 'use_min_price', 'coupon_type', 'coupon_time', ['use_start_time', []], 'sort', ['status', 0], 'type', ['product_id', []], ['range_date', ''], ['send_type', 0], ['full_reduction', 0], ['is_limited', 0], ['is_timeout', 0], ['total_count', ''], ['status', 0]]);
  101. $validate->check($data);
  102. if ($data['is_timeout']) {
  103. [$data['start_time'], $data['end_time']] = $data['range_date'];
  104. if (strtotime($data['end_time']) <= time())
  105. throw new ValidateException('优惠券领取结束时间不能小于当前');
  106. }
  107. if (!$data['use_type']) $data['use_min_price'] = 0;
  108. unset($data['use_type']);
  109. if ($data['coupon_type']) {
  110. if (count(array_filter($data['use_start_time'])) != 2)
  111. throw new ValidateException('请选择有效期限');
  112. [$data['use_start_time'], $data['use_end_time']] = $data['use_start_time'];
  113. } else unset($data['use_start_time']);
  114. unset($data['range_date']);
  115. if ($data['is_limited'] == 0) $data['total_count'] = 0;
  116. if (!in_array($data['type'], [0, 1])) {
  117. throw new ValidateException('请选择有效的优惠券类型');
  118. }
  119. return $data;
  120. }
  121. /**
  122. * @param $id
  123. * @return mixed
  124. * @throws DbException
  125. * @author xaboy
  126. * @day 2020-05-13
  127. */
  128. public function changeStatus($id)
  129. {
  130. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  131. if (!$this->repository->merExists($this->request->merId(), $id))
  132. return app('json')->fail('数据不存在');
  133. $this->repository->update($id, compact('status'));
  134. return app('json')->success('修改成功');
  135. }
  136. /**
  137. * @param $id
  138. * @return mixed
  139. * @throws DataNotFoundException
  140. * @throws DbException
  141. * @throws FormBuilderException
  142. * @throws ModelNotFoundException
  143. * @author xaboy
  144. * @day 2020/5/26
  145. */
  146. public function cloneForm($id)
  147. {
  148. if (!$this->repository->merExists($this->request->merId(), $id))
  149. return app('json')->fail('数据不存在');
  150. return app('json')->success(formToData($this->repository->cloneCouponForm($id)));
  151. }
  152. /**
  153. * @param StoreCouponUserRepository $repository
  154. * @author xaboy
  155. * @day 2020/6/2
  156. */
  157. public function issue(StoreCouponUserRepository $repository)
  158. {
  159. [$page, $limit] = $this->getPage();
  160. $where = $this->request->params(['username', 'coupon', 'status', 'coupon_id', 'type', 'send_id']);
  161. $where['mer_id'] = $this->request->merId();
  162. return app('json')->success($repository->getList($where, $page, $limit));
  163. }
  164. /**
  165. * @return mixed
  166. * @author Qinii
  167. */
  168. public function select()
  169. {
  170. $where = $this->request->params(['coupon_name']);
  171. $where['status'] = 1;
  172. $where['send_type'] = 3;
  173. [$page, $limit] = $this->getPage();
  174. return app('json')->success($this->repository->getList($this->request->merId(), $where, $page, $limit));
  175. }
  176. public function send(StoreCouponSendValidate $validate, StoreCouponSendRepository $repository)
  177. {
  178. $data = $this->request->params(['coupon_id', 'mark', 'is_all', 'search', 'uid']);
  179. $validate->check($data);
  180. if (!$data['is_all'] && !count($data['uid'])) {
  181. return app('json')->fail('请选择发送用户');
  182. }
  183. $repository->create($data, $this->request->merId());
  184. return app('json')->success('创建成功,正在发送中');
  185. }
  186. /**
  187. * @param $id
  188. * @return mixed
  189. * @throws DbException
  190. * @author xaboy
  191. * @day 2020/7/7
  192. */
  193. public function delete($id)
  194. {
  195. if (!$this->repository->merExists($this->request->merId(), $id))
  196. return app('json')->fail('数据不存在');
  197. $this->repository->delete($id);
  198. return app('json')->success('删除成功');
  199. }
  200. public function updateForm($id)
  201. {
  202. return app('json')->success(formToData($this->repository->updateForm($this->request->merId(), $id)));
  203. }
  204. public function update($id)
  205. {
  206. if (!$this->repository->merExists($this->request->merId(), $id))
  207. return app('json')->fail('数据不存在');
  208. $data = $this->request->params(['title']);
  209. $this->repository->update($id, $data);
  210. return app('json')->success('修改成功');
  211. }
  212. }