Untitled

mail@pastecode.io avatarunknown
plain_text
2 months ago
12 kB
1
Indexable
Never
public function searchNewV2($params)
    {
        // xử lý lấy liệu trang trước đó
        if (!empty($this->ids_before) && $this->type == 'back') {

            $timeRange = $this->formatDateRange($params['daterange'], 59);

            $ids = array_map('intval', explode(',',$this->ids_before));
            $queryMain = Cdr::find()->select('id,uuid,msisdn,branch_id,queue_id,sk_group_id,user_id,department_id,call_length,answer_state,start_timestamp,answer_timestamp,end_timestamp,ring_timestamp,updated_at,answer_length,created_at,customer_state,confirm_result,campaign_id,recording_path,hangup_actor,hold_time,talk_time,ring_time,extension,customer_phone, call_direction, number_service, client_id, extension_client, ivr_time, queue_time, wrap_time,agent_recieve,status_transfer,call_type, agent_team_id, from_agent_transfer, press_syntax_timestamp, press_dtmf_transfer_timestamp, dtmf_transfer_syntax,start_cdr_timestamp,holded,status,branch_timestamp');
            $queryMain->andFilterWhere(['>=', 'created_at', $timeRange['start']])
                ->andFilterWhere(['<=', 'created_at', $timeRange['end']])
                ->andFilterWhere(['IN', 'id', $ids])
                ->orderBy(['start_cdr_timestamp' => SORT_DESC]);
            //->orderBy(['created_at' => SORT_DESC, 'id' => SORT_DESC]);
            //->orderBy(['created_at' => SORT_DESC]);


            $data = $queryMain->all();

            return [
                'ids' => $ids,
                'data' => $data,
                'total' => 0
            ];
        }

        // Lấy dữ liệu trang tiếp theo
        $query = Cdr::find();

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere(['>=', 'created_at', $this->createTimeStart])
            ->andFilterWhere(['<=', 'created_at', $this->createTimeEnd + 59]);

        // Giới hạn điều kiện tìm kiếm đầu số ban đầu
        $user = Yii::$app->user->identity;
        $hotline = HotlineHelper::getPhoneLine($user, HotlineNumber::HOTLINE_KIND);
        $hotlineIdStr = HotlineHelper::prepareIdString($hotline);
        $extensionHotlineStr = '';
        if (empty($this->listHotline) && $this->call_direction !== 'local' && !empty($hotlineIdStr)) {

            $query->andWhere("hotline_id IN ($hotlineIdStr)");

            //kiem tra pham vi khi ext hotline khong duoc truyen
            if (Yii::$app->user->canSite('search_all_ext_hotline', Yii::$app->params["siteId"])) {
                $arrId = [];
                $arrIdExtHotline = ExtensionHotline::find()->select('id')->where("hotline_id IN ($hotlineIdStr)")->all();
                foreach ($arrIdExtHotline as $extHotline) {
                    $arrId[] = $extHotline->id;
                }
                $extensionHotlineStr = implode(',', $arrId);
            } else {
                $tmp = [];
                $ext = HotlineHelper::getAllExtensionByHotline($user, [], explode(',', $hotlineIdStr));
                foreach ($ext as $i) {
                    $tmp[] = $i['id'];
                }
                $extensionHotlineStr = implode(',', $tmp);
            }

        } else {
            if (!empty($this->listHotline)) {
                $query->andWhere("hotline_id IN ($this->listHotline)");

                //kiem tra pham vi khi ext hotline khong duoc truyen
                if (Yii::$app->user->canSite('search_all_ext_hotline', Yii::$app->params["siteId"])) {
                    $arrId = [];
                    $arrIdExtHotline = ExtensionHotline::find()->select('id')->where("hotline_id IN ($this->listHotline)")->all();
                    foreach ($arrIdExtHotline as $extHotline) {
                        $arrId[] = $extHotline->id;
                    }
                    $extensionHotlineStr = implode(',', $arrId);
                } else {
                    $tmp = [];
                    $ext = HotlineHelper::getAllExtensionByHotline($user, [], explode(',', $this->listHotline));
                    foreach ($ext as $i) {
                        $tmp[] = $i['id'];
                    }
                    $extensionHotlineStr = implode(',', $tmp);
                }
            }
        }

        if (empty($this->listExtensionHotline) && $this->call_direction !== 'local' && !empty($extensionHotlineStr)) {
            $query->andWhere("number_service_id IN ($extensionHotlineStr)");
        } else {
            if (!empty($this->listExtensionHotline)) {
                $query->andWhere("number_service_id IN ($this->listExtensionHotline)");
            }
        }

        $call_type = $this->call_type;
        if ($this->call_type == '0') {
            $call_type = [0,7];
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'uuid' => $this->uuid,
            'call_direction' => $this->call_direction,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'campaign_id' => $this->campaign_id,
            'confirm_result' => $this->confirm_result,
            'user_id' => $this->user_id,
            'hotline_id' => $this->hotline_id,
            'number_service_id' => $this->number_service_id,
            'call_type' => $call_type,
            'status' => $this->status
        ]);

        if ($this->isLoiHeThong) {
            $query->andFilterWhere(['OR',['customer_state' =>  $this->customer_state], ['OR', 'customer_state IS NULL']]);
        } else {
            $query->andFilterWhere(['customer_state' => $this->customer_state]);
        }

        $query->andFilterWhere(['msisdn' => $this->msisdn])
            ->andFilterWhere(['extension' => $this->extension]);

        if (!empty($this->customer_phone)) {
            $query->andFilterWhere(['customer_phone' => trim($this->customer_phone)]);
        }

        if(isset($this->searchDialNumbers) && count($this->searchDialNumbers) > 0){
            $query->andFilterWhere(['IN', 'dial_number', $this->searchDialNumbers]);
        }

        if(!empty($this->searchUsers)){
            //$query->andFilterWhere(['IN', 'user_id', $this->searchUsers]);
            $query->andFilterWhere(['user_id' => $this->searchUsers]);
        }

        if (!empty($this->branch_ids)) {
            $query->andWhere("branch_id IN ($this->branch_ids)");
        }

        if(isset($this->searchDepartments) && count($this->searchDepartments) > 0){
            $query->andFilterWhere(['IN', 'department_id', $this->searchDepartments]);
        }

        if ($this->isDial) {
            $query->andFilterWhere(
                ['not like','answer_state','hangup']);
        }

        if (!empty($this->arrTimeCall)) {
            $query->andFilterWhere(['between', 'talk_time', $this->arrTimeCall[0], $this->arrTimeCall[1]]);
        }
        if (!empty($this->arrTimeHold)) {
            $query->andFilterWhere(['between', 'hold_time', $this->arrTimeHold[0], $this->arrTimeHold[1]]);
        }

        // tìm kiếm mới
        $total = 0;
        $is_new = @$params['is_new'];
        if ($is_new >= 1) { // tải mới thì mới đếm số bản ghi
            $queryCount = clone $query;
            $total = $queryCount->count(); // đếm tổng số bản ghi tìm kiếm

            if ($total <= 0) {
                return false;
            }
        }

        if (!empty($this->type) && !empty($this->last_time) && !empty($this->last_id)) {
            if ($this->type == 'next') {
                $query->andFilterWhere(['NOT IN', 'id', explode(',',$this->last_id )]);
                //$query->andFilterWhere(['<', 'id', (int) $this->last_id]);
                //$query->orderBy(['id' => SORT_DESC]);
                //$query->andFilterWhere(['<', 'start_cdr_timestamp', $this->last_time]);
                //$query->orderBy(['start_cdr_timestamp' => SORT_DESC]);
                $query->andFilterWhere(['<=', 'created_at', $this->last_time]);
                //$query->orderBy(['created_at' => SORT_DESC, 'id' => SORT_DESC]);
                $query->orderBy(['created_at' => SORT_DESC]);

            } elseif ($this->type == 'back') {
                //$query->andFilterWhere(['>', 'id', (int) $this->last_id]);
                //$query->orderBy(['id' => SORT_ASC]);
                //$query->andFilterWhere(['>', 'start_cdr_timestamp', $this->last_time]);
                //$query->orderBy(['start_cdr_timestamp' => SORT_ASC]);
                //$query->andFilterWhere(['>', 'created_at', $this->last_time]);
                //$query->orderBy(['created_at' => SORT_ASC, 'id' => SORT_DESC]);

            }
        } else {
            //$query->orderBy(['id' => SORT_DESC]);
            //$query->orderBy(['start_cdr_timestamp' => SORT_DESC]);
            //$query->orderBy(['created_at' => SORT_DESC, 'id' => SORT_DESC]);
            $query->orderBy(['created_at' => SORT_DESC]);
        }

        // xử lý lấy dữ liệu được phân trang , mặc định là 50
        $query->limit($this->limitHistoryCall);
        $data = $query->select('id')->asArray()->all();

        if (!empty($data)) {
            $ids = ArrayHelper::getColumn($data, 'id', true, 'int');

            $queryMain = Cdr::find()->select('cdr.id,uuid,msisdn,branch_id,queue_id,sk_group_id,user_id,call_length,department_id,answer_state,start_timestamp,answer_timestamp,end_timestamp,ring_timestamp,cdr.updated_at,answer_length,cdr.created_at,customer_state,confirm_result,campaign_id,recording_path,hangup_actor,hold_time,talk_time,ring_time,extension,customer_phone, call_direction, number_service, client_id, extension_client, ivr_time, queue_time, wrap_time,agent_recieve,status_transfer,call_type, agent_team_id, from_agent_transfer, press_syntax_timestamp, press_dtmf_transfer_timestamp, dtmf_transfer_syntax,start_cdr_timestamp,holded,cdr.status,branch_timestamp,hotline_id')
                ->andFilterWhere(['>=', 'cdr.created_at', $this->createTimeStart])
                ->andFilterWhere(['<=', 'cdr.created_at', $this->createTimeEnd + 59])
                ->andFilterWhere(['IN', 'cdr.id', $ids]);

            if (!empty($this->type) && !empty($this->last_id)) {
                if ($this->type == 'next') {
                    //$queryMain->orderBy(['created_at' => SORT_DESC, 'id' => SORT_DESC]);
                    //$query->orderBy(['start_cdr_timestamp' => SORT_DESC]);
                } elseif ($this->type == 'back') {
                    //$queryMain->orderBy(['created_at' => SORT_ASC, 'id' => SORT_ASC]);
                }
            } else {
                //$query->orderBy(['start_cdr_timestamp' => SORT_DESC]);
                //$queryMain->orderBy(['created_at' => SORT_DESC, 'id' => SORT_DESC]);
            }

            $queryMain->orderBy(['start_cdr_timestamp' => SORT_DESC]);
            //$queryMain->orderBy(['created_at' => SORT_DESC, 'id' => SORT_DESC]);

            //print_r($queryMain->createCommand()->getRawSql());die;

            $data = $queryMain->all();

            return [
                'ids' => $ids,
                'data' => $data,
                'total' => $total
            ];
        }

        return false;
    }