Couponvip.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use Think\Db;
  5. /**
  6. * 会员管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Couponvip extends Backend
  11. {
  12. /**
  13. * Couponvip模型对象
  14. * @var \app\admin\model\Couponvip
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Couponvip;
  21. $this->view->assign("statusList", $this->model->getStatusList());
  22. }
  23. /**
  24. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  25. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  26. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  27. */
  28. /**
  29. * 查看
  30. */
  31. public function index()
  32. {
  33. $coupon_id = input('coupon_id', 0, 'intval'); //优惠券id
  34. if (!$coupon_id) {
  35. $coupon_id = input('ids', 0, 'intval'); //从优惠券列表第一次跳转过来
  36. }
  37. $this->assignconfig('coupon_id', $coupon_id);
  38. //当前是否为关联查询
  39. $this->relationSearch = false;
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if ($this->request->isAjax()) {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $list = $this->model
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  52. foreach ($list as $row) {
  53. $row->visible(['id','title','level','growthvalue','free','vipdiscount','birthdiscount','manypeople']);
  54. }
  55. $result = array("total" => $list->total(), "rows" => $list->items());
  56. return json($result);
  57. }
  58. return $this->view->fetch();
  59. }
  60. /**
  61. * 发放优惠券给会员组
  62. */
  63. public function giveout() {
  64. $ids = input('ids', 0, 'intval'); //会员组id
  65. $coupon_id = input('coupon_id', 0, 'intval'); //优惠券id
  66. if (!$ids || !$coupon_id) {
  67. $this->error('参数缺失');
  68. }
  69. $info = Db::name('coupon')->find($coupon_id);
  70. if (!$info) {
  71. $this->error('优惠券不存在');
  72. }
  73. if ($info['purpose'] != 3) {
  74. $this->error('优惠券类型错误');
  75. }
  76. $user_ids = Db::name('user')->where(['growthlevel' => $ids, 'status' => 1])->column('id');
  77. if (!$user_ids) {
  78. $this->error('该会员组暂时没有用户');
  79. }
  80. $coupon_data = [
  81. // 'user_id' => $ids,
  82. 'coupon_id' => $info['id'],
  83. 'title' => $info['title'],
  84. 'desc' => $info['desc'],
  85. 'type' => $info['type'],
  86. 'money' => $info['money'],
  87. 'minmoney' => $info['minmoney'],
  88. 'purpose' => $info['purpose'],
  89. 'starttime' => time(),
  90. 'endtime' => time() + $info['effectiveday'] * 86400,
  91. 'createtime' => time()
  92. ];
  93. $user_coupon = Db::name('user_coupon');
  94. foreach ($user_ids as &$v) {
  95. $coupon_data['user_id'] = $v;
  96. $user_coupon->insertGetId($coupon_data);
  97. }
  98. $this->success('发放成功');
  99. }
  100. }