end_time = $time; } /** * 获取昨日有数据的账号 * @return ReturnObject */ public function getAdminIdsToUpdate() { $admin_ids = UserCollectService::instance()->getUserCollectModel() ->where('createdate', date('Ymd', $this->end_time)) ->where('type', OrderContents::ORDER_COLLECT_TYPE_DAY) ->group('admin_id') ->column('admin_id'); return $this->setData($admin_ids)->getReturn(); } /** * 获取月度汇总 * @param $type * @param $admin_ids * @return ReturnObject */ public function getUserCollectData($type, $admin_ids) { $fields = [ 'admin_id', '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(unfollow_num),0)' => 'unfollow_num', 'IFNULL(sum(net_follow_num),0)' => 'net_follow_num', 'IFNULL(sum(guide_follow_num),0)' => 'guide_follow_num', 'IFNULL(sum(increase_recharge),0)' => 'increase_recharge', 'IFNULL(sum(day_recharge_user_money),0)' => 'day_recharge_user_money', 'IFNULL(sum(day_recharge_user_count),0)' => 'day_recharge_user_count', ]; $query = UserService::instance()->getUserCollectModel(); //查询月度统计数计 if ($type == UserConstants::USER_COLLECT_TYPE_MONTH) { $from = date('Ym01', $this->end_time); $to = date('Ymd', $this->end_time); $query ->whereBetween('createdate', "$from,$to") ->whereIn('admin_id', $admin_ids) ->where('type', UserConstants::USER_COLLECT_TYPE_DAY); //查询汇总数据 } else { $query->where('type', UserConstants::USER_COLLECT_TYPE_MONTH) ->whereIn('admin_id', $admin_ids); } $data = $query->field($fields) ->group('admin_id') ->select(); $return = []; if ($data) { foreach ($data as $item) { $collect = (new UserCollectObject())->bind($item->getData()); $collect->type = $type; $return[] = $collect; } } return $this->setData($return)->getReturn(); } /** * 数据更新 * @param $list */ public function insertOrUpdateCollect($list) { foreach ($list as $item) { $check = UserCollectService::instance()->getUserCollectModel()->mkParamsForCheck($item); $data = UserCollectService::instance()->getUserCollectModel()->where($check)->field('id')->find(); if (!$data) { try { $insert = UserCollectService::instance()->getUserCollectModel()->mkParamsForInsert($item); $result = UserCollectService::instance()->getUserCollectModel()->insert($insert); if ($result) { LogService::debug('用户统计新增完成' . json_encode($insert, JSON_UNESCAPED_UNICODE)); } else { LogService::error('用户统计新增失败' . json_encode($insert, JSON_UNESCAPED_UNICODE)); } continue; } catch (\Exception $e) { LogService::exception($e); } } $update = UserCollectService::instance()->getUserCollectModel()->mkParamsForUpdate($item); $result = UserCollectService::instance()->getUserCollectModel()->update($update, $check); if ($result) { LogService::debug('用户统计更新完成:' . json_encode($update, JSON_UNESCAPED_UNICODE) . json_encode($check, JSON_UNESCAPED_UNICODE)); } else { LogService::error('用户统计更新失败:' . json_encode($update, JSON_UNESCAPED_UNICODE) . json_encode($check, JSON_UNESCAPED_UNICODE)); } } } /** * 获取统计时期 * @param $type * @return ReturnObject */ public function getCreateDate($type) { $date = date('Ym01', $this->end_time); if ($type == UserConstants::USER_COLLECT_TYPE_TOTAL) { $date = '20180101'; } return $this->setData($date)->getReturn(); } }