Coupon.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace app\admin\controller\shopro;
  3. use think\Db;
  4. use app\admin\model\shopro\Coupon as CouponModel;
  5. use app\admin\model\shopro\user\Coupon as UserCouponModel;
  6. use addons\shopro\traits\CouponSend;
  7. use app\admin\model\shopro\user\User;
  8. /**
  9. * 优惠券
  10. */
  11. class Coupon extends Common
  12. {
  13. use CouponSend;
  14. protected $noNeedRight = ['select'];
  15. public function _initialize()
  16. {
  17. parent::_initialize();
  18. $this->model = new CouponModel;
  19. }
  20. /**
  21. * 优惠券列表
  22. *
  23. * @return \think\Response
  24. */
  25. public function index()
  26. {
  27. if (!$this->request->isAjax()) {
  28. return $this->view->fetch();
  29. }
  30. $coupons = $this->model->sheepFilter()->paginate($this->request->param('list_rows', 10))->each(function ($coupon) {
  31. // 优惠券领取和使用数量
  32. $coupon->get_num = $coupon->get_num;
  33. $coupon->use_num = $coupon->use_num;
  34. });
  35. $result = [
  36. 'coupons' => $coupons,
  37. ];
  38. $result['total_num'] = UserCouponModel::count();
  39. $result['expire_num'] = UserCouponModel::expired()->count();
  40. $result['use_num'] = UserCouponModel::used()->count();
  41. $result['use_percent'] = 0 . '%';
  42. if ($result['total_num']) {
  43. $result['use_percent'] = bcdiv(bcmul((string)$result['use_num'], '100'), (string)$result['total_num'], 1) . '%';
  44. }
  45. $this->success('获取成功', null, $result);
  46. }
  47. /**
  48. * 添加优惠券
  49. *
  50. * @return \think\Response
  51. */
  52. public function add()
  53. {
  54. if (!$this->request->isAjax()) {
  55. return $this->view->fetch();
  56. }
  57. $params = $this->request->only([
  58. 'name', 'type', 'use_scope', 'items', 'amount', 'max_amount', 'enough',
  59. 'stock', 'limit_num', 'get_time',
  60. 'use_time_type', 'use_time', 'start_days', 'days',
  61. 'is_double_discount', 'description', 'status'
  62. ]);
  63. $this->svalidate($params, ".add");
  64. // 时间转换
  65. $getTime = explode(' - ', $params['get_time']);
  66. $useTime = explode(' - ', $params['use_time']);
  67. unset($params['get_time'], $params['use_time']);
  68. $params['get_start_time'] = $getTime[0] ?? 0;
  69. $params['get_end_time'] = $getTime[1] ?? 0;
  70. $params['use_start_time'] = $useTime[0] ?? 0;
  71. $params['use_end_time'] = $useTime[1] ?? 0;
  72. $this->model->save($params);
  73. $this->success('保存成功');
  74. }
  75. /**
  76. * 优惠券详情
  77. *
  78. * @param $id
  79. * @return \think\Response
  80. */
  81. public function detail($id)
  82. {
  83. $coupon = $this->model->where('id', $id)->find();
  84. if (!$coupon) {
  85. $this->error(__('No Results were found'));
  86. }
  87. $coupon->items_value = $coupon->items_value; // 可用范围值
  88. $this->success('获取成功', null, $coupon);
  89. }
  90. /**
  91. * 修改优惠券
  92. *
  93. * @return \think\Response
  94. */
  95. public function edit($id = null)
  96. {
  97. if (!$this->request->isAjax()) {
  98. return $this->view->fetch('add');
  99. }
  100. $params = $this->request->only([
  101. 'name', 'use_scope', 'items', 'amount', 'max_amount', 'enough',
  102. 'stock', 'limit_num', 'get_time',
  103. 'use_time_type', 'use_time', 'start_days', 'days',
  104. 'is_double_discount', 'description', 'status'
  105. ]);
  106. $this->svalidate($params);
  107. // 时间转换
  108. if (isset($params['get_time'])) {
  109. $getTime = explode(' - ', $params['get_time']);
  110. $params['get_start_time'] = $getTime[0] ?? 0;
  111. $params['get_end_time'] = $getTime[1] ?? 0;
  112. unset($params['get_time']);
  113. }
  114. if (isset($params['use_time'])) {
  115. $useTime = explode(' - ', $params['use_time']);
  116. $params['use_start_time'] = $useTime[0] ?? 0;
  117. $params['use_end_time'] = $useTime[1] ?? 0;
  118. unset($params['use_time']);
  119. }
  120. $coupon = $this->model->where('id', $id)->find();
  121. if (!$coupon) {
  122. $this->error(__('No Results were found'));
  123. }
  124. $coupon->save($params);
  125. $this->success('更新成功');
  126. }
  127. /**
  128. * 删除优惠券
  129. *
  130. * @param string $id 要删除的优惠券
  131. * @return void
  132. */
  133. public function delete($id)
  134. {
  135. if (empty($id)) {
  136. $this->error(__('Parameter %s can not be empty', 'id'));
  137. }
  138. $id = explode(',', $id);
  139. $list = $this->model->where('id', 'in', $id)->select();
  140. $result = Db::transaction(function () use ($list) {
  141. $count = 0;
  142. foreach ($list as $item) {
  143. $count += $item->delete();
  144. }
  145. return $count;
  146. });
  147. if ($result) {
  148. $this->success('删除成功', null, $result);
  149. } else {
  150. $this->error(__('No rows were deleted'));
  151. }
  152. }
  153. /**
  154. * 优惠券列表
  155. */
  156. public function select()
  157. {
  158. if (!$this->request->isAjax()) {
  159. return $this->view->fetch();
  160. }
  161. $type = $this->request->param('type', 'page');
  162. $coupons = $this->model->sheepFilter();
  163. if ($type == 'select') {
  164. // 普通结果
  165. $coupons = $coupons->select();
  166. } elseif ($type == 'find') {
  167. $coupons = $coupons->find();
  168. } else {
  169. // 分页结果
  170. $coupons = $coupons->paginate($this->request->param('list_rows', 10));
  171. }
  172. $this->success('获取成功', null, $coupons);
  173. }
  174. public function send($id)
  175. {
  176. $user_ids = $this->request->post('user_ids/a');
  177. $users = User::whereIn('id', $user_ids)->select();
  178. Db::transaction(function () use ($users, $id) {
  179. $this->manualSend($users, $id);
  180. });
  181. $this->success('发放成功');
  182. }
  183. public function recyclebin()
  184. {
  185. if (!$this->request->isAjax()) {
  186. return $this->view->fetch();
  187. }
  188. $coupons = $this->model->onlyTrashed()->sheepFilter()->paginate($this->request->param('list_rows', 10));
  189. $this->success('获取成功', null, $coupons);
  190. }
  191. /**
  192. * 还原(支持批量)
  193. *
  194. * @param $id
  195. * @return \think\Response
  196. */
  197. public function restore($id = null)
  198. {
  199. if (empty($id)) {
  200. $this->error(__('Parameter %s can not be empty', 'id'));
  201. }
  202. $items = $this->model->onlyTrashed()->where('id', 'in', $id)->select();
  203. $result = Db::transaction(function () use ($items) {
  204. $count = 0;
  205. foreach ($items as $item) {
  206. $count += $item->restore();
  207. }
  208. return $count;
  209. });
  210. if ($result) {
  211. $this->success('还原成功', null, $result);
  212. } else {
  213. $this->error(__('No rows were updated'));
  214. }
  215. }
  216. /**
  217. * 销毁(支持批量)
  218. *
  219. * @param $id
  220. * @return \think\Response
  221. */
  222. public function destroy($id = null)
  223. {
  224. if (empty($id)) {
  225. $this->error(__('Parameter %s can not be empty', 'id'));
  226. }
  227. if ($id !== 'all') {
  228. $items = $this->model->onlyTrashed()->whereIn('id', $id)->select();
  229. } else {
  230. $items = $this->model->onlyTrashed()->select();
  231. }
  232. $result = Db::transaction(function () use ($items) {
  233. $count = 0;
  234. foreach ($items as $item) {
  235. // 删除商品
  236. $count += $item->delete(true);
  237. }
  238. return $count;
  239. });
  240. if ($result) {
  241. $this->success('销毁成功', null, $result);
  242. }
  243. $this->error('销毁失败');
  244. }
  245. }