CommissionSearch.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: chenkuan
  5. * Date: 2017/12/22
  6. * Time: 下午3:22
  7. */
  8. namespace backend\models\searches;
  9. use backend\models\CommissionApi;
  10. use common\helpers\ExcelHelper;
  11. use common\helpers\UserAgentHelper;
  12. use yii\data\ArrayDataProvider;
  13. use yii\data\Pagination;
  14. use yii\helpers\Url;
  15. class CommissionSearch extends DataTable
  16. {
  17. /**
  18. * @param array $params
  19. * @return ArrayDataProvider
  20. */
  21. public function search($params)
  22. {
  23. $this->setAttributes($params);
  24. // 初始化DataTable查询参数
  25. $this->initSearchParams();
  26. $pagination = new Pagination();
  27. $pagination->setPageSize($this->pageSize);
  28. $data['pageSize'] = $pagination->getPageSize();
  29. $data['page'] = $this->page;
  30. if ($this->orderBy) {
  31. $data['orderBy'] = $this->orderBy;
  32. }
  33. if (!empty($this->search['value']) && trim($this->search['value']) !== '') {
  34. $data['search'] = trim($this->search['value']);
  35. }
  36. $data['memberId'] = $params['memberId'];
  37. if ($this->pageSize > 5000) {
  38. if (\Yii::$app->getRequest()->isAjax) {
  39. \Yii::$app->getResponse()->redirect(Url::current(), UserAgentHelper::isIE() ? 200 : 302);
  40. } else {
  41. $this->exportXls($data);
  42. }
  43. \Yii::$app->end();
  44. }
  45. $api = new CommissionApi();
  46. $result = $api->getCommissionLog($data);
  47. $models = [];
  48. if ($result['code'] == 1) {
  49. $models = (array)$result['data']['dataList'];
  50. $pagination->totalCount = (int)$result['data']['totalCount'];
  51. }
  52. $dataProvider = new ArrayDataProvider([
  53. 'models' => $models,
  54. 'pagination' => $pagination,
  55. 'totalCount' => $pagination->totalCount,
  56. ]);
  57. return $dataProvider;
  58. }
  59. /**
  60. * @param $data
  61. */
  62. private function exportXls($data)
  63. {
  64. ExcelHelper::init();
  65. $attachmentName = "修改返佣记录_" . date('Y-m-d') . '.xlsx';
  66. $header = ['ID', 'LOGIN', 'Member_id', '返佣类型', 'FOREX', 'METAL', 'CFD', '黄金(GOLD)',
  67. '白银(SILVER)', '外佣(wy)', '修改人', '修改时间'];
  68. $source = [];
  69. $api = new CommissionApi();
  70. $data['pageSize'] = 5000;
  71. $data['page'] = 1;
  72. while (true) {
  73. $result = $api->getCommissionLog($data);
  74. if ($result['code'] == 1 && $result['data']['dataList']) {
  75. foreach ((array)$result['data']['dataList'] as $key => $row) {
  76. $arr = [];
  77. $arr[] = $row['id'];
  78. $arr[] = $row['login'];
  79. $arr[] = $row['member_id'];
  80. if ($row['type'] == 'direct') {
  81. $arr[] = "直属返佣";
  82. } elseif ($row['type'] == 'indirect') {
  83. $arr[] = "差佣";
  84. } else {
  85. $arr[] = "";
  86. }
  87. $arr[] = $row['forex'];
  88. $arr[] = $row['metal'];
  89. $arr[] = $row['cfd'];
  90. $arr[] = $row['gold'];
  91. $arr[] = $row['silver'];
  92. $arr[] = $row['wy'];
  93. $arr[] = $row['name'];
  94. $arr[] = date('Y-m-d H:i:s', intval($row['in_time'] / 1000));
  95. $source[] = $arr;
  96. }
  97. } else {
  98. break;
  99. }
  100. if ($data['page'] * $data['pageSize'] >= $result['data']['totalCount']) {
  101. break;
  102. }
  103. $data['page']++;
  104. }
  105. ExcelHelper::output($source, $header, $attachmentName);
  106. }
  107. }