| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963 |
- <?php
- namespace addons\Agent\backend\controllers;
- use addons\Agent\backend\services\AgentSupplierCommissionService;
- use addons\Agent\backend\services\OrderService;
- use addons\Agent\common\enums\AgentBehaviorLogsEnum;
- use addons\Agent\common\enums\AgentEnum;
- use addons\Agent\common\event\AgentAddEvent;
- use addons\Agent\common\event\AgentDeleteEvent;
- use addons\Agent\common\event\AgentUpgradeAfterEvent;
- use addons\Agent\common\models\AgentCommissionBasisLog;
- use addons\Agent\common\models\AgentLevel;
- use addons\Agent\common\models\AgentSupplierCommissionSyncLog;
- use addons\Agent\common\models\AgentSupplierOrder;
- use addons\Agent\common\services\AgentBehaviorLogsService;
- use addons\Agent\common\services\LogsServices;
- use common\enums\MemberLabelEnum;
- use common\enums\RabbitMqEnum;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\helpers\csv\CsvExportHelper;
- use common\logic\memberLabel\MemberLabelLogic;
- use common\models\common\CommissionLog;
- use Yii;
- use Exception;
- use addons\Agent\common\models\Agent;
- use addons\Agent\common\models\AgentImportLevelFailLog;
- use addons\Agent\common\models\AgentImportLevelLog;
- use common\enums\AddonsEnum;
- use common\models\user\User;
- use common\enums\DownloadEnum;
- use common\models\common\Region;
- use common\models\mall\MallAddons;
- use common\models\order\Order;
- use common\components\CsvExport;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\Agent\backend\controllers
- */
- class DefaultController extends BaseController
- {
- public $from_type = ['Agent'];
- public $enableCsrfValidation = false;
- /**
- * @desc:首页
- * @Author: hua
- * @Date: 2021/10/22
- * @Time: 9:14
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|string|void
- * @throws Exception
- */
- public function actionIndex()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $get = \Yii::$app->request->get();
- $get['mall_id'] = $this->mall_id;
- $params = [];
- $user_id_arr = [];
- if (isset($get['level']) && !empty($get['level'])) $params['where'][] = ['=', 'level', $get['level']];
- $is_search = 0;
- if (!empty($get['user_id'])){
- $is_search = 1;
- $user_id_arr[]=$get['user_id'];
- }
- if (!empty($get['keyword'])) {
- $is_search = 1;
- $user_list = User::lists(['where'=>[['mall_id'=>$this->mall_id],[
- 'or',
- ['like', 'username', $get['keyword']],
- ['like', 'nickname', $get['keyword']],
- ['like', 'mobile', $get['keyword']]
- ]],'select'=>'id']);
- $user_ids = array_column($user_list,'id');
- if(!empty($user_id_arr)){
- $user_id_arr = array_intersect($user_id_arr,$user_ids);
- }else{
- $user_id_arr = $user_ids;
- }
- }
- if($is_search)$params['where'][] = ['in', 'user_id', $user_id_arr];
- $params['where'][] = ['=', 'mall_id',$this->mall_id];
- $params['where'][] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- $params['limit'] = 10;
- $params['order'] = 'id DESC';
- $pageData = Agent::listPage($params, false);
- if ($pageData === false) return $this->error(Agent::getStaticError());
- if(!empty($pageData['list'])){
- $user_id_arr = array_column($pageData['list'],'user_id');
- $user_list = $user_list = User::lists(['where'=>[['id'=>$user_id_arr]],'select'=>'id,nickname,avatar_url,mobile,parent_id']);
- $user_arr = $parent_arr = [];
- $parent_id_arr = [];
- foreach ($user_list as $item){
- $parent_id_arr[]=$item['parent_id'];
- $user_arr[$item['id']] = $item;
- }
- $user_parent_list = $user_list = User::lists(['where'=>[['id'=>$parent_id_arr]],'select'=>'id,nickname,avatar_url,mobile']);
- foreach ($user_parent_list as $item){
- $parent_arr[$item['id']] = $item;
- }
- $province_ids = array_column($pageData['list'], 'province_id') ?? [];
- $region_list = Region::lists([
- 'where' => [
- ['id' => $province_ids]
- ],
- 'select' => 'id, name, level',
- 'index' => 'id'
- ]);
- foreach ($pageData['list'] as &$v) {
- $v['nickname'] = $v['avatar_url'] = $v['parent_id'] = $v['parent_name'] = $v['mobile'] = '';
- if (isset($user_arr[$v['user_id']])) {
- $v['mobile'] = $user_arr[$v['user_id']]['mobile'];
- $v['nickname'] = $user_arr[$v['user_id']]['nickname'];
- $v['avatar_url'] = $user_arr[$v['user_id']]['avatar_url'];
- $v['parent_id'] = $user_arr[$v['user_id']]['parent_id'];
- if (isset($parent_arr[$v['parent_id']])) {
- $v['parent_name'] = $parent_arr[$v['parent_id']]['nickname'];
- }
- $v['provinceinfo'] = $region_list[$v['province_id']] ?? [];
- }
- }
- }
- $args = [];
- $args['where'] = [['mall_id' => $this->mall_id], ['<>', 'status', StatusEnum::DELETE]];
- $args['select'] = 'id,level,name';
- $args['order'] = 'level asc';
- $level_list = AgentLevel::lists($args, true);
- if (empty($level_list)) $level_list = [['id' => 0, 'level' => 0, 'name' => '默认等级']];
- $pageData['agentLevelList'] = $level_list;
- $pageData['export_list'] = Agent::exportFieldsList();
- $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AgentEnum::ADDONS_NAME, 'basic');
- $pageData['is_enable_province'] = $configs['is_enable_province'] ?? 0;
- return $this->success('操作成功', $pageData);
- }
- }
- // if (\Yii::$app->request->post('flag') == 'EXPORT') {
- // $post = \Yii::$app->request->post();
- // $post['mall_id'] = $this->mall_id;
- // $filename = '经销商列表-' . date('YmdHis') . '_' . rand(10000, 99999);
- // $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_AGENT, Agent::class, 'exportHandle', $post);
- // if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
- // return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
- // }
- return $this->render('index', []);
- }
- public function actionExport()
- {
- if (\Yii::$app->request->post('flag') == 'EXPORT') {
- $post = \Yii::$app->request->post();
- $post['mall_id'] = $this->mall_id;
- $filename = '经销商列表-' . date('YmdHis') . '_' . rand(10000, 99999);
- $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_AGENT, Agent::class, 'exportHandle', $post);
- if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
- return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
- }
- }
- /**
- * @desc:修改经销商等级
- * @Author: hua
- * @Date: 2021/10/23
- * @Time: 17:33
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionLevelChange()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id', 'user_id', 'level'], 'required'],
- [['user_id', 'level'], 'integer'],
- [['provinceinfo'], 'safe']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $model = Agent::findOne(['mall_id' => $this->mall_id, 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- $params['mall_id'] = $this->mall_id;
- $provinceinfo = $params['provinceinfo'];
- unset($params['provinceinfo']);
- $params['province_id'] = $provinceinfo['id'] ?? 0;
- $params['admin_id'] = $this->admin->id;
- $res = Agent::setData($params);
- if ($res === false) return $this->error(AgentLevel::getStaticError());
- //会员标签
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
- 'user_id'=>$params['user_id'],
- 'mall_id'=>$params['mall_id'],
- 'cate_ids'=>[MemberLabelEnum::AGENT_LEVEL]
- ], MemberLabelLogic::class, 'executeSyncCateId2Or3');
- $event = new AgentUpgradeAfterEvent($this->mall_id);
- Yii::$app->services->events->dispatch(
- $event,
- [
- 'user_id' => $params['user_id'],
- 'mall_id' => $this->mall_id,
- 'after_level' => $params['level'],
- 'before_level' => $model->level,
- 'is_admin' => 1,
- 'operator_user_id' => $this->admin->id,
- 'agent_id' => $params['id']
- ],
- $event->listeners
- );
- // 记录更新区域行为
- if ($params['province_id'] != $model->province_id) {
- AgentBehaviorLogsService::dispatchUserBehaviorLogsEvent(
- $event->mall_id ?? 0,
- [
- 'user_id' => $params['user_id'],
- 'mall_id' => $this->mall_id,
- 'after_province_id' => $params['province_id'],
- 'before_province_id' => $model->province_id,
- 'is_admin' => 1,
- 'operator_user_id' => $this->admin->id,
- 'agent_id' => $params['id'],
- 'behavior' => AgentBehaviorLogsEnum::BEHAVIOR_MODIFY_AGENT_PROVINCE
- ]
- );
- }
- return $this->success('操作成功');
- }
- }
- return $this->error();
- }
- /**
- * @desc:修改备注
- * @Author: hua
- * @Date: 2021/10/23
- * @Time: 17:29
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionRemarksEdit()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id', 'user_id'], 'required'],
- [['user_id'], 'integer'],
- [['remarks'], 'safe']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $res = Agent::setData($params);
- if ($res === false) return $this->error(Agent::getStaticError());
- return $this->success('操作成功');
- }
- }
- return $this->error();
- }
- /**
- * @desc:搜索用户
- * @Author: hua
- * @Date: 2021/10/23
- * @Time: 17:55
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionSearchUser()
- {
- if (\Yii::$app->request->isAjax) {
- $keyword = \Yii::$app->request->get('keyword', '');
- $params = ['keyword' => $keyword, 'mall_id' => $this->mall_id];
- $query = User::find()->alias('u')
- ->where(['u.status' => StatusEnum::ENABLED, 'u.mall_id' => $params['mall_id']])
- ->andWhere(['u.is_inviter' => 1]);
- if (isset($params['keyword']) && !empty($params['keyword'])) {
- $query->andWhere(['or', ['like', 'nickname', $params['keyword']], ['like', 'mobile', $params['keyword']], ['like', 'id', $params['keyword']]]);
- }
- $list = $query->limit(20)->select('u.id,u.nickname,u.mobile,u.avatar_url')->asArray()->all();
- foreach ($list as &$item) {
- if (empty($item['nickname'])) {
- $item['nickname'] = $item['mobile'] . "(id:{$item['id']})";
- }
- }
- $list['list'] = $list;
- return $this->success('操作成功', $list);
- }
- return $this->error();
- }
- /**
- * @desc:保存经销商
- * @Author: hua
- * @Date: 2021/10/23
- * @Time: 17:39
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionEdit()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $postData = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($postData, [
- [['user_id', 'level'], 'required'],
- [['user_id', 'level'], 'integer']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $wheres = [
- 'user_id' => $postData['user_id'],
- 'mall_id' => $this->mall_id,
- 'status' => StatusEnum::ENABLED,
- ];
- $model = Agent::findOne($wheres);
- if (!empty($model)) return $this->error('该会员已经是经销商,不能重复添加');
- $postData['mall_id'] = $this->mall_id;
- $postData['status'] = StatusEnum::ENABLED;
- $postData['admin_id'] = $this->admin->id;
- $res = Agent::setData($postData);
- if ($res === false) return $this->error(Agent::getStaticError());
- //会员标签
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
- 'user_id'=>$postData['user_id'],
- 'mall_id'=>$postData['mall_id'],
- 'cate_ids'=>[MemberLabelEnum::AGENT_LEVEL]
- ], MemberLabelLogic::class, 'executeSyncCateId2Or3');
- $event = new AgentAddEvent($this->mall_id);
- Yii::$app->services->events->dispatch(
- $event,
- [
- 'user_id' => $res->user_id,
- 'mall_id' => $this->mall_id,
- 'is_admin' => 1,
- 'operator_user_id' => $this->admin->id,
- 'agent_id' => $res->id,
- 'level' => $res->level
- ],
- $event->listeners
- );
- return $this->success('操作成功');
- }
- }
- return $this->error();
- }
- /**
- * @desc:删除经销商
- * @Author: hua
- * @Date: 2021/10/23
- * @Time: 17:24
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionDelete()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- [['id'], 'integer']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $params['status'] = StatusEnum::DELETE;
- $res = Agent::setData($params);
- if ($res === false) return $this->error(Agent::getStaticError());
- $event = new AgentDeleteEvent($this->mall_id);
- Yii::$app->services->events->dispatch(
- $event,
- [
- 'user_id' => $res->user_id,
- 'mall_id' => $this->mall_id,
- 'is_admin' => 1,
- 'operator_user_id' => $this->admin->id,
- 'agent_id' => $params['id']
- ],
- $event->listeners
- );
- return $this->success('操作成功');
- }
- }
- return $this->error();
- }
- /**
- * @desc:批量修改等级
- * @Author: hua
- * @Date: 2021/10/23
- * @Time: 17:43
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionBatchLevel()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['batch_ids', 'level'], 'required'],
- [['level'], 'integer']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $params['status'] = StatusEnum::ENABLED;
- $agentBeforeLevelList = Agent::find()
- ->where(['id' => $params['batch_ids']])
- ->select('id, level, user_id')
- ->indexBy('id')
- ->asArray()
- ->all();
- $res = Agent::batchLevel($params);
- if ($res === false) return $this->error(Agent::getStaticError());
- foreach ($params['batch_ids'] as $batchId) {
- if (!isset($agentBeforeLevelList[$batchId])) {
- continue;
- }
- $event = new AgentUpgradeAfterEvent($this->mall_id);
- Yii::$app->services->events->dispatch(
- $event,
- [
- 'user_id' => $agentBeforeLevelList[$batchId]['user_id'],
- 'mall_id' => $this->mall_id,
- 'after_level' => $params['level'],
- 'before_level' => $agentBeforeLevelList[$batchId]['level'],
- 'is_admin' => 1,
- 'operator_user_id' => $this->admin->id,
- 'agent_id' => $batchId
- ],
- $event->listeners
- );
- }
- return $this->success('操作成功');
- }
- }
- return $this->error();
- }
- /**
- * 经销商配置
- */
- public function actionSetting()
- {
- if (\Yii::$app->request->isAjax) {
- return $this->error();
- }
- return $this->render('setting');
- }
- /**
- * @desc:已经启用的等级
- * @Author: hua
- * @Date: 2021/10/23
- * @Time: 17:31
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionEnableList()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $level = \Yii::$app->request->get('level');
- $where = [['mall_id' => $this->mall_id], ['=', 'status', StatusEnum::ENABLED]];
- if (!empty($level)) $where[] = ['level' => $level];
- $args['where'] = $where;
- $args['select'] = 'id,level,name';
- $args['order'] = 'level asc';
- $list['list'] = AgentLevel::lists($args, true);
- return $this->success('操作成功', $list);
- }
- }
- return $this->error();
- }
- public function actionGetProvinces()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $list = Region::lists([
- 'where' => [
- ['level' => 1]
- ],
- 'select' => 'id,name,lng,lat',
- ]);
- return $this->success('操作成功', $list);
- }
- }
- }
- public function actionCommissionBasicLog()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $get = \Yii::$app->request->get();
- $get['mall_id'] = $this->mall_id;
- $params = [];
- if (!empty($get['source_table_id'])) {
- $params['where'][] = ['source_table_id'=> $get['source_table_id']];
- }
- $params['where'][] = ['=', 'mall_id',$this->mall_id];
- $params['where'][] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- $params['order'] = 'id desc';
- $params['limit'] = 10;
- $pageData = AgentCommissionBasisLog::listPage($params, false);
- if ($pageData === false) return $this->error(AgentCommissionBasisLog::getStaticError());
- if(!empty($pageData['list'])){
- foreach ($pageData['list'] as &$item){
- if($item['basic_config']) $item['basic_config'] = json_encode(json_decode($item['basic_config'],true),JSON_UNESCAPED_UNICODE);
- if($item['level_config']) $item['level_config'] = json_encode(json_decode($item['level_config'],true),JSON_UNESCAPED_UNICODE);
- if($item['goods_config']) $item['goods_config'] = json_encode(json_decode($item['goods_config'],true),JSON_UNESCAPED_UNICODE);
- }
- }
- return $this->success('操作成功', $pageData);
- }
- }
- return $this->render('commission-basic-log', []);
- }
- public function actionGoodsModeSetting()
- {
- $request = Yii::$app->request;
- if ($request->isAjax && $request->isPost) {
- $is_install_regular_bonus = (int)MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_REGULAR_BONUS);
- return $this->success('操作成功', compact('is_install_regular_bonus'));
- }
- }
- /**
- * 供应商库存
- * @Author:hua
- * @Date:2024-02-22 17:53
- * @Copyright:copyright(c)2024 广东七件事集团
- * @return mixed|string|void
- */
- public function actionSupplierStock(){
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $get = \Yii::$app->request->get();
- $get['mall_id'] = $this->mall_id;
- $params = [];
- if (isset($get['level']) && !empty($get['level'])) $params['where'][] = ['=', 'level', $get['level']];
- if (!empty($get['keyword'])) {
- $user_list = User::lists(['where'=>[['mall_id'=>$this->mall_id],[
- 'or',
- ['=', 'id', $get['keyword']],
- ['like', 'username', $get['keyword']],
- ['like', 'nickname', $get['keyword']],
- ['like', 'mobile', $get['keyword']]
- ]],'select'=>'id']);
- $user_ids = array_column($user_list,'id');
- $params['where'][] = ['in', 'user_id', $user_ids];
- }
- $params['where'][] = ['=', 'mall_id',$this->mall_id];
- $params['where'][] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- $params['limit'] = 10;
- $params['order'] = 'id DESC';
- $pageData = Agent::listPage($params, false);
- if ($pageData === false) return $this->error(Agent::getStaticError());
- if(!empty($pageData['list'])){
- $user_id_arr = array_column($pageData['list'],'user_id');
- $user_list = User::lists(['where'=>[['id'=>$user_id_arr]],'select'=>'id,nickname,avatar_url,mobile,parent_id']);
- $user_arr = [];
- foreach ($user_list as $item){
- $user_arr[$item['id']] = $item;
- }
- $dsn = Yii::$app->db->dsn;
- if(strpos($dsn,'port=8066')===false){
- $commission_log_list = CommissionLog::lists([
- 'where'=>[
- ['user_id'=>$user_id_arr],
- ['from_type'=>'Agent']
- ],
- 'select'=>'sum(change_num) as change_num,user_id',
- 'group'=>'user_id',
- 'index'=>'user_id'
- ]);
- $commission_log_count_list = CommissionLog::lists([
- 'where'=>[
- ['user_id'=>$user_id_arr],
- ['from_type'=>'Agent']
- ],
- 'select'=>'count(*) counts,user_id',
- 'group'=>'user_id',
- 'index'=>'user_id'
- ]);
- }
- foreach ($pageData['list'] as &$v) {
- $v['nickname'] = $v['avatar_url'] = $v['mobile'] = '';
- if (isset($user_arr[$v['user_id']])) {
- $v['mobile'] = $user_arr[$v['user_id']]['mobile'];
- $v['nickname'] = $user_arr[$v['user_id']]['nickname'];
- $v['avatar_url'] = $user_arr[$v['user_id']]['avatar_url'];
- }
- $v['change_num'] = $v['counts'] = 0;
- if(strpos($dsn,'port=8066')===false){
- if (isset($commission_log_list[$v['user_id']])) {
- $v['change_num'] = $commission_log_list[$v['user_id']]['change_num'];
- }
- if (isset($commission_log_count_list[$v['user_id']])) {
- $v['counts'] = $commission_log_count_list[$v['user_id']]['counts'];
- }
- }else{
- $change_num = CommissionLog::find()->where(['user_id'=>$v['user_id'],'from_type'=>'Agent'])->sum('change_num');
- if(!empty($change_num)) $v['change_num'] = $change_num;
- $counts = CommissionLog::find()->where(['user_id'=>$v['user_id'],'from_type'=>'Agent'])->count();
- if(!empty($counts)) $v['counts'] = $counts;
- }
- }
- }
- $args = [];
- $args['where'] = [['mall_id' => $this->mall_id], ['<>', 'status', StatusEnum::DELETE]];
- $args['select'] = 'id,level,name';
- $args['order'] = 'level asc';
- $level_list = AgentLevel::lists($args, true);
- if (empty($level_list)) $level_list = [['id' => 0, 'level' => 0, 'name' => '默认等级']];
- $pageData['agentLevelList'] = $level_list;
- $pageData['export_list'] = Agent::getSupplierStockExportFieldsList();
- return $this->success('操作成功', $pageData);
- }
- if (\Yii::$app->request->post('flag') == 'EXPORT') {
- $post = \Yii::$app->request->post();
- $post['mall_id'] = $this->mall_id;
- $filename = '供应商库存-' . date('YmdHis') . '_' . rand(10000, 99999);
- $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_AGENT, Agent::class, 'exportHandleSupplierStock', $post);
- if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
- return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
- }
- }
- return $this->render('supplier-stock', []);
- }
- /**
- * 供应商订单
- * @Author:hua
- * @Date:2024-02-22 17:53
- * @Copyright:copyright(c)2024 广东七件事集团
- * @return mixed|string|void
- */
- public function actionSupplierOrder(){
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $get = \Yii::$app->request->get();
- $get['mall_id'] = $this->mall_id;
- $params = ['where' => [['status' => StatusEnum::ENABLED]]];
- if (!empty($get['order_no'])){
- $params['where'][] = ['=', 'order_no', trim($get['order_no'])];
- }
- if (!empty($get['keyword'])) {
- $user_list = User::lists(['where'=>[['mall_id'=>$this->mall_id],[
- 'or',
- ['=', 'id', $get['keyword']],
- ['like', 'username', $get['keyword']],
- ['like', 'nickname', $get['keyword']],
- ['like', 'mobile', $get['keyword']]
- ]],'select'=>'id']);
- $user_ids = array_column($user_list,'id');
- $params['where'][] = ['in', 'buy_user_id', $user_ids];
- }
- if (!empty($get['seller_keyword'])) {
- $user_list = User::lists(['where'=>[['mall_id'=>$this->mall_id],[
- 'or',
- ['=', 'id', $get['seller_keyword']],
- ['like', 'username', $get['seller_keyword']],
- ['like', 'nickname', $get['seller_keyword']],
- ['like', 'mobile', $get['seller_keyword']]
- ]],'select'=>'id']);
- $user_ids = array_column($user_list,'id');
- $params['where'][] = ['in', 'seller_user_id', $user_ids];
- }
- $params['where'][] = ['=', 'mall_id',$this->mall_id];
- $params['limit'] = 10;
- $params['order'] = 'id DESC';
- $pageData = AgentSupplierOrder::listPage($params, false);
- if ($pageData === false) return $this->error(Agent::getStaticError());
- if(!empty($pageData['list'])){
- $buy_user_ids = array_column($pageData['list'],'buy_user_id');
- $seller_user_ids = array_column($pageData['list'],'seller_user_id');
- $order_no_arr = array_column($pageData['list'],'order_no');
- $user_id_arr = array_merge($buy_user_ids,$seller_user_ids);
- $user_arr = User::lists(['where'=>[['id'=>$user_id_arr]],'index'=>'id','select'=>'id,nickname,avatar_url,mobile,parent_id']);
- $order_arr = Order::lists(['where'=>[['order_no'=>$order_no_arr]],'index'=>'order_no','select'=>'order_no,order_status']);
- foreach ($pageData['list'] as &$v) {
- $v['nickname'] = $v['avatar_url'] = $v['mobile'] = '';
- $v['seller_nickname'] = $v['seller_avatar_url'] = $v['seller_mobile'] = '';
- $v['order_status'] = 0;
- if (isset($user_arr[$v['buy_user_id']])) {
- $v['mobile'] = $user_arr[$v['buy_user_id']]['mobile'];
- $v['nickname'] = $user_arr[$v['buy_user_id']]['nickname'];
- $v['avatar_url'] = $user_arr[$v['buy_user_id']]['avatar_url'];
- }
- if (isset($order_arr[$v['order_no']])) {
- $v['order_status'] = $order_arr[$v['order_no']]['order_status'];
- }
- if (isset($user_arr[$v['seller_user_id']])) {
- $v['seller_mobile'] = $user_arr[$v['seller_user_id']]['mobile'];
- $v['seller_nickname'] = $user_arr[$v['seller_user_id']]['nickname'];
- $v['seller_avatar_url'] = $user_arr[$v['seller_user_id']]['avatar_url'];
- }
- }
- }
- $args = [];
- $args['where'] = [['mall_id' => $this->mall_id], ['<>', 'status', StatusEnum::DELETE]];
- $args['select'] = 'id,level,name';
- $args['order'] = 'level asc';
- $level_list = AgentLevel::lists($args, true);
- if (empty($level_list)) $level_list = [['id' => 0, 'level' => 0, 'name' => '默认等级']];
- $pageData['agentLevelList'] = $level_list;
- $pageData['export_list'] = AgentSupplierOrder::exportFieldsList();
- $cache_key = RedisKeyEnum::suffix(AgentSupplierOrder::OLD_DATA_GENERATE_, $this->mall_id);
- $cache = \Yii::$app->cache;
- $cache_data = $cache->get($cache_key);
- $pageData['is_generate_old_data'] = $cache_data ?? 0;
- return $this->success('操作成功', $pageData);
- }
- if (\Yii::$app->request->post('flag') == 'EXPORT') {
- $post = \Yii::$app->request->post();
- $post['mall_id'] = $this->mall_id;
- $filename = '供应商订单-' . date('YmdHis') . '_' . rand(10000, 99999);
- $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_AGENT, AgentSupplierOrder::class, 'exportHandle', $post);
- if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
- return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
- }
- }
- return $this->render('supplier-order', []);
- }
- /**
- * 生成旧数据
- * @Author:hua
- * @Date:2024-02-23 15:44
- * @Copyright:copyright(c)2024 广东七件事集团
- * @return mixed|void
- */
- public function actionOldDataGenerate(){
- $data = ['mall_id'=>$this->mall_id];
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $data, AgentSupplierOrder::class, 'oldDataGenerate');
- return $this->success('操作成功', []);
- }
- /**
- * 导入日志记录
- * @return mixed|void
- */
- public function actionGetImportLevelLog()
- {
- $params = \Yii::$app->request->get();
- $params['where'] = [
- ['mall_id'=>$this->mall_id]
- ];
- $params['order'] = 'id desc';
- $data = AgentImportLevelLog::listPage($params, false, true);
- if (!empty($data['list'])) {
- foreach ($data['list'] as &$v) {
- $v['url'] = '';
- if ($v['fail_counts']) $v['url'] = \Yii::$app->urlManager->createUrl(['agent/default/export-level-fail', 'log_id' => $v['id']]);
- }
- }
- return $this->success('操作成功', $data);
- }
- /**
- * 导出失败日志
- */
- public function actionExportLevelFail()
- {
- AgentImportLevelFailLog::exportFailLog(\Yii::$app->request->get());
- }
- /**
- * 会员批量导入模板下载
- */
- public function actionImportLevelModel()
- {
- $csv = new CsvExport();
- $file_name = '批量导入经销商等级模板-' . date('YmdHis', time());
- $head_list = array_column(Agent::$head_list, 'field_name');
- return $csv->export([], $head_list, $file_name);
- }
- /**
- * 批量导入等级
- * @return mixed|string|void
- */
- public function actionImportLevel()
- {
- set_time_limit(0);
- if (\Yii::$app->request->isAjax) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['current_num'], 'required'],
- [['file'], 'file', 'extensions' => ['csv']],
- [['file_path'], 'string'],
- [['sync'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- if(!empty($params['sync'])){
- $res = Agent::importLevelSync($params);
- }else{
- $res = Agent::importLevel($params);
- }
- if (empty($res['fail_counts'])) return $this->success('操作成功', $res);
- if (!empty($res['fail_counts'])) return $this->error('导入失败'.$res['fail_counts'].'条');
- return $this->error(Agent::getStaticError());
- } else {
- return $this->render($this->action->id);
- }
- }
- /**
- * 供应商收入列表
- *
- * @return string|void
- */
- public function actionSupplierCommission()
- {
- $service = new AgentSupplierCommissionService([]);
- $service->mall_id = $this->mall_id;
- if (!$this->request->isAjax) {
- return $this->render($this->action->id, ['allowDeliveryTask' => (int)$service->allowDeliveryTask()]);
- }
- $this->success('success', $service->getList($this->request->get(), false));
- }
- /**
- * 供应商收入导出
- *
- * @return void
- */
- public function actionSupplierCommissionExport()
- {
- $filename = '供应商收入数据-' . date('YmdHis') . '_' . random_int(10000, 99999);
- $data = Yii::$app->request->post('params');
- $data['mall_id'] = $this->mall_id;
- $res = CsvExportHelper::exportDownLoadCenter(
- $this->mall_id,
- $filename,
- DownloadEnum::TYPE_AGENT_SUPPLIER_COMMISSION,
- AgentSupplierCommissionService::class,
- 'export',
- $data
- );
- if ($res === false) {
- return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
- }
- return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
- }
- /**
- * @return mixed|null
- * @throws Exception
- */
- public function actionSyncOldData()
- {
- // 检查是否允许投递
- $service = new AgentSupplierCommissionService([]);
- $service->mall_id = $this->mall_id;
- if (!$service->allowDeliveryTask()) {
- return $this->error('当前已同步数据');
- }
- AgentSupplierCommissionSyncLog::setData([
- 'mall_id' => $this->mall_id,
- 'status' => StatusEnum::DISABLED
- ]);
- $data = ['mall_id'=>$this->mall_id];
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_LONG_DURATION, RabbitMqEnum::LONG_DURATION_QUEUE, $data, AgentSupplierCommissionService::class, 'syncOldData');
- return $this->success();
- }
- /**
- * 经销商日志
- *
- * @return mixed|string|null
- */
- public function actionLog()
- {
- if (!$this->request->isAjax) {
- foreach (AgentBehaviorLogsEnum::OPERATOR_USER_TYPE_MAP as $value => $label) {
- $operatorUserTypeOptions[] = compact('label', 'value');
- }
- foreach (AgentBehaviorLogsEnum::BEHAVIOR_MAP as $value => $label) {
- $behaviorOptions[] = compact('label', 'value');
- }
- $operatorUserTypeOptions = json_encode($operatorUserTypeOptions);
- $behaviorOptions = json_encode($behaviorOptions);
- return $this->render($this->action->id, compact('operatorUserTypeOptions', 'behaviorOptions'));
- }
- $service = new LogsServices(['mall_id' => $this->mall_id]);
- return $this->success('success', $service->getAgentLogs($this->request->get()));
- }
- /**
- * 获取订单details列表
- *
- * @return void
- */
- public function actionGetOrderDetails()
- {
- $this->success(
- 'success',
- (new OrderService(['mall_id' => $this->mall_id]))->getOrderDetails($this->request->get())
- );
- }
- /**
- * 重新分配佣金
- *
- * @return void
- */
- public function actionRedistributeCommissions()
- {
- if (!$this->request->isPost) {
- $this->error('请求类型错误');
- return;
- }
- $post = $this->request->post();
- $res = Yii::$app->services->validate->validate($post, [
- [['order_details_ids'], 'required'],
- ['order_details_ids', 'each', 'rule' => ['integer']],
- ]);
- if ($res === false) {
- return $this->error(Yii::$app->services->validate->getErrorMessage());
- }
-
- $service = new AgentSupplierCommissionService(['mall_id' => $this->mall_id]);
- if ($service->redistributeCommissions($post)) {
- $this->success('success');
- return;
- }
- $this->error($service->getError());
- }
- }
|