title = '优惠券'; $coupon_id=$this->request->get('coupon_id'); $this->byWhere(1) ->field('a.*,b.name,b.headimg') ->where('a.config_id',$coupon_id) ->order('a.id desc') ->page(); } /** * 搜索条件 * @return \library\helper\QueryHelper */ protected function byWhere($type) { if ($type == 1) { $query = $this->_query($this->table); } elseif ($type == 2) { $query = Db::name($this->table); } $query = $query->alias('a') ->join('store_member b', 'a.user_id=b.id'); if (isset($_GET['status']) && $_GET['status']) { $query->where('a.status', $_GET['status']); } if (isset($_GET['title']) && $_GET['title']) { $query->where('a.title', 'like','%'.$_GET['title'].'%'); } if (isset($_GET['user_info']) && $_GET['user_info']) { $query->where('b.name|b.phone', 'like', '%' . $_GET['user_info'] . '%'); } return $query; } /** * 数据列表处理 * @param array $data * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ protected function _index_page_filter(&$data) { foreach ($data as $k=>&$v) { $coupon=Db::name('coupon_config')->where('id',$this->request->get('coupon_id'))->find(); if ($coupon['coupon_type'] == 1) { if ($coupon['goods_id']) { $v['goods_name']=Db::name('store_goods')->where('id',$v['goods_id'])->value('name'); } } $v['coupon_type']=$coupon['coupon_type']; $v['member'] = Db::name('store_member')->where('id',$v['user_id'])->find(); } } /** * 添加优惠券 * @auth true * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function add() { $this->title = '添加优惠券'; if($this->request->isPost()){ if($this->request->post('user')==0){ $user_ids=explode(',',$this->request->post('user_ids')); //指定用户发放 }else{ //全部用户发方 $user_ids=Db::name('store_member')->where('status',1)->column('id'); } $coupon=Db::name('coupon_config')->where('id',$this->request->post('coupon_id'))->where('status',1)->where('is_deleted',0)->find(); if(empty($coupon)){ $this->error('优惠券不存在或已禁用'); } $error=[]; foreach ($user_ids as $k=>$v){ $data=[]; $count=Db::name('user_coupon_list')->where('config_id',$coupon['id'])->where('user_id',$v)->count(); if($count+1>$coupon['user_num']){ array_push($error,$v); }else{ $data['config_id']=$coupon['id']; $data['user_id']=$v; $data['goods_id']=$coupon['goods_id']; $data['title']=$coupon['title']; $data['low_amount']=$coupon['low_amount']; $data['amount']=$coupon['amount']; if($coupon['time_type']==0){ if($coupon['low_day']==0){ $data['type']=0; }else{ $data['type']=1; $data['start']=date('Y-m-d'); $data['end']=date('Y-m-d', strtotime('+'.$coupon['low_day'].' days') );; } }else{ $data['type']=1; $data['start']=$coupon['start_tm']; $data['end']=$coupon['end_tm']; } $data['status']=0; $data['create_at']=time(); $data['coupon_type']=$coupon['coupon_type']; Db::name('user_coupon_list')->insert($data); } } $msg='发放成功'; if($error){ $nickname=Db::name('store_member')->where('id','in',$error)->column('name'); $nickname=implode(',',$nickname); $msg='发放成功'.$nickname.'等用户因规则原因,发放失败'; } $this->success($msg); }else{ $this->_form($this->table, 'form'); } } /** * 编辑优惠券 * @auth true * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function edit() { $this->title = '编辑优惠券'; $this->_form($this->table, 'form'); } /** * 表单数据处理 * @param array $data * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ protected function _form_filter(&$data) { if($this->request->isPost()){ }else{ $coupon_id=$this->request->get('coupon_id'); $this->coupon_id=$coupon_id; $this->member=Db::name('store_member')->field('id,phone,name')->where('status',1)->select(); $this->member_count=count($this->member); } } /** * 禁用优惠券 * @auth true * @throws \think\Exception * @throws \think\exception\PDOException */ public function forbid() { $this->_save($this->table, ['status' => '0']); } /** * 启用优惠券 * @auth true * @throws \think\Exception * @throws \think\exception\PDOException */ public function resume() { $this->_save($this->table, ['status' => '1']); } /** * 删除优惠券 * @auth true * @throws \think\Exception * @throws \think\exception\PDOException */ public function remove() { $this->_delete($this->table); } }