AccountLogService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\Api;
  12. use App\Models\AccountLogModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * 账户-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * @package App\Services\Api
  20. */
  21. class AccountLogService extends BaseService
  22. {
  23. protected static $instance;
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new AccountLogModel();
  32. }
  33. /**
  34. * 静态入口
  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 $userId 用户ID
  46. * @param int $coinType 币种:1-USDT,2-SBT,3-收益/奖励
  47. * @param int $type 明细类型
  48. * @param int $page 分页
  49. * @param int $pageSize 页数
  50. * @return array|mixed
  51. */
  52. public function getDataList($userId, $coinType=1, $type=0, $page=1, $pageSize=12)
  53. {
  54. $cacheKey = "caches:account:list_{$userId}_{$coinType}{$type}{$page}{$pageSize}";
  55. $datas = RedisService::get($cacheKey);
  56. if($datas){
  57. return $datas;
  58. }
  59. $where = ['a.user_id'=> $userId,'a.status'=>1,'a.mark'=>1];
  60. if($type){
  61. $where['a.type'] = $type;
  62. }
  63. $list = $this->model->from('account_log as a')
  64. ->where($where)
  65. ->leftJoin('balance_logs as b',function($join) {
  66. $join->on('b.order_no','=','a.order_no')->on('b.user_id','=','a.user_id');
  67. })
  68. ->leftJoin('pledge_orders as c',function($join) {
  69. $join->on('c.order_no','=','a.order_no')->on('c.user_id','=','a.user_id');
  70. })
  71. ->where(function($query) use($coinType){
  72. if($coinType>0){
  73. $query->where('a.coin_type', $coinType);
  74. }
  75. })
  76. ->select(['a.user_id','a.type','a.order_no','a.source_uid','a.money','a.coin_type','a.before_money','a.create_time','a.remark','a.status','b.status as b_status','b.audit_remark','b.pay_status','b.pay_at','c.status as c_status','c.expired_at','c.settle_at'])
  77. ->orderBy('a.create_time','desc')
  78. ->orderBy('a.id','desc')
  79. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  80. $list = $list ? $list->toArray() : [];
  81. $datas = [
  82. 'page' => $page,
  83. 'pageSize' => $pageSize,
  84. 'total' => 0,
  85. 'list' => []
  86. ];
  87. if ($list && $list['data']) {
  88. $types = config('dapp.accountTypes');
  89. foreach ($list['data'] as &$item){
  90. $item['time_text'] = isset($item['create_time']) && $item['create_time']>0? date('Y.m.d H:i:s', strtotime($item['create_time'])):'';
  91. $remark = isset($item['remark']) && $item['remark']? $item['remark'] : '交易明细';
  92. $type = isset($item['type'])? $item['type'] : 0;
  93. $item['type_text'] = isset($types[$type])? $types[$type] : $remark;
  94. }
  95. $datas = [
  96. 'page' => $page,
  97. 'pageSize' => $pageSize,
  98. 'total' => isset($list['total']) ? $list['total'] : 0,
  99. 'list' => isset($list['data']) ? $list['data'] : []
  100. ];
  101. RedisService::set($cacheKey, $datas, rand(3,5));
  102. }
  103. return $datas;
  104. }
  105. /**
  106. * 账户统计
  107. * @param int $userId 用户ID
  108. * @param $type 类型:1-质押交易,2-闪兑交易,3-充值,4-提现,5-链上充值(平台上分),6-链上扣除(平台下分),7-质押收益,8-推荐分享奖励,9-管理奖,10-平级奖,11-全网分红奖励,12-质押退还,99-其他
  109. * @param $coinType 币种类型:1-USDT,2-SBT,3-收益
  110. * @return array|mixed
  111. */
  112. public function getCountByType(int $userId=0, $type=1,$coinType=0)
  113. {
  114. $cacheKey = "caches:account:counts_{$userId}_{$type}_{$coinType}";
  115. $counts = RedisService::get($cacheKey);
  116. if($counts){
  117. return $counts;
  118. }
  119. $where = ['user_id'=> $userId,'type'=>$type,'status'=>2,'mark'=>1];
  120. if($coinType){
  121. $where['coin_type'] = $coinType;
  122. }
  123. $data = $this->model->where($where)->sum('money');
  124. RedisService::set($cacheKey, abs($data), rand(10, 20));
  125. return abs($data);
  126. }
  127. }