Coupon.php 6.3 KB

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