Rebate.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace backend\models;
  3. use Yii;
  4. class Rebate extends \yii\db\ActiveRecord
  5. {
  6. /**
  7. * @return \yii\db\Connection the database connection used by this AR class.
  8. */
  9. public static function getDb()
  10. {
  11. return Yii::$app->get('dbXcrm');
  12. }
  13. /**
  14. * @param array $post
  15. * @return array
  16. */
  17. public function getList($post)
  18. {
  19. $result = ['code' => 0, 'data' => [], 'message' => ''];
  20. $member_id = $post['member_id'];
  21. $order = isset($post['order']) ? strtolower($post['order']) : '';
  22. $orderBy = isset($post['orderBy']) ? strtolower($post['orderBy']) : 'desc';
  23. $search = isset($post['search']) ? $post['search'] : '';
  24. $start = isset($post['start']) ? (int) $post['start'] : 0;
  25. $length = isset($post['length']) ? (int) $post['length'] : 20;
  26. $sTime = isset($post['sTime']) ? $post['sTime'] : '';
  27. $eTime = isset($post['eTime']) ? $post['eTime'] . ' 23:59:59' : '';
  28. $ibId = isset($post['ibId']) ? $post['ibId'] : 0;
  29. $draw = isset($post['draw']) ? $post['draw'] : 1;
  30. $where = ['and'];
  31. // 代理商,没有搜索条件就查当前用户
  32. $member = new Member();
  33. $ibs = $member->findChildrenIncludeSelf($member_id);
  34. $id_arr = array_column($ibs, 'id');
  35. if ($ibId && in_array($ibId, $id_arr)) {
  36. $where[] = ['=', 'ib_id', $ibId];
  37. } else {
  38. $where[] = ['=', 'ib_id', $member_id];
  39. }
  40. // 账户或订单号
  41. if ($search) {
  42. $where[] = [
  43. 'or',
  44. ['like', 'user_login', $search],
  45. ['like', 'trade_ticket', $search],
  46. ];
  47. }
  48. // 时间,数据库的是毫秒时间戳
  49. if ($sTime) {
  50. $sTime = strtotime($sTime);
  51. if ($sTime) {
  52. $sTime *= 1000;
  53. $where[] = ['>=', 'in_time', $sTime];
  54. }
  55. }
  56. if ($eTime) {
  57. $eTime = strtotime($eTime);
  58. if ($eTime) {
  59. $eTime *= 1000;
  60. $where[] = ['<=', 'in_time', $eTime];
  61. }
  62. }
  63. // 排序
  64. $allowOrderColumn = ['trade_ticket', 'user_login', 'trade_type', 'trade_volume', 'commission_rule', 'commission', 'in_time'];
  65. if (in_array($order, $allowOrderColumn) && in_array($orderBy, ['asc', 'desc'])) {
  66. if ($orderBy == 'asc') {
  67. $orderCondition = [$order => SORT_ASC];
  68. } else {
  69. $orderCondition = [$order => SORT_DESC];
  70. }
  71. } else {
  72. $orderCondition = ['id' => SORT_DESC];
  73. }
  74. $query = CommissionRecord::find();
  75. $query->where($where)
  76. ->orderBy($orderCondition);
  77. $count = $query->count();
  78. $query->offset($start)->limit($length);
  79. $list = $query->asArray()->all();
  80. if ($count) {
  81. $userLogins = array_column($list, 'user_login');
  82. $mt4Users = Mt4Users::find()->where(['in', 'login', $userLogins])->asArray()->all();
  83. foreach ($list as $k => $v) {
  84. $list[$k]['name'] = '';
  85. $list[$k]['in_time'] = (int) $v['in_time'];
  86. foreach ($mt4Users as $k2 => $v2) {
  87. if ($v['user_login'] == $v2['LOGIN']) {
  88. $list[$k]['name'] = $v2['NAME'];
  89. break;
  90. }
  91. }
  92. }
  93. // 计算手数和佣金,放到最后一行
  94. $query = CommissionRecord::find();
  95. $sum = $query->select(['SUM(trade_volume) AS trade_volume', 'SUM(commission) AS commission'])->where($where)->asArray()->limit(1)->one();
  96. $lastRow = [
  97. 'commission' => (float) $sum['commission'],
  98. 'commission_rule' => '',
  99. 'in_time' => '',
  100. 'name' => '',
  101. 'trade_ticket' => '',
  102. 'trade_type' => '99',
  103. 'trade_volume' => (float) $sum['trade_volume'],
  104. 'user_login' => '',
  105. ];
  106. array_push($list, $lastRow);
  107. } else {
  108. // 没有数据则输出默认值
  109. $list = [
  110. [
  111. 'commission' => 0,
  112. 'commission_rule' => '',
  113. 'in_time' => '',
  114. 'name' => '',
  115. 'trade_ticket' => '',
  116. 'trade_type' => '99',
  117. 'trade_volume' => 0,
  118. 'user_login' => '',
  119. ],
  120. ];
  121. }
  122. $data['data'] = $list;
  123. $data['draw'] = $draw;
  124. $data['recordsFiltered'] = $count;
  125. $data['recordsTotal'] = $count;
  126. $result['data'] = $data;
  127. $result['code'] = 1;
  128. return $result;
  129. }
  130. }