123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2019/9/16
- * Time: 下午2:41
- */
- namespace app\main\service;
- use app\main\constants\OrderContents;
- use app\main\constants\UserConstants;
- use app\main\model\object\ReturnObject;
- use app\main\model\object\UserCollectObject;
- /**
- * Class UserCollectUpdateService
- * @package app\main\service
- */
- class UserCollectUpdateService extends BaseService
- {
- /**
- * @var UserCollectUpdateService
- */
- private static $self;
- /**
- * @var int 截止时间
- */
- public $end_time;
- public static function instance()
- {
- if (!self::$self) {
- self::$self = new self();
- }
- return self::$self;
- }
- /**
- * 设置时间
- * @param $time
- */
- public function setCurrentTime($time)
- {
- $this->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();
- }
- }
|