<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/5/26
 * Time: 下午9:52
 */

namespace statistics;

class stmember extends stat_base
{
    public function __construct($stime)
    {
        parent::__construct($stime);
    }

    /*
    member_total_num int default '0' null comment '月活',
    day_active_member int default '0' null comment '日活用户数',
    week_active_member int default '0' null,
    halfmoon_active_member int default '0' null,
    month_active_member int default '0' null comment '月活用户数',
    */
    public function init_user()
    {
        $this->today_user(stat_base::cur_date_type);
    }
    public function daily()
    {
        $this->active_member(stat_base::cur_date_type,'day_active_member');
        $this->active_member(stat_base::last_week_type,'week_active_member');
        $this->active_member(stat_base::last_hmonth_type,'halfmoon_active_member');
        $this->active_member(stat_base::last_month_type,'month_active_member');
        $this->total_member();
        $this->today_user(stat_base::cur_date_type);
    }

    private function active_member($type,$name)
    {
        $this->calc_time($type);
        $mod_member = Model('member');
        $count = $mod_member->where(['member_login_time' => ['between',[$this->mStartm,$this->mEndtm]]])->count();

        if(empty($count)) {
            return false;
        }
        else
        {
            $result = $this->mModel->get_one($this->mDateId,'invite_user_count',true);
            $fEdit = empty($result) ? false : true;

            $datas[$name] = intval($count);
            return $this->save($fEdit,$datas);
        }
    }

    private function total_member()
    {
        $mod_member = Model('member');
        $count = $mod_member->count();

        if(empty($count)) {
            return false;
        }
        else
        {
            $result = $this->mModel->get_one($this->mDateId,'invite_user_count',true);
            $fEdit = empty($result) ? false : true;

            $datas['member_total_num'] = intval($count);;
            return $this->save($fEdit,$datas);
        }
    }

    private function today_user($type)
    {
        $this->calc_time($type);
        $mod_member = Model('member');
        $count = $mod_member->where(['member_time' => ['between',[$this->mStartm,$this->mEndtm]]])->count();

        if(empty($count)) {
            return false;
        }
        else
        {
            $result = $this->mModel->get_one($this->mDateId,'today_user',true);
            $fEdit = empty($result) ? false : true;

            $datas['today_user'] = intval($count);
            return $this->save($fEdit,$datas);
        }
    }

    public function onInvite()
    {
        $result = $this->mModel->get_one($this->mDateId,'invite_user_count',true);
        $fEdit = empty($result) ? false : true;
        if($fEdit) {
            $datas['invite_user_count'] = 1;
        } else {
            $datas['invite_user_count'] = $result[0]['invite_user_count'] + 1;
        }

        return $this->save($fEdit,$datas);
    }
}