Coupon.php 7.2 KB

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