DefaultController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. <?php
  2. namespace addons\Area\api\modules\v1\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\AreaCommunity;
  7. use addons\Area\common\models\AreaGoods;
  8. use addons\Area\common\models\AreaOrder;
  9. use addons\Store\common\models\StoreOrderDetail;
  10. use common\enums\AddonsEnum;
  11. use common\enums\ApiStatusEnum;
  12. use common\enums\StatusEnum;
  13. use common\enums\UserWalletEnum;
  14. use common\helpers\ArrayHelper;
  15. use common\models\common\CapitalLog;
  16. use common\models\common\Region;
  17. use common\models\order\OrderDetail;
  18. use common\models\user\Town;
  19. use common\models\user\User;
  20. use common\models\user\UserPartitionRelationship;
  21. use Couchbase\DesignDocument;
  22. use Yii;
  23. use api\controllers\OnAuthController;
  24. use Exception;
  25. /**
  26. * 默认控制器
  27. *
  28. * Class DefaultController
  29. * @package addons\Area\api\modules\v1\controllers
  30. */
  31. class DefaultController extends OnAuthController
  32. {
  33. public $modelClass = '';
  34. /**
  35. * 不用进行登录验证的方法
  36. *
  37. * 例如: ['index', 'update', 'create', 'view', 'delete']
  38. * 默认全部需要验证
  39. *
  40. * @var array
  41. */
  42. protected $authOptional = ['index'];
  43. protected $level_id_arr = [AreaEnum::PROVINCE => 'province_id', AreaEnum::CITY => 'city_id', AreaEnum::DISTRICT => 'district_id', AreaEnum::TOWN => 'town_id'];
  44. /**
  45. * @desc:区域管理中心
  46. * @Author: hua
  47. * @Date: 2022/1/20
  48. * @Time: 16:07
  49. * @Copyright: copyright (c) 2021 广东七件事集团
  50. * @return mixed|void
  51. */
  52. public function actionCenter()
  53. {
  54. $user_id = $this->user_id;
  55. $area_agent = AreaAgent::getOne([['mall_id' => $this->mall_id], ['user_id' => $user_id],['status'=>StatusEnum::ENABLED]], true, ['user']);
  56. $show_tips = '';
  57. if (empty($area_agent)) $show_tips = '请先申请区域代理';
  58. $wallet_type = 'commission';
  59. $data = CapitalLog::getSummary($user_id, $wallet_type, AreaEnum::ADDONS_NAME);
  60. $data['user'] = User::find()
  61. ->select('id, nickname, avatar_url')
  62. ->where(['id' => $this->user_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  63. ->asArray()->one();
  64. $areaAgent = AreaAgent::find()
  65. ->select('id, user_id, level, address, created_at')
  66. ->where(['user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  67. ->with([
  68. 'user' => function ($query) {
  69. $query->select('id, nickname, avatar_url');
  70. }
  71. ])
  72. ->asArray()->all();
  73. $area_agents = [];
  74. $data['level_map'] = AreaEnum::getLevelMap(null, $this->mall_id);
  75. $area_agent_list = AreaAgent::find()->where(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED], 'mall_id' => $this->mall_id])
  76. ->select('level, count(id) as counts')
  77. ->groupBy('level')
  78. ->indexBy('level')
  79. ->asArray()->all();
  80. foreach ($data['level_map'] as $k => $v) {
  81. if (empty($k)) continue;
  82. if (isset($area_agent_list[$k])) {
  83. $area_agents[] = array_merge($area_agent_list[$k], ['name' => $v]);
  84. }else {
  85. $area_agents[] = ['level' => $k, 'counts' => 0, 'name' => $v];
  86. }
  87. }
  88. if (!empty($areaAgent)) {
  89. foreach ($areaAgent as &$item) {
  90. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  91. }
  92. }
  93. if (isset($data['level_map'][6])) unset($data['level_map'][6]);
  94. $data['area_agent_list'] = $areaAgent ?? [];
  95. $data['area_agents'] = $area_agents ?? [];
  96. $data['show_tips'] = $show_tips;
  97. $result = AreaAgent::getIncomeInfo(['user_id' => $user_id, 'mall_id' => $this->mall_id, 'addons_name' => AddonsEnum::ADDONS_NAME_AREA]);
  98. $data['total_income'] = $result['total_commission'];
  99. $config = \Yii::$app->services->setting->addons->get($this->mall_id, AreaEnum::ADDONS_NAME, 'basic');
  100. $data['is_enable_apply'] = $config['is_enable_apply'] ?? StatusEnum::ENABLED;
  101. $data['protocol'] = $config['protocol'] ?? "";
  102. $data['is_buy_area'] = $config['is_buy_area'] ?? StatusEnum::DISABLED;
  103. return $this->success('成功', $data);
  104. }
  105. /**
  106. * @desc:奖励列表
  107. * @Author: hua
  108. * @Date: 2021/11/4
  109. * @Time: 17:00
  110. * @Copyright: copyright (c) 2021 广东七件事集团
  111. * @return mixed|void
  112. */
  113. public function actionLogList()
  114. {
  115. $params = \Yii::$app->request->get();
  116. $wallet_type = 'commission';
  117. if(!empty($params['list_type'])&&$params['list_type'] ==1){//未结算
  118. $wallet_type = 'commission_frozen';
  119. $params['status'] = StatusEnum::DISABLED;
  120. }
  121. $params['mall_id'] = $this->mall_id;
  122. $params['user_id'] = $this->user_id;
  123. $list = CapitalLog::getCapitalLogList($params, $wallet_type, AreaEnum::ADDONS_NAME, ', change_type');
  124. if (!empty($list)) {
  125. foreach ($list['list'] as &$item) {
  126. $item['status'] = 1;
  127. if(!empty($params['list_type'])&&$params['list_type'] ==1){
  128. $item['status'] = 0;
  129. }
  130. }
  131. }
  132. $list['capital_type_map'] = UserWalletEnum::getCapitalTypeMapByCommission();
  133. return $this->success('成功', $list);
  134. }
  135. /**
  136. * @desc:我的代理
  137. * @Author: hua
  138. * @Date: 2022/1/20
  139. * @Time: 16:37
  140. * @Copyright: copyright (c) 2021 广东七件事集团
  141. */
  142. public function actionGetChildrenList()
  143. {
  144. $child_ids = UserPartitionRelationship::getChildIdArr($this->user_id);
  145. $params = Yii::$app->request->get();
  146. $where = [['mall_id' => $this->mall_id], ['user_id' => $child_ids], ['status' => StatusEnum::ENABLED]];
  147. $where[] = !empty($params['level']) ? ['level' => $params['level']] : ['level' => AreaEnum::PROVINCE];
  148. $params['where'] = $where;
  149. $params['with'] = ['user' => function ($query) {
  150. $query->select('id,avatar_url,nickname,mobile');
  151. }];
  152. $params['select'] = 'id,user_id,address,level';
  153. $list = AreaAgent::listPage($params);
  154. $list['level_map'] = AreaEnum::getLevelMap(null, $this->mall_id);
  155. $area_agents = [];
  156. $area_agent_list = AreaAgent::lists(['where' => [['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED], ['user_id' => $child_ids]], 'group' => 'level', 'select' => 'level,count(*) counts', 'index' => 'level']);
  157. foreach ($list['level_map'] as $k => $v) {
  158. if (empty($k)) continue;
  159. if (isset($area_agent_list[$k])) {
  160. $area_agents[] = array_merge($area_agent_list[$k], ['name' => $v]);
  161. }else {
  162. $area_agents[] = ['level' => $k, 'counts' => 0, 'name' => $v];
  163. }
  164. }
  165. if (isset($list['level_map'][6])) unset($list['level_map'][6]);
  166. $list['area_agents'] = $area_agents;
  167. return $this->success('成功', $list);
  168. }
  169. /**
  170. * @desc:获取区域列表
  171. * @Author: hua
  172. * @Date: 2022/1/20
  173. * @Time: 16:07
  174. * @Copyright: copyright (c) 2021 广东七件事集团
  175. * @return mixed|void
  176. */
  177. public function actionGetRegion()
  178. {
  179. $params = Yii::$app->request->get();
  180. $res = Yii::$app->services->validate->validate($params, [
  181. [['parent_id'], 'integer']
  182. ]);
  183. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  184. if (empty($params['parent_id'])) {
  185. $region = Region::findOne(['level' => 0]);
  186. $parent_id = $region['id'];
  187. } else {
  188. $parent_id = $params['parent_id'];
  189. }
  190. $list = AreaAgent::getRegionList($parent_id, $this->mall_id, $this->level_id_arr);
  191. // $list['level_map'] = AreaEnum::getLevelMap(null, $this->mall_id);
  192. $list = ArrayHelper::removeDuplicates($list, 'name');
  193. return $this->success('成功', $list);
  194. }
  195. /**
  196. * @desc:申请页面-根据省、市、区、镇获取地区列表
  197. * @Author: hua
  198. * @Date: 2022/1/20
  199. * @Time: 16:08
  200. * @Copyright: copyright (c) 2021 广东七件事集团
  201. * @return mixed|void
  202. */
  203. public function actionGetDistrictList()
  204. {
  205. $params = Yii::$app->request->get();
  206. $res = Yii::$app->services->validate->validate($params, [
  207. [['level'], 'required'],
  208. [['level'], 'integer'],
  209. ]);
  210. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  211. if ($params['level'] > 4) $params['level'] = 4;
  212. $list = AreaAgent::getDistrictList($params);
  213. return $this->success('操作成功', $list);
  214. }
  215. /**
  216. * @desc:获取镇列表
  217. * @Author: hua
  218. * @Date: 2022/1/20
  219. * @Time: 16:08
  220. * @Copyright: copyright (c) 2021 广东七件事集团
  221. * @return mixed|void
  222. */
  223. public function actionGetTownList()
  224. {
  225. $params = \Yii::$app->request->get();
  226. $res = Yii::$app->services->validate->validate($params, [
  227. [['district_id'], 'required'],
  228. [['district_id','type'], 'integer'],
  229. ]);
  230. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  231. $district_id = $params['district_id'];
  232. $list = Town::lists(['where' => [['district_id' => $district_id]], 'select' => 'id,district_id,name']);
  233. if(!empty($params['type'])){
  234. $area_agent_list = AreaAgent::lists([
  235. 'where' => [
  236. ['district_id' => $params['district_id']],
  237. ['level' => 4],
  238. ['mall_id' => $this->mall_id],
  239. ['status' => StatusEnum::ENABLED]
  240. ],
  241. 'select' => 'id,user_id,town_id',
  242. 'with' => ['user' => function ($query) {
  243. $query->select('id,nickname,avatar_url');
  244. }],
  245. ]);
  246. $area_agent_arr = [];
  247. foreach ($area_agent_list as $val){
  248. $area_agent_arr[$val['town_id']][]=$val;
  249. }
  250. foreach ($list as &$item) {
  251. $item['agent_list'] = [];
  252. if (isset($area_agent_arr[$item['id']])) $item['agent_list'] = $area_agent_arr[$item['id']];
  253. }
  254. }
  255. if (!empty($list)) {
  256. foreach ($list as &$item) {
  257. $item['level'] = 4;
  258. }
  259. }
  260. return $this->success('操作成功', $list);
  261. }
  262. /**
  263. * @desc:获取小区列表
  264. * @Author: hua
  265. * @Date: 2022/1/20
  266. * @Time: 16:08
  267. * @Copyright: copyright (c) 2021 广东七件事集团
  268. * @return mixed|void
  269. */
  270. public function actionGetCommunityList()
  271. {
  272. $params = \Yii::$app->request->get();
  273. $res = Yii::$app->services->validate->validate($params, [
  274. [['town_id'], 'required'],
  275. [['town_id','type'], 'integer'],
  276. ]);
  277. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  278. $town_id = $params['town_id'];
  279. $list = AreaCommunity::lists(['where' => [['town_id' => $town_id, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]], 'select' => 'id,town_id,community_name as name']);
  280. if(!empty($params['type'])){
  281. $area_agent_list = AreaAgent::lists([
  282. 'where' => [
  283. ['town_id' => $params['town_id']],
  284. ['level' => 5],
  285. ['mall_id' => $this->mall_id],
  286. ['status' => StatusEnum::ENABLED]
  287. ],
  288. 'select' => 'id,user_id,community_id',
  289. 'with' => ['user' => function ($query) {
  290. $query->select('id,nickname,avatar_url');
  291. }],
  292. ]);
  293. $area_agent_arr = [];
  294. foreach ($area_agent_list as $val){
  295. $area_agent_arr[$val['community_id']][]=$val;
  296. }
  297. foreach ($list as &$item) {
  298. $item['agent_list'] = [];
  299. if (isset($area_agent_arr[$item['id']])) $item['agent_list'] = $area_agent_arr[$item['id']];
  300. }
  301. }
  302. if (!empty($list)) {
  303. foreach ($list as &$item) {
  304. $item['level'] = 5;
  305. }
  306. }
  307. return $this->success('操作成功', $list);
  308. }
  309. /**
  310. * @desc:提交申请
  311. * @Author: hua
  312. * @Date: 2022/1/20
  313. * @Time: 9:42
  314. * @Copyright: copyright (c) 2021 广东七件事集团
  315. * @return mixed|void
  316. */
  317. public function actionApply()
  318. {
  319. $params = \Yii::$app->request->post();
  320. $res = Yii::$app->services->validate->validate($params, [
  321. [['level', 'mobile', 'realname'], 'required'],
  322. [['level', 'province_id', 'city_id', 'district_id', 'town_id', 'community_id'], 'integer'],
  323. [['marks',], 'string']
  324. ]);
  325. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  326. $params['town_id'] = $params['town_id'] ?:0;
  327. $params['community_id'] = $params['community_id'] ?: 0;
  328. $model = AreaAgent::findOne([
  329. 'user_id' => $this->user_id,
  330. 'mall_id' => $this->mall_id,
  331. 'province_id' => $params['province_id'],
  332. 'city_id' => $params['city_id'],
  333. 'district_id' => $params['district_id'],
  334. 'town_id' => $params['town_id'],
  335. 'community_id' => $params['community_id'],
  336. 'level' => $params['level'],
  337. 'status' => StatusEnum::ENABLED,
  338. ]);
  339. if (!empty($model)) return $this->error('您已经是该区域代理!');
  340. switch ($params['level']) {
  341. case AreaEnum::PROVINCE:
  342. $field = 'province_id';
  343. $value = $params['province_id'];
  344. break;
  345. case AreaEnum::CITY:
  346. $field = 'city_id';
  347. $value = $params['city_id'];
  348. break;
  349. case AreaEnum::DISTRICT:
  350. $field = 'district_id';
  351. $value = $params['district_id'];
  352. break;
  353. case AreaEnum::TOWN:
  354. $field = 'town_id';
  355. $value = $params['town_id'];
  356. break;
  357. case AreaEnum::COMMUNITY:
  358. $field = 'community_id';
  359. $value = $params['community_id'];
  360. break;
  361. default:
  362. return $this->error('请选择区域');
  363. }
  364. $res = AreaApply::findOne([
  365. 'user_id' => $this->user_id,
  366. 'mall_id' => $this->mall_id,
  367. 'check_status' => AreaEnum::NOT_APPROVED,
  368. $field => $value,
  369. ]);
  370. if (!empty($res)) return $this->error('你已经提交过该地区的申请了,请耐心等待审核结果!');
  371. $config = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_AREA, 'basic');
  372. $counts = AreaAgent::find()->where(['user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])->count();
  373. $apply_counts = AreaApply::find()->where(['user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'check_status' => AreaEnum::NOT_APPROVED])->count();
  374. if (!empty($config['can_apply_area_num']) && $config['can_apply_area_num'] != -1 && ($counts + $apply_counts) >= $config['can_apply_area_num']) return $this->error('超出可申请最大数量!');
  375. //如果开启了购买申请资格按钮
  376. if ($config['is_buy_area'] == 1 && $params['level'] == 3) {
  377. // if (empty($params['order_id'])) return $this->error('请先购买申请资格');
  378. //查询是否已购买资格
  379. $order_data = AreaOrder::getOne([
  380. ['mall_id' => $this->mall_id],
  381. ['user_id' => $this->user_id],
  382. ['level' => $params['level']],
  383. ['area_id' => $params['district_id']],
  384. ['is_pay' => StatusEnum::ENABLED],
  385. ['status' => StatusEnum::ENABLED],
  386. // ['id' => $params['order_id']]
  387. ], true);
  388. // print_r($order_data);exit;
  389. if (empty($order_data)) return $this->error('请先购买申请资格');
  390. $config['is_check'] = 0;
  391. $params['pay_price'] = $order_data['price'];
  392. $params['status'] = StatusEnum::ENABLED;
  393. $params['order_id'] = $order_data['id'];
  394. }
  395. $params['mall_id'] = $this->mall_id;
  396. $params['user_id'] = $this->user_id;
  397. $params['mobile'] = (string)$params['mobile'];
  398. if (empty($config['is_check'])) {
  399. $res = AreaAgent::setData($params);
  400. if ($res === false) return $this->error(AreaAgent::getStaticError());
  401. $params['check_status'] = '1';
  402. $params['marks'] = '系统管理员审核通过';
  403. $res = AreaApply::setData($params);
  404. if ($res === false) return $this->error(AreaApply::getStaticError());
  405. } else {
  406. $res = AreaApply::setData($params);
  407. if ($res === false) return $this->error(AreaApply::getStaticError());
  408. }
  409. return $this->success('操作成功');
  410. }
  411. /**
  412. * @desc:是否是区域代理
  413. * @Author: hua
  414. * @Date: 2022/2/7
  415. * @Time: 11:39
  416. * @Copyright: copyright (c) 2021 广东七件事集团
  417. * @return mixed|void
  418. */
  419. public function actionIsAreaAgent(){
  420. $user_id = $this->user_id;
  421. $mall_id = $this->mall_id;
  422. $config = \Yii::$app->services->setting->addons->get($this->mall_id, AreaEnum::ADDONS_NAME, 'basic');
  423. $is_enable_apply = $config['is_enable_apply'] ?? StatusEnum::ENABLED;
  424. // 根据需求当用户为代理时优先返回
  425. $res = AreaAgent::findOne(['user_id'=>$user_id,'mall_id'=>$mall_id,'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  426. if(!empty($res)) return $this->success('操作成功',['is_area_agent'=>1,'is_apply'=>0,'is_enable_apply' => $is_enable_apply]);
  427. $res = AreaApply::findOne(['user_id'=>$user_id,'mall_id'=>$mall_id,'check_status'=>AreaEnum::NOT_APPROVED]);
  428. // 根据需求当用户已经用户审核通过的代理身份了。这个时候如果用户在重新申请其它代理。并且在审核中。则不应该阻止用户进入代理页面
  429. $isCheckArea = AreaApply::find()
  430. ->where(['user_id'=>$user_id,'mall_id'=>$mall_id,'check_status'=>AreaEnum::APPROVED, 'status' => 1])->exists();
  431. if(!empty($res) && !$isCheckArea) return $this->success('操作成功',['is_apply'=>1,'is_area_agent'=>0,'is_enable_apply' => $is_enable_apply]);
  432. return $this->success('操作成功',[
  433. 'is_area_agent'=> 0,
  434. 'is_apply'=> 0,
  435. 'is_enable_apply' => $is_enable_apply
  436. ]);
  437. }
  438. /**
  439. * @desc:申请审核列表
  440. * @Author: hua
  441. * @Date: 2022/1/20
  442. * @Time: 9:54
  443. * @Copyright: copyright (c) 2021 广东七件事集团
  444. * @return mixed|void
  445. */
  446. public function actionApplyList()
  447. {
  448. $params = \Yii::$app->request->get();
  449. $mall_id = $this->mall_id;
  450. $where[] = ['mall_id' => $mall_id];
  451. $where[] = ['user_id' => $this->user_id];
  452. $where[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  453. $params['limit'] = 10;
  454. $params['where'] = $where;
  455. $params['order'] = 'id DESC';
  456. $params['select'] = 'id,user_id,level,address,realname, mobile,check_status,marks,created_at,is_refund,pay_price,refund_remark';
  457. $pageData = AreaApply::listPage($params, false);
  458. if ($pageData === false) return $this->error(AreaApply::getStaticError());
  459. $pageData['level_map'] = AreaEnum::getLevelMap(null, $this->mall_id);
  460. $pageData['check_status'] = AreaEnum::getCheckStatusMap();
  461. if (!empty($pageData['list'])) {
  462. foreach ($pageData['list'] as &$datum) {
  463. $datum['goods'] = [];
  464. }
  465. }
  466. return $this->success('操作成功', $pageData);
  467. }
  468. /**
  469. * 获取所属代理列表
  470. * @return mixed|null
  471. */
  472. public function actionList()
  473. {
  474. $only_dividend_goods_type = \Yii::$app->services->setting->addons->get($this->mall_id,
  475. AreaEnum::ADDONS_NAME, 'basic.only_dividend_goods_type');
  476. $with = [];
  477. if ($only_dividend_goods_type == StatusEnum::DISABLED) {
  478. $with = ['goods' => function ($query) {
  479. $query->with(['goods' => function($query1) {
  480. $query1->select('id, goods_name, cover_pic, price');
  481. }])->select('agent_id, goods_id');
  482. }];
  483. }
  484. $ret = AreaAgent::listPage([
  485. 'where' => [
  486. ['mall_id' => $this->mall_id],
  487. ['user_id' => $this->user_id],
  488. ['status' => StatusEnum::ENABLED]
  489. ],
  490. 'order' => 'id desc',
  491. 'select' => 'id, level, address, created_at',
  492. 'with' => $with,
  493. 'limit' => 10
  494. ]);
  495. if (!empty(AreaAgent::getStaticError())) return $this->error(AreaAgent::getStaticError());
  496. $ret['level_map'] = AreaEnum::getLevelMap(null, $this->mall_id);
  497. if (!empty($ret['list'])) {
  498. foreach ($ret['list'] as &$item) {
  499. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  500. if (!empty($item['goods'])) {
  501. $item['goods'] = array_column($item['goods'], 'goods');
  502. } else {
  503. $item['goods'] = [];
  504. }
  505. }
  506. }
  507. $ret['is_any_goods'] = $only_dividend_goods_type;
  508. return $this->success('获取成功', $ret);
  509. }
  510. /**
  511. * 我的个人收益
  512. */
  513. public function actionMyIncome()
  514. {
  515. $params = Yii::$app->request->get();
  516. $type = in_array($params['type'] ?? null, ['0', '1'])
  517. ? (int)$params['type']
  518. : 0;; // 收益类型0:未结算 1: 已结算
  519. $where = [
  520. ['<>', 'capital_type', 'withdrawal_refund'], // 过滤提现驳回
  521. ['=', 'mall_id', $this->mall_id],
  522. ['=', 'user_id', $this->user_id],
  523. ['=', 'from_type', AreaEnum::ADDONS_NAME],
  524. ['=', 'change_type', 'add'],
  525. ];
  526. try {
  527. if (empty($type)) {
  528. $where[] = ['=', 'status', StatusEnum::ENABLED];
  529. }
  530. // 设置收益类型
  531. CapitalLog::$capitalType = $type === 1 ? "commission" : "commission_frozen";
  532. $params['where'] = $where;
  533. $params['order'] = 'id desc';
  534. $params['select'] = 'id, user_id, change_type, change_num, source_table, source_table_id, created_at';
  535. $params['with'] = [
  536. 'user' => function ($query) {
  537. $query->select('id, nickname, avatar_url');
  538. }
  539. ];
  540. $list = CapitalLog::listPage($params);
  541. if (!empty($list['list'])) {
  542. foreach ($list['list'] as &$item) {
  543. if (!empty($item['created_at'])) $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  544. // 因为订单详情有门店订单和主商城订单,所以需要过滤掉
  545. if ($item['source_table'] == 'order_detail') { // 主商城
  546. $orderDetail = OrderDetail::find()
  547. ->select('id, order_id, goods_id, num, goods_name, price')
  548. ->where(['id' => $item['source_table_id']])
  549. ->with([
  550. 'order' => function ($query) {
  551. $query->select('id, order_no');
  552. }
  553. ])->asArray()->one();
  554. if (!empty($orderDetail)) {
  555. $item['order_detail'] = $orderDetail;
  556. }
  557. } else if ($item['source_table'] == 'store_order_detail') { // 门店订单
  558. $orderDetail = StoreOrderDetail::find()
  559. ->select('id, order_id, num, goods_name, price')
  560. ->where(['id' => $item['source_table_id']])
  561. ->with([
  562. 'order' => function ($query) {
  563. $query->select('id, order_no');
  564. }
  565. ])->asArray()->one();
  566. if (!empty($orderDetail)) {
  567. $item['order_detail'] = $orderDetail;
  568. }
  569. }
  570. }
  571. }
  572. return $this->success('请求成功', $list);
  573. } catch (\Exception $e) {
  574. return $this->error('请求失败');
  575. }
  576. }
  577. /**
  578. * 我的区域代理人员
  579. */
  580. public function actionMyChildArea()
  581. {
  582. try {
  583. // 获取当前用户直推下级
  584. $userIds = UserPartitionRelationship::getChildIdArr($this->user_id, 1);
  585. // 获取当前商城设置的区域代理等级
  586. $areaLevelList = AreaEnum::getLevelMap(null, $this->mall_id);
  587. $areaList = AreaAgent::find()->where(['mall_id' => $this->mall_id, 'user_id' => $userIds, 'status' => StatusEnum::ENABLED])
  588. ->select('level, count(1) as count')
  589. ->groupBy('level')
  590. ->indexBy('level')
  591. ->asArray()
  592. ->all();
  593. $areaLevelSystemList = [];
  594. foreach ($areaLevelList as $key => $value) {
  595. if ($key == 0) continue; // 过滤掉无用数据
  596. $areaLevelSystemList[] = [
  597. 'level' => $key,
  598. 'name' => $value,
  599. 'count' => $areaList[$key]['count'] ?? 0
  600. ];
  601. }
  602. return $this->success('请求成功', $areaLevelSystemList);
  603. } catch (\Exception $e) {
  604. return $this->error('请求失败');
  605. }
  606. }
  607. /**
  608. * 直推下级区域人员列表
  609. * @return void
  610. */
  611. public function actionMyChildAreaList()
  612. {
  613. try {
  614. // 获取当前用户直推下级
  615. $params = Yii::$app->request->get();
  616. $userIds = UserPartitionRelationship::getChildIdArr($this->user_id, 1);
  617. if (empty($params['level'])) return $this->error('请选择对应区域');
  618. $where[] = ['=', 'level', $params['level']];
  619. $where = [
  620. ['=', 'mall_id', $this->mall_id],
  621. ['=', 'user_id', $userIds],
  622. ['=', 'level', $params['level']],
  623. ['=', 'status', StatusEnum::ENABLED],
  624. ];
  625. $params['where'] = $where;
  626. $params['order'] = 'id desc';
  627. $params['select'] = 'id, user_id, level, address';
  628. $params['with'] = [
  629. 'user' => function ($query) {
  630. $query->select('id, nickname, avatar_url, mobile');
  631. }
  632. ];
  633. $list = AreaAgent::listPage($params);
  634. return $this->success('请求成功', $list);
  635. } catch (Exception $e) {
  636. return $this->error('请求失败');
  637. }
  638. }
  639. }