123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\AccountLogModel;
- use App\Models\FinanceModel;
- use App\Models\MemberModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- use Illuminate\Support\Facades\DB;
- /**
- * 平台账户(财务)-服务类
- * @author laravel开发员
- * @since 2020/11/12
- * Class ActionLogService
- * @package App\Services\Common
- */
- class FinanceService extends BaseService
- {
- protected static $instance=null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * FinanceService constructor.
- */
- public function __construct()
- {
- $this->model = new FinanceModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 15)
- {
- $where = ['a.mark' => 1];
- $status = isset($params['status'])? $params['status'] : 0;
- $type = isset($params['type'])? $params['type'] : 0;
- if($status>0){
- $where['a.status'] = $status;
- }
- if($type>0){
- $where['a.type'] = $type;
- }
- $list = $this->model->from('finance as a')
- ->where($where)
- ->where(function ($query) use($params){
- // 日期
- $date = isset($params['date']) ? $params['date'] : [];
- $start = isset($date[0])? $date[0] : '';
- $end = isset($date[1])? $date[1] : '';
- $end = $start>=$end? '' : $end;
- if ($start) {
- $query->where('a.create_time','>=', strtotime($start));
- }
- if($end){
- $query->where('a.create_time','<', strtotime($end));
- }
- })
- ->select(['a.*'])
- ->orderBy('a.create_time','desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list? $list->toArray() :[];
- if($list){
- foreach($list['data'] as &$item){
- $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
- }
- }
- return [
- 'pageSize'=> $pageSize,
- 'total'=>isset($list['total'])? $list['total'] : 0,
- 'list'=> isset($list['data'])? $list['data'] : []
- ];
- }
- /**
- * 平台结算
- * @param $money
- * @param int $changeType 交易类型:1-进账,2-出账
- * @param int $type 类型:1-消费,2-佣金,3-充值提现转账,4-退款,99-其他
- * @param int $coinType 币种类型: 1-USDT,2-星豆
- * @return mixed
- */
- public function saveLog($userId, $money, $changeType = 1, $type=1, $coinType = 1)
- {
- $date = date('Y-m-d');
- $info = $this->model->where(['user_id'=> $userId,'date'=> $date,'type'=> $type,'mark'=>1])->first();
- if(!$info){
- $data = ['user_id'=>$userId,'date'=> $date,'type'=>$type,'create_time'=>time(),'update_time'=> time(),'status'=>1];
- if($changeType==1){
- $data['income'] = $money;
- }else{
- $data['expend'] = $money;
- }
- return $this->model->insertGetId($data);
- }else{
- if($changeType == 1){
- $info->income += $money;
- }else{
- $info->expend += $money;
- }
- return $info->save();
- }
- }
- /**
- * 任务算力奖励
- * @param $userId 用户ID
- * @param $power 算力
- * @param $type 类型
- * @param int $sourceId 任务ID
- * @param string $remark 备注说明
- * @return false
- */
- public function settleTaskPower($userId, $power, $sourceId=0, $remark='')
- {
- if($power<=0){
- $this->error = 2014;
- return false;
- }
- $userInfo = MemberModel::where(['id'=> $userId, 'status'=>1,'mark'=>1])
- ->select(['id','nickname','usdt','power_num','status'])
- ->first();
- $userPower = isset($userInfo['power_num'])? $userInfo['power_num'] : 0;
- if(empty($userInfo) || $userId<=0)
- {
- $this->error = 1041;
- return false;
- }
- DB::beginTransaction();
- // 更新账户算力
- $updateData = ['power_num'=> DB::raw("power_num + {$power}"),'update_time'=>time()];
- if(!MemberModel::where(['id'=> $userId])->update($updateData)){
- DB::rollBack();
- $this->error = 1042;
- return false;
- }
- // 明细
- $orderNo = get_order_num('TS');
- $log = [
- 'user_id' => $userId,
- 'source_id' => $sourceId,
- 'source_order_no' => $orderNo,
- 'type' => 9,
- 'coin_type' => 3,
- 'user_type'=> 1,
- 'money' => $power,
- 'actual_money' => $power,
- 'balance' => $userPower,
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => $remark? $remark : '算力奖励',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($log)) {
- $this->error = 2407;
- DB::rollBack();
- return false;
- }
- DB::commit();
- // 站内消息
- $dateTime = date('Y-m-d H:i:s');
- MessageService::make()->pushMessage($userId, $log['remark'] , "您在{$dateTime}(UTC+8){$log['remark']}{$power}算力已到账,请及时查看账户!!!");
- }
- }
|