Voucher.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\admin\model\system\GrantVoucher;
  4. use app\admin\model\User;
  5. use app\common\controller\Backend;
  6. use think\Db;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 优惠券
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Voucher extends Backend
  15. {
  16. /**
  17. * Voucher模型对象
  18. * @var \app\admin\model\user\Voucher
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\user\Voucher;
  25. $this->view->assign("statusList", $this->model->getStatusList());
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. //当前是否为关联查询
  38. $this->relationSearch = true;
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags', 'trim']);
  41. if ($this->request->isAjax()) {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  47. $list = $this->model
  48. ->with(['user'])
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  52. foreach ($list as $row) {
  53. $row->visible(['id', 'takeAmount', 'fullAmount', 'status', 'expiretime', 'updatetime']);
  54. $row->visible(['user']);
  55. $row->getRelation('user')->visible(['username']);
  56. }
  57. $result = array("total" => $list->total(), "rows" => $list->items());
  58. return json($result);
  59. }
  60. return $this->view->fetch();
  61. }
  62. public function add()
  63. {
  64. if (false === $this->request->isPost()) {
  65. return $this->view->fetch();
  66. }
  67. $params = $this->request->post('row/a');
  68. if (empty($params)) {
  69. $this->error(__('Parameter %s can not be empty', ''));
  70. }
  71. $params = $this->preExcludeFields($params);
  72. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  73. $params[$this->dataLimitField] = $this->auth->id;
  74. }
  75. $sys_voucher = (\app\admin\model\Voucher::where("id", $params["voucher_id"]))->find();
  76. if (!$sys_voucher)
  77. $this->error("选择的优惠券不存在!");
  78. $user_ids = explode(",", $params["user_ids"]) ?: [];
  79. $result = false;
  80. Db::startTrans();
  81. try {
  82. $result = $this->model->insertAll(array_map(function ($u_id) use ($sys_voucher) {
  83. return [
  84. "user_id" => $u_id,
  85. "voucher_id" => $sys_voucher["id"],
  86. "takeAmount" => $sys_voucher["takeAmount"],
  87. "fullAmount" => $sys_voucher["fullAmount"],
  88. "status" => \E_BASE_STATUS::Normal,
  89. "expiretime" => time() + (24 * 60 * 60 * $sys_voucher["indate_day"]),
  90. "createtime" => time(),
  91. "updatetime" => time()
  92. ];
  93. }, $user_ids));
  94. Db::commit();
  95. } catch (ValidateException | PDOException | Exception $e) {
  96. Db::rollback();
  97. $this->error($e->getMessage());
  98. }
  99. if ($result === false) {
  100. $this->error(__('No rows were inserted'));
  101. }
  102. $this->success();
  103. }
  104. public function grant_voucher()
  105. {
  106. if (false === $this->request->isPost()) {
  107. return $this->view->fetch();
  108. }
  109. $params = $this->request->post('row/a');
  110. if (empty($params)) {
  111. $this->error(__('Parameter %s can not be empty', ''));
  112. }
  113. if ($params["user_group"] == "ALL") {
  114. $users = (new User())
  115. ->where("status", "normal")
  116. ->field("id")
  117. ->select();
  118. } else {
  119. $users = (new User())
  120. ->where("status", "normal")
  121. ->where("grouping", $params["user_group"])
  122. ->field("id")
  123. ->select();
  124. }
  125. $gVoucher = [];
  126. foreach ($users as $user) {
  127. array_push($gVoucher, [
  128. "to_user_id" => $user["id"],
  129. "take_effect" => $params["take_effect"],
  130. "is_read" => 0,
  131. "voucher_ids" => $params["voucher_ids"],
  132. "draw" => 0,
  133. "createtime" => time(),
  134. "updatetime" => time(),
  135. ]);
  136. }
  137. if (count($gVoucher) > 0) {
  138. (new GrantVoucher())->insertAll($gVoucher);
  139. }
  140. $this->success();
  141. }
  142. }