public static function communitySignTopSigner($communityId, $currentSignRange, $per = 3)
 {
     $signs = Model_Broker_CommunitySign::data_access(date('Ym', strtotime($currentSignRange[0])))->filter('communityId', $communityId)->filter_by_op_multi(array(array('signTime', '>=', $currentSignRange[0]), array('signTime', '<=', $currentSignRange[1])))->sort('signTime', 'asc')->limit(3)->find_all();
     $signers = array();
     if ($signs) {
         $brokerIds = array();
         foreach ($signs as $sign) {
             $brokerIds[] = $sign['brokerId'];
         }
         $brokers = Model_Broker_AjkBrokerExtend::data_access()->filter('brokerId', $brokerIds)->find_all();
         $indexedBrokers = array();
         foreach ($brokers as $broker) {
             $indexedBrokers[$broker['brokerId']] = $broker;
         }
         foreach ($signs as $sign) {
             if (!isset($indexedBrokers[$sign['brokerId']])) {
                 continue;
             }
             $broker = $indexedBrokers[$sign['brokerId']];
             $signer = array();
             $signer['brokerId'] = $broker['brokerId'];
             $signer['brokerTrueName'] = $broker['trueName'];
             // 图片地址替换
             if ($broker['userPhoto']) {
                 $signer['brokerPhoto'] = Util_DisplayImages::get_img_url($broker['userPhoto'], -1, '100x133');
             } else {
                 $signer['brokerPhoto'] = PageHelper::pure_static_url('/img/bknoimg.gif');
                 // 默认头像
             }
             $signers[] = $signer;
         }
     }
     return $signers;
 }
 public function handle_request_internal()
 {
     /**
      * 签到小区列表
      *
      * - 验证参数
      *
      * - 获取经纪人
      *
      * - 根据lat和lng获取1.5km内的小区列表(查询Solr)
      *
      * - 判断经纪人是否签到过(用于设置当前时间段内经纪人是否签到过)
      *
      * - 返回数据
      */
     $result = array('status' => 'error', 'errcode' => '', 'message' => '');
     // 获取参数
     $brokerId = $this->_params['brokerId'];
     $lat = $this->_params['lat'];
     $lng = $this->_params['lng'];
     // 获取经纪人
     $broker = Model_Broker_AjkBrokerExtend::data_access()->filter('brokerId', $brokerId)->find_only();
     if (!$broker) {
         throw new Exception('经纪人不存在', Const_APIStatus::E_BROKER_NOT_EXISTS);
     }
     if (empty($lat) || empty($lng)) {
         $result['message'] = '获取定位数据失败,请重新定位';
         return $result;
     }
     // 获取数据
     $communities = $this->getCommList($brokerId, $lat, $lng);
     // 判断经纪人是否签到过
     if ($communities) {
         $communityIds = array();
         foreach ($communities as $community) {
             $communityIds[] = $community['commId'];
         }
         // 获取当前所处签到周期
         $signedCommunities = array();
         $currentSignRange = Bll_Broker_CommunitySign::getCurrentSignRange();
         if ($currentSignRange) {
             $signs = Model_Broker_CommunitySign::data_access(date('Ym', strtotime($currentSignRange[0])))->filter('brokerId', $brokerId)->filter_by_op_multi(array(array('signTime', '>=', $currentSignRange[0]), array('signTime', '<=', $currentSignRange[1])))->get_all();
             foreach ($signs as $sign) {
                 $signedCommunities[$sign['communityId']] = $sign['communityId'];
             }
         }
         // 标记小区签到状态
         foreach ($communities as &$row) {
             if (isset($signedCommunities[$row['commId']])) {
                 $row['signed'] = 1;
             } else {
                 $row['signed'] = 0;
             }
         }
     }
     return array('status' => Const_APIStatus::RETURN_CODE_OK, 'total' => count($communities), 'data' => $communities);
 }
 public function handle_request_internal()
 {
     /**
      * 签到小区详情
      *
      * - 验证参数
      *
      *
      * - 获取经纪人
      *
      * - 获取小区(AjkCommunity)
      * -
      *
      * - 获取小区签到人数
      *
      * - 判断经纪人是否可签到(经纪人是否已签到、当前是否在签到时间段)
      * - 校验小区坐标和lat、lng的距离5公里,超出不可签到(如小区无坐标,不可签到)
      *
      * - 计算距离下一时间点的签到倒计时(单位:s)
      *
      * - 获取小区各个时间段签到前3名
      *
      * - 返回数据
      */
     // 获取参数
     $brokerId = $this->_params['brokerId'];
     $commId = $this->_params['commId'];
     $lat = $this->_params['lat'];
     $lng = $this->_params['lng'];
     // 根据ID获取经纪人
     $broker = Bll_Commsign::getBrokerInfo($brokerId);
     if (!$broker) {
         $broker = Model_Broker_AjkBrokerExtend::data_access()->filter('brokerId', $brokerId)->find_only();
         if (!$broker) {
             throw new Exception('经纪人不存在', Const_APIStatus::E_BROKER_NOT_EXISTS);
         }
         $broker_photo = Util_ImageUtils::get_broker_photo_url($broker['userPhoto'], '200x200');
         $brokerInfo = array('brokerId' => $broker['brokerId'], 'username' => $broker['trueName'], 'userPhoto' => $broker_photo, 'cityId' => $broker['cityId']);
         Bll_Commsign::redisBrokerInfo($brokerId, $brokerInfo);
     }
     // 根据ID获取小区
     $community = Bll_Commsign::getCommInfo($commId);
     if (empty($community)) {
         $community = Bll_Community_APIComm::getInstance()->getInfoByIdMapping($commId, 2);
         $commInfo = array();
         $commInfo['commId'] = $community['commId'];
         $commInfo['commName'] = $community['commName'];
         $commInfo['sosolng'] = $community['sosolng'];
         $commInfo['sosolat'] = $community['sosolat'];
         Bll_Commsign::setCommInfo($commId, $commInfo);
     }
     if (!$community || !$community['sosolat'] || !$community['sosolng']) {
         throw new Exception('小区不存在', Const_APIStatus::E_COMMUNITY_NOT_EXISTS);
     }
     // 获取小区签到人数
     $communitySignCount = Bll_Commsign::getCommCount($commId);
     // 判断经纪人是否可签到
     $signAble = true;
     $currentSignRange = Bll_Broker_CommunitySign::getCurrentSignRange();
     if (!$currentSignRange) {
         $signAble = false;
     } else {
         // 经纪人是否已经签过到
         if (Bll_Commsign::isSignedAlready($brokerId, $commId, $currentSignRange[0])) {
             $signAble = false;
         }
     }
     // 对比小区坐标和参数坐标两点的距离(5km)
     $distance = Util_Map::distance($lat, $lng, $community['sosolat'], $community['sosolng']);
     if ($distance > 5000) {
         $signAble = false;
     }
     $data = array();
     $data['signAble'] = intval($signAble);
     $data['signCount'] = $communitySignCount;
     $data['countDown'] = $signAble ? 0 : Bll_Broker_CommunitySign::nextSignCountDown();
     $data['signList'] = array();
     // 获取各时间段签到前三名
     if (Bll_Broker_CommunitySign::isTopSignerActivityOngoing()) {
         $data['signList'] = array(array('hour' => '10:00', 'brokers' => array()), array('hour' => '15:00', 'brokers' => array()), array('hour' => '20:00', 'brokers' => array()));
         foreach ($data['signList'] as &$theSignList) {
             $theHour = intval($theSignList['hour']);
             $hour = intval(date('H'));
             if ($hour >= 0 && $hour < 10) {
                 break;
             } elseif ($hour >= 10 && $hour < 20) {
                 if ($theHour > $hour) {
                     continue;
                 }
             }
             $theSignRange = array();
             switch ($theHour) {
                 case 10:
                     $theSignRange = array(date('Y-m-d 10:00:00'), date('Y-m-d 14:59:59'));
                     break;
                 case 15:
                     $theSignRange = array(date('Y-m-d 15:00:00'), date('Y-m-d 19:59:59'));
                     break;
                 case 20:
                     $theSignRange = array(date('Y-m-d 20:00:00'), date('Y-m-d 23:59:59'));
                     break;
             }
             $theSignList['brokers'] = Bll_Commsign::getFirstThr($commId, $theSignRange[0]);
             if (empty($theSignList['brokers'])) {
                 $theSignList['brokers'] = Model_Broker_CommunitySign::data_access(date('Ym', strtotime($currentSignRange[0])))->filter('communityId', $commId)->filter_by_op_multi(array(array('signTime', '>=', $theSignRange[0]), array('signTime', '<=', $theSignRange[1])))->sort('signTime', 'asc')->limit(3)->get_all();
             }
             $tmp = array();
             if (!empty($theSignList['brokers'])) {
                 foreach ($theSignList['brokers'] as $k => $v) {
                     $brokerTmp = Bll_Commsign::getBrokerInfo($v['brokerId']);
                     if (!$brokerTmp) {
                         $broker = Model_Broker_AjkBrokerExtend::data_access()->filter('brokerId', $v['brokerId'])->find_only();
                         $broker_photo = Util_ImageUtils::get_broker_photo_url($broker['userPhoto'], '200x200');
                         $brokerTmp = array('brokerId' => $broker['brokerId'], 'username' => $broker['trueName'], 'userPhoto' => $broker_photo, 'cityId' => $broker['cityId']);
                         Bll_Commsign::redisBrokerInfo($v['brokerId'], $brokerTmp);
                     }
                     if (empty($brokerTmp['userPhoto'])) {
                         $brokerTmp['userPhoto'] = PageHelper::pure_static_url('/img/bknoimg.gif');
                     }
                     $dataTmp = array('brokerId' => $brokerTmp['brokerId'], 'brokerTrueName' => $brokerTmp['username'], 'brokerPhoto' => $brokerTmp['userPhoto']);
                     $tmp[] = $dataTmp;
                 }
                 $theSignList['brokers'] = $tmp;
             }
         }
     }
     return array('status' => Const_APIStatus::RETURN_CODE_OK, 'data' => $data);
 }