| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- <?php
- namespace addons\Happiness\api\modules\v1\controllers;
- use addons\Happiness\common\enums\HappinessEnum;
- use addons\Happiness\common\enums\HappinessRewardEnum;
- use addons\Happiness\common\models\HappinessAgent;
- use addons\Happiness\common\models\HappinessBusiness;
- use addons\Happiness\common\models\HappinessCashierOrderDetail;
- use addons\Happiness\common\models\HappinessCommissionLog;
- use addons\Happiness\common\models\HappinessStore;
- use common\enums\StatusEnum;
- use common\models\common\Region;
- use common\models\order\OrderDetail;
- use common\models\user\UserWallet;
- use Yii;
- use api\controllers\OnAuthController;
- class CommissionController extends OnAuthController
- {
- public $modelClass = '';
- public function actionLog()
- {
- if (Yii::$app->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);
- }
- }
- }
|