123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace app\api\controller\company;
- use app\common\controller\Apic;
- use think\Db;
- /**
- * 优惠券
- */
- class Coupon extends Apic
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = '*';
- //优惠券管理
- public function lists(){
- $status = input('status',1);
- $where = [
- 'company_id' => $this->auth->id,
- 'type' => 1,
- ];
- if($status == 1){ //未开始
- $where['usetimestart'] = ['gt',time()];
- }elseif($status == 2){ //进行中
- $where['usetimestart'] = ['lt',time()];
- $where['usetimeend'] = ['gt',time()];
- }else{ //已结束
- $where['usetimeend'] = ['lt',time()];
- }
- $list = Db::name('coupons')->where($where)->order('id desc')->autopage()->select();
- $this->success(1,$list);
- }
- //新增
- public function add(){
- $field = ['enough','amount','number','limit','usetimestart','usetimeend'];
- $data = request_post_hub($field);
- $data['company_id'] = $this->auth->id;
- $data['type'] = 1;
- $data['score'] = 0;
- $data['stock'] = $data['number'];
- $data['createtime'] = time();
- $data['updatetime'] = time();
- Db::name('coupons')->insertGetId($data);
- $this->success('添加成功');
- }
- public function info(){
- $id = input('id',0);
- $info = Db::name('coupons')->where('id',$id)->find();
- $this->success(1,$info);
- }
- //编辑
- public function edit(){
- $id = input('id','');
- $check = Db::name('coupons')->where('id',$id)->where('company_id',$this->auth->id)->find();
- if(empty($check)){
- $this->error('不存在的优惠券');
- }
- if($check['usetimestart'] < time()){
- $this->error('优惠券已经进行中,不能修改');
- }
- //
- $field = ['enough','amount','number','limit','usetimestart','usetimeend'];
- $data = request_post_hub($field);
- $data['stock'] = $data['number'];
- $data['updatetime'] = time();
- Db::name('coupons')->where('id',$id)->update($data);
- $this->success('编辑成功');
- }
- //删除
- public function delete(){
- $id = input('id','');
- $check = Db::name('coupons')->where('id',$id)->where('company_id',$this->auth->id)->find();
- if(empty($check)){
- $this->error('不存在的优惠券');
- }
- if($check['usetimestart'] < time()){
- $this->error('优惠券已经进行中,不能删除');
- }
- Db::name('coupons')->where('id',$id)->delete();
- $this->success('删除成功');
- }
- }
|