UserCollectService.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2018/12/13
  6. * Time: 下午4:59
  7. */
  8. namespace app\main\service;
  9. use app\common\library\Redis;
  10. use app\common\library\Ssdb;
  11. use app\common\model\UserCollect;
  12. use app\main\constants\AdminConstants;
  13. use app\main\constants\ApiConstants;
  14. use app\main\constants\CacheConstants;
  15. use app\main\constants\UserConstants;
  16. use app\main\helper\ArrayHelper;
  17. use app\main\helper\NumberHelper;
  18. use app\main\helper\StringHelper;
  19. /**
  20. * 用户统计分析
  21. * Class UserCollectService
  22. * @package app\main\service
  23. */
  24. class UserCollectService extends BaseService
  25. {
  26. protected $default = [
  27. 'follow' => 0,
  28. 'recharge' => 0,
  29. 'increase' => 0,
  30. 'increase_m' => 0,
  31. 'increase_f' => 0,
  32. 'increase_fllow' => 0,
  33. 'increase_recharge' => 0,
  34. 'unfollow_num' => 0,
  35. 'net_follow_num' => 0,
  36. 'day_recharge_user_money' => 0,
  37. 'day_recharge_user_count' => 0,
  38. ];
  39. protected $replaceFields = [
  40. 'adminId' => 'admin_id',
  41. 'increaseM' => 'increase_m',
  42. 'increaseF' => 'increase_f',
  43. 'increaseFllow' => 'increase_fllow',
  44. 'unfollowNum' => 'unfollow_num',
  45. 'netFollowNum' => 'net_follow_num',
  46. 'increaseRecharge' => 'increase_recharge',
  47. 'dayRechargeUserCount' => 'day_recharge_user_count',
  48. 'dayRechargeUserMoney' => 'day_recharge_user_money',
  49. 'date' => 'createdate',
  50. ];
  51. /**
  52. * @var UserCollectService
  53. */
  54. protected static $self = NULL;
  55. /**
  56. * @return UserCollectService
  57. */
  58. public static function instance()
  59. {
  60. if (self::$self == NULL) {
  61. self::$self = new self();
  62. }
  63. return self::$self;
  64. }
  65. /**
  66. * @return UserCollect
  67. */
  68. public function getUserCollectModel()
  69. {
  70. return model('UserCollect');
  71. }
  72. /**
  73. * 获取今日数据
  74. * @param $admin_id
  75. * @return \app\main\model\object\ReturnObject
  76. */
  77. public function getMigrateTodayData($admin_id)
  78. {
  79. $default = $this->default;
  80. $data = ApiService::instance()->getCollectFromApi(ApiConstants::API_USER_TODAY, ['adminId' => $admin_id])->data;
  81. if (!$data) {
  82. return $this->setData($default)->getReturn();
  83. }
  84. $replaceFields = $this->replaceFields;
  85. $data = ArrayHelper::array_replace_key($data, $replaceFields);
  86. $data = array_merge($default, $data);
  87. return $this->setData($data)->getReturn();
  88. }
  89. /**
  90. * 获取今天的用户统计数据
  91. * @param $admin_id
  92. * @return \app\main\model\object\ReturnObject
  93. */
  94. public function getUserCollectToday($admin_id)
  95. {
  96. $admin_id = (array)AdminService::instance()->getAdminId($admin_id)->data;
  97. $default = $this->default;
  98. $fields = [
  99. 'IFNULL(SUM(increase),0)' => 'increase',
  100. 'IFNULL(SUM(increase_m),0)' => 'increase_m',
  101. 'IFNULL(SUM(increase_f),0)' => 'increase_f',
  102. 'IFNULL(SUM(increase_fllow),0)' => 'increase_fllow',
  103. 'IFNULL(SUM(increase_recharge),0)' => 'increase_recharge',
  104. 'IFNULL(SUM(guide_follow_num),0)' => 'guide_follow_num',
  105. 'IFNULL(SUM(unfollow_num),0)' => 'unfollow_num',
  106. 'IFNULL(SUM(net_follow_num),0)' => 'net_follow_num',
  107. 'IFNULL(SUM(day_recharge_user_money),0)' => 'day_recharge_user_money',
  108. 'IFNULL(SUM(day_recharge_user_count),0)' => 'day_recharge_user_count',
  109. ];
  110. try {
  111. //取数据库数据
  112. $where = [
  113. 'type' => UserConstants::USER_COLLECT_TYPE_DAY,
  114. 'createdate' => date('Ymd', time()),
  115. 'admin_id' => ['in', $admin_id],
  116. ];
  117. $data = $this->getUserCollectModel()
  118. ->where($where)
  119. ->field($fields)
  120. ->find()
  121. ->toArray();
  122. if ($data) {
  123. if ($data['increase']) {
  124. $data['follow'] = NumberHelper::getFloat($data['increase_fllow'] * 100 / $data['increase']);
  125. $data['recharge'] = NumberHelper::getFloat($data['increase_recharge'] * 100 / $data['increase']);
  126. $data['guide_follow_num_percent'] = NumberHelper::getFloat($data['guide_follow_num'] * 100 / $data['increase']);
  127. $data['unfollow_num_percent'] = NumberHelper::getFloat($data['unfollow_num'] * 100 / $data['increase']);
  128. $data['net_follow_num_percent'] = NumberHelper::getFloat($data['net_follow_num'] * 100 / $data['increase']);
  129. } else {
  130. $data['follow'] = 0;
  131. $data['recharge'] = 0;
  132. $data['guide_follow_num_percent'] = 0;
  133. $data['unfollow_num_percent'] = 0;
  134. $data['net_follow_num_percent'] = 0;
  135. }
  136. } else {
  137. $data = $default;
  138. }
  139. $pay = intval($data['increase_recharge'] ?? 0);
  140. //算 支付比例
  141. if ($pay && $data['increase']) {
  142. $data['recharge'] = round($pay * 100 / $data['increase'], 2);
  143. }
  144. return $this->setData($data)->getReturn();
  145. } catch (\Exception $e) {
  146. LogService::exception($e);
  147. return $this->setData($default)->getReturn();
  148. }
  149. }
  150. /**
  151. * 获取昨日统计数据
  152. * @param $admin_id
  153. * @return \app\main\model\object\ReturnObject
  154. */
  155. public function getUserCollectYesterday($admin_id)
  156. {
  157. $admin_id = (array)AdminService::instance()->getAdminId($admin_id)->data;
  158. $default = $this->default;
  159. $fields = [
  160. 'IFNULL(SUM(increase),0)' => 'increase',
  161. 'IFNULL(SUM(increase_m),0)' => 'increase_m',
  162. 'IFNULL(SUM(increase_f),0)' => 'increase_f',
  163. 'IFNULL(SUM(increase_fllow),0)' => 'increase_fllow',
  164. 'IFNULL(SUM(increase_recharge),0)' => 'increase_recharge',
  165. 'IFNULL(SUM(guide_follow_num),0)' => 'guide_follow_num',
  166. 'IFNULL(SUM(unfollow_num),0)' => 'unfollow_num',
  167. 'IFNULL(SUM(net_follow_num),0)' => 'net_follow_num',
  168. 'IFNULL(SUM(day_recharge_user_money),0)' => 'day_recharge_user_money',
  169. 'IFNULL(SUM(day_recharge_user_count),0)' => 'day_recharge_user_count',
  170. ];
  171. try {
  172. $where = [
  173. 'type' => UserConstants::USER_COLLECT_TYPE_DAY,
  174. 'createdate' => date('Ymd', strtotime('-1 days')),
  175. 'admin_id' => ['in', $admin_id],
  176. ];
  177. $data = $this->getUserCollectModel()
  178. ->where($where)
  179. ->whereIn('admin_id', $admin_id)
  180. ->field($fields)
  181. ->find()
  182. ->toArray();
  183. if (!$data) {
  184. $data = $default;
  185. }
  186. return $this->setData($data)->getReturn();
  187. } catch (\Exception $e) {
  188. LogService::exception($e);
  189. return $this->setData($default)->getReturn();
  190. }
  191. }
  192. /**
  193. * 获取月度统计数据
  194. * @param $admin_id
  195. * @return \app\main\model\object\ReturnObject
  196. */
  197. public function getUserCollectMonth($admin_id)
  198. {
  199. $admin_id = (array)AdminService::instance()->getAdminId($admin_id)->data;
  200. $default = $this->default;
  201. $fields = [
  202. 'IFNULL(SUM(increase),0)' => 'increase',
  203. 'IFNULL(SUM(increase_m),0)' => 'increase_m',
  204. 'IFNULL(SUM(increase_f),0)' => 'increase_f',
  205. 'IFNULL(SUM(increase_fllow),0)' => 'increase_fllow',
  206. 'IFNULL(SUM(increase_recharge),0)' => 'increase_recharge',
  207. 'IFNULL(SUM(guide_follow_num),0)' => 'guide_follow_num',
  208. 'IFNULL(SUM(unfollow_num),0)' => 'unfollow_num',
  209. 'IFNULL(SUM(net_follow_num),0)' => 'net_follow_num',
  210. 'IFNULL(SUM(day_recharge_user_money),0)' => 'day_recharge_user_money',
  211. 'IFNULL(SUM(day_recharge_user_count),0)' => 'day_recharge_user_count',
  212. ];
  213. try {
  214. $where = [
  215. 'type' => UserConstants::USER_COLLECT_TYPE_DAY,
  216. 'admin_id' => ['in', $admin_id],
  217. ];
  218. $data = $this->getUserCollectModel()
  219. ->where($where)
  220. ->where("createdate>=" . date('Ym') . "01")
  221. ->where("createdate<=" . date('Ymd', strtotime('-1 days')))
  222. ->field($fields)
  223. ->find()
  224. ->toArray();
  225. if (!$data) {
  226. $data = $default;
  227. }
  228. return $this->setData($data)->getReturn();
  229. } catch (\Exception $e) {
  230. LogService::exception($e);
  231. return $this->setData($default)->getReturn();
  232. }
  233. }
  234. /**
  235. * 获取总数据
  236. * @param $admin_id
  237. * @return \app\main\model\object\ReturnObject
  238. */
  239. public function getTotalData($admin_id)
  240. {
  241. $admin_id = (array)AdminService::instance()->getAdminId($admin_id)->data;
  242. $default = $this->default;
  243. $fields = [
  244. 'IFNULL(SUM(increase),0)' => 'increase',
  245. 'IFNULL(SUM(increase_m),0)' => 'increase_m',
  246. 'IFNULL(SUM(increase_f),0)' => 'increase_f',
  247. 'IFNULL(SUM(increase_fllow),0)' => 'increase_fllow',
  248. 'IFNULL(SUM(increase_recharge),0)' => 'increase_recharge',
  249. 'IFNULL(SUM(guide_follow_num),0)' => 'guide_follow_num',
  250. 'IFNULL(SUM(unfollow_num),0)' => 'unfollow_num',
  251. 'IFNULL(SUM(net_follow_num),0)' => 'net_follow_num',
  252. 'IFNULL(SUM(day_recharge_user_money),0)' => 'day_recharge_user_money',
  253. 'IFNULL(SUM(day_recharge_user_count),0)' => 'day_recharge_user_count',
  254. ];
  255. try {
  256. $where = [
  257. 'type' => UserConstants::USER_COLLECT_TYPE_DAY,
  258. 'admin_id' => ['in', $admin_id],
  259. ];
  260. $data = $this->getUserCollectModel()
  261. ->field($fields)
  262. ->where($where)
  263. ->where("createdate<=" . date('Ymd', strtotime('-1 days')))
  264. ->find()
  265. ->toArray();
  266. if (!$data) {
  267. $data = $default;
  268. }
  269. return $this->setData($data)->getReturn();
  270. } catch (\Exception $e) {
  271. LogService::exception($e);
  272. return $this->setData($default)->getReturn();
  273. }
  274. }
  275. /**
  276. * 获取渠道30日数据
  277. * @param $admin_id
  278. * @param $where
  279. * @param $sort
  280. * @param $order
  281. * @param $offset
  282. * @param $limit
  283. * @return \app\main\model\object\ReturnObject
  284. */
  285. public function getUserCollectDateListData($admin_id, $where = '', $sort = 'createdate', $order = 'desc', $offset = 0, $limit = 10, $group = AdminConstants::ADMIN_GROUP_ID_CHANNEL)
  286. {
  287. try {
  288. $total = 0;
  289. $list = [];
  290. if ($group == AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN) {
  291. $admin_id = 0;
  292. }
  293. if (VisitLimitService::instance()->checkMigratedV2()) {
  294. $data = ApiService::instance()->getCollectFromApi(ApiConstants::API_USER_TOTAL, ['adminId' => $admin_id])->data;
  295. if ($data) {
  296. $list = [];
  297. foreach ($data['thirty'] as $item) {
  298. if ($item['date'] == date('Ymd')) {
  299. continue;
  300. }
  301. $date = ArrayHelper::array_replace_key($item, $this->replaceFields);
  302. $list[] = array_merge($this->default, $date);
  303. }
  304. usort($list, function($v1, $v2){
  305. return $v1['createdate'] < $v2['createdate'];
  306. });
  307. $total = count($list);
  308. $list = array_slice($list, $offset, $limit);
  309. }
  310. } else {
  311. $beginDate = date('Ymd', strtotime('-31 days'));
  312. $endDate = date('Ymd', strtotime('-1 days'));
  313. $total = $this->getUserCollectModel()
  314. ->where($where)
  315. ->where('admin_id', $admin_id)
  316. ->where('createdate', '>=', $beginDate)
  317. ->where('createdate', '<=', $endDate)
  318. ->where(['type' => UserConstants::USER_COLLECT_TYPE_DAY])
  319. ->count();
  320. //总的数据
  321. $list = $this->getUserCollectModel()
  322. ->where($where)
  323. ->where('admin_id', $admin_id)
  324. ->where(['type' => UserConstants::USER_COLLECT_TYPE_DAY])
  325. ->where('createdate', '>=', $beginDate)
  326. ->where('createdate', '<=', $endDate)
  327. ->order($sort, $order)
  328. ->limit($offset, $limit)
  329. ->select();
  330. }
  331. $result = ["total" => $total, "rows" => $list];
  332. return $this->setData($result)->getReturn();
  333. } catch (\Exception $e) {
  334. LogService::exception($e);
  335. return $this->setData([
  336. 'total' => 0,
  337. 'rows' => [],
  338. ])->getReturn();
  339. }
  340. }
  341. /**
  342. * 获取VIP30日数据汇总
  343. * @param $admin_id
  344. * @param int $offset
  345. * @param int $limit
  346. * @return \app\main\model\object\ReturnObject
  347. */
  348. public function getMigratedUserCollectDateAggsListData($admin_id, $offset = 0, $limit = 10)
  349. {
  350. $total = 0;
  351. $list = [];
  352. $admin_ids = (array)AdminService::instance()->getAdminId($admin_id)->data;
  353. if ($admin_ids != [-1]) {
  354. $data = ApiService::instance()->getCollectFromApi(ApiConstants::API_VIP_USER_TOTAL, ['ids' => $admin_ids])->data;
  355. if ($data) {
  356. $list = [];
  357. foreach ($data['thirty'] as $item) {
  358. if ($item['date'] == date('Ymd')) {
  359. continue;
  360. }
  361. $date = ArrayHelper::array_replace_key($item, $this->replaceFields);
  362. $list[] = array_merge($this->default, $date);
  363. }
  364. usort($list, function ($v1, $v2) {
  365. return $v1['createdate'] < $v2['createdate'];
  366. });
  367. $total = count($list);
  368. $list = array_slice($list, $offset, $limit);
  369. }
  370. }
  371. $result = ["total" => $total, "rows" => $list];
  372. return $this->setData($result)->getReturn();
  373. }
  374. /**
  375. * @param $admin_id
  376. * @param $where
  377. * @param $sort
  378. * @param $order
  379. * @param $offset
  380. * @param $limit
  381. * @return \app\main\model\object\ReturnObject
  382. */
  383. public function getUserCollectDateAggsListData($admin_id, $offset = 0, $limit = 10)
  384. {
  385. $fields = [
  386. 'IFNULL(SUM(increase),0)' => 'increase',
  387. 'IFNULL(SUM(increase_m),0)' => 'increase_m',
  388. 'IFNULL(SUM(increase_f),0)' => 'increase_f',
  389. 'IFNULL(SUM(increase_fllow),0)' => 'increase_fllow',
  390. 'IFNULL(SUM(increase_recharge),0)' => 'increase_recharge',
  391. 'IFNULL(SUM(guide_follow_num),0)' => 'guide_follow_num',
  392. 'IFNULL(SUM(unfollow_num),0)' => 'unfollow_num',
  393. 'IFNULL(SUM(net_follow_num),0)' => 'net_follow_num',
  394. 'IFNULL(SUM(day_recharge_user_money),0)' => 'day_recharge_user_money',
  395. 'IFNULL(SUM(day_recharge_user_count),0)' => 'day_recharge_user_count',
  396. 'DATE_FORMAT(createdate,"%Y-%m-%d")' => 'createdate',
  397. ];
  398. try {
  399. $total = 0;
  400. $list = [];
  401. $admin_id = (array)AdminService::instance()->getAdminId($admin_id)->data;
  402. if ($admin_id != [-1]) {
  403. $beginDate = date('Ymd', strtotime('-31 days'));
  404. $endDate = date('Ymd', strtotime('-1 days'));
  405. $total = $this->getUserCollectModel()
  406. ->whereIn('admin_id', $admin_id)
  407. ->where('createdate', '>=', $beginDate)
  408. ->where('createdate', '<=', $endDate)
  409. ->where(['type' => UserConstants::USER_COLLECT_TYPE_DAY])
  410. ->group('createdate')
  411. ->count();
  412. //总的数据
  413. $list = $this->getUserCollectModel()
  414. ->whereIn('admin_id', $admin_id)
  415. ->where(['type' => UserConstants::USER_COLLECT_TYPE_DAY])
  416. ->where('createdate', '>=', $beginDate)
  417. ->where('createdate', '<=', $endDate)
  418. ->order('createdate', 'desc')
  419. ->limit($offset, $limit)
  420. ->group('createdate')
  421. ->field($fields)
  422. ->select();
  423. foreach ($list as $key => $val) {
  424. if ($val['increase']) {
  425. $list[$key]['follow'] = NumberHelper::getFloat($val['increase_fllow']/ $val['increase']* 100);
  426. $list[$key]['recharge'] = NumberHelper::getFloat($val['increase_recharge']/ $val['increase']* 100);
  427. $list[$key]['guide_follow_num_percent'] = NumberHelper::getFloat($val['guide_follow_num']/ $val['increase']* 100);
  428. $list[$key]['unfollow_num_percent'] = NumberHelper::getFloat($val['unfollow_num']/ $val['increase']* 100);
  429. $list[$key]['net_follow_num_percent'] = NumberHelper::getFloat($val['net_follow_num']/ $val['increase']* 100);
  430. } else {
  431. $list[$key]['follow'] = 0;
  432. $list[$key]['recharge'] = 0;
  433. $list[$key]['guide_follow_num_percent'] = 0;
  434. $list[$key]['unfollow_num_percent'] = 0;
  435. $list[$key]['net_follow_num_percent'] = 0;
  436. }
  437. }
  438. }
  439. $result = ["total" => $total, "rows" => $list];
  440. return $this->setData($result)->getReturn();
  441. } catch (\Exception $e) {
  442. LogService::exception($e);
  443. return $this->setData([
  444. 'total' => 0,
  445. 'rows' => [],
  446. ])->getReturn();
  447. }
  448. }
  449. /**
  450. * 获取指定渠道今日数据
  451. * @param $admin_ids
  452. * @return \app\main\model\object\ReturnObject
  453. */
  454. public function getMigrateUserCollectChannelListDetail($admin_ids)
  455. {
  456. $list = [];
  457. $data = ApiService::instance()->getCollectFromApi(ApiConstants::API_VIP_USER_TODAY, ['ids' => $admin_ids])->data;
  458. $admins = AdminService::instance()->getAdminConfigModel()->field([
  459. 'replace(JSON_EXTRACT(json,"$.authorizer_info.nick_name"),\'"\',\'\')' => 'wx_nickname',
  460. 'admin_id' => 'admin_id',
  461. ])->whereIn('admin_id', $admin_ids)->select();
  462. $names = [];
  463. foreach ($admins as $admin) {
  464. $names[$admin['admin_id']] = $admin['wx_nickname'];
  465. }
  466. foreach ($data as $item) {
  467. $date = ArrayHelper::array_replace_key($item, $this->replaceFields);
  468. if (array_key_exists($item['adminId'], $names) && $names[$item['adminId']]) {
  469. $date['wx_nickname'] = $names[$item['adminId']];
  470. $list[] = array_merge($this->default, $date);
  471. }
  472. }
  473. return $this->setData($list)->getReturn();
  474. }
  475. /**
  476. * 获取单页渠道数据
  477. * @param $admin_id
  478. * @param int $offset
  479. * @param int $limit
  480. * @return \app\main\model\object\ReturnObject
  481. */
  482. public function getMigrateUserCollectChannelList($admin_id, $offset = 0, $limit = 10)
  483. {
  484. $admin_ids = (array)AdminService::instance()->getAdminId($admin_id)->data;
  485. //手动分页
  486. $filterIds = array_slice($admin_ids, $offset, $limit);
  487. $result = $this->getMigrateUserCollectChannelListDetail($filterIds)->data;
  488. $total = count($result);
  489. $result = ["total" => $total, "rows" => $result];
  490. return $this->setData($result)->getReturn();
  491. }
  492. /**
  493. * 获取渠道商今日用户统计
  494. * @param $admin_id
  495. * @param $offset
  496. * @param $limit
  497. * @return \app\main\model\object\ReturnObject
  498. */
  499. public function getUserCollectChannelList($admin_id, $offset = 0, $limit = 10)
  500. {
  501. try {
  502. $admin_ids = (array)AdminService::instance()->getAdminId($admin_id)->data;
  503. $today = date('Ymd');
  504. //查询总数量
  505. $total = AdminService::instance()->getAdminConfigModel()
  506. ->join('user_collect',
  507. "user_collect.admin_id = admin_config.admin_id and user_collect.type = 1 and createdate = '$today'", 'left')
  508. ->where(['admin_config.admin_id' => ['in', $admin_ids]])
  509. ->count();
  510. #region 从数据库中取出列表数据
  511. $list = AdminService::instance()->getAdminConfigModel()
  512. ->join('user_collect',
  513. "user_collect.admin_id = admin_config.admin_id and user_collect.type = 1 and createdate = '$today'", 'left')
  514. ->where(['admin_config.admin_id' => ['in', $admin_ids]])
  515. ->field([
  516. 'replace(JSON_EXTRACT(admin_config.json,"$.authorizer_info.nick_name"),\'"\',\'\')' => 'wx_nickname',
  517. 'admin_config.admin_id' => 'admin_id',
  518. 'increase',
  519. 'increase_m',
  520. 'increase_f',
  521. 'increase_fllow',
  522. 'increase_recharge',
  523. 'guide_follow_num',
  524. 'unfollow_num',
  525. 'net_follow_num',
  526. 'day_recharge_user_money',
  527. 'day_recharge_user_count',
  528. ])
  529. ->limit($offset, $limit)
  530. ->select();
  531. $result = [];
  532. foreach ($list as $item) {
  533. $tmp = [
  534. 'wx_nickname' => $item['wx_nickname'],
  535. 'admin_id' => $item['admin_id'],
  536. 'increase' => (int)$item['increase'],
  537. 'increase_m' => (int)$item['increase_m'],
  538. 'increase_f' => (int)$item['increase_f'],
  539. 'increase_fllow' => (int)$item['increase_fllow'],
  540. 'increase_recharge' => (int)$item['increase_recharge'],
  541. 'guide_follow_num' => (int)$item['guide_follow_num'],
  542. 'unfollow_num' => (int)$item['unfollow_num'],
  543. 'net_follow_num' => (int)$item['net_follow_num'],
  544. 'day_recharge_user_count' => (int)$item['day_recharge_user_count'],
  545. 'day_recharge_user_money' => (float)$item['day_recharge_user_money'],
  546. ];
  547. $result[] = $tmp;
  548. }
  549. $result = ["total" => $total, "rows" => $result];
  550. return $this->setData($result)->getReturn();
  551. } catch (\Exception $e) {
  552. LogService::exception($e);
  553. return $this->setData([])->getReturn();
  554. }
  555. }
  556. /**
  557. * 获取渠道-VIP-admin数据汇总-新
  558. * @param $admin_id
  559. * @param $group
  560. * @return \app\main\model\object\ReturnObject
  561. */
  562. public function getMigrateCollectData($admin_id, $group)
  563. {
  564. $default = $this->default;
  565. $result = [
  566. 'todayData' => $default,
  567. 'yesterdayData' => $default,
  568. 'totalData' => $default,
  569. 'monthData' => $default,
  570. ];
  571. $replaceFields = $this->replaceFields;
  572. //超管按照单渠道数据统计
  573. if (in_array($group, [AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN])) {
  574. $data = ApiService::instance()->getCollectFromApi(ApiConstants::API_USER_TOTAL, ['adminId' => 0])->data;
  575. } else {
  576. //获取关联的渠道
  577. $admin_ids = (array)AdminService::instance()->getAdminId($admin_id)->data;
  578. //没有关联渠道直接返回默认值
  579. if ($admin_ids == [-1]) {
  580. return $this->setData($result)->getReturn();
  581. }
  582. //渠道数据为1则按照渠道数据查询
  583. if (count($admin_ids) == 1) {
  584. $data = ApiService::instance()->getCollectFromApi(ApiConstants::API_USER_TOTAL, ['adminId' => $admin_ids[0]])->data;
  585. //多个渠道按照渠道数据汇总
  586. } else {
  587. $data = ApiService::instance()->getCollectFromApi(ApiConstants::API_VIP_USER_TOTAL, ['ids' => $admin_ids])->data;
  588. }
  589. }
  590. if ($data) {
  591. $all = ArrayHelper::array_replace_key($data['all'], $replaceFields);
  592. $result['totalData'] = array_merge($default, $all);
  593. $month = ArrayHelper::array_replace_key($data['month'], $replaceFields);
  594. $result['monthData'] = array_merge($default, $month);
  595. if (!empty($data['thirty'])) {
  596. foreach ($data['thirty'] as $date) {
  597. if ($date['date'] == date('Ymd')) {
  598. $date = ArrayHelper::array_replace_key($date, $replaceFields);
  599. $result['todayData'] = array_merge($default, $date);
  600. }else if ($date['date'] == date('Ymd', time() - 86400)) {
  601. $date = ArrayHelper::array_replace_key($date, $replaceFields);
  602. $result['yesterdayData'] = array_merge($default, $date);
  603. }
  604. }
  605. }
  606. }
  607. return $this->setData($result)->getReturn();
  608. }
  609. /**
  610. * 获取渠道-VIP-admin汇总数据-旧
  611. * @param $admin_id
  612. * @return \app\main\model\object\ReturnObject
  613. */
  614. public function getCollectData($admin_id)
  615. {
  616. $return = [];
  617. $return['totalData'] = UserCollectService::instance()->getTotalData($admin_id)->data;
  618. $return['todayData'] = UserCollectService::instance()->getUserCollectToday($admin_id)->data;
  619. $return['yesterdayData'] = UserCollectService::instance()->getUserCollectYesterday($admin_id)->data;
  620. $return['monthData'] = UserCollectService::instance()->getUserCollectMonth($admin_id)->data;
  621. return $this->setData($return)->getReturn();
  622. }
  623. }