DefaultController.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. <?php
  2. namespace addons\Agent\backend\controllers;
  3. use addons\Agent\backend\services\AgentSupplierCommissionService;
  4. use addons\Agent\backend\services\OrderService;
  5. use addons\Agent\common\enums\AgentBehaviorLogsEnum;
  6. use addons\Agent\common\enums\AgentEnum;
  7. use addons\Agent\common\event\AgentAddEvent;
  8. use addons\Agent\common\event\AgentDeleteEvent;
  9. use addons\Agent\common\event\AgentUpgradeAfterEvent;
  10. use addons\Agent\common\models\AgentCommissionBasisLog;
  11. use addons\Agent\common\models\AgentLevel;
  12. use addons\Agent\common\models\AgentSupplierCommissionSyncLog;
  13. use addons\Agent\common\models\AgentSupplierOrder;
  14. use addons\Agent\common\services\AgentBehaviorLogsService;
  15. use addons\Agent\common\services\LogsServices;
  16. use common\enums\MemberLabelEnum;
  17. use common\enums\RabbitMqEnum;
  18. use common\enums\RedisKeyEnum;
  19. use common\enums\StatusEnum;
  20. use common\helpers\csv\CsvExportHelper;
  21. use common\logic\memberLabel\MemberLabelLogic;
  22. use common\models\common\CommissionLog;
  23. use Yii;
  24. use Exception;
  25. use addons\Agent\common\models\Agent;
  26. use addons\Agent\common\models\AgentImportLevelFailLog;
  27. use addons\Agent\common\models\AgentImportLevelLog;
  28. use common\enums\AddonsEnum;
  29. use common\models\user\User;
  30. use common\enums\DownloadEnum;
  31. use common\models\common\Region;
  32. use common\models\mall\MallAddons;
  33. use common\models\order\Order;
  34. use common\components\CsvExport;
  35. /**
  36. * 默认控制器
  37. *
  38. * Class DefaultController
  39. * @package addons\Agent\backend\controllers
  40. */
  41. class DefaultController extends BaseController
  42. {
  43. public $from_type = ['Agent'];
  44. public $enableCsrfValidation = false;
  45. /**
  46. * @desc:首页
  47. * @Author: hua
  48. * @Date: 2021/10/22
  49. * @Time: 9:14
  50. * @Copyright: copyright (c) 2021 广东七件事集团
  51. * @return mixed|string|void
  52. * @throws Exception
  53. */
  54. public function actionIndex()
  55. {
  56. if (\Yii::$app->request->isAjax) {
  57. if (\Yii::$app->request->isGet) {
  58. $get = \Yii::$app->request->get();
  59. $get['mall_id'] = $this->mall_id;
  60. $params = [];
  61. $user_id_arr = [];
  62. if (isset($get['level']) && !empty($get['level'])) $params['where'][] = ['=', 'level', $get['level']];
  63. $is_search = 0;
  64. if (!empty($get['user_id'])){
  65. $is_search = 1;
  66. $user_id_arr[]=$get['user_id'];
  67. }
  68. if (!empty($get['keyword'])) {
  69. $is_search = 1;
  70. $user_list = User::lists(['where'=>[['mall_id'=>$this->mall_id],[
  71. 'or',
  72. ['like', 'username', $get['keyword']],
  73. ['like', 'nickname', $get['keyword']],
  74. ['like', 'mobile', $get['keyword']]
  75. ]],'select'=>'id']);
  76. $user_ids = array_column($user_list,'id');
  77. if(!empty($user_id_arr)){
  78. $user_id_arr = array_intersect($user_id_arr,$user_ids);
  79. }else{
  80. $user_id_arr = $user_ids;
  81. }
  82. }
  83. if($is_search)$params['where'][] = ['in', 'user_id', $user_id_arr];
  84. $params['where'][] = ['=', 'mall_id',$this->mall_id];
  85. $params['where'][] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  86. $params['limit'] = 10;
  87. $params['order'] = 'id DESC';
  88. $pageData = Agent::listPage($params, false);
  89. if ($pageData === false) return $this->error(Agent::getStaticError());
  90. if(!empty($pageData['list'])){
  91. $user_id_arr = array_column($pageData['list'],'user_id');
  92. $user_list = $user_list = User::lists(['where'=>[['id'=>$user_id_arr]],'select'=>'id,nickname,avatar_url,mobile,parent_id']);
  93. $user_arr = $parent_arr = [];
  94. $parent_id_arr = [];
  95. foreach ($user_list as $item){
  96. $parent_id_arr[]=$item['parent_id'];
  97. $user_arr[$item['id']] = $item;
  98. }
  99. $user_parent_list = $user_list = User::lists(['where'=>[['id'=>$parent_id_arr]],'select'=>'id,nickname,avatar_url,mobile']);
  100. foreach ($user_parent_list as $item){
  101. $parent_arr[$item['id']] = $item;
  102. }
  103. $province_ids = array_column($pageData['list'], 'province_id') ?? [];
  104. $region_list = Region::lists([
  105. 'where' => [
  106. ['id' => $province_ids]
  107. ],
  108. 'select' => 'id, name, level',
  109. 'index' => 'id'
  110. ]);
  111. foreach ($pageData['list'] as &$v) {
  112. $v['nickname'] = $v['avatar_url'] = $v['parent_id'] = $v['parent_name'] = $v['mobile'] = '';
  113. if (isset($user_arr[$v['user_id']])) {
  114. $v['mobile'] = $user_arr[$v['user_id']]['mobile'];
  115. $v['nickname'] = $user_arr[$v['user_id']]['nickname'];
  116. $v['avatar_url'] = $user_arr[$v['user_id']]['avatar_url'];
  117. $v['parent_id'] = $user_arr[$v['user_id']]['parent_id'];
  118. if (isset($parent_arr[$v['parent_id']])) {
  119. $v['parent_name'] = $parent_arr[$v['parent_id']]['nickname'];
  120. }
  121. $v['provinceinfo'] = $region_list[$v['province_id']] ?? [];
  122. }
  123. }
  124. }
  125. $args = [];
  126. $args['where'] = [['mall_id' => $this->mall_id], ['<>', 'status', StatusEnum::DELETE]];
  127. $args['select'] = 'id,level,name';
  128. $args['order'] = 'level asc';
  129. $level_list = AgentLevel::lists($args, true);
  130. if (empty($level_list)) $level_list = [['id' => 0, 'level' => 0, 'name' => '默认等级']];
  131. $pageData['agentLevelList'] = $level_list;
  132. $pageData['export_list'] = Agent::exportFieldsList();
  133. $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AgentEnum::ADDONS_NAME, 'basic');
  134. $pageData['is_enable_province'] = $configs['is_enable_province'] ?? 0;
  135. return $this->success('操作成功', $pageData);
  136. }
  137. }
  138. // if (\Yii::$app->request->post('flag') == 'EXPORT') {
  139. // $post = \Yii::$app->request->post();
  140. // $post['mall_id'] = $this->mall_id;
  141. // $filename = '经销商列表-' . date('YmdHis') . '_' . rand(10000, 99999);
  142. // $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_AGENT, Agent::class, 'exportHandle', $post);
  143. // if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
  144. // return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  145. // }
  146. return $this->render('index', []);
  147. }
  148. public function actionExport()
  149. {
  150. if (\Yii::$app->request->post('flag') == 'EXPORT') {
  151. $post = \Yii::$app->request->post();
  152. $post['mall_id'] = $this->mall_id;
  153. $filename = '经销商列表-' . date('YmdHis') . '_' . rand(10000, 99999);
  154. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_AGENT, Agent::class, 'exportHandle', $post);
  155. if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
  156. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  157. }
  158. }
  159. /**
  160. * @desc:修改经销商等级
  161. * @Author: hua
  162. * @Date: 2021/10/23
  163. * @Time: 17:33
  164. * @Copyright: copyright (c) 2021 广东七件事集团
  165. * @return mixed|void
  166. */
  167. public function actionLevelChange()
  168. {
  169. if (\Yii::$app->request->isAjax) {
  170. if (\Yii::$app->request->isPost) {
  171. $params = \Yii::$app->request->post();
  172. $res = Yii::$app->services->validate->validate($params, [
  173. [['id', 'user_id', 'level'], 'required'],
  174. [['user_id', 'level'], 'integer'],
  175. [['provinceinfo'], 'safe']
  176. ]);
  177. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  178. $model = Agent::findOne(['mall_id' => $this->mall_id, 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  179. $params['mall_id'] = $this->mall_id;
  180. $provinceinfo = $params['provinceinfo'];
  181. unset($params['provinceinfo']);
  182. $params['province_id'] = $provinceinfo['id'] ?? 0;
  183. $params['admin_id'] = $this->admin->id;
  184. $res = Agent::setData($params);
  185. if ($res === false) return $this->error(AgentLevel::getStaticError());
  186. //会员标签
  187. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
  188. 'user_id'=>$params['user_id'],
  189. 'mall_id'=>$params['mall_id'],
  190. 'cate_ids'=>[MemberLabelEnum::AGENT_LEVEL]
  191. ], MemberLabelLogic::class, 'executeSyncCateId2Or3');
  192. $event = new AgentUpgradeAfterEvent($this->mall_id);
  193. Yii::$app->services->events->dispatch(
  194. $event,
  195. [
  196. 'user_id' => $params['user_id'],
  197. 'mall_id' => $this->mall_id,
  198. 'after_level' => $params['level'],
  199. 'before_level' => $model->level,
  200. 'is_admin' => 1,
  201. 'operator_user_id' => $this->admin->id,
  202. 'agent_id' => $params['id']
  203. ],
  204. $event->listeners
  205. );
  206. // 记录更新区域行为
  207. if ($params['province_id'] != $model->province_id) {
  208. AgentBehaviorLogsService::dispatchUserBehaviorLogsEvent(
  209. $event->mall_id ?? 0,
  210. [
  211. 'user_id' => $params['user_id'],
  212. 'mall_id' => $this->mall_id,
  213. 'after_province_id' => $params['province_id'],
  214. 'before_province_id' => $model->province_id,
  215. 'is_admin' => 1,
  216. 'operator_user_id' => $this->admin->id,
  217. 'agent_id' => $params['id'],
  218. 'behavior' => AgentBehaviorLogsEnum::BEHAVIOR_MODIFY_AGENT_PROVINCE
  219. ]
  220. );
  221. }
  222. return $this->success('操作成功');
  223. }
  224. }
  225. return $this->error();
  226. }
  227. /**
  228. * @desc:修改备注
  229. * @Author: hua
  230. * @Date: 2021/10/23
  231. * @Time: 17:29
  232. * @Copyright: copyright (c) 2021 广东七件事集团
  233. * @return mixed|void
  234. */
  235. public function actionRemarksEdit()
  236. {
  237. if (\Yii::$app->request->isAjax) {
  238. if (\Yii::$app->request->isPost) {
  239. $params = \Yii::$app->request->post();
  240. $res = Yii::$app->services->validate->validate($params, [
  241. [['id', 'user_id'], 'required'],
  242. [['user_id'], 'integer'],
  243. [['remarks'], 'safe']
  244. ]);
  245. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  246. $params['mall_id'] = $this->mall_id;
  247. $res = Agent::setData($params);
  248. if ($res === false) return $this->error(Agent::getStaticError());
  249. return $this->success('操作成功');
  250. }
  251. }
  252. return $this->error();
  253. }
  254. /**
  255. * @desc:搜索用户
  256. * @Author: hua
  257. * @Date: 2021/10/23
  258. * @Time: 17:55
  259. * @Copyright: copyright (c) 2021 广东七件事集团
  260. * @return mixed|void
  261. */
  262. public function actionSearchUser()
  263. {
  264. if (\Yii::$app->request->isAjax) {
  265. $keyword = \Yii::$app->request->get('keyword', '');
  266. $params = ['keyword' => $keyword, 'mall_id' => $this->mall_id];
  267. $query = User::find()->alias('u')
  268. ->where(['u.status' => StatusEnum::ENABLED, 'u.mall_id' => $params['mall_id']])
  269. ->andWhere(['u.is_inviter' => 1]);
  270. if (isset($params['keyword']) && !empty($params['keyword'])) {
  271. $query->andWhere(['or', ['like', 'nickname', $params['keyword']], ['like', 'mobile', $params['keyword']], ['like', 'id', $params['keyword']]]);
  272. }
  273. $list = $query->limit(20)->select('u.id,u.nickname,u.mobile,u.avatar_url')->asArray()->all();
  274. foreach ($list as &$item) {
  275. if (empty($item['nickname'])) {
  276. $item['nickname'] = $item['mobile'] . "(id:{$item['id']})";
  277. }
  278. }
  279. $list['list'] = $list;
  280. return $this->success('操作成功', $list);
  281. }
  282. return $this->error();
  283. }
  284. /**
  285. * @desc:保存经销商
  286. * @Author: hua
  287. * @Date: 2021/10/23
  288. * @Time: 17:39
  289. * @Copyright: copyright (c) 2021 广东七件事集团
  290. * @return mixed|void
  291. */
  292. public function actionEdit()
  293. {
  294. if (\Yii::$app->request->isAjax) {
  295. if (\Yii::$app->request->isPost) {
  296. $postData = \Yii::$app->request->post();
  297. $res = Yii::$app->services->validate->validate($postData, [
  298. [['user_id', 'level'], 'required'],
  299. [['user_id', 'level'], 'integer']
  300. ]);
  301. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  302. $wheres = [
  303. 'user_id' => $postData['user_id'],
  304. 'mall_id' => $this->mall_id,
  305. 'status' => StatusEnum::ENABLED,
  306. ];
  307. $model = Agent::findOne($wheres);
  308. if (!empty($model)) return $this->error('该会员已经是经销商,不能重复添加');
  309. $postData['mall_id'] = $this->mall_id;
  310. $postData['status'] = StatusEnum::ENABLED;
  311. $postData['admin_id'] = $this->admin->id;
  312. $res = Agent::setData($postData);
  313. if ($res === false) return $this->error(Agent::getStaticError());
  314. //会员标签
  315. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
  316. 'user_id'=>$postData['user_id'],
  317. 'mall_id'=>$postData['mall_id'],
  318. 'cate_ids'=>[MemberLabelEnum::AGENT_LEVEL]
  319. ], MemberLabelLogic::class, 'executeSyncCateId2Or3');
  320. $event = new AgentAddEvent($this->mall_id);
  321. Yii::$app->services->events->dispatch(
  322. $event,
  323. [
  324. 'user_id' => $res->user_id,
  325. 'mall_id' => $this->mall_id,
  326. 'is_admin' => 1,
  327. 'operator_user_id' => $this->admin->id,
  328. 'agent_id' => $res->id,
  329. 'level' => $res->level
  330. ],
  331. $event->listeners
  332. );
  333. return $this->success('操作成功');
  334. }
  335. }
  336. return $this->error();
  337. }
  338. /**
  339. * @desc:删除经销商
  340. * @Author: hua
  341. * @Date: 2021/10/23
  342. * @Time: 17:24
  343. * @Copyright: copyright (c) 2021 广东七件事集团
  344. * @return mixed|void
  345. */
  346. public function actionDelete()
  347. {
  348. if (\Yii::$app->request->isAjax) {
  349. if (\Yii::$app->request->isPost) {
  350. $params = \Yii::$app->request->post();
  351. $res = Yii::$app->services->validate->validate($params, [
  352. [['id'], 'required'],
  353. [['id'], 'integer']
  354. ]);
  355. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  356. $params['mall_id'] = $this->mall_id;
  357. $params['status'] = StatusEnum::DELETE;
  358. $res = Agent::setData($params);
  359. if ($res === false) return $this->error(Agent::getStaticError());
  360. $event = new AgentDeleteEvent($this->mall_id);
  361. Yii::$app->services->events->dispatch(
  362. $event,
  363. [
  364. 'user_id' => $res->user_id,
  365. 'mall_id' => $this->mall_id,
  366. 'is_admin' => 1,
  367. 'operator_user_id' => $this->admin->id,
  368. 'agent_id' => $params['id']
  369. ],
  370. $event->listeners
  371. );
  372. return $this->success('操作成功');
  373. }
  374. }
  375. return $this->error();
  376. }
  377. /**
  378. * @desc:批量修改等级
  379. * @Author: hua
  380. * @Date: 2021/10/23
  381. * @Time: 17:43
  382. * @Copyright: copyright (c) 2021 广东七件事集团
  383. * @return mixed|void
  384. */
  385. public function actionBatchLevel()
  386. {
  387. if (\Yii::$app->request->isAjax) {
  388. if (\Yii::$app->request->isPost) {
  389. $params = \Yii::$app->request->post();
  390. $res = Yii::$app->services->validate->validate($params, [
  391. [['batch_ids', 'level'], 'required'],
  392. [['level'], 'integer']
  393. ]);
  394. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  395. $params['mall_id'] = $this->mall_id;
  396. $params['status'] = StatusEnum::ENABLED;
  397. $agentBeforeLevelList = Agent::find()
  398. ->where(['id' => $params['batch_ids']])
  399. ->select('id, level, user_id')
  400. ->indexBy('id')
  401. ->asArray()
  402. ->all();
  403. $res = Agent::batchLevel($params);
  404. if ($res === false) return $this->error(Agent::getStaticError());
  405. foreach ($params['batch_ids'] as $batchId) {
  406. if (!isset($agentBeforeLevelList[$batchId])) {
  407. continue;
  408. }
  409. $event = new AgentUpgradeAfterEvent($this->mall_id);
  410. Yii::$app->services->events->dispatch(
  411. $event,
  412. [
  413. 'user_id' => $agentBeforeLevelList[$batchId]['user_id'],
  414. 'mall_id' => $this->mall_id,
  415. 'after_level' => $params['level'],
  416. 'before_level' => $agentBeforeLevelList[$batchId]['level'],
  417. 'is_admin' => 1,
  418. 'operator_user_id' => $this->admin->id,
  419. 'agent_id' => $batchId
  420. ],
  421. $event->listeners
  422. );
  423. }
  424. return $this->success('操作成功');
  425. }
  426. }
  427. return $this->error();
  428. }
  429. /**
  430. * 经销商配置
  431. */
  432. public function actionSetting()
  433. {
  434. if (\Yii::$app->request->isAjax) {
  435. return $this->error();
  436. }
  437. return $this->render('setting');
  438. }
  439. /**
  440. * @desc:已经启用的等级
  441. * @Author: hua
  442. * @Date: 2021/10/23
  443. * @Time: 17:31
  444. * @Copyright: copyright (c) 2021 广东七件事集团
  445. * @return mixed|void
  446. */
  447. public function actionEnableList()
  448. {
  449. if (\Yii::$app->request->isAjax) {
  450. if (\Yii::$app->request->isGet) {
  451. $level = \Yii::$app->request->get('level');
  452. $where = [['mall_id' => $this->mall_id], ['=', 'status', StatusEnum::ENABLED]];
  453. if (!empty($level)) $where[] = ['level' => $level];
  454. $args['where'] = $where;
  455. $args['select'] = 'id,level,name';
  456. $args['order'] = 'level asc';
  457. $list['list'] = AgentLevel::lists($args, true);
  458. return $this->success('操作成功', $list);
  459. }
  460. }
  461. return $this->error();
  462. }
  463. public function actionGetProvinces()
  464. {
  465. if (\Yii::$app->request->isAjax) {
  466. if (\Yii::$app->request->isGet) {
  467. $list = Region::lists([
  468. 'where' => [
  469. ['level' => 1]
  470. ],
  471. 'select' => 'id,name,lng,lat',
  472. ]);
  473. return $this->success('操作成功', $list);
  474. }
  475. }
  476. }
  477. public function actionCommissionBasicLog()
  478. {
  479. if (\Yii::$app->request->isAjax) {
  480. if (\Yii::$app->request->isGet) {
  481. $get = \Yii::$app->request->get();
  482. $get['mall_id'] = $this->mall_id;
  483. $params = [];
  484. if (!empty($get['source_table_id'])) {
  485. $params['where'][] = ['source_table_id'=> $get['source_table_id']];
  486. }
  487. $params['where'][] = ['=', 'mall_id',$this->mall_id];
  488. $params['where'][] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  489. $params['order'] = 'id desc';
  490. $params['limit'] = 10;
  491. $pageData = AgentCommissionBasisLog::listPage($params, false);
  492. if ($pageData === false) return $this->error(AgentCommissionBasisLog::getStaticError());
  493. if(!empty($pageData['list'])){
  494. foreach ($pageData['list'] as &$item){
  495. if($item['basic_config']) $item['basic_config'] = json_encode(json_decode($item['basic_config'],true),JSON_UNESCAPED_UNICODE);
  496. if($item['level_config']) $item['level_config'] = json_encode(json_decode($item['level_config'],true),JSON_UNESCAPED_UNICODE);
  497. if($item['goods_config']) $item['goods_config'] = json_encode(json_decode($item['goods_config'],true),JSON_UNESCAPED_UNICODE);
  498. }
  499. }
  500. return $this->success('操作成功', $pageData);
  501. }
  502. }
  503. return $this->render('commission-basic-log', []);
  504. }
  505. public function actionGoodsModeSetting()
  506. {
  507. $request = Yii::$app->request;
  508. if ($request->isAjax && $request->isPost) {
  509. $is_install_regular_bonus = (int)MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_REGULAR_BONUS);
  510. return $this->success('操作成功', compact('is_install_regular_bonus'));
  511. }
  512. }
  513. /**
  514. * 供应商库存
  515. * @Author:hua
  516. * @Date:2024-02-22 17:53
  517. * @Copyright:copyright(c)2024 广东七件事集团
  518. * @return mixed|string|void
  519. */
  520. public function actionSupplierStock(){
  521. if (\Yii::$app->request->isAjax) {
  522. if (\Yii::$app->request->isGet) {
  523. $get = \Yii::$app->request->get();
  524. $get['mall_id'] = $this->mall_id;
  525. $params = [];
  526. if (isset($get['level']) && !empty($get['level'])) $params['where'][] = ['=', 'level', $get['level']];
  527. if (!empty($get['keyword'])) {
  528. $user_list = User::lists(['where'=>[['mall_id'=>$this->mall_id],[
  529. 'or',
  530. ['=', 'id', $get['keyword']],
  531. ['like', 'username', $get['keyword']],
  532. ['like', 'nickname', $get['keyword']],
  533. ['like', 'mobile', $get['keyword']]
  534. ]],'select'=>'id']);
  535. $user_ids = array_column($user_list,'id');
  536. $params['where'][] = ['in', 'user_id', $user_ids];
  537. }
  538. $params['where'][] = ['=', 'mall_id',$this->mall_id];
  539. $params['where'][] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  540. $params['limit'] = 10;
  541. $params['order'] = 'id DESC';
  542. $pageData = Agent::listPage($params, false);
  543. if ($pageData === false) return $this->error(Agent::getStaticError());
  544. if(!empty($pageData['list'])){
  545. $user_id_arr = array_column($pageData['list'],'user_id');
  546. $user_list = User::lists(['where'=>[['id'=>$user_id_arr]],'select'=>'id,nickname,avatar_url,mobile,parent_id']);
  547. $user_arr = [];
  548. foreach ($user_list as $item){
  549. $user_arr[$item['id']] = $item;
  550. }
  551. $dsn = Yii::$app->db->dsn;
  552. if(strpos($dsn,'port=8066')===false){
  553. $commission_log_list = CommissionLog::lists([
  554. 'where'=>[
  555. ['user_id'=>$user_id_arr],
  556. ['from_type'=>'Agent']
  557. ],
  558. 'select'=>'sum(change_num) as change_num,user_id',
  559. 'group'=>'user_id',
  560. 'index'=>'user_id'
  561. ]);
  562. $commission_log_count_list = CommissionLog::lists([
  563. 'where'=>[
  564. ['user_id'=>$user_id_arr],
  565. ['from_type'=>'Agent']
  566. ],
  567. 'select'=>'count(*) counts,user_id',
  568. 'group'=>'user_id',
  569. 'index'=>'user_id'
  570. ]);
  571. }
  572. foreach ($pageData['list'] as &$v) {
  573. $v['nickname'] = $v['avatar_url'] = $v['mobile'] = '';
  574. if (isset($user_arr[$v['user_id']])) {
  575. $v['mobile'] = $user_arr[$v['user_id']]['mobile'];
  576. $v['nickname'] = $user_arr[$v['user_id']]['nickname'];
  577. $v['avatar_url'] = $user_arr[$v['user_id']]['avatar_url'];
  578. }
  579. $v['change_num'] = $v['counts'] = 0;
  580. if(strpos($dsn,'port=8066')===false){
  581. if (isset($commission_log_list[$v['user_id']])) {
  582. $v['change_num'] = $commission_log_list[$v['user_id']]['change_num'];
  583. }
  584. if (isset($commission_log_count_list[$v['user_id']])) {
  585. $v['counts'] = $commission_log_count_list[$v['user_id']]['counts'];
  586. }
  587. }else{
  588. $change_num = CommissionLog::find()->where(['user_id'=>$v['user_id'],'from_type'=>'Agent'])->sum('change_num');
  589. if(!empty($change_num)) $v['change_num'] = $change_num;
  590. $counts = CommissionLog::find()->where(['user_id'=>$v['user_id'],'from_type'=>'Agent'])->count();
  591. if(!empty($counts)) $v['counts'] = $counts;
  592. }
  593. }
  594. }
  595. $args = [];
  596. $args['where'] = [['mall_id' => $this->mall_id], ['<>', 'status', StatusEnum::DELETE]];
  597. $args['select'] = 'id,level,name';
  598. $args['order'] = 'level asc';
  599. $level_list = AgentLevel::lists($args, true);
  600. if (empty($level_list)) $level_list = [['id' => 0, 'level' => 0, 'name' => '默认等级']];
  601. $pageData['agentLevelList'] = $level_list;
  602. $pageData['export_list'] = Agent::getSupplierStockExportFieldsList();
  603. return $this->success('操作成功', $pageData);
  604. }
  605. if (\Yii::$app->request->post('flag') == 'EXPORT') {
  606. $post = \Yii::$app->request->post();
  607. $post['mall_id'] = $this->mall_id;
  608. $filename = '供应商库存-' . date('YmdHis') . '_' . rand(10000, 99999);
  609. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_AGENT, Agent::class, 'exportHandleSupplierStock', $post);
  610. if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
  611. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  612. }
  613. }
  614. return $this->render('supplier-stock', []);
  615. }
  616. /**
  617. * 供应商订单
  618. * @Author:hua
  619. * @Date:2024-02-22 17:53
  620. * @Copyright:copyright(c)2024 广东七件事集团
  621. * @return mixed|string|void
  622. */
  623. public function actionSupplierOrder(){
  624. if (\Yii::$app->request->isAjax) {
  625. if (\Yii::$app->request->isGet) {
  626. $get = \Yii::$app->request->get();
  627. $get['mall_id'] = $this->mall_id;
  628. $params = ['where' => [['status' => StatusEnum::ENABLED]]];
  629. if (!empty($get['order_no'])){
  630. $params['where'][] = ['=', 'order_no', trim($get['order_no'])];
  631. }
  632. if (!empty($get['keyword'])) {
  633. $user_list = User::lists(['where'=>[['mall_id'=>$this->mall_id],[
  634. 'or',
  635. ['=', 'id', $get['keyword']],
  636. ['like', 'username', $get['keyword']],
  637. ['like', 'nickname', $get['keyword']],
  638. ['like', 'mobile', $get['keyword']]
  639. ]],'select'=>'id']);
  640. $user_ids = array_column($user_list,'id');
  641. $params['where'][] = ['in', 'buy_user_id', $user_ids];
  642. }
  643. if (!empty($get['seller_keyword'])) {
  644. $user_list = User::lists(['where'=>[['mall_id'=>$this->mall_id],[
  645. 'or',
  646. ['=', 'id', $get['seller_keyword']],
  647. ['like', 'username', $get['seller_keyword']],
  648. ['like', 'nickname', $get['seller_keyword']],
  649. ['like', 'mobile', $get['seller_keyword']]
  650. ]],'select'=>'id']);
  651. $user_ids = array_column($user_list,'id');
  652. $params['where'][] = ['in', 'seller_user_id', $user_ids];
  653. }
  654. $params['where'][] = ['=', 'mall_id',$this->mall_id];
  655. $params['limit'] = 10;
  656. $params['order'] = 'id DESC';
  657. $pageData = AgentSupplierOrder::listPage($params, false);
  658. if ($pageData === false) return $this->error(Agent::getStaticError());
  659. if(!empty($pageData['list'])){
  660. $buy_user_ids = array_column($pageData['list'],'buy_user_id');
  661. $seller_user_ids = array_column($pageData['list'],'seller_user_id');
  662. $order_no_arr = array_column($pageData['list'],'order_no');
  663. $user_id_arr = array_merge($buy_user_ids,$seller_user_ids);
  664. $user_arr = User::lists(['where'=>[['id'=>$user_id_arr]],'index'=>'id','select'=>'id,nickname,avatar_url,mobile,parent_id']);
  665. $order_arr = Order::lists(['where'=>[['order_no'=>$order_no_arr]],'index'=>'order_no','select'=>'order_no,order_status']);
  666. foreach ($pageData['list'] as &$v) {
  667. $v['nickname'] = $v['avatar_url'] = $v['mobile'] = '';
  668. $v['seller_nickname'] = $v['seller_avatar_url'] = $v['seller_mobile'] = '';
  669. $v['order_status'] = 0;
  670. if (isset($user_arr[$v['buy_user_id']])) {
  671. $v['mobile'] = $user_arr[$v['buy_user_id']]['mobile'];
  672. $v['nickname'] = $user_arr[$v['buy_user_id']]['nickname'];
  673. $v['avatar_url'] = $user_arr[$v['buy_user_id']]['avatar_url'];
  674. }
  675. if (isset($order_arr[$v['order_no']])) {
  676. $v['order_status'] = $order_arr[$v['order_no']]['order_status'];
  677. }
  678. if (isset($user_arr[$v['seller_user_id']])) {
  679. $v['seller_mobile'] = $user_arr[$v['seller_user_id']]['mobile'];
  680. $v['seller_nickname'] = $user_arr[$v['seller_user_id']]['nickname'];
  681. $v['seller_avatar_url'] = $user_arr[$v['seller_user_id']]['avatar_url'];
  682. }
  683. }
  684. }
  685. $args = [];
  686. $args['where'] = [['mall_id' => $this->mall_id], ['<>', 'status', StatusEnum::DELETE]];
  687. $args['select'] = 'id,level,name';
  688. $args['order'] = 'level asc';
  689. $level_list = AgentLevel::lists($args, true);
  690. if (empty($level_list)) $level_list = [['id' => 0, 'level' => 0, 'name' => '默认等级']];
  691. $pageData['agentLevelList'] = $level_list;
  692. $pageData['export_list'] = AgentSupplierOrder::exportFieldsList();
  693. $cache_key = RedisKeyEnum::suffix(AgentSupplierOrder::OLD_DATA_GENERATE_, $this->mall_id);
  694. $cache = \Yii::$app->cache;
  695. $cache_data = $cache->get($cache_key);
  696. $pageData['is_generate_old_data'] = $cache_data ?? 0;
  697. return $this->success('操作成功', $pageData);
  698. }
  699. if (\Yii::$app->request->post('flag') == 'EXPORT') {
  700. $post = \Yii::$app->request->post();
  701. $post['mall_id'] = $this->mall_id;
  702. $filename = '供应商订单-' . date('YmdHis') . '_' . rand(10000, 99999);
  703. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_AGENT, AgentSupplierOrder::class, 'exportHandle', $post);
  704. if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
  705. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  706. }
  707. }
  708. return $this->render('supplier-order', []);
  709. }
  710. /**
  711. * 生成旧数据
  712. * @Author:hua
  713. * @Date:2024-02-23 15:44
  714. * @Copyright:copyright(c)2024 广东七件事集团
  715. * @return mixed|void
  716. */
  717. public function actionOldDataGenerate(){
  718. $data = ['mall_id'=>$this->mall_id];
  719. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $data, AgentSupplierOrder::class, 'oldDataGenerate');
  720. return $this->success('操作成功', []);
  721. }
  722. /**
  723. * 导入日志记录
  724. * @return mixed|void
  725. */
  726. public function actionGetImportLevelLog()
  727. {
  728. $params = \Yii::$app->request->get();
  729. $params['where'] = [
  730. ['mall_id'=>$this->mall_id]
  731. ];
  732. $params['order'] = 'id desc';
  733. $data = AgentImportLevelLog::listPage($params, false, true);
  734. if (!empty($data['list'])) {
  735. foreach ($data['list'] as &$v) {
  736. $v['url'] = '';
  737. if ($v['fail_counts']) $v['url'] = \Yii::$app->urlManager->createUrl(['agent/default/export-level-fail', 'log_id' => $v['id']]);
  738. }
  739. }
  740. return $this->success('操作成功', $data);
  741. }
  742. /**
  743. * 导出失败日志
  744. */
  745. public function actionExportLevelFail()
  746. {
  747. AgentImportLevelFailLog::exportFailLog(\Yii::$app->request->get());
  748. }
  749. /**
  750. * 会员批量导入模板下载
  751. */
  752. public function actionImportLevelModel()
  753. {
  754. $csv = new CsvExport();
  755. $file_name = '批量导入经销商等级模板-' . date('YmdHis', time());
  756. $head_list = array_column(Agent::$head_list, 'field_name');
  757. return $csv->export([], $head_list, $file_name);
  758. }
  759. /**
  760. * 批量导入等级
  761. * @return mixed|string|void
  762. */
  763. public function actionImportLevel()
  764. {
  765. set_time_limit(0);
  766. if (\Yii::$app->request->isAjax) {
  767. $params = \Yii::$app->request->post();
  768. $res = Yii::$app->services->validate->validate($params, [
  769. [['current_num'], 'required'],
  770. [['file'], 'file', 'extensions' => ['csv']],
  771. [['file_path'], 'string'],
  772. [['sync'], 'safe'],
  773. ]);
  774. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  775. $params['mall_id'] = $this->mall_id;
  776. if(!empty($params['sync'])){
  777. $res = Agent::importLevelSync($params);
  778. }else{
  779. $res = Agent::importLevel($params);
  780. }
  781. if (empty($res['fail_counts'])) return $this->success('操作成功', $res);
  782. if (!empty($res['fail_counts'])) return $this->error('导入失败'.$res['fail_counts'].'条');
  783. return $this->error(Agent::getStaticError());
  784. } else {
  785. return $this->render($this->action->id);
  786. }
  787. }
  788. /**
  789. * 供应商收入列表
  790. *
  791. * @return string|void
  792. */
  793. public function actionSupplierCommission()
  794. {
  795. $service = new AgentSupplierCommissionService([]);
  796. $service->mall_id = $this->mall_id;
  797. if (!$this->request->isAjax) {
  798. return $this->render($this->action->id, ['allowDeliveryTask' => (int)$service->allowDeliveryTask()]);
  799. }
  800. $this->success('success', $service->getList($this->request->get(), false));
  801. }
  802. /**
  803. * 供应商收入导出
  804. *
  805. * @return void
  806. */
  807. public function actionSupplierCommissionExport()
  808. {
  809. $filename = '供应商收入数据-' . date('YmdHis') . '_' . random_int(10000, 99999);
  810. $data = Yii::$app->request->post('params');
  811. $data['mall_id'] = $this->mall_id;
  812. $res = CsvExportHelper::exportDownLoadCenter(
  813. $this->mall_id,
  814. $filename,
  815. DownloadEnum::TYPE_AGENT_SUPPLIER_COMMISSION,
  816. AgentSupplierCommissionService::class,
  817. 'export',
  818. $data
  819. );
  820. if ($res === false) {
  821. return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
  822. }
  823. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  824. }
  825. /**
  826. * @return mixed|null
  827. * @throws Exception
  828. */
  829. public function actionSyncOldData()
  830. {
  831. // 检查是否允许投递
  832. $service = new AgentSupplierCommissionService([]);
  833. $service->mall_id = $this->mall_id;
  834. if (!$service->allowDeliveryTask()) {
  835. return $this->error('当前已同步数据');
  836. }
  837. AgentSupplierCommissionSyncLog::setData([
  838. 'mall_id' => $this->mall_id,
  839. 'status' => StatusEnum::DISABLED
  840. ]);
  841. $data = ['mall_id'=>$this->mall_id];
  842. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_LONG_DURATION, RabbitMqEnum::LONG_DURATION_QUEUE, $data, AgentSupplierCommissionService::class, 'syncOldData');
  843. return $this->success();
  844. }
  845. /**
  846. * 经销商日志
  847. *
  848. * @return mixed|string|null
  849. */
  850. public function actionLog()
  851. {
  852. if (!$this->request->isAjax) {
  853. foreach (AgentBehaviorLogsEnum::OPERATOR_USER_TYPE_MAP as $value => $label) {
  854. $operatorUserTypeOptions[] = compact('label', 'value');
  855. }
  856. foreach (AgentBehaviorLogsEnum::BEHAVIOR_MAP as $value => $label) {
  857. $behaviorOptions[] = compact('label', 'value');
  858. }
  859. $operatorUserTypeOptions = json_encode($operatorUserTypeOptions);
  860. $behaviorOptions = json_encode($behaviorOptions);
  861. return $this->render($this->action->id, compact('operatorUserTypeOptions', 'behaviorOptions'));
  862. }
  863. $service = new LogsServices(['mall_id' => $this->mall_id]);
  864. return $this->success('success', $service->getAgentLogs($this->request->get()));
  865. }
  866. /**
  867. * 获取订单details列表
  868. *
  869. * @return void
  870. */
  871. public function actionGetOrderDetails()
  872. {
  873. $this->success(
  874. 'success',
  875. (new OrderService(['mall_id' => $this->mall_id]))->getOrderDetails($this->request->get())
  876. );
  877. }
  878. /**
  879. * 重新分配佣金
  880. *
  881. * @return void
  882. */
  883. public function actionRedistributeCommissions()
  884. {
  885. if (!$this->request->isPost) {
  886. $this->error('请求类型错误');
  887. return;
  888. }
  889. $post = $this->request->post();
  890. $res = Yii::$app->services->validate->validate($post, [
  891. [['order_details_ids'], 'required'],
  892. ['order_details_ids', 'each', 'rule' => ['integer']],
  893. ]);
  894. if ($res === false) {
  895. return $this->error(Yii::$app->services->validate->getErrorMessage());
  896. }
  897. $service = new AgentSupplierCommissionService(['mall_id' => $this->mall_id]);
  898. if ($service->redistributeCommissions($post)) {
  899. $this->success('success');
  900. return;
  901. }
  902. $this->error($service->getError());
  903. }
  904. }