Coupon.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace app\api\controller\company;
  3. use app\common\controller\Apic;
  4. use think\Db;
  5. /**
  6. * 优惠券
  7. */
  8. class Coupon extends Apic
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = '*';
  12. public function _initialize()
  13. {
  14. parent::_initialize();
  15. }
  16. //优惠券管理
  17. public function lists(){
  18. $status = input('status',1);
  19. $where = [
  20. 'company_id' => $this->auth->id,
  21. 'type' => 1,
  22. ];
  23. if($status == 1){ //未开始
  24. $where['usetimestart'] = ['gt',time()];
  25. }elseif($status == 2){ //进行中
  26. $where['usetimestart'] = ['lt',time()];
  27. $where['usetimeend'] = ['gt',time()];
  28. }else{ //已结束
  29. $where['usetimeend'] = ['lt',time()];
  30. }
  31. $list = Db::name('coupons')->where($where)->order('id desc')->autopage()->select();
  32. $this->success(1,$list);
  33. }
  34. //新增
  35. public function add(){
  36. $field = ['enough','amount','number','limit','usetimestart','usetimeend'];
  37. $data = request_post_hub($field);
  38. $data['company_id'] = $this->auth->id;
  39. $data['type'] = 1;
  40. $data['score'] = 0;
  41. $data['stock'] = $data['number'];
  42. $data['createtime'] = time();
  43. $data['updatetime'] = time();
  44. Db::name('coupons')->insertGetId($data);
  45. $this->success('添加成功');
  46. }
  47. public function info(){
  48. $id = input('id',0);
  49. $info = Db::name('coupons')->where('id',$id)->find();
  50. $this->success(1,$info);
  51. }
  52. //编辑
  53. public function edit(){
  54. $id = input('id','');
  55. $check = Db::name('coupons')->where('id',$id)->where('company_id',$this->auth->id)->find();
  56. if(empty($check)){
  57. $this->error('不存在的优惠券');
  58. }
  59. if($check['usetimestart'] < time()){
  60. $this->error('优惠券已经进行中,不能修改');
  61. }
  62. //
  63. $field = ['enough','amount','number','limit','usetimestart','usetimeend'];
  64. $data = request_post_hub($field);
  65. $data['stock'] = $data['number'];
  66. $data['updatetime'] = time();
  67. Db::name('coupons')->where('id',$id)->update($data);
  68. $this->success('编辑成功');
  69. }
  70. //删除
  71. public function delete(){
  72. $id = input('id','');
  73. $check = Db::name('coupons')->where('id',$id)->where('company_id',$this->auth->id)->find();
  74. if(empty($check)){
  75. $this->error('不存在的优惠券');
  76. }
  77. if($check['usetimestart'] < time()){
  78. $this->error('优惠券已经进行中,不能删除');
  79. }
  80. Db::name('coupons')->where('id',$id)->delete();
  81. $this->success('删除成功');
  82. }
  83. }