DefaultController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. namespace addons\Distribution\backend\controllers;
  3. use addons\Distribution\common\models\Distribution;
  4. use addons\Distribution\common\models\DistributionCommissionBasisLog;
  5. use addons\Distribution\common\models\DistributionLevelUpgradeLog;
  6. use addons\Distribution\common\models\DistributionLevel;
  7. use common\enums\AddonsEnum;
  8. use common\enums\DownloadEnum;
  9. use common\enums\LevelUpgradeLogEnum;
  10. use common\enums\StatusEnum;
  11. use common\helpers\csv\CsvExportHelper;
  12. use common\models\user\User;
  13. use Yii;
  14. /**
  15. * 默认控制器
  16. *
  17. * Class DefaultController
  18. * @package addons\Distribution\backend\controllers
  19. */
  20. class DefaultController extends BaseController
  21. {
  22. public $enableCsrfValidation = false;
  23. public $from_type = ['Distribution'];
  24. /**
  25. * @desc:首页
  26. * @Author: hua
  27. * @Date: 2021/10/23
  28. * @Time: 18:17
  29. * @Copyright: copyright (c) 2021 广东七件事集团
  30. * @return mixed|string|void
  31. * @throws \Exception
  32. */
  33. public function actionIndex()
  34. {
  35. if (\Yii::$app->request->isAjax) {
  36. if (\Yii::$app->request->isGet) {
  37. $get = \Yii::$app->request->get();
  38. $get['mall_id'] = $this->mall_id;
  39. $params = [];
  40. $user_id_arr = [];
  41. if (isset($get['level'])&&$get['level']!=='') $params['where'][] = ['=', 'level', $get['level']];
  42. $is_search = 0;
  43. if (!empty($get['user_id'])){
  44. $is_search = 1;
  45. $user_id_arr[]=$get['user_id'];
  46. }
  47. if (!empty($get['keyword'])) {
  48. $is_search = 1;
  49. $user_list = User::lists(['where'=>[['mall_id'=>$this->mall_id],[
  50. 'or',
  51. ['like', 'username', $get['keyword']],
  52. ['like', 'nickname', $get['keyword']],
  53. ['like', 'mobile', $get['keyword']]
  54. ]],'select'=>'id']);
  55. $user_ids = array_column($user_list,'id');
  56. if(!empty($user_id_arr)){
  57. $user_id_arr = array_intersect($user_id_arr,$user_ids);
  58. }else{
  59. $user_id_arr = $user_ids;
  60. }
  61. }
  62. if($is_search)$params['where'][] = ['in', 'user_id', $user_id_arr];
  63. $params['where'][] = ['=', 'mall_id',$this->mall_id];
  64. $params['where'][] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  65. $params['order'] = 'id desc';
  66. $params['limit'] = 10;
  67. $pageData = Distribution::listPage($params, false);
  68. if ($pageData === false) return $this->error(Distribution::getStaticError());
  69. if(!empty($pageData['list'])){
  70. $user_id_arr = array_column($pageData['list'],'user_id');
  71. $user_list = $user_list = User::lists(['where'=>[['id'=>$user_id_arr]],'select'=>'id,nickname,avatar_url,mobile,parent_id']);
  72. $user_arr = $parent_arr = [];
  73. $parent_id_arr = [];
  74. foreach ($user_list as $item){
  75. $parent_id_arr[]=$item['parent_id'];
  76. $user_arr[$item['id']] = $item;
  77. }
  78. $user_parent_list = $user_list = User::lists(['where'=>[['id'=>$parent_id_arr]],'select'=>'id,nickname,avatar_url']);
  79. foreach ($user_parent_list as $item){
  80. $parent_arr[$item['id']] = $item;
  81. }
  82. foreach ($pageData['list'] as &$v) {
  83. $v['nickname'] = $v['avatar_url'] = $v['parent_id'] = $v['parent_name'] = $v['mobile'] = '';
  84. if (isset($user_arr[$v['user_id']])) {
  85. $v['mobile'] = $user_arr[$v['user_id']]['mobile'];
  86. $v['nickname'] = $user_arr[$v['user_id']]['nickname'];
  87. $v['avatar_url'] = $user_arr[$v['user_id']]['avatar_url'];
  88. $v['parent_id'] = $user_arr[$v['user_id']]['parent_id'];
  89. if (isset($parent_arr[$v['parent_id']])) $v['parent_name'] = $parent_arr[$v['parent_id']]['nickname'];
  90. }
  91. }
  92. }
  93. $wheres = [];
  94. $wheres[] = ['mall_id' => $this->mall_id];
  95. $wheres[] = ['status' => StatusEnum::ENABLED];
  96. $level_list = DistributionLevel::getLevelList($wheres, [], true, 'id,level,name');
  97. $pageData['distributionLevelList'] = $level_list;
  98. $pageData['export_list'] = Distribution::exportFieldsList();
  99. return $this->success('操作成功', $pageData);
  100. }
  101. }
  102. if (\Yii::$app->request->post('flag') == 'EXPORT') {
  103. $post = \Yii::$app->request->post();
  104. $post['mall_id'] = $this->mall_id;
  105. $filename = '分销商列表-' . date('YmdHis').'_'.rand(10000,99999);
  106. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id,$filename,DownloadEnum::TYPE_DISTRIBUTION,Distribution::class,'exportHandle',$post);
  107. if ($res === false) return $this->error('加入导出任务失败'.CsvExportHelper::getStaticError());
  108. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  109. }
  110. return $this->render('index', []);
  111. }
  112. /**
  113. * @desc:编辑
  114. * @Author: hua
  115. * @Date: 2021/10/23
  116. * @Time: 18:17
  117. * @Copyright: copyright (c) 2021 广东七件事集团
  118. * @return mixed|void
  119. */
  120. public function actionEdit()
  121. {
  122. if (\Yii::$app->request->isAjax) {
  123. if (\Yii::$app->request->isPost) {
  124. $params = \Yii::$app->request->post();
  125. $res = Yii::$app->services->validate->validate($params, [
  126. [['user_id'], 'required'],
  127. [[ 'level'], 'required','message'=>'请选择等级'],
  128. [['user_id', 'level'], 'integer'],
  129. [['id'], 'safe'],
  130. ]);
  131. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  132. // $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_DISTRIBUTION, 'basic');
  133. // if(isset($configs['is_default_level'])&&empty($configs['is_default_level'])) return $this->error('默认等级已经关闭');
  134. $wheres = [
  135. 'user_id' => $params['user_id'],
  136. 'mall_id' => $this->mall_id,
  137. 'status' => StatusEnum::ENABLED,
  138. ];
  139. $model = Distribution::findOne($wheres);
  140. if (!empty($model)) return $this->error('该会员已经是分销商,不能重复添加');
  141. $params['mall_id'] = $this->mall_id;
  142. $params['status'] = StatusEnum::ENABLED;
  143. $res = Distribution::setData($params);
  144. if ($res === false) return $this->error(Distribution::getStaticError());
  145. //添加升级日志
  146. $upgrade_log_obj_clone = new DistributionLevelUpgradeLog();
  147. $upgrade_log_obj_clone->mall_id = $this->mall_id;
  148. $upgrade_log_obj_clone->user_id = $res['user_id'];
  149. $upgrade_log_obj_clone->before_level = 0;
  150. $upgrade_log_obj_clone->after_level = $res['level'];
  151. $upgrade_log_obj_clone->upgrade_type = LevelUpgradeLogEnum::MANUALLY_UPGRADE;
  152. $upgrade_log_obj_clone->order_id = 0;
  153. $upgrade_log_obj_clone->admin_id = $this->admin->id;
  154. $upgrade_log_obj_clone->upgrade_condition = [];
  155. $upgrade_log_obj_clone->save();
  156. return $this->success('操作成功');
  157. }
  158. }
  159. return $this->error();
  160. }
  161. /**
  162. * @desc:修改备注
  163. * @Author: hua
  164. * @Date: 2021/10/29
  165. * @Time: 10:34
  166. * @Copyright: copyright (c) 2021 广东七件事集团
  167. * @return mixed|void
  168. */
  169. public function actionRemarksEdit()
  170. {
  171. if (\Yii::$app->request->isAjax) {
  172. if (\Yii::$app->request->isPost) {
  173. $params = \Yii::$app->request->post();
  174. $res = Yii::$app->services->validate->validate($params, [
  175. [['id'], 'required'],
  176. [['remarks'], 'safe']
  177. ]);
  178. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  179. $params['mall_id'] = $this->mall_id;
  180. $res = Distribution::setData($params);
  181. if ($res === false) return $this->error(Distribution::getStaticError());
  182. return $this->success('操作成功');
  183. }
  184. }
  185. return $this->error();
  186. }
  187. /**
  188. * @desc:
  189. * @Author: hua
  190. * @Date: 2021/10/29
  191. * @Time: 10:37
  192. * @Copyright: copyright (c) 2021 广东七件事集团
  193. * @return mixed|void
  194. */
  195. public function actionSearchUser()
  196. {
  197. if (\Yii::$app->request->isAjax) {
  198. if (!\Yii::$app->request->isGet) return $this->error('请求方式错误');
  199. $keyword = \Yii::$app->request->get('keyword');
  200. $params = [];
  201. $where[] = ['mall_id' => $this->mall_id];
  202. $where[] = ['status' => StatusEnum::ENABLED];
  203. if (!empty($keyword)) $where[] = ['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword], ['like', 'id', $keyword]];
  204. $params['where'] = $where;
  205. $params['select'] = 'id,nickname,mobile,username,avatar_url';
  206. $params['limit'] = 20;
  207. $list = User::lists($params);
  208. if (!empty($list)) {
  209. foreach ($list as &$item) {
  210. if (empty($item['nickname'])) $item['nickname'] = $item['mobile'] . "(id:{$item['id']})";
  211. }
  212. }
  213. return $this->success('操作成功', ['list' => $list]);
  214. }
  215. }
  216. /**
  217. * @desc:
  218. * @Author: hua
  219. * @Date: 2021/11/12
  220. * @Time: 12:19
  221. * @Copyright: copyright (c) 2021 广东七件事集团
  222. * @return mixed|void
  223. */
  224. public function actionLevelChange()
  225. {
  226. try {
  227. if (\Yii::$app->request->isAjax) {
  228. $post = \Yii::$app->request->post();
  229. if ($post['level']) {
  230. $distributionLevel = DistributionLevel::findOne(['mall_id'=>$this->mall_id,'level'=>$post['level'],'status'=>StatusEnum::ENABLED]);
  231. if (empty($distributionLevel)) return $this->error('无效的分销商等级');
  232. } else {
  233. $post['level'] = 0;
  234. }
  235. $post['mall_id'] =$this->mall_id;
  236. $post['upgrade_status'] = Distribution::UPGRADE_STATUS_MANUAL;
  237. $detail = Distribution::getOne([['id' => $post['id']]], true);
  238. $res = Distribution::setData($post);
  239. if ($res==false) throw new \Exception(DistributionLevel::getStaticError());
  240. if ($detail['level'] != $post['level']) {
  241. $upgrade_log_obj_clone = new DistributionLevelUpgradeLog();
  242. $upgrade_log_obj_clone->mall_id = $this->mall_id;
  243. $upgrade_log_obj_clone->user_id = $detail['user_id'];
  244. $upgrade_log_obj_clone->before_level = $detail['level'];
  245. $upgrade_log_obj_clone->after_level = $post['level'];
  246. $upgrade_log_obj_clone->upgrade_type = $detail['level'] < $post['level'] ? LevelUpgradeLogEnum::MANUALLY_UPGRADE : LevelUpgradeLogEnum::DOWNGRADE_MANUALLY;
  247. $upgrade_log_obj_clone->order_id = 0;
  248. $upgrade_log_obj_clone->admin_id = $this->admin->id;
  249. $upgrade_log_obj_clone->upgrade_condition = [];
  250. $upgrade_log_obj_clone->save();
  251. }
  252. return $this->success('修改成功');
  253. }
  254. } catch (\Exception $exception) {
  255. return $this->error($exception->getMessage().$exception->getLine().$exception->getFile());
  256. }
  257. }
  258. /**
  259. * @desc:批量修改等级
  260. * @Author: hua
  261. * @Date: 2021/10/29
  262. * @Time: 10:41
  263. * @Copyright: copyright (c) 2021 广东七件事集团
  264. * @return mixed|void
  265. */
  266. public function actionBatchLevel()
  267. {
  268. if (\Yii::$app->request->isAjax) {
  269. if (\Yii::$app->request->isPost) {
  270. $params = \Yii::$app->request->post();
  271. $res = Yii::$app->services->validate->validate($params, [
  272. [['batch_ids', 'level'], 'required'],
  273. [['level'], 'integer']
  274. ]);
  275. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  276. $params['mall_id'] = $this->mall_id;
  277. $params['status'] = StatusEnum::ENABLED;
  278. $res = Distribution::batchLevel($params);
  279. if ($res === false) return $this->error(Distribution::getStaticError());
  280. return $this->success('操作成功');
  281. }
  282. }
  283. return $this->error();
  284. }
  285. /**
  286. * @desc:
  287. * @Author: hua
  288. * @Date: 2021/10/29
  289. * @Time: 10:31
  290. * @Copyright: copyright (c) 2021 广东七件事集团
  291. * @return mixed|void
  292. */
  293. public function actionDelete()
  294. {
  295. if (\Yii::$app->request->isAjax) {
  296. if (!\Yii::$app->request->isPost) return $this->error('请求方式错误');
  297. $params = \Yii::$app->request->post();
  298. $res = Yii::$app->services->validate->validate($params, [
  299. [['id'], 'required'],
  300. [['id'], 'integer']
  301. ]);
  302. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  303. $params['mall_id'] = $this->mall_id;
  304. $params['status'] = StatusEnum::DELETE;
  305. $res = Distribution::setData($params);
  306. if ($res) return $this->success('删除成功');
  307. return $this->error(Distribution::getStaticError());
  308. }
  309. }
  310. public function actionEnableList()
  311. {
  312. if (\Yii::$app->request->isAjax) {
  313. if (\Yii::$app->request->isGet) {
  314. $level = \Yii::$app->request->get('level');
  315. $where = [['mall_id'=>$this->mall_id],['=','status',StatusEnum::ENABLED]];
  316. if(!empty($level)) $where[]=['level'=>$level];
  317. $args['where'] = $where;
  318. $args['select'] = 'id,level,name';
  319. $args['order'] = 'level asc';
  320. $list['list'] = DistributionLevel::lists($args,true);
  321. $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_DISTRIBUTION, 'basic');
  322. if(!isset($configs['is_default_level'])||!empty($configs['is_default_level'])){
  323. $default_level_name=!empty($configs['default_level_name'])?$configs['default_level_name']:'默认等级';
  324. array_unshift($list['list'],['id'=>0, 'level'=> 0, 'name'=> $default_level_name]);
  325. }
  326. return $this->success('操作成功', $list);
  327. }
  328. }
  329. return $this->error();
  330. }
  331. public function actionCommissionBasicLog()
  332. {
  333. if (\Yii::$app->request->isAjax) {
  334. if (\Yii::$app->request->isGet) {
  335. $get = \Yii::$app->request->get();
  336. $get['mall_id'] = $this->mall_id;
  337. $params = [];
  338. if (!empty($get['source_table_id'])) {
  339. $params['where'][] = ['source_table_id'=> $get['source_table_id']];
  340. }
  341. $params['where'][] = ['=', 'mall_id',$this->mall_id];
  342. $params['where'][] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  343. $params['order'] = 'id desc';
  344. $params['limit'] = 10;
  345. $pageData = DistributionCommissionBasisLog::listPage($params, false);
  346. if ($pageData === false) return $this->error(Distribution::getStaticError());
  347. if(!empty($pageData['list'])){
  348. foreach ($pageData['list'] as &$item){
  349. if($item['basic_config']) $item['basic_config'] = json_encode(json_decode($item['basic_config'],true),JSON_UNESCAPED_UNICODE);
  350. if($item['level_config']) $item['level_config'] = json_encode(json_decode($item['level_config'],true),JSON_UNESCAPED_UNICODE);
  351. if($item['goods_config']) $item['goods_config'] = json_encode(json_decode($item['goods_config'],true),JSON_UNESCAPED_UNICODE);
  352. }
  353. }
  354. return $this->success('操作成功', $pageData);
  355. }
  356. }
  357. return $this->render('commission-basic-log', []);
  358. }
  359. /**
  360. * 处理
  361. * @Author: lun
  362. * @DateTime: 2022/5/14 0014 16:47
  363. * @Copyright: copyright (c) 2021 广东七件事集团
  364. * @return mixed|void
  365. */
  366. public function actionHandle()
  367. {
  368. $request = Yii::$app->request;
  369. if ($request->isAjax) {
  370. if ($request->isGet) {
  371. $params = Yii::$app->request->get();
  372. // 检查提交的参数
  373. $res = Yii::$app->services->validate->validate($params,[
  374. [['id'], 'required'],
  375. [['id','status','is_height_commission'], 'integer'],
  376. ]);
  377. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  378. if (!isset($params['status'])) unset($params['status']);
  379. if (!isset($params['is_height_commission'])) unset($params['is_height_commission']);
  380. $params['mall_id'] = $this->mall_id;
  381. $res = Distribution::setData($params);
  382. if (!$res) return $this->error('修改失败:msg=' . DoubleCopyLevel::getStaticError());
  383. return $this->success('修改成功');
  384. }
  385. }
  386. }
  387. /**
  388. * 处理
  389. * @Author: lun
  390. * @DateTime: 2022/5/14 0014 16:47
  391. * @Copyright: copyright (c) 2021 广东七件事集团
  392. * @return mixed|void
  393. */
  394. public function actionCommission()
  395. {
  396. $request = Yii::$app->request;
  397. if ($request->isAjax) {
  398. if ($request->isPost) {
  399. $params = Yii::$app->request->post();
  400. // 检查提交的参数
  401. $res = Yii::$app->services->validate->validate($params,[
  402. [['id','height_commission'], 'required'],
  403. ]);
  404. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  405. $params['mall_id'] = $this->mall_id;
  406. $res = Distribution::setData($params);
  407. if (!$res) return $this->error('设置失败:msg=' . Distribution::getStaticError());
  408. return $this->success('设置成功');
  409. }
  410. }
  411. }
  412. }