AgentUserController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. <?php
  2. namespace addons\CloudStock\api\modules\v1\controllers;
  3. use addons\CloudStock\common\enums\CloudStockEnum;
  4. use addons\CloudStock\common\models\CloudStockAgent;
  5. use addons\CloudStock\common\models\CloudStockAgentGoods;
  6. use addons\CloudStock\common\models\CloudStockAgentQuota;
  7. use addons\CloudStock\common\models\CloudStockDeductionDetail;
  8. use addons\CloudStock\common\models\CloudStockGoodsStockLog;
  9. use addons\CloudStock\common\models\CloudStockIncreaseDetail;
  10. use addons\CloudStock\common\models\CloudStockLevel;
  11. use addons\CloudStock\common\services\AgentService;
  12. use addons\CloudStock\common\services\WithdrawService;
  13. use addons\MerchantAuth\common\models\MerchantAuthLog;
  14. use common\enums\AddonsEnum;
  15. use common\enums\ApiStatusEnum;
  16. use common\enums\StatusEnum;
  17. use common\enums\WithdrawWayEnum;
  18. use common\globals\GlobalInvoke;
  19. use common\helpers\DateHelper;
  20. use common\models\common\CommissionLog;
  21. use common\models\common\PlatformSetting;
  22. use common\models\common\UserBankCard;
  23. use common\models\mall\MallAddons;
  24. use common\models\user\User;
  25. use common\models\user\UserFreezeCommission;
  26. use common\models\user\UserPartitionRelationship;
  27. use common\models\user\UserWallet;
  28. use common\models\user\UserWithdrawLog;
  29. use common\models\user\WithdrawsLimitLog;
  30. use yii\helpers\Json;
  31. use Yii;
  32. class AgentUserController extends BaseController
  33. {
  34. public $modelClass = '';
  35. /**
  36. * 不用进行登录验证的方法
  37. *
  38. * 例如: ['index', 'update', 'create', 'view', 'delete']
  39. * 默认全部需要验证
  40. *
  41. * @var array
  42. */
  43. protected $authOptional = [];
  44. /**
  45. * @name:
  46. * @msg: 我的供应商
  47. * @param {*}
  48. * @return {*}
  49. */
  50. public function actionMySupplier()
  51. {
  52. if (!\Yii::$app->request->isGet) {
  53. return $this->error('请求方式错误');
  54. }
  55. $parent_agent = CloudStockAgent::getUserParentAgent($this->user_id, $this->stock_agent->level);
  56. if (!empty($parent_agent)) {
  57. $parent_user = User::find()
  58. ->select('id,nickname,mobile,avatar_url,inviter_at')
  59. ->where(['mall_id' => $this->mall_id, 'id' => $parent_agent->user_id, 'status' => StatusEnum::ENABLED])
  60. ->one();
  61. $parent_agent_info = [
  62. 'id' => $parent_agent->id,
  63. 'user_id' => $parent_agent->user_id,
  64. 'level' => $parent_agent->level,
  65. 'level_name' => $parent_agent->stockLevel->name,
  66. 'nickname' => $parent_user->nickname ?? '平台',
  67. 'avatar_url' => $parent_user->avatar_url ?? \Yii::$app->request->hostInfo . '/resources/img/default_avatar_url.png',
  68. 'mobile' => $parent_user->mobile ?? '',
  69. ];
  70. } else {
  71. $parent_agent_info = [
  72. 'id' => 0,
  73. 'user_id' => 0,
  74. 'level' => 0,
  75. 'level_name' => '默认等级',
  76. 'nickname' => '平台',
  77. 'avatar_url' => \Yii::$app->request->hostInfo . '/resources/img/default_avatar_url.png',
  78. 'mobile' => '',
  79. ];
  80. }
  81. return $this->success('success', $parent_agent_info);
  82. }
  83. /**
  84. * @name:
  85. * @msg: 我的批发商列表
  86. * @param {*}
  87. * @return {*}
  88. */
  89. public function actionWholesalerList()
  90. {
  91. if (!\Yii::$app->request->isGet) {
  92. return $this->error('请求方式错误');
  93. }
  94. $page = \Yii::$app->request->get('page', 1);
  95. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  96. $user_keyword = \Yii::$app->request->get('user_keyword');
  97. $level = \Yii::$app->request->get('level');
  98. // 批发商列表
  99. $child_id_arr = UserPartitionRelationship::getChildIdArr($this->user_id);
  100. $child_id_arr = array_reverse($child_id_arr);
  101. if (!empty($user_keyword)) {
  102. $users = User::find()
  103. ->select('id,nickname,mobile,avatar_url,inviter_at')
  104. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  105. ->andWhere(['or', ['like', 'id', $user_keyword], ['like', 'nickname', $user_keyword]])
  106. ->asArray()
  107. ->indexBy('id')
  108. ->all();
  109. $uids = array_column($users, 'id');
  110. $child_id_arr = array_intersect($child_id_arr,$uids);
  111. }
  112. //跟自身平级的代理商下的所有代理都不会找自身补货
  113. $cloud_stock_agent = CloudStockAgent::findOne(['user_id'=>$this->user_id,'status'=>[StatusEnum::DISABLED, StatusEnum::ENABLED],'mall_id'=>$this->mall_id]);
  114. if(!empty($child_id_arr)){
  115. $condition = [];
  116. $condition[] = ['mall_id'=> $this->mall_id];
  117. $condition[] = ['status'=> [StatusEnum::DISABLED, StatusEnum::ENABLED]];
  118. $condition[] = ['user_id'=> $child_id_arr];
  119. $cloud_stock_agent_list = CloudStockAgent::lists(['where'=>$condition]);
  120. $children_ids = [];
  121. foreach ($cloud_stock_agent_list as $agent){
  122. if($agent['level']>=$cloud_stock_agent['level']){
  123. $res = UserPartitionRelationship::getChildIdArr($agent['user_id']);
  124. $children_ids=array_merge($children_ids,$res);
  125. $children_ids[] = $agent['user_id'];
  126. }
  127. }
  128. foreach ($cloud_stock_agent_list as $k=>$agent){
  129. if(in_array($agent['user_id'],$children_ids)){
  130. unset($cloud_stock_agent_list[$k]);
  131. }
  132. }
  133. $child_id_arr = array_column($cloud_stock_agent_list,'user_id');
  134. }
  135. $wheres[] = [
  136. 'mall_id' => $this->mall_id,
  137. 'status' => StatusEnum::ENABLED,
  138. 'user_id' => $child_id_arr,
  139. ];
  140. // 批发商等级比自己小的下级
  141. $wheres[] = ['<', 'level', $this->stock_agent->level];
  142. if (!empty($level)) {
  143. $wheres[] = ['level' => $level];
  144. }
  145. // 统计 query 对象
  146. $stat_query = CloudStockAgent::find();
  147. foreach ($wheres as $where) {
  148. $stat_query->andWhere($where);
  149. }
  150. $params = [
  151. 'select' => 'id,mall_id,user_id,level,created_at',
  152. 'where' => $wheres,
  153. 'order' => 'created_at desc',
  154. 'page' => $page,
  155. 'limit' => $limit,
  156. ];
  157. $child_agent_list = CloudStockAgent::listPage($params);
  158. if (empty($child_agent_list['list'])) {
  159. if (!is_array($child_agent_list['list'])) {
  160. return $this->error(CloudStockAgent::getStaticError());
  161. }
  162. }
  163. if (!empty($users)) {
  164. $child_users = $users;
  165. $user_ids = array_column($child_users, 'user_id');
  166. } else {
  167. $user_ids = array_column($child_agent_list['list'], 'user_id');
  168. $child_users = User::find()
  169. ->select('id,nickname,mobile,avatar_url,inviter_at')
  170. ->where(['mall_id' => $this->mall_id, 'id' => $user_ids, 'status' => StatusEnum::ENABLED])
  171. ->asArray()
  172. ->indexBy('id')
  173. ->all();
  174. }
  175. $child_good_numbers = CloudStockAgentGoods::find()
  176. ->select('user_id,sum(num) as stock_good_counts')
  177. ->where(['mall_id' => $this->mall_id, 'user_id' => $user_ids, 'status' => StatusEnum::ENABLED])
  178. ->groupBy('user_id')
  179. ->asArray()
  180. ->indexBy('user_id')
  181. ->all();
  182. $stock_levels = CloudStockLevel::find()
  183. ->select('id,level,name as level_name')
  184. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  185. ->asArray()
  186. ->indexBy('level')
  187. ->all();
  188. foreach ($child_agent_list['list'] as &$list) {
  189. $user = $child_users[$list['user_id']] ?? [];
  190. $good_number = $child_good_numbers[$list['user_id']] ?? [];
  191. $list['level_name'] = $stock_levels[$list['level']]['level_name'] ?? '默认等级';
  192. $list['nickname'] = $user['nickname'] ?? '';
  193. $list['avatar_url'] = $user['avatar_url'] ?? \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  194. $list['mobiel'] = $user['mobile'] ?? '';
  195. $list['inviter_at'] = $user['inviter_at'] ?? 0;
  196. $list['stock_good_counts'] = $good_number['stock_good_counts'] ?? 0;
  197. }
  198. $this_month = DateHelper::thisMonth();
  199. $this_week = DateHelper::thisWeek();
  200. $stat_query_week = clone $stat_query;
  201. $stat_query_month = clone $stat_query;
  202. $all_child_agent = $stat_query->count();
  203. $week_number = $stat_query_week->andWhere(['>=','created_at',$this_week['start']])->andWhere(['<=','created_at',$this_week['end']])->count();
  204. $month_number = $stat_query_month->andWhere(['>=','created_at',$this_month['start']])->andWhere(['<=','created_at',$this_month['end']])->count();
  205. $child_agent_list['all_child_agent'] = $all_child_agent;
  206. $child_agent_list['week_number'] = $week_number;
  207. $child_agent_list['month_number'] = $month_number;
  208. $child_agent_list['level_list'] = array_values($stock_levels);
  209. return $this->success('success', $child_agent_list);
  210. }
  211. /**
  212. * @name:
  213. * @msg: 设置代理商,获取等级和设置名额数据
  214. * @param {*}
  215. * @return {*}
  216. */
  217. public function actionSetWholesalerInfo()
  218. {
  219. if (!\Yii::$app->request->isGet) {
  220. return $this->error('请求方式错误');
  221. }
  222. $levels = CloudStockLevel::getLevelList($this->mall_id);
  223. if ($this->stock_agent->level < 2) {
  224. $give_quotas = [];
  225. } else {
  226. $give_quotas = CloudStockAgentQuota::find()
  227. ->select('id,mall_id,user_id,level,num,status as is_use')
  228. ->where(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED])
  229. ->asArray()
  230. ->all();
  231. if (!empty($give_quotas)) {
  232. foreach ($give_quotas as &$give_quota) {
  233. $give_quota['name'] = $levels[$give_quota['level']]['level_name'] ?? '默认等级';
  234. }
  235. }
  236. }
  237. return $this->success('success', ['level_list' => array_values($levels), 'give_quota' => $give_quotas]);
  238. }
  239. /**
  240. * @name:
  241. * @msg: 根据 id 获取会员基本信息
  242. * @param {*}
  243. * @return {*}
  244. */
  245. public function actionUserInfo()
  246. {
  247. if (!\Yii::$app->request->isGet) {
  248. return $this->error('请求方式错误');
  249. }
  250. $user_keyword = \Yii::$app->request->get('user_keyword');
  251. $user_keyword = trim($user_keyword);
  252. if (empty($user_keyword)) return $this->error('参数错误');
  253. $userModel = User::find()
  254. ->select('id,nickname,avatar_url,mobile')
  255. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
  256. // 判断是否符合国内手机号11位 如果不符合则按用户id查询
  257. $pattern = '/^1\d{10}$/';
  258. if (preg_match($pattern, $user_keyword)) {
  259. $userModel->andWhere(['mobile' => $user_keyword]);
  260. } else {
  261. $userModel->andWhere(['id' => $user_keyword]);
  262. }
  263. $user = $userModel->asArray()
  264. ->one();
  265. return $this->success('success', $user);
  266. }
  267. /**
  268. * @name:
  269. * @msg: 设定代理商
  270. * @param {integer} $user_id
  271. * @param {integer} $level
  272. * @return {*}
  273. */
  274. public function actionSetWholesaler()
  275. {
  276. if (!\Yii::$app->request->isPost) {
  277. return $this->error('请求方式错误');
  278. }
  279. $user_id = \Yii::$app->request->post('user_id');
  280. $level = \Yii::$app->request->post('level');
  281. if (empty($user_id) || empty($level)) return $this->error('参数错误');
  282. if ($this->stock_agent->level < 2) return $this->error('您当前等级不能设置代理商');
  283. $is_give_quota = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic.is_give_quota');
  284. if ($is_give_quota != 1) return $this->error('系统未开启设置代理商功能');
  285. $user = User::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_id]);
  286. if (empty($user)) return $this->error('目标会员不存在');
  287. $agent = CloudStockAgent::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $user_id]);
  288. if (!empty($agent)) return $this->error('目标会员已经是代理商');
  289. $parent_id_arr = UserPartitionRelationship::getParentIdArr($user_id);
  290. // dd($parent_id_arr);
  291. if (!in_array($this->user_id, $parent_id_arr)) return $this->error('目标会员不是您的下级会员');
  292. $level_info = CloudStockLevel::getLevelList($this->mall_id, $level);
  293. $give_quota = CloudStockLevel::getGiveQuota($this->mall_id, $level, $this->user_id);
  294. if ($give_quota <= 0) {
  295. return $this->error('设定代理[等级:' . $level_info['level_name'] . ']的名额已经用完');
  296. }
  297. $params = [
  298. 'user_id' => $this->user_id,
  299. 'agent_id' => $this->stock_agent->id,
  300. 'child_id' => $user_id,
  301. 'set_level' => $level,
  302. 'upgrade_status' => CloudStockEnum::UPGRADE_STATUS_BY_PARENT,
  303. ];
  304. $res = CloudStockAgent::setWholesaler($this->mall_id, $params);
  305. if (false === $res) return $this->error(CloudStockAgent::getStaticError());
  306. return $this->success('操作成功');
  307. }
  308. /**
  309. * @name:
  310. * @msg: 我的报表数据
  311. * @param {*}
  312. * @return {*}
  313. */
  314. public function actionReportForm()
  315. {
  316. if (!\Yii::$app->request->isGet) {
  317. return $this->error('请求方式错误');
  318. }
  319. $date = date('Y-m');
  320. $start_time = strtotime($date . '-01 00:00:00');
  321. $end_time = strtotime($date . '-' . date('t', $start_time) . ' 23:59:59');
  322. // 本月收入/支出数据
  323. $in_income = $out_income = 0;
  324. $asset_logs = CommissionLog::find()
  325. ->select('id,mall_id,user_id,change_type,change_num,current_num,desc,from_type,capital_type,created_at')
  326. ->where(['mall_id' => $this->mall_id, 'user_id' => $this->user_id,'from_type'=>AddonsEnum::ADDONS_NAME_CLOUD_STOCK])
  327. ->andWhere(['and', ['>=', 'created_at', $start_time], ['<=', 'created_at', $end_time]])
  328. ->orderBy('created_at desc')
  329. ->all();
  330. if (!empty($asset_logs)) {
  331. foreach ($asset_logs as $log) {
  332. if ('sub' == $log->change_type) $out_income += $log->change_num;
  333. if ('add' == $log->change_type) $in_income += $log->change_num;
  334. }
  335. }
  336. // 本月出库/入库数据
  337. $out_stock = $in_stock = 0;
  338. $dedution_log_ids = CloudStockDeductionDetail::find()
  339. ->select('stock_log_id')
  340. ->where(['user_id' => $this->user_id, 'status' => StatusEnum::ENABLED])
  341. ->asArray()
  342. ->column();
  343. $increase_log_ids = CloudStockIncreaseDetail::find()
  344. ->select('stock_log_id')
  345. ->where(['user_id' => $this->user_id, 'status' => StatusEnum::ENABLED])
  346. ->asArray()
  347. ->column();
  348. $log_ids = array_merge($dedution_log_ids, $increase_log_ids);
  349. $stock_logs = CloudStockGoodsStockLog::find()
  350. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $log_ids])
  351. ->andWhere(['and', ['>=', 'created_at', $start_time], ['<=', 'created_at', $end_time]])
  352. ->orderBy('created_at desc')
  353. ->all();
  354. if (!empty($stock_logs)) {
  355. foreach ($stock_logs as $log) {
  356. if (CloudStockEnum::STOCK_CHANGE_SUB == $log->changes_dir) $out_stock += $log->num;
  357. if (CloudStockEnum::STOCK_CHANGE_ADD == $log->changes_dir) $in_stock += $log->num;
  358. }
  359. }
  360. return $this->success('success', compact('in_income', 'out_income', 'out_stock', 'in_stock'));
  361. }
  362. /**
  363. * 我的批发商列表-最新
  364. * @return mixed|void
  365. */
  366. public function actionWholesalerListNew()
  367. {
  368. if (!\Yii::$app->request->isGet) {
  369. return $this->error('请求方式错误');
  370. }
  371. $user_keyword = \Yii::$app->request->get('user_keyword');
  372. $level = \Yii::$app->request->get('level');
  373. $get_user_partition_relationship_id = \Yii::$app->request->get('user_partition_relationship_id');
  374. $user_partition_relationship = UserPartitionRelationship::findOne(['user_id'=>$this->user_id]);
  375. $cloud_stock_agent = CloudStockAgent::findOne(['user_id'=>$this->user_id,'status'=>StatusEnum::ENABLED]);
  376. $i = 1;
  377. $page_size = 100;
  378. $temp_list = [];
  379. $stock_levels = CloudStockLevel::find()
  380. ->select('id,level,name as level_name')
  381. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  382. ->asArray()
  383. ->indexBy('level')
  384. ->all();
  385. $user_partition_relationship_id = 0;
  386. while (true){
  387. $query = UserPartitionRelationship::find()
  388. ->where(['!=','user_id',$this->user_id])
  389. ->andWhere(['like','tree',$user_partition_relationship['tree'].'%',false]);
  390. if(!empty($get_user_partition_relationship_id)) $query->andWhere(['>','id',$get_user_partition_relationship_id]);
  391. if(!empty($user_keyword)) $query->andWhere(['=','user_id',$user_keyword]);
  392. $child_list = $query->select('id,user_id')->offset(($i - 1) * $page_size)->limit($page_size)->orderBy('id asc')->all();
  393. if(empty($child_list)) break;
  394. $user_ids = array_column($child_list,'user_id');
  395. $user_id_arr = array_column($child_list,'id','user_id');
  396. $where = [['status'=>StatusEnum::ENABLED], ['user_id'=>$user_ids],];
  397. $where[]= ['<','level',$cloud_stock_agent['level']];
  398. if(!empty($level)){
  399. $where[]= ['level'=>$level];
  400. }
  401. $cloud_stock_agent_list = CloudStockAgent::lists(['where'=>$where,'with'=>'user']);
  402. foreach ($cloud_stock_agent_list as $item){
  403. $parent_ids = UserPartitionRelationship::getParentIdArr($item['user_id']);
  404. $temp_parent_ids = [];
  405. $parent_ids = array_reverse($parent_ids);
  406. foreach ($parent_ids as $uid){
  407. if($uid==$this->user_id) break;
  408. $temp_parent_ids[]=$uid;
  409. }
  410. if(empty($temp_parent_ids)){
  411. $temp_list[]=$item;
  412. if(count($temp_list)>=5){
  413. $user_partition_relationship_id = $user_id_arr[$item['user_id']];
  414. break;
  415. }
  416. }else{
  417. $temp_cloud_stock_agent_list = CloudStockAgent::lists([
  418. 'where'=>[
  419. ['status'=>StatusEnum::ENABLED],
  420. ['user_id'=>$temp_parent_ids],
  421. ['>=','level',$cloud_stock_agent['level']]
  422. ]
  423. ]);
  424. if(empty($temp_cloud_stock_agent_list)){
  425. $user_partition_relationship_id = $user_id_arr[$item['user_id']];
  426. $temp_list[]=$item;
  427. if(count($temp_list)>=5) {
  428. $user_partition_relationship_id = $user_id_arr[$item['user_id']];
  429. break;
  430. }
  431. }
  432. }
  433. }
  434. if(count($temp_list)>=5) break;
  435. $i++;
  436. }
  437. $user_ids = array_column($temp_list,'user_id');
  438. $child_good_numbers = CloudStockAgentGoods::find()
  439. ->select('user_id,sum(num) as stock_good_counts')
  440. ->where(['mall_id' => $this->mall_id, 'user_id' => $user_ids, 'status' => StatusEnum::ENABLED])
  441. ->groupBy('user_id')
  442. ->asArray()
  443. ->indexBy('user_id')
  444. ->all();
  445. $list = [];
  446. foreach ($temp_list as $item){
  447. $good_number = $child_good_numbers[$item['user_id']] ?? [];
  448. $list[]=[
  449. 'avatar_url'=>$item['user']['avatar_url'],
  450. 'inviter_at'=>$item['user']['inviter_at'],
  451. 'mobiel'=>$item['user']['mobile'],
  452. 'nickname'=>$item['user']['nickname'],
  453. 'created_at'=>$item['created_at'],
  454. 'user_id'=>$item['user_id'],
  455. 'level'=>$item['level'],
  456. 'level_name'=>$stock_levels[$item['level']]['level_name'] ?? '默认等级',
  457. 'id'=>$item['id'],
  458. 'mall_id'=>$item['mall_id'],
  459. 'stock_good_counts'=>$good_number['stock_good_counts'] ?? 0,
  460. ];
  461. }
  462. return $this->success('success', ['list'=>$list,'user_partition_relationship_id'=>$user_partition_relationship_id]);
  463. }
  464. /**
  465. * 我的批发商-统计
  466. * @return mixed|void
  467. */
  468. public function actionWholesalerTotal()
  469. {
  470. ini_set('memory_limit','1024M');
  471. set_time_limit(0);
  472. if (!\Yii::$app->request->isGet) {
  473. return $this->error('请求方式错误');
  474. }
  475. $user_keyword = \Yii::$app->request->get('user_keyword');
  476. $level = \Yii::$app->request->get('level');
  477. $get_user_partition_relationship_id = \Yii::$app->request->get('user_partition_relationship_id');
  478. $user_partition_relationship = UserPartitionRelationship::findOne(['user_id'=>$this->user_id]);
  479. $cloud_stock_agent = CloudStockAgent::findOne(['user_id'=>$this->user_id,'status'=>StatusEnum::ENABLED]);
  480. $i = 1;
  481. $page_size = 100;
  482. $all_child_agent = 0;
  483. $month_number = 0;
  484. $week_number = 0;
  485. $this_week = DateHelper::thisWeek();
  486. $this_month = DateHelper::thisMonth();
  487. while (true){
  488. $query = UserPartitionRelationship::find()
  489. ->where(['!=','user_id',$this->user_id])
  490. ->andWhere(['like','tree',$user_partition_relationship['tree'].'%',false]);
  491. if(!empty($get_user_partition_relationship_id)) $query->andWhere(['>','id',$get_user_partition_relationship_id]);
  492. if(!empty($user_keyword)) $query->andWhere(['=','user_id',$user_keyword]);
  493. $child_list = $query->select('id,user_id')->offset(($i - 1) * $page_size)->limit($page_size)->orderBy('id asc')->all();
  494. if(empty($child_list)) break;
  495. $user_ids = array_column($child_list,'user_id');
  496. $where = [['status'=>StatusEnum::ENABLED], ['user_id'=>$user_ids],];
  497. if(!empty($level)){
  498. $where[] = ['level'=>$level];
  499. }else{
  500. $where[] = ['<','level',$cloud_stock_agent['level']];
  501. }
  502. $cloud_stock_agent_list = CloudStockAgent::lists(['where'=>$where,'with'=>'user']);
  503. foreach ($cloud_stock_agent_list as $item){
  504. $parent_ids = UserPartitionRelationship::getParentIdArr($item['user_id']);
  505. $temp_parent_ids = [];
  506. $parent_ids = array_reverse($parent_ids);
  507. foreach ($parent_ids as $uid){
  508. if($uid==$this->user_id) break;
  509. $temp_parent_ids[]=$uid;
  510. }
  511. if(empty($temp_parent_ids)){
  512. $all_child_agent++;
  513. if($item['created_at']>=$this_week['start']&&$item['created_at']<=$this_week['end']) $week_number++;
  514. if($item['created_at']>=$this_month['start']&&$item['created_at']<=$this_month['end']) $month_number++;
  515. }else{
  516. $temp_cloud_stock_agent_list = CloudStockAgent::lists([
  517. 'where'=>[
  518. ['status'=>StatusEnum::ENABLED],
  519. ['user_id'=>$temp_parent_ids],
  520. ['>=','level',$cloud_stock_agent['level']]
  521. ]
  522. ]);
  523. if(empty($temp_cloud_stock_agent_list)){
  524. $all_child_agent++;
  525. if($item['created_at']>=$this_week['start']&&$item['created_at']<=$this_week['end']) $week_number++;
  526. if($item['created_at']>=$this_month['start']&&$item['created_at']<=$this_month['end']) $month_number++;
  527. }
  528. }
  529. }
  530. $i++;
  531. }
  532. $stock_levels = CloudStockLevel::find()
  533. ->select('id,level,name as level_name')
  534. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  535. ->asArray()
  536. ->indexBy('level')
  537. ->all();
  538. return $this->success('success', ['level_list'=>array_values($stock_levels),'all_child_agent'=>$all_child_agent,'month_number'=>$month_number,'week_number'=>$week_number]);
  539. }
  540. public function actionWithdraw()
  541. {
  542. try {
  543. $params = Yii::$app->request->post();
  544. $data = (new WithdrawService(['mall_id' => $this->mall_id,'user_id'=>$this->user_id]))->withdraw($params);
  545. return $this->success('操作成功', $data);
  546. } catch (\Exception $e) {
  547. return $this->error($e->getMessage(), []);
  548. }
  549. }
  550. public function actionCashSetting(){
  551. try {
  552. $request = Yii::$app->request;
  553. if ($request->isGet) {
  554. $params = Yii::$app->request->get();
  555. $data = (new WithdrawService(['mall_id' => $this->mall_id,'user_id'=>$this->user_id]))->cashSetting();
  556. return $this->success('操作成功', $data);
  557. }
  558. } catch (\Exception $e) {
  559. return $this->error($e->getMessage(), []);
  560. }
  561. }
  562. public function actionTransferBalance()
  563. {
  564. try {
  565. $request = Yii::$app->request;
  566. if ($request->isPost) {
  567. $params = Yii::$app->request->post();
  568. $data = (new WithdrawService(['mall_id' => $this->mall_id,'user_id'=>$this->user_id]))->transferBalance($params);
  569. return $this->success('操作成功', $data);
  570. }
  571. } catch (\Exception $e) {
  572. return $this->error($e->getMessage(), []);
  573. }
  574. }
  575. /**
  576. * 修改代理配置
  577. *
  578. * @return void
  579. */
  580. public function actionModifyInfo()
  581. {
  582. $post = Yii::$app->request->post();
  583. // 验证
  584. $res = Yii::$app->services->validate->validate($post,[
  585. [['is_stock_deducted_by_subordinate'], 'integer'],
  586. ]);
  587. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  588. if (empty($post)) {
  589. $this->success();
  590. return;
  591. }
  592. $service = new AgentService(['mall_id' => $this->mall_id]);
  593. $service->modifyInfo($this->user_id, $post);
  594. $this->success();
  595. }
  596. }