0, 'recharge' => 0, 'increase' => 0, 'increase_m' => 0, 'increase_f' => 0, 'increase_fllow' => 0, 'increase_recharge' => 0, 'unfollow_num' => 0, 'net_follow_num' => 0, 'day_recharge_user_money' => 0, 'day_recharge_user_count' => 0, ]; protected $replaceFields = [ 'adminId' => 'admin_id', 'increaseM' => 'increase_m', 'increaseF' => 'increase_f', 'increaseFllow' => 'increase_fllow', 'unfollowNum' => 'unfollow_num', 'netFollowNum' => 'net_follow_num', 'increaseRecharge' => 'increase_recharge', 'dayRechargeUserCount' => 'day_recharge_user_count', 'dayRechargeUserMoney' => 'day_recharge_user_money', 'date' => 'createdate', ]; /** * @var UserCollectService */ protected static $self = NULL; /** * @return UserCollectService */ public static function instance() { if (self::$self == NULL) { self::$self = new self(); } return self::$self; } /** * @return UserCollect */ public function getUserCollectModel() { return model('UserCollect'); } /** * 获取今日数据 * @param $admin_id * @return \app\main\model\object\ReturnObject */ public function getMigrateTodayData($admin_id) { $default = $this->default; $data = ApiService::instance()->getCollectFromApi(ApiConstants::API_USER_TODAY, ['adminId' => $admin_id])->data; if (!$data) { return $this->setData($default)->getReturn(); } $replaceFields = $this->replaceFields; $data = ArrayHelper::array_replace_key($data, $replaceFields); $data = array_merge($default, $data); return $this->setData($data)->getReturn(); } /** * 获取今天的用户统计数据 * @param $admin_id * @return \app\main\model\object\ReturnObject */ public function getUserCollectToday($admin_id) { $admin_id = (array)AdminService::instance()->getAdminId($admin_id)->data; $default = $this->default; $fields = [ 'IFNULL(SUM(increase),0)' => 'increase', 'IFNULL(SUM(increase_m),0)' => 'increase_m', 'IFNULL(SUM(increase_f),0)' => 'increase_f', 'IFNULL(SUM(increase_fllow),0)' => 'increase_fllow', 'IFNULL(SUM(increase_recharge),0)' => 'increase_recharge', 'IFNULL(SUM(guide_follow_num),0)' => 'guide_follow_num', 'IFNULL(SUM(unfollow_num),0)' => 'unfollow_num', 'IFNULL(SUM(net_follow_num),0)' => 'net_follow_num', 'IFNULL(SUM(day_recharge_user_money),0)' => 'day_recharge_user_money', 'IFNULL(SUM(day_recharge_user_count),0)' => 'day_recharge_user_count', ]; try { //取数据库数据 $where = [ 'type' => UserConstants::USER_COLLECT_TYPE_DAY, 'createdate' => date('Ymd', time()), 'admin_id' => ['in', $admin_id], ]; $data = $this->getUserCollectModel() ->where($where) ->field($fields) ->find() ->toArray(); if ($data) { if ($data['increase']) { $data['follow'] = NumberHelper::getFloat($data['increase_fllow'] * 100 / $data['increase']); $data['recharge'] = NumberHelper::getFloat($data['increase_recharge'] * 100 / $data['increase']); $data['guide_follow_num_percent'] = NumberHelper::getFloat($data['guide_follow_num'] * 100 / $data['increase']); $data['unfollow_num_percent'] = NumberHelper::getFloat($data['unfollow_num'] * 100 / $data['increase']); $data['net_follow_num_percent'] = NumberHelper::getFloat($data['net_follow_num'] * 100 / $data['increase']); } else { $data['follow'] = 0; $data['recharge'] = 0; $data['guide_follow_num_percent'] = 0; $data['unfollow_num_percent'] = 0; $data['net_follow_num_percent'] = 0; } } else { $data = $default; } $pay = intval($data['increase_recharge'] ?? 0); //算 支付比例 if ($pay && $data['increase']) { $data['recharge'] = round($pay * 100 / $data['increase'], 2); } return $this->setData($data)->getReturn(); } catch (\Exception $e) { LogService::exception($e); return $this->setData($default)->getReturn(); } } /** * 获取昨日统计数据 * @param $admin_id * @return \app\main\model\object\ReturnObject */ public function getUserCollectYesterday($admin_id) { $admin_id = (array)AdminService::instance()->getAdminId($admin_id)->data; $default = $this->default; $fields = [ 'IFNULL(SUM(increase),0)' => 'increase', 'IFNULL(SUM(increase_m),0)' => 'increase_m', 'IFNULL(SUM(increase_f),0)' => 'increase_f', 'IFNULL(SUM(increase_fllow),0)' => 'increase_fllow', 'IFNULL(SUM(increase_recharge),0)' => 'increase_recharge', 'IFNULL(SUM(guide_follow_num),0)' => 'guide_follow_num', 'IFNULL(SUM(unfollow_num),0)' => 'unfollow_num', 'IFNULL(SUM(net_follow_num),0)' => 'net_follow_num', 'IFNULL(SUM(day_recharge_user_money),0)' => 'day_recharge_user_money', 'IFNULL(SUM(day_recharge_user_count),0)' => 'day_recharge_user_count', ]; try { $where = [ 'type' => UserConstants::USER_COLLECT_TYPE_DAY, 'createdate' => date('Ymd', strtotime('-1 days')), 'admin_id' => ['in', $admin_id], ]; $data = $this->getUserCollectModel() ->where($where) ->whereIn('admin_id', $admin_id) ->field($fields) ->find() ->toArray(); if (!$data) { $data = $default; } return $this->setData($data)->getReturn(); } catch (\Exception $e) { LogService::exception($e); return $this->setData($default)->getReturn(); } } /** * 获取月度统计数据 * @param $admin_id * @return \app\main\model\object\ReturnObject */ public function getUserCollectMonth($admin_id) { $admin_id = (array)AdminService::instance()->getAdminId($admin_id)->data; $default = $this->default; $fields = [ 'IFNULL(SUM(increase),0)' => 'increase', 'IFNULL(SUM(increase_m),0)' => 'increase_m', 'IFNULL(SUM(increase_f),0)' => 'increase_f', 'IFNULL(SUM(increase_fllow),0)' => 'increase_fllow', 'IFNULL(SUM(increase_recharge),0)' => 'increase_recharge', 'IFNULL(SUM(guide_follow_num),0)' => 'guide_follow_num', 'IFNULL(SUM(unfollow_num),0)' => 'unfollow_num', 'IFNULL(SUM(net_follow_num),0)' => 'net_follow_num', 'IFNULL(SUM(day_recharge_user_money),0)' => 'day_recharge_user_money', 'IFNULL(SUM(day_recharge_user_count),0)' => 'day_recharge_user_count', ]; try { $where = [ 'type' => UserConstants::USER_COLLECT_TYPE_DAY, 'admin_id' => ['in', $admin_id], ]; $data = $this->getUserCollectModel() ->where($where) ->where("createdate>=" . date('Ym') . "01") ->where("createdate<=" . date('Ymd', strtotime('-1 days'))) ->field($fields) ->find() ->toArray(); if (!$data) { $data = $default; } return $this->setData($data)->getReturn(); } catch (\Exception $e) { LogService::exception($e); return $this->setData($default)->getReturn(); } } /** * 获取总数据 * @param $admin_id * @return \app\main\model\object\ReturnObject */ public function getTotalData($admin_id) { $admin_id = (array)AdminService::instance()->getAdminId($admin_id)->data; $default = $this->default; $fields = [ 'IFNULL(SUM(increase),0)' => 'increase', 'IFNULL(SUM(increase_m),0)' => 'increase_m', 'IFNULL(SUM(increase_f),0)' => 'increase_f', 'IFNULL(SUM(increase_fllow),0)' => 'increase_fllow', 'IFNULL(SUM(increase_recharge),0)' => 'increase_recharge', 'IFNULL(SUM(guide_follow_num),0)' => 'guide_follow_num', 'IFNULL(SUM(unfollow_num),0)' => 'unfollow_num', 'IFNULL(SUM(net_follow_num),0)' => 'net_follow_num', 'IFNULL(SUM(day_recharge_user_money),0)' => 'day_recharge_user_money', 'IFNULL(SUM(day_recharge_user_count),0)' => 'day_recharge_user_count', ]; try { $where = [ 'type' => UserConstants::USER_COLLECT_TYPE_DAY, 'admin_id' => ['in', $admin_id], ]; $data = $this->getUserCollectModel() ->field($fields) ->where($where) ->where("createdate<=" . date('Ymd', strtotime('-1 days'))) ->find() ->toArray(); if (!$data) { $data = $default; } return $this->setData($data)->getReturn(); } catch (\Exception $e) { LogService::exception($e); return $this->setData($default)->getReturn(); } } /** * 获取渠道30日数据 * @param $admin_id * @param $where * @param $sort * @param $order * @param $offset * @param $limit * @return \app\main\model\object\ReturnObject */ public function getUserCollectDateListData($admin_id, $where = '', $sort = 'createdate', $order = 'desc', $offset = 0, $limit = 10, $group = AdminConstants::ADMIN_GROUP_ID_CHANNEL) { try { $total = 0; $list = []; if ($group == AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN) { $admin_id = 0; } if (VisitLimitService::instance()->checkMigratedV2()) { $data = ApiService::instance()->getCollectFromApi(ApiConstants::API_USER_TOTAL, ['adminId' => $admin_id])->data; if ($data) { $list = []; foreach ($data['thirty'] as $item) { if ($item['date'] == date('Ymd')) { continue; } $date = ArrayHelper::array_replace_key($item, $this->replaceFields); $list[] = array_merge($this->default, $date); } usort($list, function($v1, $v2){ return $v1['createdate'] < $v2['createdate']; }); $total = count($list); $list = array_slice($list, $offset, $limit); } } else { $beginDate = date('Ymd', strtotime('-31 days')); $endDate = date('Ymd', strtotime('-1 days')); $total = $this->getUserCollectModel() ->where($where) ->where('admin_id', $admin_id) ->where('createdate', '>=', $beginDate) ->where('createdate', '<=', $endDate) ->where(['type' => UserConstants::USER_COLLECT_TYPE_DAY]) ->count(); //总的数据 $list = $this->getUserCollectModel() ->where($where) ->where('admin_id', $admin_id) ->where(['type' => UserConstants::USER_COLLECT_TYPE_DAY]) ->where('createdate', '>=', $beginDate) ->where('createdate', '<=', $endDate) ->order($sort, $order) ->limit($offset, $limit) ->select(); } $result = ["total" => $total, "rows" => $list]; return $this->setData($result)->getReturn(); } catch (\Exception $e) { LogService::exception($e); return $this->setData([ 'total' => 0, 'rows' => [], ])->getReturn(); } } /** * 获取VIP30日数据汇总 * @param $admin_id * @param int $offset * @param int $limit * @return \app\main\model\object\ReturnObject */ public function getMigratedUserCollectDateAggsListData($admin_id, $offset = 0, $limit = 10) { $total = 0; $list = []; $admin_ids = (array)AdminService::instance()->getAdminId($admin_id)->data; if ($admin_ids != [-1]) { $data = ApiService::instance()->getCollectFromApi(ApiConstants::API_VIP_USER_TOTAL, ['ids' => $admin_ids])->data; if ($data) { $list = []; foreach ($data['thirty'] as $item) { if ($item['date'] == date('Ymd')) { continue; } $date = ArrayHelper::array_replace_key($item, $this->replaceFields); $list[] = array_merge($this->default, $date); } usort($list, function ($v1, $v2) { return $v1['createdate'] < $v2['createdate']; }); $total = count($list); $list = array_slice($list, $offset, $limit); } } $result = ["total" => $total, "rows" => $list]; return $this->setData($result)->getReturn(); } /** * @param $admin_id * @param $where * @param $sort * @param $order * @param $offset * @param $limit * @return \app\main\model\object\ReturnObject */ public function getUserCollectDateAggsListData($admin_id, $offset = 0, $limit = 10) { $fields = [ 'IFNULL(SUM(increase),0)' => 'increase', 'IFNULL(SUM(increase_m),0)' => 'increase_m', 'IFNULL(SUM(increase_f),0)' => 'increase_f', 'IFNULL(SUM(increase_fllow),0)' => 'increase_fllow', 'IFNULL(SUM(increase_recharge),0)' => 'increase_recharge', 'IFNULL(SUM(guide_follow_num),0)' => 'guide_follow_num', 'IFNULL(SUM(unfollow_num),0)' => 'unfollow_num', 'IFNULL(SUM(net_follow_num),0)' => 'net_follow_num', 'IFNULL(SUM(day_recharge_user_money),0)' => 'day_recharge_user_money', 'IFNULL(SUM(day_recharge_user_count),0)' => 'day_recharge_user_count', 'DATE_FORMAT(createdate,"%Y-%m-%d")' => 'createdate', ]; try { $total = 0; $list = []; $admin_id = (array)AdminService::instance()->getAdminId($admin_id)->data; if ($admin_id != [-1]) { $beginDate = date('Ymd', strtotime('-31 days')); $endDate = date('Ymd', strtotime('-1 days')); $total = $this->getUserCollectModel() ->whereIn('admin_id', $admin_id) ->where('createdate', '>=', $beginDate) ->where('createdate', '<=', $endDate) ->where(['type' => UserConstants::USER_COLLECT_TYPE_DAY]) ->group('createdate') ->count(); //总的数据 $list = $this->getUserCollectModel() ->whereIn('admin_id', $admin_id) ->where(['type' => UserConstants::USER_COLLECT_TYPE_DAY]) ->where('createdate', '>=', $beginDate) ->where('createdate', '<=', $endDate) ->order('createdate', 'desc') ->limit($offset, $limit) ->group('createdate') ->field($fields) ->select(); foreach ($list as $key => $val) { if ($val['increase']) { $list[$key]['follow'] = NumberHelper::getFloat($val['increase_fllow']/ $val['increase']* 100); $list[$key]['recharge'] = NumberHelper::getFloat($val['increase_recharge']/ $val['increase']* 100); $list[$key]['guide_follow_num_percent'] = NumberHelper::getFloat($val['guide_follow_num']/ $val['increase']* 100); $list[$key]['unfollow_num_percent'] = NumberHelper::getFloat($val['unfollow_num']/ $val['increase']* 100); $list[$key]['net_follow_num_percent'] = NumberHelper::getFloat($val['net_follow_num']/ $val['increase']* 100); } else { $list[$key]['follow'] = 0; $list[$key]['recharge'] = 0; $list[$key]['guide_follow_num_percent'] = 0; $list[$key]['unfollow_num_percent'] = 0; $list[$key]['net_follow_num_percent'] = 0; } } } $result = ["total" => $total, "rows" => $list]; return $this->setData($result)->getReturn(); } catch (\Exception $e) { LogService::exception($e); return $this->setData([ 'total' => 0, 'rows' => [], ])->getReturn(); } } /** * 获取指定渠道今日数据 * @param $admin_ids * @return \app\main\model\object\ReturnObject */ public function getMigrateUserCollectChannelListDetail($admin_ids) { $list = []; $data = ApiService::instance()->getCollectFromApi(ApiConstants::API_VIP_USER_TODAY, ['ids' => $admin_ids])->data; $admins = AdminService::instance()->getAdminConfigModel()->field([ 'replace(JSON_EXTRACT(json,"$.authorizer_info.nick_name"),\'"\',\'\')' => 'wx_nickname', 'admin_id' => 'admin_id', ])->whereIn('admin_id', $admin_ids)->select(); $names = []; foreach ($admins as $admin) { $names[$admin['admin_id']] = $admin['wx_nickname']; } foreach ($data as $item) { $date = ArrayHelper::array_replace_key($item, $this->replaceFields); if (array_key_exists($item['adminId'], $names) && $names[$item['adminId']]) { $date['wx_nickname'] = $names[$item['adminId']]; $list[] = array_merge($this->default, $date); } } return $this->setData($list)->getReturn(); } /** * 获取单页渠道数据 * @param $admin_id * @param int $offset * @param int $limit * @return \app\main\model\object\ReturnObject */ public function getMigrateUserCollectChannelList($admin_id, $offset = 0, $limit = 10) { $admin_ids = (array)AdminService::instance()->getAdminId($admin_id)->data; //手动分页 $filterIds = array_slice($admin_ids, $offset, $limit); $result = $this->getMigrateUserCollectChannelListDetail($filterIds)->data; $total = count($result); $result = ["total" => $total, "rows" => $result]; return $this->setData($result)->getReturn(); } /** * 获取渠道商今日用户统计 * @param $admin_id * @param $offset * @param $limit * @return \app\main\model\object\ReturnObject */ public function getUserCollectChannelList($admin_id, $offset = 0, $limit = 10) { try { $admin_ids = (array)AdminService::instance()->getAdminId($admin_id)->data; $today = date('Ymd'); //查询总数量 $total = AdminService::instance()->getAdminConfigModel() ->join('user_collect', "user_collect.admin_id = admin_config.admin_id and user_collect.type = 1 and createdate = '$today'", 'left') ->where(['admin_config.admin_id' => ['in', $admin_ids]]) ->count(); #region 从数据库中取出列表数据 $list = AdminService::instance()->getAdminConfigModel() ->join('user_collect', "user_collect.admin_id = admin_config.admin_id and user_collect.type = 1 and createdate = '$today'", 'left') ->where(['admin_config.admin_id' => ['in', $admin_ids]]) ->field([ 'replace(JSON_EXTRACT(admin_config.json,"$.authorizer_info.nick_name"),\'"\',\'\')' => 'wx_nickname', 'admin_config.admin_id' => 'admin_id', 'increase', 'increase_m', 'increase_f', 'increase_fllow', 'increase_recharge', 'guide_follow_num', 'unfollow_num', 'net_follow_num', 'day_recharge_user_money', 'day_recharge_user_count', ]) ->limit($offset, $limit) ->select(); $result = []; foreach ($list as $item) { $tmp = [ 'wx_nickname' => $item['wx_nickname'], 'admin_id' => $item['admin_id'], 'increase' => (int)$item['increase'], 'increase_m' => (int)$item['increase_m'], 'increase_f' => (int)$item['increase_f'], 'increase_fllow' => (int)$item['increase_fllow'], 'increase_recharge' => (int)$item['increase_recharge'], 'guide_follow_num' => (int)$item['guide_follow_num'], 'unfollow_num' => (int)$item['unfollow_num'], 'net_follow_num' => (int)$item['net_follow_num'], 'day_recharge_user_count' => (int)$item['day_recharge_user_count'], 'day_recharge_user_money' => (float)$item['day_recharge_user_money'], ]; $result[] = $tmp; } $result = ["total" => $total, "rows" => $result]; return $this->setData($result)->getReturn(); } catch (\Exception $e) { LogService::exception($e); return $this->setData([])->getReturn(); } } /** * 获取渠道-VIP-admin数据汇总-新 * @param $admin_id * @param $group * @return \app\main\model\object\ReturnObject */ public function getMigrateCollectData($admin_id, $group) { $default = $this->default; $result = [ 'todayData' => $default, 'yesterdayData' => $default, 'totalData' => $default, 'monthData' => $default, ]; $replaceFields = $this->replaceFields; //超管按照单渠道数据统计 if (in_array($group, [AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN])) { $data = ApiService::instance()->getCollectFromApi(ApiConstants::API_USER_TOTAL, ['adminId' => 0])->data; } else { //获取关联的渠道 $admin_ids = (array)AdminService::instance()->getAdminId($admin_id)->data; //没有关联渠道直接返回默认值 if ($admin_ids == [-1]) { return $this->setData($result)->getReturn(); } //渠道数据为1则按照渠道数据查询 if (count($admin_ids) == 1) { $data = ApiService::instance()->getCollectFromApi(ApiConstants::API_USER_TOTAL, ['adminId' => $admin_ids[0]])->data; //多个渠道按照渠道数据汇总 } else { $data = ApiService::instance()->getCollectFromApi(ApiConstants::API_VIP_USER_TOTAL, ['ids' => $admin_ids])->data; } } if ($data) { $all = ArrayHelper::array_replace_key($data['all'], $replaceFields); $result['totalData'] = array_merge($default, $all); $month = ArrayHelper::array_replace_key($data['month'], $replaceFields); $result['monthData'] = array_merge($default, $month); if (!empty($data['thirty'])) { foreach ($data['thirty'] as $date) { if ($date['date'] == date('Ymd')) { $date = ArrayHelper::array_replace_key($date, $replaceFields); $result['todayData'] = array_merge($default, $date); }else if ($date['date'] == date('Ymd', time() - 86400)) { $date = ArrayHelper::array_replace_key($date, $replaceFields); $result['yesterdayData'] = array_merge($default, $date); } } } } return $this->setData($result)->getReturn(); } /** * 获取渠道-VIP-admin汇总数据-旧 * @param $admin_id * @return \app\main\model\object\ReturnObject */ public function getCollectData($admin_id) { $return = []; $return['totalData'] = UserCollectService::instance()->getTotalData($admin_id)->data; $return['todayData'] = UserCollectService::instance()->getUserCollectToday($admin_id)->data; $return['yesterdayData'] = UserCollectService::instance()->getUserCollectYesterday($admin_id)->data; $return['monthData'] = UserCollectService::instance()->getUserCollectMonth($admin_id)->data; return $this->setData($return)->getReturn(); } }