| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\TradeOrderModel;
- use App\Services\BaseService;
- /**
- * 用户交易订单-服务类
- * Class TradeOrderService
- * @package App\Services\Common
- */
- class TradeOrderService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @since 2020/11/10
- * TradeOrderService constructor.
- */
- public function __construct()
- {
- $this->model = new TradeOrderModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * 成交交易笔数
- * @param int $type 类型:1-买单,2-卖单
- * @param int $bsid 承兑商用户ID
- * @param int $status 状态:4-成交
- * @return mixed
- */
- public function getCompleteCount($type = 1, $bsid=0, $status = 4)
- {
- return (int)$this->model->where(['type' => $type, 'status' => $status, 'mark' => 1])->where(function ($query) use ($bsid) {
- if ($bsid > 0) {
- $query->where('user_ids','=', $bsid)->orWhere('business_id','=', $bsid);
- }
- })->count('id');
- }
- /**
- * 成交交易金额
- * @param int $type 类型:1-买单,2-卖单
- * @param int $bsid 承兑商用户ID
- * @param int $status 状态:4-成交
- * @param string $field 统计字段:total-总金额(默认),num-交易USDT数量
- * @return mixed
- */
- public function getCompleteTotal($type = 1, $bsid=0, $status = 4, $field = 'total')
- {
- $total = $this->model->where(['type' => $type, 'status' => $status, 'mark' => 1])
- ->where(function ($query) use ($bsid) {
- if ($bsid > 0) {
- $query->where('user_id','=', $bsid)->orWhere('business_id','=', $bsid);
- }
- })->sum($field);
- return moneyFormat($total, 2);
- }
- /**
- * 获取某个时间段的买卖交易量比
- * @param int $type 状态类型:1-成交的
- * @param int $bsid 承兑商用户ID
- * @param int $time 时间/分钟,默认15分钟
- * @return array
- */
- public function getCountRateByTime($type = 1, $bsid = 0, $time = 15)
- {
- $where = ['mark' => 1];
- if ($type == 1) {
- $where['status'] = 4;
- }
- $buyCount = $this->model->where(['type' => 1])->where($where)
- ->where(function ($query) use ($bsid) {
- if ($bsid > 0) {
- $query->where('user_id','=', $bsid)->orWhere('business_id','=', $bsid);
- }
- })
- ->where('create_time', '>=', time() - $time * 60)
- ->count('id');
- $sellCount = $this->model->where(['type' => 2])->where($where)
- ->where(function ($query) use ($bsid) {
- if ($bsid > 0) {
- $query->where('user_id','=', $bsid)->orWhere('business_id','=', $bsid);
- }
- })
- ->where('create_time', '>=', time() - $time * 60)
- ->count('id');
- $rate = ($buyCount || $sellCount) > 0 ? moneyFormat($buyCount / ($buyCount + $sellCount)) * 100 : 0;
- return ['buy' => $buyCount, 'sell' => $sellCount, 'buy_rate' => $rate, 'sell_rate' => ($sellCount>0? 100 - $rate : 0)];
- }
- }
|