public function handle_request() { // 获取查询范围 $beginTime = $this->cursor['updatedDatetime']; $endTime = $beginTime + $this->interval; // 获取当前的DB最新时间(考虑到Web Server和DB时间可能有差异,以DB时间为准) $fetchTime = $this->getDbCurrentTime(); $formatBeginTime = date('Y-m-d H:i:s', $beginTime); $formatEndTime = date('Y-m-d H:i:s', $endTime); $formatFetchTime = date('Y-m-d H:i:s', $fetchTime); $this->log("*** BeginTime: {$formatBeginTime}; EndTime: {$formatEndTime}; FetchTime: {$formatFetchTime}"); // 获取更新信息的经纪人列表,逐个判断待同步的字段是否有更新 $brokers = Model_Broker_AjkBrokerExtend::getBrokersBetween($formatBeginTime, $formatEndTime); foreach ($brokers as $broker) { // 获取经纪人待同步字段 $columns = $broker->getSyncFields(); $columns = array_merge($columns, array('From' => 'User_BrokerSyncQueue')); // 提取新字段的MD5值 ksort($columns); $context = json_encode($columns); $md5 = md5($context); // 获取经纪人最新同步记录 $brokerInfo = Model_Log_LogMergeQueneNew::getLatestBroker($broker->brokerId); // 经纪人同步记录不存在 或者 存在且MD5值不一致,插入新纪录 if (!$brokerInfo || $brokerInfo->md5 != $md5) { $params = array(); $params['cityId'] = $broker->cityId; $params['brokerId'] = $broker->brokerId; $params['context'] = $context; $params['md5'] = $md5; $params['addTime'] = date('Y-m-d H:i:s'); Model_Log_LogMergeQueneNew::insertQuene($params); $this->log("BrokerId: {$broker->brokerId} - Remark: Success,CheckState: {$broker->checkState}"); } else { $this->log("BrokerId: {$broker->brokerId} - Remark: 经纪人待同步字段没有变更,CheckState: {$broker->checkState}"); } } // 更新游标 $this->cursor['updatedDatetime'] = min($endTime, $fetchTime); $this->setFlag($this->cursor); }
/** * @return array|bool */ protected function getBrokers() { switch ($this->action) { case 'hotfix': // 获取指定的经纪人 $brokers = Model_Broker_AjkBrokerExtend::findMultiBrokers($this->brokerIds); break; case 'default': // 获取最近更新过的经纪人记录 // 获取最近更新过的经纪人记录 default: $brokers = array(); $items = Model_Log_LogMergeQueneNew::getMultiBrokersByIdCursor($this->cursor['id'], $this->per); foreach ($items as $item) { $id = $item['id']; $brokers[$id] = $item->getContext(); $brokers[$id]['brokerId'] = $item['brokerId']; $brokers[$id]['id'] = $id; } } return $brokers; }
/** * 获取经纪人最新的更新记录 */ public static function getLatestBroker($brokerId) { $da = Model_Log_LogMergeQueneNew::data_access(); $data = $da->filter('brokerId', $brokerId)->sort('id', 'desc')->find(); return $data[0]; }