DefaultController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. <?php
  2. namespace addons\Area\backend\controllers;
  3. use addons\Area\common\enums\AreaEnum;
  4. use addons\Area\common\models\AreaAgent;
  5. use addons\Area\common\models\AreaApply;
  6. use addons\Area\common\models\AreaOrder;
  7. use addons\Area\common\models\AreaCommissionLog;
  8. use addons\Area\common\models\AreaRegional;
  9. use common\enums\StatusEnum;
  10. use common\enums\UserWalletEnum;
  11. use common\helpers\DateHelper;
  12. use common\models\goods\Goods;
  13. use common\models\user\Town;
  14. use common\models\user\User;
  15. use services\common\UserWalletService;
  16. use Yii;
  17. use yii\helpers\Json;
  18. /**
  19. * 默认控制器
  20. *
  21. * Class DefaultController
  22. * @package addons\Area\backend\controllers
  23. */
  24. class DefaultController extends BaseController
  25. {
  26. public $enableCsrfValidation = false;
  27. public function actionIndex()
  28. {
  29. if (\Yii::$app->request->isAjax) {
  30. if (\Yii::$app->request->isGet) {
  31. $params = \Yii::$app->request->get();
  32. $mall_id = $this->mall_id;
  33. $where[] = ['mall_id' => $mall_id];
  34. $where[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  35. $user_ids = [];
  36. if (!empty($params['level'])) $where[] = ['level' => $params['level']];
  37. if (!empty($params['start'])) $where[] = ['>=', 'created_at', strtotime($params['start'] . ' 00:00:00')];
  38. if (!empty($params['end'])) $where[] = ['<=', 'created_at', strtotime($params['end'] . ' 23:59:59')];
  39. if (!empty($params['keyword'])) {
  40. $is_search = 1;
  41. $user_list = User::lists(['where' => [['mall_id' => $this->mall_id], [
  42. 'or',
  43. ['=', 'id', $params['keyword']],
  44. ['like', 'nickname', $params['keyword'] . '%', false],
  45. ['like', 'mobile', $params['keyword'] . '%', false]
  46. ]], 'select' => 'id']);
  47. $user_ids = array_column($user_list, 'id');
  48. }
  49. if(!empty($params['province'])) {
  50. $where[] =['IN', 'id', (array)AreaAgent::find()->where(['mall_id' => $mall_id])
  51. ->andWhere(['OR',['province_id' => $params['province']],['city_id' => $params['province']], ['district_id' => $params['province']]])
  52. ->select('id')
  53. ->asArray()
  54. ->column()];
  55. }
  56. if (!empty($is_search)) $where[] = ['user_id' => $user_ids];
  57. $params['limit'] = 10;
  58. $params['where'] = $where;
  59. $params['order'] = 'id DESC';
  60. $params['select'] = 'id,user_id,realname,mobile,level,address,regional_id,province_id,city_id,district_id,town_id,total_commission,created_at,community_id, is_enable_dividend';
  61. $params['with'] = ['user' => function ($query) use ($mall_id) {
  62. $query->where(['mall_id' => $mall_id])->select('id,nickname,avatar_url,mobile,parent_id')->with(['parent']);
  63. }, 'goods' => function($query) {
  64. $query->with(['goods']);
  65. }];
  66. $pageData = AreaAgent::listPage($params, false);
  67. if ($pageData === false) return $this->error(AreaAgent::getStaticError());
  68. $pageData['level_map'] = AreaEnum::getLevelMap(null, $this->mall_id);
  69. $config = \Yii::$app->services->setting->addons->get($this->mall_id, AreaEnum::ADDONS_NAME, 'basic');
  70. $pageData['config']['is_enable_only_dividend'] = $config['is_enable_only_dividend'] ?? StatusEnum::DISABLED;
  71. $pageData['config']['only_dividend_goods_type'] = $config['only_dividend_goods_type'] ?? StatusEnum::DISABLED;
  72. return $this->success('操作成功', $pageData);
  73. }
  74. }
  75. return $this->render('index', []);
  76. }
  77. /**
  78. * @desc:审核列表
  79. * @Author: hua
  80. * @Date: 2022/1/21
  81. * @Time: 14:28
  82. * @Copyright: copyright (c) 2021 广东七件事集团
  83. * @return mixed|string|void
  84. */
  85. public function actionApplyList()
  86. {
  87. if (\Yii::$app->request->isAjax) {
  88. if (\Yii::$app->request->isGet) {
  89. $params = \Yii::$app->request->get();
  90. $mall_id = $this->mall_id;
  91. $where[] = ['mall_id' => $mall_id];
  92. $where[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  93. $user_ids = [];
  94. if (isset($params['check_status']) && $params['check_status'] != -1 && $params['check_status'] !== '') $where[] = ['check_status' => $params['check_status']];
  95. if (!empty($params['month'])) {
  96. $where[] = ['>=', 'created_at', strtotime($params['month'])];
  97. $where[] = ['<=', 'created_at', DateHelper::endOfMonth($params['month'])];
  98. }
  99. if (!empty($params['keyword'])) {
  100. $is_search = 1;
  101. $user_list = User::lists(['where' => [['mall_id' => $this->mall_id], [
  102. 'or',
  103. ['=', 'id', $params['keyword']],
  104. ['like', 'nickname', $params['keyword']],
  105. ]], 'select' => 'id']);
  106. $user_ids = array_column($user_list, 'id');
  107. }
  108. if (!empty($is_search)) $where[] = ['user_id' => $user_ids];
  109. $params['limit'] = 10;
  110. $params['where'] = $where;
  111. $params['order'] = 'id DESC';
  112. $params['with'] = ['user' => function ($query) use ($mall_id) {
  113. $query->where(['mall_id' => $mall_id])->select('id,nickname,avatar_url,mobile');
  114. }];
  115. $pageData = AreaApply::listPage($params, false);
  116. if ($pageData === false) return $this->error(AreaAgent::getStaticError());
  117. $pageData['level_map'] = AreaEnum::getLevelMap(null, $this->mall_id);
  118. $pageData['check_status_map'] = AreaEnum::getCheckStatusMap();
  119. return $this->success('操作成功', $pageData);
  120. }
  121. }
  122. return $this->render('apply-list', []);
  123. }
  124. /**
  125. * @desc:审核
  126. * @Author: hua
  127. * @Date: 2022/1/21
  128. * @Time: 14:40
  129. * @Copyright: copyright (c) 2021 广东七件事集团
  130. * @return mixed|void
  131. */
  132. public function actionApply()
  133. {
  134. $params = \Yii::$app->request->post();
  135. $params['mall_id'] = $this->mall_id;
  136. $res = AreaApply::apply($params);
  137. if ($res === false) return $this->error(AreaApply::getStaticError());
  138. return $this->success('操作成功');
  139. }
  140. /**
  141. * @desc:基础配置
  142. * @Author: hua
  143. * @Date: 2022/1/21
  144. * @Time: 14:40
  145. * @Copyright: copyright (c) 2021 广东七件事集团
  146. * @return mixed|string|void
  147. */
  148. public function actionSetting()
  149. {
  150. if (\Yii::$app->request->isAjax) {
  151. try {
  152. if (\Yii::$app->request->isPost) {
  153. $params = \Yii::$app->request->post();
  154. $params['mall_id'] = $this->mall_id;
  155. \Yii::$app->services->setting->addons->set($this->mall_id, AreaEnum::ADDONS_NAME, ['basic' => $params]);
  156. return $this->success('操作成功');
  157. }
  158. if (\Yii::$app->request->isGet) {
  159. $list = \Yii::$app->services->setting->addons->get($this->mall_id, AreaEnum::ADDONS_NAME, 'basic');
  160. if(!isset($list['is_show_at_center'])) $list['is_show_at_center'] = 1;
  161. if(!isset($list['equal_reward_is_equal'])) $list['equal_reward_is_equal'] = 0;
  162. return $this->success('操作成功', ['list' => $list, 'addons_ext_config' => $this->getAddonsConfig()]);
  163. }
  164. }catch (\Exception $e) {
  165. return $this->error($e->getMessage());
  166. }
  167. }
  168. return $this->render('setting', []);
  169. }
  170. /**
  171. * @desc:保存区域管理
  172. * @Author: hua
  173. * @Date: 2022/1/19
  174. * @Time: 17:05
  175. * @Copyright: copyright (c) 2021 广东七件事集团
  176. * @return mixed|void
  177. */
  178. public function actionEdit()
  179. {
  180. try {
  181. if (\Yii::$app->request->isAjax) {
  182. if (\Yii::$app->request->isPost) {
  183. $params = \Yii::$app->request->post();
  184. $res = Yii::$app->services->validate->validate($params, [
  185. [['level'], 'required'],
  186. [['id', 'user_id', 'level', 'regional_id', 'province_id', 'city_id', 'district_id', 'town_id',
  187. 'community_id', 'is_enable_dividend'], 'integer'],
  188. [['realname', 'mobile'], 'string'],
  189. [['goods'], 'safe'],
  190. ]);
  191. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  192. $params['mall_id'] = $this->mall_id;
  193. $params['status'] = StatusEnum::ENABLED;
  194. if (!empty($params['goods'])) {
  195. $params['goods'] = Json::decode($params['goods']);
  196. } else {
  197. $params['goods'] = [];
  198. }
  199. $configs = \Yii::$app->services->setting->addons->get($params['mall_id'], AreaEnum::ADDONS_NAME, 'basic');
  200. if ($configs['is_buy_area'] == 1 && $params['level'] == 3) throw new \Exception('已开启区代申请条件,无法进行添加、编辑区代操作');
  201. $res = AreaAgent::setData($params);
  202. if ($res === false) return $this->error(AreaAgent::getStaticError());
  203. return $this->success('操作成功');
  204. }
  205. }
  206. }catch (\Exception $e) {
  207. return $this->error($e->getMessage());
  208. }
  209. }
  210. /**
  211. * @desc:删除区域代理
  212. * @Author: hua
  213. * @Date: 2022/1/21
  214. * @Time: 9:33
  215. * @Copyright: copyright (c) 2021 广东七件事集团
  216. * @return mixed|void
  217. */
  218. public function actionDelete()
  219. {
  220. try {
  221. if (\Yii::$app->request->isAjax) {
  222. if (\Yii::$app->request->isPost) {
  223. $params = \Yii::$app->request->post();
  224. $res = Yii::$app->services->validate->validate($params, [
  225. [['id'], 'required'],
  226. [['id'], 'integer']
  227. ]);
  228. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  229. //获取当前代理数据
  230. $check = AreaAgent::getOne([
  231. ['mall_id' => $this->mall_id],
  232. ['id' => $params['id']]
  233. ]);
  234. $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AreaEnum::ADDONS_NAME, 'basic');
  235. if ($configs['is_buy_area'] == 1 && $check['level'] == 3) return $this->error('已开启区代申请条件,无法删除区代身份');
  236. $params['mall_id'] = $this->mall_id;
  237. $params['status'] = StatusEnum::DELETE;
  238. $res = AreaAgent::updateAll(['status' => StatusEnum::DELETE], ['id' => $params['id'], 'mall_id' => $this->mall_id]);
  239. if ($res === false) return $this->error(AreaAgent::getStaticError());
  240. if ($check['level'] == 3) {
  241. $order_data = AreaOrder::getOne([
  242. ['mall_id' => $this->mall_id],
  243. ['user_id' => $check['user_id']],
  244. ['area_id' => $check['district_id']],
  245. ['status' => StatusEnum::ENABLED],
  246. ['is_pay' => StatusEnum::ENABLED],
  247. ]);
  248. //如果删除的区代存在购买数据,则退回购买金额
  249. if (!empty($order_data)) {
  250. $insert_wallet[] = [
  251. 'mall_id' => $order_data->mall_id,
  252. 'user_id' => $order_data->user_id,
  253. 'is_frozen' => false,
  254. 'wallet_type' => 'balance',
  255. 'money' => $order_data->price, //转入金额
  256. 'desc' => '代理身份被删除,退回金额',
  257. 'source_table' => 'addons_area_order',
  258. 'source_table_id' => $order_data->id,//结算周期id
  259. 'from_type' => UserWalletEnum::REFUND,
  260. 'capital_type' => UserWalletEnum::AREA_BONUS_REFUND,
  261. // 'not_add_total' => true,
  262. ];
  263. $order_data->status = StatusEnum::DELETE;
  264. $order_data->save();
  265. //维护申请记录数据
  266. AreaApply::updateAll(['is_refund' => 1, 'refund_time' => time(), 'refund_remark' => '代理身份被删除', 'check_status' => 2], ['mall_id' => $this->mall_id, 'order_id' => $order_data['id']]);
  267. if (!empty($insert_wallet)) {
  268. UserWalletService::batchHandleByQueue($insert_wallet);
  269. }
  270. }
  271. }
  272. return $this->success('操作成功');
  273. }
  274. }
  275. }catch (\Exception $e) {
  276. return $this->error($e->getMessage());
  277. }
  278. }
  279. /**
  280. * @desc:添加区域代理,搜索用户,不用验证权限
  281. * @Author: hua
  282. * @Date: 2022/1/19
  283. * @Time: 16:09
  284. * @Copyright: copyright (c) 2021 广东七件事集团
  285. * @return mixed|void
  286. */
  287. public function actionSearchUser()
  288. {
  289. if (\Yii::$app->request->isAjax) {
  290. $keyword = \Yii::$app->request->get('keyword', '');
  291. $list['list'] = User::searchUser(['keyword' => $keyword, 'mall_id' => $this->mall_id]);
  292. return $this->success('操作成功', $list);
  293. }
  294. return $this->error();
  295. }
  296. /**
  297. * @desc:添加区域代理,获取省市区,不需要验证权限
  298. * @Author: hua
  299. * @Date: 2022/1/19
  300. * @Time: 16:09
  301. * @Copyright: copyright (c) 2021 广东七件事集团
  302. * @return mixed|void
  303. */
  304. public function actionDistrict()
  305. {
  306. $params = \Yii::$app->request->get();
  307. $res = Yii::$app->services->validate->validate($params, [
  308. [['level'], 'required'],
  309. [['level'], 'integer'],
  310. ]);
  311. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  312. $list = AreaAgent::getDistrictList($params);
  313. return $this->success('操作成功', ['district' => $list]);
  314. }
  315. /**
  316. * @desc:添加区域代理,获取镇列表,不用验证权限
  317. * @Author: hua
  318. * @Date: 2022/1/19
  319. * @Time: 16:10
  320. * @Copyright: copyright (c) 2021 广东七件事集团
  321. * @return mixed|void
  322. */
  323. public function actionGetTownList()
  324. {
  325. $params = \Yii::$app->request->get();
  326. $res = Yii::$app->services->validate->validate($params, [
  327. [['district_id'], 'required'],
  328. [['district_id'], 'integer'],
  329. ]);
  330. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  331. $district_id = $params['district_id'];
  332. $list = Town::lists(['where' => [['district_id' => $district_id]], 'select' => 'id,district_id,name']);
  333. return $this->success('操作成功', ['list' => $list]);
  334. }
  335. /**
  336. * @return mixed|string|null
  337. */
  338. public function actionCommission()
  339. {
  340. if (Yii::$app->request->isAjax && Yii::$app->request->isGet) {
  341. $params = \Yii::$app->request->get();
  342. $res = Yii::$app->services->validate->validate($params, [
  343. [['user_id', 'level', 'status', 'limit', 'page'], 'integer'],
  344. [['goods_name', 'date_start', 'date_end', 'member'], 'string'],
  345. ], [
  346. 'user_id' => '用户ID',
  347. 'area_level' => '区域代理等级',
  348. 'status' => '佣金状态',
  349. 'member' => '会员昵称/手机号',
  350. 'goods_name' => '商品名称',
  351. 'date_start' => '开始时间',
  352. 'date_end' => '结束时间'
  353. ]);
  354. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  355. // 查询
  356. $where = [
  357. ['mall_id' => $this->mall_id],
  358. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
  359. ];
  360. // 分佣状态
  361. if (isset($params['status']) && $params['status'] !== '' && in_array($params['status'], [0, 1])) {
  362. $where[] = ['commission_status' => $params['status']];
  363. }
  364. // 时间区间
  365. if (!empty($params['date_start']) && !empty($params['date_end'])) {
  366. $s = strtotime($params['date_start']);
  367. $e = strtotime($params['date_end']);
  368. $where[] = ['between', 'created_at', $s, $e];
  369. } elseif (!empty($params['date_start'])) {
  370. $s = strtotime($params['date_start']);
  371. $where[] = ['<=', 'created_at', $s];
  372. } elseif (!empty($params['date_end'])) {
  373. $e = strtotime($params['date_end']);
  374. $where[] = ['>=', 'created_at', $e];
  375. }
  376. // 商品
  377. if (!empty($params['goods_name'])) {
  378. $gids = Goods::find()->where(['mall_id' => $this->mall_id])
  379. ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  380. ->andWhere(['like', 'goods_name', $params['goods_name']])
  381. ->select('id')
  382. ->column();
  383. if (!empty($gids)) {
  384. $where[] = ['goods_id' => $gids];
  385. } else {
  386. return $this->success('获取成功');
  387. }
  388. }
  389. $uids = [];
  390. if (!empty($params['user_id'])) {
  391. $uids[] = $params['user_id'];
  392. }
  393. if (!empty($params['member'])) {
  394. $s_uids = User::find()->where(['mall_id' => $this->mall_id])
  395. ->andWhere(['status' => StatusEnum::ENABLED])
  396. ->andWhere(['or', ['like', 'nickname', $params['member']],
  397. ['like', 'username', $params['member']],
  398. ['like', 'mobile', $params['member']]])
  399. ->select('id')->column();
  400. if (!empty($s_uids)) {
  401. array_push($uids, ...$s_uids);
  402. } else {
  403. return $this->success('获取成功');
  404. }
  405. }
  406. // 区域代理等级
  407. if (!empty($params['level'])) {
  408. // $a_uids = AreaAgent::find()
  409. // ->where(['mall_id' => $this->mall_id])
  410. // ->andWhere(['status' => StatusEnum::ENABLED])
  411. // ->andWhere(['level' => $params['area_level']])
  412. // ->select('user_id')
  413. // ->column();
  414. // if (!empty($a_uids)) array_push($uids, ...$a_uids);
  415. $where[] = ['area_level' => $params['level']];
  416. }
  417. if (!empty($uids)) $where[] = ['user_id' => $uids];
  418. $ret = AreaCommissionLog::listPage([
  419. 'where' => $where,
  420. 'order' => 'id desc',
  421. 'with' => ['user', 'order', 'goods'],
  422. 'limit' => $params['limit'] ?? 10
  423. ]);
  424. if (!empty(AreaCommissionLog::getStaticError())) return $this->error(AreaCommissionLog::getStaticError());
  425. $ret['level_map'] = AreaEnum::getLevelMap(null, $this->mall_id);
  426. return $this->success('获取成功', $ret);
  427. }
  428. return $this->render('commission');
  429. }
  430. /**
  431. * @return array|mixed
  432. */
  433. protected function getAddonsConfig()
  434. {
  435. return Yii::$app->params['addons_ext_config'][AreaEnum::ADDONS_NAME] ?? [];
  436. }
  437. public function actionLevelList()
  438. {
  439. $ret = AreaEnum::getLevelMap(null, $this->mall_id);
  440. // if (!empty($ret[0])) unset($ret[0]);
  441. $this->success('获取成功', $ret);
  442. }
  443. }