Mt5Deal.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Titan 名字就是密码
  5. * Date: 2020/1/6
  6. * Time: 16:05
  7. */
  8. namespace backend\models;
  9. use common\helpers\Utils;
  10. use yii\db\ActiveRecord;
  11. class Mt5Deal extends ActiveRecord
  12. {
  13. public static function tableName()
  14. {
  15. return "mt5_deals";
  16. }
  17. public static function getDb()
  18. {
  19. return \Yii::$app->get('mt5db');
  20. }
  21. public function getDealList($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','Deal',$search],
  35. ['like',"Login",$search]
  36. ];
  37. }
  38. $allowOrderColumn = ['Time','Login','Deal','Symbol','Action','Entry','Volume','Price','PriceSL','PriceTP','Storage','Profit'];
  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 = ['Order' => 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]['Price'] = Utils::formatFloatOrInt($v['Price'],5);
  57. $list[$k]['Profit'] = Utils::formatFloatOrInt($v['Profit'],2);
  58. $list[$k]['PriceSL'] = Utils::formatFloatOrInt($v['PriceSL'],2);
  59. $list[$k]['PriceTP'] = Utils::formatFloatOrInt($v['PriceTP'],2);
  60. }
  61. }
  62. $data['data'] = $list;
  63. $data['draw'] = $draw;
  64. $data['recordsFiltered'] = $count;
  65. $data['recordsTotal'] = $count;
  66. $result['data'] = $data;
  67. $result['code'] = 1;
  68. return $result;
  69. }
  70. }