<?php
/**
 * Created by PhpStorm.
 * User: chenkuan
 * Date: 2017/12/22
 * Time: 下午3:22
 */

namespace backend\models\searches;


use backend\models\CommissionApi;
use common\helpers\ExcelHelper;
use common\helpers\UserAgentHelper;
use yii\data\ArrayDataProvider;
use yii\data\Pagination;
use yii\helpers\Url;

class CommissionSearch extends DataTable
{
    /**
     * @param array $params
     * @return ArrayDataProvider
     */
    public function search($params)
    {
        $this->setAttributes($params);
        // 初始化DataTable查询参数
        $this->initSearchParams();

        $pagination = new Pagination();
        $pagination->setPageSize($this->pageSize);
        $data['pageSize'] = $pagination->getPageSize();
        $data['page'] = $this->page;
        if ($this->orderBy) {
            $data['orderBy'] = $this->orderBy;
        }
        if (!empty($this->search['value']) && trim($this->search['value']) !== '') {
            $data['search'] = trim($this->search['value']);
        }
        $data['memberId'] = $params['memberId'];

        if ($this->pageSize > 5000) {
            if (\Yii::$app->getRequest()->isAjax) {
                \Yii::$app->getResponse()->redirect(Url::current(), UserAgentHelper::isIE() ? 200 : 302);
            } else {
                $this->exportXls($data);
            }
            \Yii::$app->end();
        }

        $api = new CommissionApi();
        $result = $api->getCommissionLog($data);
        $models = [];
        if ($result['code'] == 1) {
            $models = (array)$result['data']['dataList'];
            $pagination->totalCount = (int)$result['data']['totalCount'];
        }
        $dataProvider = new ArrayDataProvider([
            'models' => $models,
            'pagination' => $pagination,
            'totalCount' => $pagination->totalCount,
        ]);
        return $dataProvider;
    }

    /**
     * @param $data
     */
    private function exportXls($data)
    {
        ExcelHelper::init();
        $attachmentName = "修改返佣记录_" . date('Y-m-d') . '.xlsx';
        $header = ['ID', 'LOGIN', 'Member_id', '返佣类型', 'FOREX', 'METAL', 'CFD', '黄金(GOLD)',
            '白银(SILVER)', '外佣(wy)', '修改人', '修改时间'];
        $source = [];
        $api = new CommissionApi();
        $data['pageSize'] = 5000;
        $data['page'] = 1;
        while (true) {
            $result = $api->getCommissionLog($data);
            if ($result['code'] == 1 && $result['data']['dataList']) {
                foreach ((array)$result['data']['dataList'] as $key => $row) {
                    $arr = [];
                    $arr[] = $row['id'];
                    $arr[] = $row['login'];
                    $arr[] = $row['member_id'];
                    if ($row['type'] == 'direct') {
                        $arr[] = "直属返佣";
                    } elseif ($row['type'] == 'indirect') {
                        $arr[] = "差佣";
                    } else {
                        $arr[] = "";
                    }
                    $arr[] = $row['forex'];
                    $arr[] = $row['metal'];
                    $arr[] = $row['cfd'];
                    $arr[] = $row['gold'];
                    $arr[] = $row['silver'];
                    $arr[] = $row['wy'];
                    $arr[] = $row['name'];
                    $arr[] = date('Y-m-d H:i:s', intval($row['in_time'] / 1000));
                    $source[] = $arr;
                }
            } else {
                break;
            }
            if ($data['page'] * $data['pageSize'] >= $result['data']['totalCount']) {
                break;
            }
            $data['page']++;
        }
        ExcelHelper::output($source, $header, $attachmentName);
    }
}