Mt5Positions.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Titan 名字就是密码
  5. * Date: 2020/1/2
  6. * Time: 15:55
  7. */
  8. namespace backend\models;
  9. use common\helpers\Utils;
  10. use yii\db\ActiveRecord;
  11. class Mt5Positions extends ActiveRecord
  12. {
  13. public static function tableName()
  14. {
  15. return "mt5_positions";
  16. }
  17. public static function getDb()
  18. {
  19. return \Yii::$app->get('mt5db');
  20. }
  21. public function getPositionList($post)
  22. {
  23. $result = ['code'=>0,'data'=>[],'message'=>''];
  24. $order = isset($post['order']) ? $post['order'] : '';
  25. $orderBy = isset($post['orderBy']) ? strtolower($post['orderBy']) : 'desc';
  26. $search = isset($post['search']) ? $post['search'] : '';
  27. $start = isset($post['start']) ? (int) $post['start'] : 0;
  28. $length = isset($post['length']) ? (int) $post['length'] : 20;
  29. $draw = isset($post['draw']) ? $post['draw'] : 1;
  30. $where = ['and'];
  31. if($search) {
  32. $where[] = [
  33. 'or',
  34. ['like','Login',$search],
  35. ['like',"Position",$search]
  36. ];
  37. }
  38. $allowOrderColumn = ['Position','Login','Action','Symbol','PriceSL','PriceTP','Volume','PriceOpen','TimeCreate','PriceCurrent','Storage','Profit','Comment'];
  39. if(in_array($order,$allowOrderColumn) && in_array($orderBy,['asc','desc'])){
  40. if($orderBy == "asc"){
  41. $orderCondition = [$order => SORT_ASC];
  42. }else{
  43. $orderCondition = [$order => SORT_DESC];
  44. }
  45. }else{
  46. $orderCondition = ['Position' => SORT_DESC];
  47. }
  48. $query = static::find();
  49. $query->where($where)
  50. ->orderBy($orderCondition);
  51. $count = $query->count();
  52. $query->offset($start)->limit($length);
  53. $list = $query->asArray()->all();
  54. if($count){
  55. foreach ($list as $k => $v){
  56. $list[$k]['Profit'] = Utils::formatFloatOrInt($v['Profit']);
  57. $list[$k]['PriceOpen'] = Utils::formatFloatOrInt($v['PriceOpen']);
  58. $list[$k]['PriceCurrent'] = Utils::formatFloatOrInt($v['PriceCurrent']);
  59. }
  60. }
  61. $data['data'] = $list;
  62. $data['draw'] = $draw;
  63. $data['recordsFiltered'] = $count;
  64. $data['recordsTotal'] = $count;
  65. $result['data'] = $data;
  66. $result['code'] = 1;
  67. return $result;
  68. }
  69. }