Coupon.php 6.3 KB

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