FinanceService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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\Models\FinanceModel;
  14. use App\Models\MemberModel;
  15. use App\Services\BaseService;
  16. use App\Services\RedisService;
  17. use Illuminate\Support\Facades\DB;
  18. /**
  19. * 平台账户(财务)-服务类
  20. * @author laravel开发员
  21. * @since 2020/11/12
  22. * Class ActionLogService
  23. * @package App\Services\Common
  24. */
  25. class FinanceService extends BaseService
  26. {
  27. protected static $instance=null;
  28. /**
  29. * 构造函数
  30. * @author laravel开发员
  31. * @since 2020/11/11
  32. * FinanceService constructor.
  33. */
  34. public function __construct()
  35. {
  36. $this->model = new FinanceModel();
  37. }
  38. /**
  39. * 静态入口
  40. * @return static|null
  41. */
  42. public static function make()
  43. {
  44. if (!self::$instance) {
  45. self::$instance = (new static());
  46. }
  47. return self::$instance;
  48. }
  49. /**
  50. * @param $params
  51. * @param int $pageSize
  52. * @return array
  53. */
  54. public function getDataList($params, $pageSize = 15)
  55. {
  56. $where = ['a.mark' => 1];
  57. $status = isset($params['status'])? $params['status'] : 0;
  58. $type = isset($params['type'])? $params['type'] : 0;
  59. if($status>0){
  60. $where['a.status'] = $status;
  61. }
  62. if($type>0){
  63. $where['a.type'] = $type;
  64. }
  65. $list = $this->model->from('finance as a')
  66. ->where($where)
  67. ->where(function ($query) use($params){
  68. // 日期
  69. $date = isset($params['date']) ? $params['date'] : [];
  70. $start = isset($date[0])? $date[0] : '';
  71. $end = isset($date[1])? $date[1] : '';
  72. $end = $start>=$end? '' : $end;
  73. if ($start) {
  74. $query->where('a.create_time','>=', strtotime($start));
  75. }
  76. if($end){
  77. $query->where('a.create_time','<', strtotime($end));
  78. }
  79. })
  80. ->select(['a.*'])
  81. ->orderBy('a.create_time','desc')
  82. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  83. $list = $list? $list->toArray() :[];
  84. if($list){
  85. foreach($list['data'] as &$item){
  86. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  87. }
  88. }
  89. return [
  90. 'pageSize'=> $pageSize,
  91. 'total'=>isset($list['total'])? $list['total'] : 0,
  92. 'list'=> isset($list['data'])? $list['data'] : []
  93. ];
  94. }
  95. /**
  96. * 平台结算
  97. * @param $money
  98. * @param int $changeType 交易类型:1-进账,2-出账
  99. * @param int $type 类型:1-消费,2-佣金,3-充值提现转账,4-退款,99-其他
  100. * @param int $coinType 币种类型: 1-USDT,2-星豆
  101. * @return mixed
  102. */
  103. public function saveLog($userId, $money, $changeType = 1, $type=1, $coinType = 1)
  104. {
  105. $date = date('Y-m-d');
  106. $info = $this->model->where(['user_id'=> $userId,'date'=> $date,'type'=> $type,'mark'=>1])->first();
  107. if(!$info){
  108. $data = ['user_id'=>$userId,'date'=> $date,'type'=>$type,'create_time'=>time(),'update_time'=> time(),'status'=>1];
  109. if($changeType==1){
  110. $data['income'] = $money;
  111. }else{
  112. $data['expend'] = $money;
  113. }
  114. return $this->model->insertGetId($data);
  115. }else{
  116. if($changeType == 1){
  117. $info->income += $money;
  118. }else{
  119. $info->expend += $money;
  120. }
  121. return $info->save();
  122. }
  123. }
  124. /**
  125. * 任务算力奖励
  126. * @param $userId 用户ID
  127. * @param $power 算力
  128. * @param $type 类型
  129. * @param int $sourceId 任务ID
  130. * @param string $remark 备注说明
  131. * @return false
  132. */
  133. public function settleTaskPower($userId, $power, $sourceId=0, $remark='')
  134. {
  135. if($power<=0){
  136. $this->error = 2014;
  137. return false;
  138. }
  139. $userInfo = MemberModel::where(['id'=> $userId, 'status'=>1,'mark'=>1])
  140. ->select(['id','nickname','usdt','power_num','status'])
  141. ->first();
  142. $userPower = isset($userInfo['power_num'])? $userInfo['power_num'] : 0;
  143. if(empty($userInfo) || $userId<=0)
  144. {
  145. $this->error = 1041;
  146. return false;
  147. }
  148. DB::beginTransaction();
  149. // 更新账户算力
  150. $updateData = ['power_num'=> DB::raw("power_num + {$power}"),'update_time'=>time()];
  151. if(!MemberModel::where(['id'=> $userId])->update($updateData)){
  152. DB::rollBack();
  153. $this->error = 1042;
  154. return false;
  155. }
  156. // 明细
  157. $orderNo = get_order_num('TS');
  158. $log = [
  159. 'user_id' => $userId,
  160. 'source_id' => $sourceId,
  161. 'source_order_no' => $orderNo,
  162. 'type' => 9,
  163. 'coin_type' => 3,
  164. 'user_type'=> 1,
  165. 'money' => $power,
  166. 'actual_money' => $power,
  167. 'balance' => $userPower,
  168. 'create_time' => time(),
  169. 'update_time' => time(),
  170. 'remark' => $remark? $remark : '算力奖励',
  171. 'status' => 1,
  172. 'mark' => 1,
  173. ];
  174. if (!AccountLogModel::insertGetId($log)) {
  175. $this->error = 2407;
  176. DB::rollBack();
  177. return false;
  178. }
  179. DB::commit();
  180. // 站内消息
  181. $dateTime = date('Y-m-d H:i:s');
  182. MessageService::make()->pushMessage($userId, $log['remark'] , "您在{$dateTime}(UTC+8){$log['remark']}{$power}算力已到账,请及时查看账户!!!");
  183. }
  184. }