STradeSearch.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace backend\models\searches;
  3. use backend\models\Mt4tradeApi;
  4. use common\helpers\ExcelHelper;
  5. use Yii;
  6. use yii\data\ArrayDataProvider;
  7. use yii\data\Pagination;
  8. use yii\helpers\Url;
  9. use common\helpers\UserAgentHelper;
  10. class STradeSearch extends DataTable
  11. {
  12. public $type;
  13. public $sTime;
  14. public $eTime;
  15. public $login;
  16. /**
  17. * @inheritdoc
  18. */
  19. public function rules()
  20. {
  21. $rules = parent::rules();
  22. $rules[] = [['type', 'login'], 'required'];
  23. $rules[] = [['sTime', 'eTime'], 'safe'];
  24. return $rules;
  25. }
  26. /**
  27. * @param array $params
  28. * @return ArrayDataProvider
  29. */
  30. public function search($params)
  31. {
  32. $this->setAttributes($params);
  33. // 初始化DataTable查询参数
  34. $this->initSearchParams();
  35. $pagination = new Pagination();
  36. $pagination->setPageSize($this->pageSize);
  37. $data['pageSize'] = $pagination->getPageSize();
  38. $data['page'] = $this->page;
  39. if ($this->orderBy) {
  40. $data['orderBy'] = $this->orderBy;
  41. }
  42. if (!empty($this->search['value']) && trim($this->search['value']) !== '') {
  43. $data['search'] = trim($this->search['value']);
  44. }
  45. $data['login'] = $this->login;
  46. $data['type'] = $this->type;
  47. $this->sTime && $data['closeTimeStart'] = date('Y-m-d 00:00:00', strtotime($this->sTime));
  48. $this->eTime && $data['closeTimeEnd'] = date('Y-m-d 23:59:59', strtotime($this->eTime));
  49. if ($this->pageSize > 5000) {
  50. if (Yii::$app->getRequest()->getIsAjax()) {
  51. Yii::$app->getResponse()->redirect(Url::current(), UserAgentHelper::isIE() ? 200 : 302);
  52. } else {
  53. $this->exportXls($data);
  54. }
  55. Yii::$app->end();
  56. }
  57. $api = new Mt4tradeApi();
  58. $result = $api->getTradeList($data);
  59. // $result = $this->multiQuery($api, 'getTradeList', $data);
  60. $models = [];
  61. if ($result['code'] == 1) {
  62. $models = (array)$result['data']['dataList'];
  63. $pagination->totalCount = $result['data']['totalCount'];
  64. }
  65. $dataProvider = new ArrayDataProvider([
  66. 'models' => $models,
  67. 'pagination' => $pagination,
  68. 'totalCount' => $pagination->totalCount,
  69. ]);
  70. return $dataProvider;
  71. }
  72. /**
  73. * @param array $data
  74. */
  75. protected function exportXls($data)
  76. {
  77. ExcelHelper::init();
  78. $nameMap = [
  79. 'deposit' => '入金',
  80. 'withdraw' => '出金',
  81. 'position' => '持仓',
  82. 'history' => '历史',
  83. 'pending' => '挂单',
  84. 'credit' => '信用',
  85. ];
  86. $filename = isset($nameMap[$data['type']]) ? $nameMap[$data['type']] . '报表' : '交易报表';
  87. $attachmentName = Yii::$app->user->identity->name . "_{$filename}_" . date('Y-m-d') . '.xlsx';
  88. $header = [];
  89. if (in_array($data['type'], ['position', 'pending'])) {
  90. $header = ['订单号', '类型', '品种', 'SL', 'TP', '手数', '开仓价格', '开仓时间', '利息', '佣金', '获利'];
  91. } elseif (in_array($data['type'], ['history'])) {
  92. $header = ['订单号', '类型', '品种', 'SL', 'TP', '手数', '开仓价格', '开仓时间', '平仓价格', '平仓时间',
  93. '利息', '佣金', '获利'];
  94. } elseif (in_array($data['type'], ['deposit', 'withdraw'])) {
  95. $header = ['代理商', '金额', '时间'];
  96. }
  97. $source = [];
  98. $summaryRow = [];
  99. $api = new Mt4tradeApi();
  100. $cmdMap = [
  101. 0 => 'BUY',
  102. 1 => 'SELL',
  103. 2 => 'BUY LIMIT',
  104. 3 => 'SELL LIMIT',
  105. 4 => 'BUY STOP',
  106. 5 => 'SELL STOP',
  107. 6 => 'BALANCE',
  108. 7 => 'CREDIT',
  109. ];
  110. $data['pageSize'] = 5000;
  111. $data['page'] = 1;
  112. while (true) {
  113. $result = $api->getTradeList($data);
  114. if ($result['code'] == 1 && !empty($result['data']['dataList'])) {
  115. foreach ((array)$result['data']['dataList'] as $key => $row) {
  116. $cmd = isset($cmdMap[$row['CMD']]) ? $cmdMap[$row['CMD']] : '';
  117. $volume = $row['VOLUME'] / 100;
  118. $arr = [];
  119. if (in_array($data['type'], ['position', 'pending'])) {
  120. $arr[] = $row['TICKET'];
  121. $arr[] = $cmd;
  122. $arr[] = $row['SYMBOL'];
  123. $arr[] = $row['SL'];
  124. $arr[] = $row['TP'];
  125. $arr[] = $volume;
  126. $arr[] = $row['OPEN_PRICE'];
  127. $arr[] = $row['OPEN_TIME'];
  128. $arr[] = $row['SWAPS'];
  129. $arr[] = $row['COMMISSION'];
  130. $arr[] = $row['PROFIT'];
  131. } elseif (in_array($data['type'], ['history'])) {
  132. $arr[] = $row['TICKET'];
  133. $arr[] = $cmd;
  134. $arr[] = $row['SYMBOL'];
  135. $arr[] = $row['SL'];
  136. $arr[] = $row['TP'];
  137. $arr[] = $volume;
  138. $arr[] = $row['OPEN_PRICE'];
  139. $arr[] = $row['OPEN_TIME'];
  140. $arr[] = $row['CLOSE_PRICE'];
  141. $arr[] = $row['CLOSE_TIME'];
  142. $arr[] = $row['SWAPS'];
  143. $arr[] = $row['COMMISSION'];
  144. $arr[] = $row['PROFIT'];
  145. } elseif (in_array($data['type'], ['deposit', 'withdraw'])) {
  146. $arr[] = $row['TICKET'];
  147. $arr[] = $row['PROFIT'];
  148. $arr[] = $row['OPEN_TIME'];
  149. }
  150. $source[] = $arr;
  151. }
  152. $summaryRow = array_pop($source);
  153. } else {
  154. break;
  155. }
  156. if ($data['page'] * $data['pageSize'] >= $result['data']['totalCount']) {
  157. break;
  158. }
  159. $data['page']++;
  160. }
  161. $source[] = $summaryRow;
  162. ExcelHelper::output($source, $header, $attachmentName);
  163. }
  164. }