IbTradeSearch.php 6.7 KB

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