Coupon.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 Coupon extends Backend
  11. {
  12. /**
  13. * Coupon模型对象
  14. * @var \app\admin\model\Coupon
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Coupon;
  21. $this->assign('types',\app\admin\model\Coupon::getTypes());
  22. }
  23. public function import()
  24. {
  25. parent::import();
  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 = false;
  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. ->where($where)
  49. ->order($sort, $order)
  50. ->paginate($limit);
  51. foreach ($list as $row) {
  52. }
  53. $result = array("total" => $list->total(), "rows" => $list->items());
  54. return json($result);
  55. }
  56. return $this->view->fetch();
  57. }
  58. public function add()
  59. {
  60. if($this->request->isGet()){
  61. return $this->fetch();
  62. }else{
  63. $data=input('row/a');
  64. $this->validate($data,[
  65. 'type|类型'=>['require'],
  66. 'amount|金额'=>['require'],
  67. 'num|数量'=>['require','gt:0'],
  68. 'time_start|开始时间'=>['require','gt:0'],
  69. 'time_end|结束时间'=>['require','gt:0'],
  70. ]);
  71. if($data['type']==1){
  72. $this->validate($data,[
  73. 'amount_full|满足金额'=>['require','egt:'.input('row.amount')],
  74. ]);
  75. }
  76. Db::startTrans();
  77. $coupon=new \app\admin\model\Coupon();
  78. $coupon->allowField(true)->data($data)->save();
  79. $goods_id=array_filter(explode(',',$data['goods_id']));
  80. /*if(!$goods_id){
  81. $goods_id=\app\admin\model\Goods::show()->column('id');
  82. }*/
  83. $arr=[];
  84. foreach ($goods_id?:[] as $id){
  85. $new['goods_id']=$id;
  86. $arr[]=$new;
  87. }
  88. $arr && $coupon->bindGoods()->saveAll($arr);
  89. Db::commit();
  90. $this->success();
  91. }
  92. }
  93. public function edit($ids = null)
  94. {
  95. $this->error('');
  96. }
  97. public function send($ids){
  98. if($this->request->isGet()){
  99. return $this->fetch();
  100. }else{
  101. $data=input('row/a');
  102. $this->validate($data,[
  103. 'num|数量'=>['require','gt:0'],
  104. 'users|用户'=>['require']
  105. ]);
  106. Db::startTrans();
  107. $coupons=$this->model->whereIn('id',$ids)->lock(true)->select();
  108. foreach ($coupons as $coupon){
  109. $coupon->sendToUser(explode(',',$data['users']),$data['num']);
  110. }
  111. Db::commit();
  112. $this->success();
  113. }
  114. }
  115. }