request->isGet) { $get = Yii::$app->request->get(); // 检查提交的参数 $res = Yii::$app->services->validate->validate($get, [ [['page', 'limit', 'store_id','agent_id'], 'integer'], [['type', 'time', 'commission_status'], 'string'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); // 获取收益列表 $where = []; $where[] = ['=', 'mall_id', $this->mall_id]; $where[] = ['=', 'user_id', $this->user_id]; $where[] = ['=', 'status', StatusEnum::ENABLED]; if (!empty($get['store_id'])) { $where[] = ['=', 'store_id', $get['store_id']]; } if (!empty($get['agent_id'])) { $where[] = ['=', 'agent_id', $get['agent_id']]; } if (!empty($get['type'])) { switch ($get['type']) { case 'share': $where[] = ['=', 'commission_type', HappinessRewardEnum::COMMISSION_TYPE_SHARE]; break; case 'lock': $where[] = ['=', 'commission_type', HappinessRewardEnum::COMMISSION_TYPE_LOCK]; break; case 'agent': $where[] = ['=', 'commission_type', HappinessRewardEnum::COMMISSION_TYPE_AGENT]; break; case 'command': $where[] = ['in', 'commission_type', [HappinessRewardEnum::COMMISSION_TYPE_COMMAND, HappinessRewardEnum::COMMISSION_TYPE_COMMAND_AGENT]]; break; case 'command_agent': $where[] = ['=', 'commission_type', HappinessRewardEnum::COMMISSION_TYPE_COMMAND_AGENT]; break; case 'command_store': $where[] = ['=', 'commission_type', HappinessRewardEnum::COMMISSION_TYPE_COMMAND]; break; } } if (!empty($get['time'])) { $time = explode(',', $get['time']); $where[] = ['>=', 'created_at', strtotime($time[0])]; if (count($time) == 2) { $where[] = ['<=', 'created_at', strtotime($time[1]) + 86400]; } } if ($get['commission_status'] >= 0) { $where[] = ['=', 'commission_status', $get['commission_status']]; } $order = 'created_at desc'; $select = '*'; $with = ['user' => function ($query) { $query->select('id,nickname,avatar_url'); }, 'store' => function ($query) { $query->select('id,store_name'); }]; $params = array('where' => $where, 'with' => $with, 'order' => $order, 'select' => $select, 'limit' => $get['limit']);; $list = HappinessCommissionLog::listPage($params, false, true); $order_ids_1=[]; $order_ids_2=[]; //筛选订单号带c开头的和不带c开头的 foreach ($list['list'] as $v) { if(strpos($v['order_no'],'C')===0) { $order_ids_1[]=$v['order_id']; } else { $order_ids_2[]=$v['order_id']; } } $order_list_main=OrderDetail::find()->where(['order_id'=>$order_ids_2])->select('id,order_id,goods_name') ->asArray()->all(); $order_list_cashier=HappinessCashierOrderDetail::find()->where(['cashier_order_id'=>$order_ids_1]) ->select ('id,cashier_order_id,goods_name') ->asArray()->all(); foreach ($list['list'] as &$v) { $goods_list=array_values(array_filter($order_list_main, function($value) use($v) { if($value['order_id']==$v['order_id']) { return true; } })); if(empty($goods_list)) { $goods_list=array_values(array_filter($order_list_cashier, function($value) use($v) { if($value['cashier_order_id']==$v['order_id']) { return true; } })); } $v['created_at'] = date('Y-m-d H:i:s', $v['created_at']); $v['commission_status_text'] = HappinessRewardEnum::getMap()[$v['commission_status']]; $v['store_name'] = $v['store']['store_name']; $v['goods_name'] = $goods_list[0]['goods_name']; unset($v['store']); } $data['list'] = $list; $this->success('获取成功', $data); } } //业务员查询小店列表 public function actionGetStoreList() { if (Yii::$app->request->isGet) { $get = Yii::$app->request->get(); $res = Yii::$app->services->validate->validate($get, [ [['page', 'limit'], 'integer'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $where = []; $where[] = ['=', 'mall_id', $this->mall_id]; $where[] = ['=', 'business_id', $this->user_id]; $where[] = ['=', 'status', StatusEnum::ENABLED]; $order = 'created_at desc'; $select = '*'; $with = []; //总的收益 $total = HappinessCommissionLog::find()->where([ 'status' => StatusEnum::ENABLED, 'user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'commission_status' => HappinessRewardEnum::SEND, 'commission_type' => HappinessRewardEnum::COMMISSION_TYPE_COMMAND]) ->sum('money')??0; $params = array('where' => $where, 'with' => $with, 'order' => $order, 'select' => $select, 'limit' => $get['limit']);; $list = HappinessStore::listPage($params, false, true); foreach ($list['list'] as &$v) { //该小店带来的收益 $v['commission_money'] = HappinessCommissionLog::find()->where(['store_id' => $v['id'], 'status' => StatusEnum::ENABLED, 'user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'commission_status' => HappinessRewardEnum::SEND, 'commission_type' => HappinessRewardEnum::COMMISSION_TYPE_COMMAND]) ->sum('money')??0; } $list['total'] = $total; return $this->success('获取成功', $list); } } //查询代理列表 public function actionGetAgentList() { if (Yii::$app->request->isGet) { $get = Yii::$app->request->get(); $res = Yii::$app->services->validate->validate($get, [ [['page', 'limit'], 'integer'], ['type', 'in', 'range' => ['city', 'district','store','province']], ['role', 'in', 'range' => ['agent', 'command']], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $where = []; if (!empty($get['type'])) { switch ($get['type']) { case 'city': $where[] = ['=', 'role', 2]; break; case 'district': $where[] = ['=', 'role', 3]; break; case 'province': $where[] = ['=', 'role', 1]; break; } } $where[] = ['=', 'mall_id', $this->mall_id]; $commission_type = HappinessRewardEnum::COMMISSION_TYPE_COMMAND_AGENT; if(!empty($get['role']) && $get['role']=='agent') { //代理查询 $where[] = ['=', 'user_id', $this->user_id]; $commission_type=HappinessRewardEnum::COMMISSION_TYPE_AGENT; } else { //业务员查询 $where[] = ['=', 'parent_id', $this->user_id]; } $where[] = ['=', 'status', StatusEnum::ENABLED]; $order = 'created_at desc'; $select = '*'; $with = []; $params = array('where' => $where, 'with' => $with, 'order' => $order, 'select' => $select, 'limit' => $get['limit']);; $list = HappinessAgent::listPage($params, false, true); $total = HappinessCommissionLog::find()->where([ 'status' => StatusEnum::ENABLED, 'user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'commission_status' => HappinessRewardEnum::SEND, 'commission_type' => $commission_type]) ->sum('money')??0; foreach ($list['list'] as &$v) { //收益 $v['store_name'] = $v['region_name']; $v['commission_money'] = HappinessCommissionLog::find()->where(['agent_id' => $v['user_id'], 'status' => StatusEnum::ENABLED, 'user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'commission_status' => HappinessRewardEnum::SEND, 'commission_type' => $commission_type]) ->sum('money')??0; } $list['total'] = $total; return $this->success('获取成功', $list); } } //代理获取小店列表 public function actionGetAgentStoreList() { if (Yii::$app->request->isGet) { $get = Yii::$app->request->get(); $res = Yii::$app->services->validate->validate($get, [ [['page', 'limit'], 'integer'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $where = []; $where[] = ['=', 'mall_id', $this->mall_id]; $where[] = ['=', 'status', StatusEnum::ENABLED]; $where[] =['=','check_status',HappinessEnum::CHECK_ADOPT]; $order = 'created_at desc'; $select = '*'; $with = []; //总的收益 //我代理的所有区域 $city_region=[];$district_region=[];$province_region=[]; $userAgentList = HappinessAgent::find()->where(['user_id' => $this->user_id, 'status'=>StatusEnum::ENABLED,'mall_id'=>$this->mall_id])->select('id,role,city_id,district_id,province_id') ->asArray()->all(); if(!empty($userAgentList)) { foreach ($userAgentList as $userAgent) { //城市代理 if ($userAgent['role'] == HappinessEnum::AGENT_CITY) { $city_region[] = $userAgent['city_id']; } //区域代理 if ($userAgent['role'] == HappinessEnum::AGENT_DISTRICT) { $district_region[] = $userAgent['district_id']; } //省代理 if ($userAgent['role'] == HappinessEnum::AGENT_PROVINCE) { $province_region[] = $userAgent['province_id']; } } } $where[] = ['or', ['in', 'city_id', $city_region], ['in', 'district_id', $district_region],['in', 'province_id', $province_region]]; $total = HappinessCommissionLog::find()->where([ 'status' => StatusEnum::ENABLED, 'user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'commission_status' => HappinessRewardEnum::SEND, 'commission_type' => HappinessRewardEnum::COMMISSION_TYPE_AGENT]) ->sum('money')??0; $params = array('where' => $where, 'with' => $with, 'order' => $order, 'select' => $select, 'limit' => $get['limit']);; $list = HappinessStore::listPage($params, false, true); foreach ($list['list'] as &$v) { //该小店带来的收益 $v['commission_money'] = HappinessCommissionLog::find()->where(['store_id' => $v['id'], 'status' => StatusEnum::ENABLED, 'user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'commission_status' => HappinessRewardEnum::SEND, 'commission_type' => HappinessRewardEnum::COMMISSION_TYPE_AGENT]) ->sum('money')??0; } $list['total'] = $total; return $this->success('获取成功', $list); } } // 获取服务中心数据 public function actionGetServiceData() { if (Yii::$app->request->isGet) { $get = Yii::$app->request->get(); // 检查提交的参数 $type = $get['type']; $res = Yii::$app->services->validate->validate($get, [ [['type'], 'string'], ]); $userWallet = UserWallet::find()->where(['user_id' => $this->user_id, 'mall_id' => $this->mall_id])->asArray()->one(); $data = []; if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $basic = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic'); $agent_list = !empty($basic['agent_list'])? json_decode($basic['agent_list'],true):[];//身份收益设置 $agent_list = array_column($agent_list, null, 'level'); $business_list = !empty($basic['business_list'])? json_decode($basic['business_list'],true):[];//身份收益设置 $business_list = array_column($business_list, null, 'level'); $city_name='市代'; $province_name='省代'; $district_name='区代'; foreach ($agent_list as $agent) { if($agent['role']==HappinessEnum::AGENT_PROVINCE) { $province_name=$agent['name']; } if($agent['role']==HappinessEnum::AGENT_CITY) { $city_name=$agent['name']; } if($agent['role']==HappinessEnum::AGENT_DISTRICT) { $district_name=$agent['name']; } } //代理(区域代理) if ($type == 'agent') { //判断是不是代理 $userAgentList = HappinessAgent::find()->where(['user_id' => $this->user_id, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id])->asArray()->all(); if (!empty($userAgentList)) { $agent_name = ''; $city_region = []; $district_region = []; $province_region=[]; $city_num = 0; $district_num = 0; $province_num=0; foreach ($userAgentList as $userAgent) { //城市代理 if ($userAgent['role'] == HappinessEnum::AGENT_CITY) { $city_num++; $city_region[] = $userAgent['city_id']; } //区域代理 if ($userAgent['role'] == HappinessEnum::AGENT_DISTRICT) { $district_num++; $district_region[] = $userAgent['district_id']; } if ($userAgent['role'] == HappinessEnum::AGENT_PROVINCE) { $province_region[]=$userAgent['province_id']; $province_num++; } } //确认名称 $agent_name_list=[]; foreach ($userAgentList as $userAgent) { $agent_name_list[] = isset($agent_list[$userAgent['level']])?$agent_list[$userAgent['level']]['name']:''; } //去重复 if(count($agent_name_list)>1) { $agent_name_list = array_unique($agent_name_list); $agent_name = implode('|',$agent_name_list); } $data = [ 'role_name' => $agent_name, 'role_arr' => $agent_name_list, 'district_number' => $district_num, 'district_name' => $district_name, 'city_number' => $city_num, 'city_name' => $city_name, 'province_name' => $province_name, 'province_number' => $province_num, 'store_number' => HappinessStore::find()->where([ 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id, 'check_status' => HappinessEnum::CHECK_ADOPT])->andWhere([ 'or', ['in', 'city_id', $city_region], ['in', 'district_id', $district_region], ['in', 'province_id', $province_region] ])->count() ?? 0, ]; } else { return $this->error('您还不是代理'); } } //推广(业务员) if ($type == 'command') { //判断是不是业务员 $userBusiness = HappinessBusiness::findOne(['user_id' => $this->user_id, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id]); if (!empty($userBusiness)) { $data = [ 'role_name' =>isset($business_list[$userBusiness['level']])?$business_list[$userBusiness['level']]['name']:'' , 'district_number' => HappinessAgent::find()->where(['parent_id' => $this->user_id, 'role' => 3, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id])->count () ?? 0, 'store_number' => HappinessStore::find()->where(['business_id' => $this->user_id, 'check_status' => HappinessEnum::CHECK_ADOPT, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id])->count() ?? 0, 'city_number' => HappinessAgent::find()->where(['parent_id' => $this->user_id, 'role' => 2, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id])->count () ?? 0, 'province_number' => HappinessAgent::find()->where(['parent_id' => $this->user_id, 'role' => 1, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id])->count () ?? 0, 'city_name'=>$city_name, 'province_name'=>$province_name, 'district_name'=>$district_name, ]; } else { return $this->error('您还不是推广员'); } } $data['total_commission'] = empty($userWallet) ? [] : $userWallet['commission']; $this->success('获取成功', $data); } } }