/**
  * 获取二手房房源的推广状态
  *
  * @param int|array $houseId
  * @return array
  */
 private function getHouseSpreadStateAjk($houseId)
 {
     $result = array('spread_house' => array(), 'not_spread_house' => array());
     try {
         $dataAccess = Model_Plan_EsfAjkPropSpread::data_access();
         $dataAccess->filter('propId', $houseId);
         $dataAccess->filter_by_op('status', '<>', Model_Plan_EsfAjkPropSpread::DELETE);
         $dataAccess->filter_by_op('tradetype', '=', Model_Plan_EsfAjkPropSpread::SALE_TYPE);
         $planList = $dataAccess->get_all();
     } catch (Exception $e) {
         return $result;
     }
     if (empty($planList)) {
         $result['not_spread_house'] = is_array($houseId) ? $houseId : array($houseId);
         return $result;
     }
     foreach ($planList as $planInfo) {
         if ($planInfo['status'] == Model_Plan_EsfAjkPropSpread::ONLINE) {
             $result['spread_house'][] = $planInfo['propId'];
         }
     }
     if (is_array($houseId)) {
         $result['not_spread_house'] = array_values(array_diff($houseId, $result['spread_house']));
     } elseif (empty($result['spread_house'])) {
         $result['not_spread_house'][] = $houseId;
     }
     return $result;
 }
 /**
  * 获取二手房 精选状态发生变更的房源
  *
  * @param int $startTime
  * @param int $endTime
  * @param int $startId
  * @return array
  */
 private function getHouseSpreadStateChangeResultAjk($startTime, $endTime, $startId)
 {
     $result = array('spread_house' => array(), 'not_spread_house' => array(), 'last_id' => 0);
     try {
         $dataAccess = Model_Plan_EsfAjkPropSpread::data_access('');
         $dataAccess->filter_by_op('updatedDatetime', '>=', date('Y-m-d H:i:s', $startTime));
         $dataAccess->filter_by_op('updatedDatetime', '<=', date('Y-m-d H:i:s', $endTime));
         if ($startId > 0) {
             $dataAccess->filter_by_op('id', '>', $startId);
         }
         $dataAccess->load_field(array('id', 'status', 'propId', 'brokerId'));
         $dataAccess->sort('id', 'asc');
         $dataAccess->limit(1000);
         $stateChangePlanList = $dataAccess->get_all();
     } catch (Exception $e) {
         return $result;
     }
     if (empty($stateChangePlanList)) {
         return $result;
     }
     foreach ($stateChangePlanList as $stateChangePlan) {
         $result['last_id'] = $stateChangePlan['id'];
         if ($stateChangePlan['status'] == Model_Plan_EsfAjkPropSpread::ONLINE) {
             $result['spread_house'][] = $stateChangePlan['propId'];
         } else {
             $result['not_spread_house'][] = $stateChangePlan['propId'];
         }
     }
     return $result;
 }
 public function dealTask($task)
 {
     $currentUsedNum = $task['usedNum'];
     //获取价格信息
     $priceIntervalInfo = Model_House_EsfPrice::data_access()->filter('id', $task['priceId'])->find_only();
     if (empty($priceIntervalInfo)) {
         printf("价格段priceId:" . $task['priceId'] . "获取失败" . PHP_EOL);
         return false;
     }
     //获取实际计划数目
     $trueUsedNum = Model_Plan_EsfAjkPropSpread::data_access()->filter("areacode", $task['typeCode'])->filter("smallprice", $priceIntervalInfo->minPrice)->filter("bigprice", $priceIntervalInfo->maxPrice)->filter("bidVersion", Model_Plan_EsfAjkPropSpread::CHOICEVERSION)->filter("model", Model_Plan_EsfAjkPropSpread::BLOCK_CHOICE)->filter_by_op('status', 'in', array(Model_Plan_EsfAjkPropSpread::ONLINE, Model_Plan_EsfAjkPropSpread::ONQUEUE))->count();
     printf('正在处理 队列id %s, areaCode %s, priceId %s 结果是: userNum: %s, 实际: %s, 状态:%s' . PHP_EOL, $task['id'], $task['typeCode'], $task['priceId'], $currentUsedNum, $trueUsedNum, $currentUsedNum == $trueUsedNum ? 'success' : 'error');
     if ($currentUsedNum == $trueUsedNum) {
         return true;
     }
     $result = Model_Choice_EsfBlockBidBucket::updateUsedNum($task['id'], $trueUsedNum, $task['usedNum']);
     printf('更新usedNum结果: %s' . PHP_EOL, $result ? '更新成功' : '更新失败');
     return true;
 }
 /**
  * 获取经纪人精选推广的房源Id
  *
  * @param int $brokerId
  * @return array
  */
 private function getBrokerHouseSpreadStateResultAjk($brokerId)
 {
     $result = array('spread_house' => array(), 'not_spread_house' => array());
     try {
         $dataAccess = Model_Plan_EsfAjkPropSpread::data_access('');
         $dataAccess->filter('brokerId', $brokerId);
         $dataAccess->filter('status', Model_Plan_EsfAjkPropSpread::ONLINE);
         $dataAccess->load_field(array('id', 'status', 'propId', 'brokerId'));
         $choicePlanList = $dataAccess->get_all();
     } catch (Exception $e) {
         return $result;
     }
     if (empty($choicePlanList)) {
         return $result;
     }
     foreach ($choicePlanList as $planInfo) {
         $result['spread_house'][] = $planInfo['propId'];
     }
     return $result;
 }
 public function run()
 {
     //todo 临时编写job,没有利用游标
     $nativeSql = sprintf("SELECT prop_id FROM `ajk_propspread` WHERE STATUS != 2 AND bidversion = 2 GROUP BY prop_id HAVING COUNT(*)>1");
     $allRecords = Model_Plan_EsfAjkPropSpread::data_access()->native_sql($nativeSql, array(), true);
     foreach ($allRecords as $row) {
         $costMoney = 0;
         //获取所有计划
         $propRecords = Model_Plan_EsfAjkPropSpread::data_access()->filter_by_op('status', '<>', 2)->filter_by_op('bidVersion', '=', 2)->filter_by_op('propId', '=', $row['prop_id'])->find_all();
         foreach ($propRecords as $subRow) {
             if ($subRow->budget != $subRow->balance) {
                 $costMoney++;
             }
         }
         if ($costMoney == 0) {
             //都没有消费,保留最后一个
             foreach ($propRecords as $key => $subRow) {
                 if ($key == count($propRecords) - 1) {
                     $this->addLogger(sprintf("保留计划%d,该计划为最后计划", $subRow->id));
                     continue;
                 }
                 //塞入自动停止队列
                 Model_Plan_EsfAjkPropspreadQueue::insertOne($subRow->id, time(), 1);
                 $this->addLogger(sprintf("计划%d塞入自动停止队列", $subRow->id));
             }
         } else {
             foreach ($propRecords as $key => $subRow) {
                 if ($subRow->budget != $subRow->balance) {
                     $this->addLogger(sprintf("保留计划%d,该计划有消费", $subRow->id));
                 } else {
                     //塞入自动停止队列
                     Model_Plan_EsfAjkPropspreadQueue::insertOne($subRow->id, time(), 1);
                     $this->addLogger(sprintf("计划%d塞入自动停止队列", $subRow->id));
                 }
             }
         }
     }
 }
 /**
  * 页面删除推广操作封装
  *
  * @param $planId
  * @return array
  */
 public static function delete_plan($planId, $brokerId)
 {
     $rstArr = array('status' => 'ok');
     //获取计划信息
     $planInfo = Model_Plan_EsfAjkPropSpread::data_access()->filter('id', $planId)->find_only();
     if (empty($planInfo)) {
         return array('status' => 'fail', 'info' => 'empty plan');
     }
     if ($planInfo->status == Model_Plan_EsfAjkPropSpread::ONLINE) {
         return array('status' => 'fail', 'info' => '计划正在推广中');
     }
     if ($planInfo->status == Model_Plan_EsfAjkPropSpread::ONQUEUE) {
         return array('status' => 'fail', 'info' => '计划正在排队中');
     }
     if ($planInfo->tradestatus != Model_Plan_EsfAjkPropSpread::COMPLETETTRADESTATU) {
         return array('status' => 'fail', 'info' => '计划正在结算中');
     }
     if ($planInfo->status == Model_Plan_EsfAjkPropSpread::DELETE) {
         return array('status' => 'ok', 'info' => '计划已经被删除');
     }
     if ($brokerId != $planInfo->brokerId) {
         return array('status' => 'fail', 'info' => 'invalid broker');
     }
     if (Model_Plan_EsfAjkPropSpread::COMM_CHOICE != $planInfo->model) {
         return array('status' => 'fail', 'info' => 'invalid house');
     }
     //验证choice城市
     $cityId = $planInfo->cityId;
     if (!Bll_City::isChoiceCity($cityId)) {
         return array('status' => 'fail', 'info' => 'starttime not achieved ');
     }
     //更新状态
     $oldStatus = $planInfo->status;
     $planInfo->status = Model_Plan_EsfAjkPropSpread::DELETE;
     if (!$planInfo->save()) {
         return array('status' => 'fail', 'info' => 'update plan status fail');
     }
     //记录日志
     $remark = sprintf("STATUS:%d=>%d", $oldStatus, Model_Plan_EsfAjkPropSpread::DELETE);
     Model_Plan_LogAjkPropspread::addNewLog($planId, $brokerId, Model_Plan_LogAjkPropspread::DEL, $remark);
     return $rstArr;
 }
 public function run()
 {
     //根据游标获取一定数量的坑位信息
     $bucketInfos = Model_Choice_EsfBidBucket::getBucketInfoById($this->cursor, 0, $this->limit);
     //无需要处理的数据
     if (empty($bucketInfos)) {
         $this->setShStopFlag();
         $this->setCursor(0);
         $this->logQuit("无需要处理的数据.");
     }
     //检验具体的坑位信息
     foreach ($bucketInfos as $bucketInfo) {
         //获取价格信息
         $priceIntervalInfo = Model_House_EsfPrice::data_access()->filter('id', $bucketInfo->priceId)->find_only();
         if (empty($priceIntervalInfo)) {
             $this->addLogger(sprintf("坑位信息%d,价格段id:%d获取失败", $bucketInfo->id, $bucketInfo->priceId));
             continue;
         }
         //获取实际计划数目
         $planNum = Model_Plan_EsfAjkPropSpread::data_access()->filter("commId", $bucketInfo->communityId)->filter("smallprice", $priceIntervalInfo->minPrice)->filter("bigprice", $priceIntervalInfo->maxPrice)->filter("bidVersion", Model_Plan_EsfAjkPropSpread::CHOICEVERSION)->filter("model", Model_Plan_EsfAjkPropSpread::COMM_CHOICE)->filter_by_op('status', 'in', array(Model_Plan_EsfAjkPropSpread::ONLINE, Model_Plan_EsfAjkPropSpread::ONQUEUE))->count();
         if ($planNum == $bucketInfo->usedNum) {
             if (isset($this->exceptionBucket[$bucketInfo->id])) {
                 //移除已经正常的坑位标记
                 unset($this->exceptionBucket[$bucketInfo->id]);
                 $this->addLogger(sprintf("坑位信息%d,坑位恢复到正常%d", $bucketInfo->id, $planNum));
             } else {
                 $this->addLogger(sprintf("坑位信息%d正常", $bucketInfo->id));
             }
             //更新游标
             $this->setCursor($bucketInfo->id);
             //更新异常文件
             $this->exceptionBucketFile->coverLog(json_encode($this->exceptionBucket));
             continue;
         }
         //坑位异常处理
         if (isset($this->exceptionBucket[$bucketInfo->id])) {
             //坑位已异常
             if ($this->exceptionBucket[$bucketInfo->id]["occur"] <= $this->repairOccurNum) {
                 //达到警告线,需要修复数据
                 //已实际计划数为依据更新
                 if (Model_Choice_EsfBidBucket::updateUsedNum($bucketInfo->id, $planNum, $bucketInfo->usedNum)) {
                     unset($this->exceptionBucket[$bucketInfo->id]);
                     $this->addLogger(sprintf("坑位信息%d,更新坑位信息成功, SQL:%s", $bucketInfo->id, Model_Choice_EsfBidBucket::$lastSql));
                 } else {
                     $this->addLogger(sprintf("坑位信息%d,更新坑位信息失败, SQL:%s", $bucketInfo->id, Model_Choice_EsfBidBucket::$lastSql));
                     continue;
                 }
             } else {
                 //未达到警告线,继续标记
                 //记录历史信息
                 if (isset($this->exceptionBucket[$bucketInfo->id]['history'])) {
                     $history = $this->exceptionBucket[$bucketInfo->id]['history'];
                 } else {
                     $history = array();
                 }
                 $history[] = $this->exceptionBucket[$bucketInfo->id];
                 //继续标记
                 $this->exceptionBucket[$bucketInfo->id] = array('planNum' => $planNum, 'usedNum' => $bucketInfo->usedNum, 'occur' => $this->exceptionBucket[$bucketInfo->id]["occur"] + 1);
                 $this->exceptionBucket[$bucketInfo->id]['history'] = $history;
                 //记录日志
                 $this->addLogger(sprintf("坑位信息%d,出现异常%s", $bucketInfo->id, json_encode($this->exceptionBucket[$bucketInfo->id])));
             }
         } else {
             $this->exceptionBucket[$bucketInfo->id] = array('planNum' => $planNum, 'usedNum' => $bucketInfo->usedNum, 'occur' => 1);
             $this->addLogger(sprintf("坑位信息%d,出现异常%s", $bucketInfo->id, json_encode($this->exceptionBucket[$bucketInfo->id])));
         }
         //更新异常文件
         $this->exceptionBucketFile->coverLog(json_encode($this->exceptionBucket));
         //更新游标
         $this->setCursor($bucketInfo->id);
     }
 }
 /**
  * 根据房源id获取可以删除的计划
  *
  * @param $houseId
  */
 public static function fitDeletePlan($houseId)
 {
     return Model_Plan_EsfAjkPropSpread::data_access()->filter_by_op('propId', '=', $houseId)->filter_by_op('status', '<>', Model_Plan_EsfAjkPropSpread::ONLINE)->filter_by_op('status', '<>', Model_Plan_EsfAjkPropSpread::ONQUEUE)->filter_by_op('status', '<>', Model_Plan_EsfAjkPropSpread::DELETE)->filter_by_op('tradetype', '=', 1)->filter_by_op('tradestatus', '=', Model_Plan_EsfAjkPropSpread::COMPLETETTRADESTATU)->sort('stoptime', 'asc')->find_only();
 }