Groupon.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\admin\controller\shopro\activity;
  4. use think\Db;
  5. use app\admin\controller\shopro\Common;
  6. use app\admin\model\shopro\activity\Activity as ActivityModel;
  7. use app\admin\model\shopro\activity\Groupon as GrouponModel;
  8. use app\admin\model\shopro\Admin;
  9. use addons\shopro\library\activity\traits\Groupon as GrouponTrait;
  10. /**
  11. * 团管理
  12. */
  13. class Groupon extends Common
  14. {
  15. use GrouponTrait;
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new GrouponModel;
  21. }
  22. public function index()
  23. {
  24. if (!$this->request->isAjax()) {
  25. return $this->view->fetch();
  26. }
  27. $groupons = $this->model->sheepFilter()->with(['goods', 'user', 'grouponLogs'])
  28. ->paginate(request()->param('list_rows', 10));
  29. $this->success('获取成功', null, $groupons);
  30. }
  31. /**
  32. * 团详情
  33. *
  34. * @param $id
  35. * @return \think\Response
  36. */
  37. public function detail($id)
  38. {
  39. if (!$this->request->isAjax()) {
  40. return $this->view->fetch();
  41. }
  42. $groupon = $this->model->with(['goods', 'user', 'grouponLogs'])->where('id', $id)->find();
  43. if (!$groupon) {
  44. $this->error(__('No Results were found'));
  45. }
  46. $this->success('获取成功', null, $groupon);
  47. }
  48. /**
  49. * 添加虚拟用户(人满自动成团)
  50. *
  51. * @param Request $request
  52. * @param integer $id
  53. */
  54. public function addUser($id)
  55. {
  56. $groupon = $this->model->where('id', $id)->find();
  57. if (!$groupon) {
  58. $this->error(__('No Results were found'));
  59. }
  60. $activity = ActivityModel::where('id', $groupon['activity_id'])->find();
  61. if (!$activity) {
  62. $this->error('活动不存在');
  63. }
  64. if ($groupon['status'] != 'ing' || $groupon['current_num'] > $groupon['num']) {
  65. $this->error('团已完成或已失效');
  66. }
  67. $avatar = $this->request->param('avatar', '');
  68. $nickname = $this->request->param('nickname', '');
  69. $user = ['avatar' => $avatar, 'nickname' => $nickname];
  70. Db::transaction(function () use ($activity, $groupon, $user) {
  71. // 增加人数
  72. $this->finishFictitiousGroupon($activity, $groupon, false, 1, [$user]);
  73. });
  74. $this->success('操作成功');
  75. }
  76. /**
  77. * 解散团,自动退款
  78. *
  79. * @param Request $request
  80. * @param integer $id
  81. */
  82. public function invalid($id)
  83. {
  84. $admin = $this->auth->getUserInfo();
  85. $admin = Admin::find($admin['id']);
  86. $groupon = $this->model->where('id', $id)->find();
  87. if ($groupon['status'] != 'ing') {
  88. $this->error('团已完成或已失效');
  89. }
  90. Db::transaction(function () use ($groupon, $admin) {
  91. // 解散团,并退款
  92. $this->invalidRefundGroupon($groupon, $admin);
  93. });
  94. $this->success('操作成功');
  95. }
  96. }