request->isGet) { $params = \Yii::$app->request->get(); $where[] = ['=', 'mall_id', $this->mall_id]; $where[] = ['status' => StatusEnum::ENABLED]; $where[] = ['user_id' => $this->user['id']]; if(isset($params['type']) && $params['type'] == 2){ $where[] = [ 'or', ['<','expired_at',time()], ['is_recycle'=>1], ]; }elseif($params['type']==1){ $members_card_give = MembersCardGive::find() ->where(['mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED]) ->andWhere(['>','parent_card_id',0]) ->asArray() ->all(); $parent_card_ids = array_column($members_card_give,'parent_card_id'); $where[] = ['in','id',$parent_card_ids]; $where[] = [ 'and', ['>=','expired_at',time()], ['is_recycle'=>0], ]; }else{ $where[] = [ 'and', ['>=','expired_at',time()], ['is_recycle'=>0], ]; } $params['where'] = $where; $params['select'] = 'id,card_id,discount,child_num,parent_card_id,expired_at,created_at,card_no,is_recycle,recycle_at'; $data = MembersCardGive::listPage($params, false); if(!empty($data['list'])){ $config = Yii::$app->services->setting->addons->get($this->mall_id, MembersCardEnum::ADDONS_NAME, 'basic'); foreach($data['list'] as &$list){ $list['expired_at'] = date("Y-m-d",$list['expired_at']); $list['created_at'] = date("Y-m-d",$list['created_at']); $list['name'] = ''; $list['background_url'] = ''; //是否显示赠送亲友 $list['is_give'] = 0; //是否显示子卡列表 $list['is_list'] = 0; //是否显示使用记录 $list['is_used'] = 1; //是否显示去升级 $list['is_up'] = 0; $list['is_up_desc'] = ''; $list['is_up_link'] = ''; $list['is_show'] = 0; $list['user'] = []; $members_card = MembersCard::findOne(['id'=>$list['card_id']]); if($members_card){ $list['name'] = $members_card['name']; $list['background_url'] = $members_card['background_url']; $levels = json_decode($members_card['member_limit'],true); if(!empty($levels) && !in_array($this->user['level'],$levels) && $list['parent_card_id']){ $list['is_up'] = 1; $list['is_up_desc'] = $config ? $config['description'] : ''; $list['is_up_link'] = $members_card['child_upgrade_links']; } } //子卡已使用数量 $members_card_give_num = MembersCardGive::find()->where(['mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED,'parent_card_id'=>$list['id']])->count(); $list['child_used_num'] = $members_card_give_num ?? 0; if(!$list['parent_card_id']){ if($list['child_used_num'] < $list['child_num']){ $list['is_give'] = 1; } } $list['child_num'] = $list['child_num'] - $list['child_used_num']; $list['child_num'] = $list['child_num'] > 0 ? $list['child_num'] : 0; //已赠送列表不显示送亲友按钮 if($members_card_give_num && isset($params['type']) && $params['type'] == 1){ $list['is_list'] = 1; $list['is_give'] = 0; } if($list['parent_card_id']){ $list['is_show'] = 1; $parent = MembersCardGive::findOne(['mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED,'id'=>$list['parent_card_id']]); if($parent){ $parent_user = User::findOne(['id'=>$parent['user_id']]); } $list['give_user'] = [ 'user_id' => $parent_user['id'], 'avatar_url' => $parent_user['avatar_url'], 'nickname' => $parent_user['nickname'], ]; } } } return $this->success('操作成功', $data); }else{ throw new \Exception("请求方法错误!"); } }catch(\Exception $e){ return $this->error(); } } //子卡列表 public function actionChildList(){ try{ if (\Yii::$app->request->isGet) { $params = \Yii::$app->request->get(); if(!isset($params['id'])){ throw new \Exception("参数错误!"); } $where[] = ['=','parent_card_id',$params['id']]; $where[] = ['=', 'mall_id', $this->mall_id]; $where[] = ['status' => StatusEnum::ENABLED]; // $where[] = ['user_id' => $this->user['id']]; $params['where'] = $where; $params['select'] = 'id,card_id,user_id,discount,expired_at,created_at,is_recycle,card_no'; $data = MembersCardGive::listPage($params, false); if(!empty($data['list'])){ foreach($data['list'] as &$list){ $list['expired_at'] = date("Y-m-d",$list['expired_at']); $list['created_at'] = date("Y-m-d",$list['created_at']); $list['name'] = ''; $list['background_url'] = ''; $members_card = MembersCard::findOne(['id'=>$list['card_id']]); if($members_card){ $list['name'] = $members_card['name']; $list['background_url'] = $members_card['background_url']; } $user = User::findOne(['id'=>$list['user_id']]); $list['user'] = [ 'user_id' => $user['id'], 'avatar_url' => $user['avatar_url'], 'nickname' => $user['nickname'], ]; } } return $this->success('操作成功', $data); }else{ throw new \Exception("请求方法错误!"); } }catch(\Exception $e){ return $this->error(); } } public function actionUsedLog(){ try{ if (\Yii::$app->request->isGet) { $params = \Yii::$app->request->get(); if(!isset($params['id'])){ throw new \Exception("参数错误!"); } $where[] = ['=','card_give_id',$params['id']]; $where[] = ['=', 'mall_id', $this->mall_id]; $where[] = ['status' => StatusEnum::ENABLED]; // $where[] = ['user_id' => $this->user['id']]; if($params['created_at']){ $start_time = strtotime(date('Y-m-01 00:00:00', strtotime($params['created_at']))); $end_time = strtotime(date('Y-m-t 23:59:59', strtotime($params['created_at']))); $where[] = ['>', 'created_at', $start_time]; $where[] = ['<=', 'created_at', $end_time]; } $params['where'] = $where; $params['select'] = 'id,discount_money,pay_money,order_id,order_detail_id,store_id,is_cashier'; $data = MembersCardUsedLog::listPage($params, false); $newList = []; if(!empty($data['list'])){ foreach($data['list'] as &$list){ if(!isset($newList[$list['order_id']])){ $list['store_name'] = '平台'; if($list['store_id']){ $store = Store::getOne([['id' => $list['store_id']], ['mall_id' => $this->mall_id]]); if(!empty($store)){ $list['store_name'] = $store['store_name']; } } if($list['is_cashier']){ $order = StoreCashierOrder::findOne(['id'=>$list['order_id']]); $list['goods'][] = [ 'name' => '门店扫码', 'price' => $order['money'], 'goods_attr_name' => '', 'num' => 1, 'discount_money' => $list['discount_money'], 'pic_url' => '', ]; }else{ $order = Order::findOne(['id'=>$list['order_id']]); $order_detail = OrderDetail::findOne(['id'=>$list['order_detail_id']]); if($order_detail){ $list['goods'][] = [ 'name' => $order_detail['goods_name'], 'price' => $order_detail['price'], 'goods_attr_name' => $order_detail['goods_attr_name'], 'num' => $order_detail['num'], 'discount_money' => $list['discount_money'], 'pic_url' => $order_detail['pic_url'], ]; } } if($order){ $list['pay_time'] = date("Y-m-d H:i:s",$order['pay_time']); $list['pay_money'] = $order['pay_money']; } $newList[$list['order_id']] = $list; unset($newList[$list['order_id']]['discount_money'],$newList[$list['order_id']]['order_id'],$newList[$list['order_id']]['order_detail_id']); }else{ if(!$list['is_cashier']){ $order_detail = OrderDetail::findOne(['id'=>$list['order_detail_id']]); if($order_detail){ $newList[$list['order_id']]['goods'][] = [ 'name' => $order_detail['goods_name'], 'price' => $order_detail['price'], 'goods_attr_name' => $order_detail['goods_attr_name'], 'num' => $order_detail['num'], 'discount_money' => $list['discount_money'], 'pic_url' => $order_detail['pic_url'], ]; } } } } // $data['list'] = $newList; } $arr = []; foreach($newList as $key => $item){ $arr[] = $item; } $data['list'] = $arr; return $this->success('操作成功', $data); }else{ throw new \Exception("请求方法错误!"); } }catch(\Exception $e){ return $this->error(); } } /** * @name: * @msg: 会员卡信息信息 * @param {integer} $card_give_id * @return {*} */ public function actionGiveCardInfo() { if (!\Yii::$app->request->isGet) { return $this->error('请求方式错误'); } $user_id = $this->user_id; $card_give_id = \Yii::$app->request->get('card_give_id'); if (empty($card_give_id)) { return $this->error('参数错误'); } $card_give = MembersCardGive::find()->where(['id' => $card_give_id, 'mall_id' => $this->mall_id])->asArray()->one(); if (empty($card_give)) return $this->error('母卡不存在'); $members_card = MembersCard::find()->where(['id' => $card_give['card_id'], 'mall_id' => $this->mall_id])->asArray()->one(); if (empty($members_card)) return $this->error('数据不存在'); $members_card_give_num = MembersCardGive::find()->where(['mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED,'parent_card_id'=>$card_give_id])->count(); if($members_card_give_num && $members_card_give_num >= $card_give['child_num']){ return $this->error('来晚啦,亲友卡已被领完~'); } $give_user = User::findOne(['id'=>$card_give['user_id']]); if(!$give_user){ return $this->error('分享用户不存在'); } $detail = [ 'card_give_id' => $card_give_id, 'discount' => $card_give['child_discount'], 'name' => $members_card['name'], 'background_url' => $members_card['background_url'], 'desc' => '领取亲友卡,可享受'.$card_give['child_discount'].'的优惠折扣', 'user_id' => $give_user['id'], 'avatar_url' => $give_user['avatar_url'], 'nickname' => $give_user['nickname'], 'created_at' => date("Y-m-d",time()), 'expired_at' => date("Y-m-d",strtotime("+".$card_give['child_expired_days']." day")), ]; return $this->success('操作成功', ['card_info' => $detail]); } //领取亲友卡 public function actionReceive(){ $params = \Yii::$app->request->post(); $res = Yii::$app->services->validate->validate($params, [ [['card_give_id'], 'required'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_USER_WALLET , $params['card_give_id']); $identification = uniqid(); if (!LockHelper::lock($lock_key, $identification, 100, 100)) { \Yii::error(__CLASS__ . ' batchHandle error : 抢不到锁,task_id:' . $task_id); return $this->error('网络异常,请稍后再试~'); } $trans = \Yii::$app->db->beginTransaction(); try { $card_give = MembersCardGive::find()->where(['id' => $params['card_give_id'], 'mall_id' => $this->mall_id])->asArray()->one(); if (empty($card_give)) throw new \Exception('数据异常'); if($this->user['id'] == $card_give['user_id']){ throw new \Exception('不能领取自己赠送的亲友卡'); } $card_give_log = MembersCardGive::find()->where(['parent_card_id' =>$params['card_give_id'], 'mall_id' => $this->mall_id,'user_id'=>$this->user['id'],'status'=>StatusEnum::ENABLED])->one(); if($card_give_log){ throw new \Exception('您已领取过亲友卡!'); } $members_card_give_num = MembersCardGive::find()->where(['mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED,'parent_card_id'=>$params['card_give_id']])->count(); if($members_card_give_num && $members_card_give_num + 1 >= $card_give['child_num']){ throw new \Exception('来晚啦,亲友卡已被领完~'); } if(!$members_card_give_num){ $card_no = 1; }else{ $card_no = $members_card_give_num + 1; } $expired_at = strtotime("+".$card_give['child_expired_days']." day"); if($expired_at > $card_give['expired_at']){ $expired_at = $card_give['expired_at']; } $data = [ 'mall_id' => $this->mall_id, 'user_id' => $this->user['id'], 'discount' => $card_give['child_discount'], 'parent_card_id' => $card_give['id'], 'card_id' => $card_give['card_id'], 'receive_at' => time(), 'expired_at' => $expired_at, 'card_no' => $card_no, ]; $model = MembersCardGive::setData($data); if(!$model){ throw new \Exception('领取失败'); } $members_card = MembersCard::find()->where(['id' => $card_give['card_id'], 'mall_id' => $this->mall_id])->asArray()->one(); $detail = [ 'id' => $model['id'], 'discount' => $model['discount'], 'name' => $members_card['name'], 'background_url' => $members_card['background_url'], 'created_at' => date("Y-m-d",$model['created_at']), 'expired_at' => date("Y-m-d",$model['expired_at']), ]; $trans->commit(); LockHelper::uLock($lock_key, $identification); return $this->success('领取成功', ['card_info' => $detail]); } catch (\Exception $e) { $trans->rollBack(); LockHelper::uLock($lock_key, $identification); \Yii::error($e->getMessage()); return $this->error($e->getMessage()); } } /** * @name: * @msg: 回收亲友卡 * @param {integer} $card_give_id * @return {*} */ public function actionRecycle() { if (!\Yii::$app->request->isPost) { return $this->error('请求方式错误'); } $user_id = $this->user['id']; $card_give_id = \Yii::$app->request->post('id'); if (empty($card_give_id)) { return $this->error('参数错误'); } $card_give = MembersCardGive::find()->where(['id' => $card_give_id, 'mall_id' => $this->mall_id])->one(); if (empty($card_give)) return $this->error('亲友卡不存在'); $parent_card = MembersCardGive::find()->where(['id' => $card_give['parent_card_id'], 'mall_id' => $this->mall_id])->asArray()->one(); if($parent_card['user_id'] != $user_id){ return $this->error('您无权回收该亲友卡'); } $card_give->is_recycle = 1; $card_give->recycle_at = time(); $card_give->status = StatusEnum::DISABLED; if(!$card_give->save()){ return $this->error('回收失败'); } return $this->success('回收成功'); } /** * @name: * @msg: 批量回收亲友卡 * @param {integer} $card_give_id * @return {*} */ public function actionRecycleAll() { if (!\Yii::$app->request->isPost) { return $this->error('请求方式错误'); } $user_id = $this->user['id']; $card_give_id = \Yii::$app->request->post('id'); if (empty($card_give_id)) { return $this->error('参数错误'); } $card_give = MembersCardGive::find()->where(['id' => $card_give_id, 'mall_id' => $this->mall_id])->one(); if (empty($card_give)) return $this->error('母卡不存在'); if($card_give['user_id'] != $user_id){ return $this->error('您无权回收该亲友卡子卡'); } $t = \Yii::$app->db->beginTransaction(); try{ $card_list = MembersCardGive::find()->where(['parent_card_id' => $card_give_id, 'mall_id' => $this->mall_id,'is_recycle'=>0,'status'=>StatusEnum::ENABLED])->all(); if($card_list){ foreach($card_list as $list){ $list->is_recycle = 1; $list->recycle_at = time(); $list->status = StatusEnum::DISABLED; if(!$list->save()){ throw new \Exception('保存失败'); } } } $t->commit(); return $this->success('回收成功'); }catch(\Exception $e){ $t->rollBack(); $this->error('回收失败'); } } //佣金列表 public function actionIncomeList(){ try{ if (\Yii::$app->request->isGet) { $params = \Yii::$app->request->get(); if(isset($params['type']) && $params['type'] == 1){ $where[] = ['=','is_frozen',1]; }else{ $where[] = ['=','is_frozen',0]; } $where[] = ['=', 'mall_id', $this->mall_id]; $where[] = ['status' => StatusEnum::ENABLED]; $where[] = ['reward_user_id' => $this->user['id']]; $params['where'] = $where; $params['select'] = 'id,discount_money,user_id,card_give_id,reward_money,goods_name,order_id,store_id,is_cashier,order_detail_id'; $data = CommissionLog::listPage($params, false); if(!empty($data['list'])){ foreach($data['list'] as &$list){ $list['nickname'] = $list['avatar_url'] = ''; $user = User::findOne(['id'=>$list['user_id']]); if($user){ $list['nickname'] = $user['nickname']; $list['avatar_url'] = $user['avatar_url']; } $list['store_name'] = '平台'; if($list['store_id']){ $store = Store::findOne(['id'=>$list['store_id']]); if($store){ $list['store_name'] = $store['store_name']; } } if($list['is_cashier']){ $order = StoreCashierOrder::findOne(['id'=>$list['order_id']]); $list['money'] = $order['money']; }else{ $order = Order::findOne(['id'=>$list['order_id']]); $order_detail = OrderDetail::findOne(['id'=>$list['order_detail_id']]); $list['money'] = $order_detail['price']; } if($order){ $list['pay_time'] = date("Y-m-d H:i:s",$order['pay_time']); $list['pay_money'] = $order['pay_money']; $list['order_no'] = $order['order_no']; } $card_give = MembersCardGive::findOne(['id'=>$list['card_give_id']]); $members_card = MembersCard::findOne(['id'=>$card_give['card_id']]); $list['card_name'] = $members_card['name'] ?? ''; unset($list['card_give_id'],$list['order_id'],$list['store_id'],$list['is_cashier'],$list['order_detail_id']); } } $data['user'] = [ 'user_id' => $this->user['id'], 'nickname' => $this->user['nickname'], 'avatar_url' => $this->user['avatar_url'], ]; $data['imcome']['total_income'] = CommissionLog::find()->where(['mall_id'=>$this->mall_id,'reward_user_id'=>$this->user['id'],'is_frozen'=>0])->sum('reward_money') ?? 0; $data['imcome']['wait_income'] = CommissionLog::find()->where(['mall_id'=>$this->mall_id,'reward_user_id'=>$this->user['id'],'is_frozen'=>1])->sum('reward_money') ?? 0; return $this->success('操作成功', $data); }else{ throw new \Exception("请求方法错误!"); } }catch(\Exception $e){ return $this->error(); } } }