$this->auth->company_id, 'status' => $status, ]; $list = Db::name('coupons')->where($where)->order('id desc')->autopage()->select(); $this->success(1,$list); } //新增 public function add(){ $field = ['name','info','days']; $data = request_post_hub($field); $data['company_id'] = $this->auth->company_id; $data['createtime'] = time(); $data['status'] = 1; Db::name('coupons')->insertGetId($data); $this->success('添加成功'); } //上下架 public function changestatus(){ $id = input('id',0); $info = Db::name('coupons')->where('id',$id)->update(['status'=>0]); $this->success(1,$info); } //详情 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->company_id)->find(); if(empty($check)){ $this->error('不存在的卡券'); } // $field = ['name','info','days']; $data = request_post_hub($field); 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->company_id)->find(); if(empty($check)){ $this->error('不存在的优惠券'); } Db::name('coupons')->where('id',$id)->delete(); $this->success('删除成功'); } //核销用户一张卡券 public function hexiao(){ $user_coupon_id = input('user_coupon_id',0); $map = [ 'company_id' => $this->auth->company_id, 'id' => $user_coupon_id, ]; Db::startTrans(); $check = Db::name('user_coupons')->where($map)->find(); if(empty($check)){ Db::rollback(); $this->error('不存在的卡券'); } if($check['remain'] <= 0){ Db::rollback(); $this->error('卡券数量不足'); } if($check['endtime'] <= time()){ Db::rollback(); $this->error('卡券已过期'); } //核销日志 $log = [ 'user_id' => $check['user_id'], 'company_id' => $check['company_id'], 'coupons_id' => $check['coupons_id'], 'coupon_name' => $check['coupon_name'], 'user_coupon_id' => $check['id'], 'createtime' => time(), ]; $log_id = Db::name('user_coupons_log')->insertGetId($log); if(!$log_id){ Db::rollback(); $this->error('核销失败'); } //数量减一 $rs = Db::name('user_coupons')->where($map)->update(['remain'=>$check['remain']-1]); if($rs === false){ Db::rollback(); $this->error('核销失败'); } Db::commit(); $this->success('核销完成'); } }