|
@@ -6,7 +6,154 @@
|
|
* Date: 2016/11/2
|
|
* Date: 2016/11/2
|
|
* Time: 下午2:57
|
|
* Time: 下午2:57
|
|
*/
|
|
*/
|
|
|
|
+
|
|
class statistics_helper
|
|
class statistics_helper
|
|
{
|
|
{
|
|
|
|
+ private static $stInstance;
|
|
|
|
+ private $mItems;
|
|
|
|
+ private $mOther;
|
|
|
|
+
|
|
|
|
+ private $mRecordTime;
|
|
|
|
+
|
|
|
|
+ public static function instance()
|
|
|
|
+ {
|
|
|
|
+ if(self::$stInstance == null) {
|
|
|
|
+ self::$stInstance = new statistics_helper();
|
|
|
|
+ }
|
|
|
|
+ return self::$stInstance;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function __construct()
|
|
|
|
+ {
|
|
|
|
+ $this->mItems = [];
|
|
|
|
+ $this->mOther = [];
|
|
|
|
+ $this->mRecordTime = time();
|
|
|
|
+ }
|
|
|
|
+ public function __destroy()
|
|
|
|
+ {
|
|
|
|
+ $this->save();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function expired()
|
|
|
|
+ {
|
|
|
|
+ $day = new DateTime();
|
|
|
|
+ $day->setTimestamp($this->mRecordTime);
|
|
|
|
+ $day->setTime(0,0,0);
|
|
|
|
+ $time_stamp = $day->getTimestamp() + 24 * 60 * 60;
|
|
|
|
+ return ($this->mRecordTime >= $time_stamp);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function save()
|
|
|
|
+ {
|
|
|
|
+ $pid = posix_getpid();
|
|
|
|
+ $date = gmdate('Y-M-d',$this->mRecordTime);
|
|
|
|
+ $file = BASE_DATA_PATH_PATH . '/log/' . "{$date}-{$pid}.txt";
|
|
|
|
+ $data = array('star_time'=> $this->mRecordTime,'end_time' => time(),'function' => $this->mItems,'other' => $this->mOther);
|
|
|
|
+ $data = json_encode($data);
|
|
|
|
+ file_put_contents($file,$data);
|
|
|
|
+
|
|
|
|
+ $this->mRecordTime = time();
|
|
|
|
+ $this->mOther = [];
|
|
|
|
+ $this->mItems = [];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function add_call($param)
|
|
|
|
+ {
|
|
|
|
+ if($this->expired()) {
|
|
|
|
+ $this->save();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $act = $param['act'];
|
|
|
|
+ $op = $param['op'];
|
|
|
|
+
|
|
|
|
+ if($this->act($act) == false) return;
|
|
|
|
+ if($this->op($act,$op) == false) return;
|
|
|
|
+
|
|
|
|
+ $this->add_data($act,$op,$param);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function add_data($act,$op,$param)
|
|
|
|
+ {
|
|
|
|
+ $oper = &$this->mItems[$act][$op];
|
|
|
|
+
|
|
|
|
+ if(empty($oper['count'])) {
|
|
|
|
+ $oper['count'] = 1;
|
|
|
|
+ } else {
|
|
|
|
+ $oper['count'] += 1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if($act == 'goods_common') {
|
|
|
|
+ return $this->add_goods($op,$param);
|
|
|
|
+ }
|
|
|
|
+ if($act == 'special') {
|
|
|
|
+ return $this->add_special($op,$param);
|
|
|
|
+ }
|
|
|
|
+ if($act == 'member_bonus') {
|
|
|
|
+ return$this->add_bonus($op,$param);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function add_other($key,$count) {
|
|
|
|
+ if(array_key_exists($key,$this->mOther) == false) {
|
|
|
|
+ $this->mOther[$key] = [];
|
|
|
|
+ $this->mOther[$key]['count'] = $count;
|
|
|
|
+ } else {
|
|
|
|
+ $this->mOther[$key]['count'] += $count;
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function add_goods($op,$param)
|
|
|
|
+ {
|
|
|
|
+ if($op != 'index') return false;
|
|
|
|
+
|
|
|
|
+ $common_id = intval($param['goods_commonid']);
|
|
|
|
+ $goods_id = intval($param['goods_id']);
|
|
|
|
+ if($common_id > 0 && $goods_id > 0) {
|
|
|
|
+ $key = 'goods_' . $goods_id;
|
|
|
|
+ return $this->add_other($key,1);
|
|
|
|
+ } else {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ private function add_special($op,$param) {
|
|
|
|
+ if($op != 'index') return false;
|
|
|
|
+
|
|
|
|
+ $special_id = intval($param['special_id']);
|
|
|
|
+ if($special_id > 0) {
|
|
|
|
+ $key = 'special_' . $special_id;
|
|
|
|
+ return $this->add_other($key,1);
|
|
|
|
+ } else {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ private function add_bonus($op,$param)
|
|
|
|
+ {
|
|
|
|
+ if($op == 'make') {
|
|
|
|
+ $key = 'make_bonus';
|
|
|
|
+ $count = intval($param['total_num']);
|
|
|
|
+ return $this->add_other($key,$count);
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function act($act)
|
|
|
|
+ {
|
|
|
|
+ if(empty($act)) return false;
|
|
|
|
+
|
|
|
|
+ if(array_key_exists($act,$this->mItems) == false) {
|
|
|
|
+ $this->mItems[$act] = [];
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ private function op($act,$op) {
|
|
|
|
+ if(empty($op)) return false;
|
|
|
|
|
|
|
|
+ if(array_key_exists($op,$this->mItems[$act]) == false) {
|
|
|
|
+ $act[$op] = [];
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
}
|
|
}
|