mItems = []; $this->mOther = []; $this->reset_time(); } public function __destroy() { $this->save(); } public function add_logs($params) { $this->add_items($params); $this->add_others($params); if($this->expired()) { $this->save(); } } private function add_others($params) { $others = $params['other']; foreach ($others as $key => $count) { $this->add_other($key,$count); } } private function add_items($params) { $acts = $params['function']; foreach ($acts as $act => $ops) { if($this->act($act) == false) continue; foreach ($ops as $opx => $data) { if($this->op($act,$opx) == false) continue; $oper = &$this->mItems[$act][$opx]; foreach ($data as $key => $value) { if($key = 'count') { if(empty($oper['count'])) { $oper['count'] = 0; } $oper['count'] += intval($value); } } } } } private function expired() { return (time() >= $this->mSaveTime); } private function reset_time() { $this->mRecordTime = time(); $day = new DateTime(); $day->setTimestamp($this->mRecordTime); $day->setTime(0,0,0); $morning = $day->getTimestamp(); $noon = $morning + 12 * 60 * 60; $midnight = $day->getTimestamp() + 24 * 60 * 60; if(time() < $noon) { $this->mSaveTime = $noon; } else { $this->mSaveTime = $midnight; } } private function save() { $pid = posix_getpid(); $date = date('Ymd-H',time()); $file = BASE_DATA_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->mOther = []; $this->mItems = []; $this->reset_time(); } public function send_queue() { $data = array('star_time'=> $this->mRecordTime,'end_time' => time(),'function' => $this->mItems,'other' => $this->mOther); QueueClient::push("savelog",$data); $this->mRecordTime = time(); $this->mOther = []; $this->mItems = []; } public function add_call($param) { if(time() >= $this->mRecordTime + self::interval_time) { $this->send_queue(); } $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(isset($this->mOther[$key]) == 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(isset($this->mItems[$act]) == false) { $this->mItems[$act] = []; } return true; } private function op($act,$op) { if(empty($op)) return false; if(isset($this->mItems[$act][$op]) == false) { $this->mItems[$act][$op] = []; } return true; } }