TradeOrderService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\TradeOrderModel;
  13. use App\Services\BaseService;
  14. /**
  15. * 用户交易订单-服务类
  16. * Class TradeOrderService
  17. * @package App\Services\Common
  18. */
  19. class TradeOrderService extends BaseService
  20. {
  21. // 静态对象
  22. protected static $instance = null;
  23. /**
  24. * 构造函数
  25. * @since 2020/11/10
  26. * TradeOrderService constructor.
  27. */
  28. public function __construct()
  29. {
  30. $this->model = new TradeOrderModel();
  31. }
  32. /**
  33. * 静态入口
  34. * @return static|null
  35. */
  36. public static function make()
  37. {
  38. if (!self::$instance) {
  39. self::$instance = (new static());
  40. }
  41. return self::$instance;
  42. }
  43. /**
  44. * 成交交易笔数
  45. * @param int $type 类型:1-买单,2-卖单
  46. * @param int $bsid 承兑商用户ID
  47. * @param int $status 状态:4-成交
  48. * @return mixed
  49. */
  50. public function getCompleteCount($type = 1, $bsid=0, $status = 4)
  51. {
  52. return (int)$this->model->where(['type' => $type, 'status' => $status, 'mark' => 1])->where(function ($query) use ($bsid) {
  53. if ($bsid > 0) {
  54. $query->where('user_ids','=', $bsid)->orWhere('business_id','=', $bsid);
  55. }
  56. })->count('id');
  57. }
  58. /**
  59. * 成交交易金额
  60. * @param int $type 类型:1-买单,2-卖单
  61. * @param int $bsid 承兑商用户ID
  62. * @param int $status 状态:4-成交
  63. * @param string $field 统计字段:total-总金额(默认),num-交易USDT数量
  64. * @return mixed
  65. */
  66. public function getCompleteTotal($type = 1, $bsid=0, $status = 4, $field = 'total')
  67. {
  68. $total = $this->model->where(['type' => $type, 'status' => $status, 'mark' => 1])
  69. ->where(function ($query) use ($bsid) {
  70. if ($bsid > 0) {
  71. $query->where('user_id','=', $bsid)->orWhere('business_id','=', $bsid);
  72. }
  73. })->sum($field);
  74. return moneyFormat($total, 2);
  75. }
  76. /**
  77. * 获取某个时间段的买卖交易量比
  78. * @param int $type 状态类型:1-成交的
  79. * @param int $bsid 承兑商用户ID
  80. * @param int $time 时间/分钟,默认15分钟
  81. * @return array
  82. */
  83. public function getCountRateByTime($type = 1, $bsid = 0, $time = 15)
  84. {
  85. $where = ['mark' => 1];
  86. if ($type == 1) {
  87. $where['status'] = 4;
  88. }
  89. $buyCount = $this->model->where(['type' => 1])->where($where)
  90. ->where(function ($query) use ($bsid) {
  91. if ($bsid > 0) {
  92. $query->where('user_id','=', $bsid)->orWhere('business_id','=', $bsid);
  93. }
  94. })
  95. ->where('create_time', '>=', time() - $time * 60)
  96. ->count('id');
  97. $sellCount = $this->model->where(['type' => 2])->where($where)
  98. ->where(function ($query) use ($bsid) {
  99. if ($bsid > 0) {
  100. $query->where('user_id','=', $bsid)->orWhere('business_id','=', $bsid);
  101. }
  102. })
  103. ->where('create_time', '>=', time() - $time * 60)
  104. ->count('id');
  105. $rate = ($buyCount || $sellCount) > 0 ? moneyFormat($buyCount / ($buyCount + $sellCount)) * 100 : 0;
  106. return ['buy' => $buyCount, 'sell' => $sellCount, 'buy_rate' => $rate, 'sell_rate' => ($sellCount>0? 100 - $rate : 0)];
  107. }
  108. }