public function fixapierror20150626Action()
 {
     set_time_limit(300);
     $dbSql = $this->getServiceLocator()->get('dbSql');
     $dbAdapter = $this->getServiceLocator()->get('dbAdapter');
     /*@var $dbAdapter \Zend\Db\Adapter\Adapter */
     $fromDate = '2015-06-26 00:00:00';
     $toDate = '2015-06-30 00:00:00';
     $defaultSupporter = 21;
     // lấy ra các lead do api tạo thành trong khoảng từ 26/6 -> 29/6 (arr1)
     $select = $dbSql->select(['l' => \Crm\Model\LeadMapper::TABLE_NAME]);
     $select->where(['createdById' => 1]);
     $select->where(['createdDateTime >= ?' => $fromDate]);
     $select->where(['createdDateTime <= ?' => $toDate]);
     $query = $dbSql->buildSqlString($select);
     echo $query;
     echo '<br/>';
     echo '<br/>';
     $rows = $dbAdapter->query($query, $dbAdapter::QUERY_MODE_EXECUTE);
     $leadIds = [];
     $leads = [];
     if ($rows->count()) {
         foreach ($rows->toArray() as $row) {
             $leadIds[$row['id']] = $row['id'];
         }
     }
     unset($select);
     unset($rows);
     // thêm ra các lead có yêu cầu tính năng hoặc đăng kí dùng thử trong đoạn thời gian này
     $select = $dbSql->select(['a' => \Crm\Model\ActivityMapper::TABLE_NAME]);
     $select->columns(['leadId']);
     $select->where(['accountId IS NULL']);
     $select->where(['createdById' => 1]);
     $select->where(['type' => [\Crm\Model\Activity::TYPE_CUSTOMER_REQUEST, \Crm\Model\Activity::TYPE_REGISTER_FOR_TRIAL]]);
     $select->where(['createdDateTime >= ?' => $fromDate]);
     $select->where(['createdDateTime <= ?' => $toDate]);
     $query = $dbSql->buildSqlString($select);
     echo $query;
     echo '<br/>';
     echo '<br/>';
     $rows = $dbAdapter->query($query, $dbAdapter::QUERY_MODE_EXECUTE);
     if ($rows->count()) {
         foreach ($rows->toArray() as $row) {
             if (!isset($leadIds[$row['leadId']])) {
                 $leadIds[$row['leadId']] = $row['leadId'];
             }
         }
     }
     unset($select);
     unset($rows);
     // loại trừ trong số lead này các lead  mà lan đang năm giữ (arr2)
     $select = $dbSql->select(['l' => \Crm\Model\LeadMapper::TABLE_NAME]);
     $select->join(['lu' => \Crm\Model\Lead\UserMapper::TABLE_NAME], 'l.id=lu.leadId', []);
     $select->columns(['id']);
     $select->where(['leadId' => $leadIds]);
     $select->where(['lu.userId' => $defaultSupporter]);
     $select->group(['l.id']);
     $query = $dbSql->buildSqlString($select);
     echo $query;
     echo '<br/>';
     echo '<br/>';
     $rows = $dbAdapter->query($query, $dbAdapter::QUERY_MODE_EXECUTE);
     if ($rows->count()) {
         foreach ($rows->toArray() as $row) {
             if (isset($leadIds[$row['id']])) {
                 unset($leadIds[$row['id']]);
             }
         }
     }
     unset($select);
     unset($rows);
     // loaij trừ các leadId trong số đó mà có bất cứ hành động nào ko thuộc (nhận, bàn giao, yêu cầu tính năng, tự đăng kí, yêu cầu gọi)
     $select = $dbSql->select(['a' => \Crm\Model\ActivityMapper::TABLE_NAME]);
     $select->columns(['leadId']);
     $select->where(['leadId' => $leadIds]);
     $select->where(new NotIn('type', [\Crm\Model\Activity::TYPE_ASSIGN_LEAD, \Crm\Model\Activity::TYPE_SELF_ASSIGN_LEAD, \Crm\Model\Activity::TYPE_REQUEST_PHONECALL, \Crm\Model\Activity::TYPE_CUSTOMER_REQUEST, \Crm\Model\Activity::TYPE_REGISTER_FOR_TRIAL]));
     $select->where(['companyId' => 1]);
     $select->group(['leadId']);
     $query = $dbSql->buildSqlString($select);
     echo $query;
     echo '<br/>';
     echo '<br/>';
     $rows = $dbAdapter->query($query, $dbAdapter::QUERY_MODE_EXECUTE);
     if ($rows->count()) {
         foreach ($rows->toArray() as $row) {
             if (isset($leadIds[$row['leadId']])) {
                 unset($leadIds[$row['leadId']]);
             }
         }
     }
     unset($select);
     unset($rows);
     // với mỗi leadId này, gỡ hết user ra, gắn lại cho Lan + thêm yêu cầu gọi cho cái lan
     if (count($leadIds)) {
         $activityMapper = $this->getServiceLocator()->get('\\Crm\\Model\\ActivityMapper');
         $leadMapper = $this->getServiceLocator()->get('\\Crm\\Model\\LeadMapper');
         foreach ($leadIds as $leadId) {
             // xóa action nhận - bàn giao của lead này
             $delete = $dbSql->delete(\Crm\Model\ActivityMapper::TABLE_NAME);
             $delete->where(['leadId' => $leadId]);
             $delete->where(['companyId' => 1]);
             $delete->where(['type' => [\Crm\Model\Activity::TYPE_ASSIGN_LEAD, \Crm\Model\Activity::TYPE_SELF_ASSIGN_LEAD]]);
             $delete->where(['relatedUserId != ?' => $defaultSupporter]);
             $query = $dbSql->buildSqlString($delete);
             $dbAdapter->query($query, $dbAdapter::QUERY_MODE_EXECUTE);
             // xóa user đang chăm sóc lead này
             $delete = $dbSql->delete(\Crm\Model\Lead\UserMapper::TABLE_NAME);
             $delete->where(['leadId' => $leadId]);
             $delete->where(['companyId' => 1]);
             $delete->where(['userId != ?' => $defaultSupporter]);
             $query = $dbSql->buildSqlString($delete);
             $dbAdapter->query($query, $dbAdapter::QUERY_MODE_EXECUTE);
             // lay ra lead
             $lead = new \Crm\Model\Lead();
             $lead->setId($leadId);
             $leadMapper->get($lead);
             // thêm action yêu cầu gọi cho Lan
             $activity = new \Crm\Model\Activity();
             $activity->setLeadId($leadId);
             $activity->setCompanyId(1);
             $activity->setType(\Crm\Model\Activity::TYPE_REQUEST_PHONECALL);
             $activity->setStatus(\Crm\Model\Activity::STATUS_SUCCESS);
             $activity->setTitle('Bổ sung xác nhận dùng thử 26/6 - 30/6');
             $content = [];
             if ($lead->getNhanhStoreId()) {
                 $content[] = '<b>nhanhStoreId: </b>' . $lead->getNhanhStoreId();
             }
             if ($lead->getNhanhStoreName()) {
                 $content[] = '<b>nhanhStoreName: </b>' . $lead->getNhanhStoreName();
             }
             $activity->setContent(count($content) ? implode('<br/>', $content) : null);
             $activity->setCreatedById(1);
             $activity->setCreatedDate(DateBase::getCurrentDate());
             $activity->setCreatedDateTime(DateBase::getCurrentDateTime());
             $activity->setRelatedUserId($defaultSupporter);
             $activityMapper->save($activity);
             //gắn cho lan
             $leadUser = new \Crm\Model\Lead\User();
             $leadUser->setLeadId($leadId);
             $leadUser->setCompanyId(1);
             $leadUser->setUserId($defaultSupporter);
             $leadUser->setType(\Crm\Model\Lead\User::TYPE_SALE);
             $leadUserMapper = $this->getServiceLocator()->get('\\Crm\\Model\\Lead\\UserMapper');
             if (!$leadUserMapper->isExisted($leadUser)) {
                 $leadUser->setCreatedById(1);
                 $leadUser->setCreatedDateTime(DateBase::getCurrentDateTime());
                 $leadUserMapper->save($leadUser);
             }
             //update lại companyStatus + lastActivity
             $leadCompany = new \Crm\Model\Lead\Company();
             $leadCompany->setLeadId($leadId);
             $leadCompany->setCompanyId(1);
             $leadCompanyMapper = $this->getServiceLocator()->get('\\Crm\\Model\\Lead\\CompanyMapper');
             $leadCompanyMapper->isExisted($leadCompany);
             $leadCompany->setLastActivityDateTime(DateBase::getCurrentDateTime());
             $leadCompany->setStatus(\Crm\Model\Lead\Company::STATUS_BELONG);
             $leadCompanyMapper->save($leadCompany);
             echo 'update lead : <b>' . $leadId . '</b><br/>';
         }
     }
     die;
 }