FinanceService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\FinanceModel;
  13. use App\Services\BaseService;
  14. /**
  15. * 平台账户(财务)-服务类
  16. * @author laravel开发员
  17. * @since 2020/11/12
  18. * Class ActionLogService
  19. * @package App\Services\Common
  20. */
  21. class FinanceService extends BaseService
  22. {
  23. protected static $instance=null;
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. * FinanceService constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new FinanceModel();
  33. }
  34. /**
  35. * 静态入口
  36. * @return static|null
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = (new static());
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * @param $params
  47. * @param int $pageSize
  48. * @return array
  49. */
  50. public function getDataList($params, $pageSize = 15)
  51. {
  52. $where = ['a.mark' => 1];
  53. $status = isset($params['status'])? $params['status'] : 0;
  54. $type = isset($params['type'])? $params['type'] : 0;
  55. if($status>0){
  56. $where['a.status'] = $status;
  57. }
  58. if($type>0){
  59. $where['a.type'] = $type;
  60. }
  61. $list = $this->model->from('finance as a')
  62. ->where($where)
  63. ->where(function ($query) use($params){
  64. // 日期
  65. $date = isset($params['date']) ? $params['date'] : [];
  66. $start = isset($date[0])? $date[0] : '';
  67. $end = isset($date[1])? $date[1] : '';
  68. $end = $start>=$end? '' : $end;
  69. if ($start) {
  70. $query->where('a.create_time','>=', strtotime($start));
  71. }
  72. if($end){
  73. $query->where('a.create_time','<', strtotime($end));
  74. }
  75. })
  76. ->select(['a.*'])
  77. ->orderBy('a.create_time','desc')
  78. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  79. $list = $list? $list->toArray() :[];
  80. if($list){
  81. foreach($list['data'] as &$item){
  82. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  83. }
  84. }
  85. return [
  86. 'pageSize'=> $pageSize,
  87. 'total'=>isset($list['total'])? $list['total'] : 0,
  88. 'list'=> isset($list['data'])? $list['data'] : []
  89. ];
  90. }
  91. /**
  92. * 平台结算
  93. * @param $money
  94. * @param int $changeType 交易类型:1-进账,2-出账
  95. * @param int $type 类型:1-消费,2-佣金,3-充值提现转账,4-退款,99-其他
  96. * @param int $coinType 币种类型: 1-USDT,2-星豆
  97. * @return mixed
  98. */
  99. public function saveLog($userId, $money, $changeType = 1, $type=1, $coinType = 1)
  100. {
  101. $date = date('Y-m-d');
  102. $info = $this->model->where(['user_id'=> $userId,'date'=> $date,'type'=> $type,'mark'=>1])->first();
  103. if(!$info){
  104. $data = ['user_id'=>$userId,'date'=> $date,'type'=>$type,'create_time'=>time(),'update_time'=> time(),'status'=>1];
  105. if($changeType==1){
  106. $data['income'] = $money;
  107. }else{
  108. $data['expend'] = $money;
  109. }
  110. return $this->model->insertGetId($data);
  111. }else{
  112. if($changeType == 1){
  113. $info->income += $money;
  114. }else{
  115. $info->expend += $money;
  116. }
  117. return $info->save();
  118. }
  119. }
  120. }