| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <?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\ActionLogModel;
- use App\Models\BalanceLogModel;
- use App\Models\MemberBankModel;
- use App\Models\MemberModel;
- use App\Services\BaseService;
- use App\Services\ConfigService;
- use App\Services\RedisService;
- use Illuminate\Support\Facades\DB;
- /**
- * 余额管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- */
- class BalanceLogService extends BaseService
- {
- public static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * AccountService constructor.
- */
- public function __construct()
- {
- $this->model = new BalanceLogModel();
- }
- /**
- * 静态入口
- * @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)
- {
- $query = $this->getQuery($params);
- $list = $query->select(['a.*'])
- ->orderBy('a.status','asc')
- ->orderBy('a.create_time','desc')
- ->orderBy('a.id','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') : '';
- $item['time_text'] = $item['create_time']? datetime($item['create_time'],'Y年m月d日') : '';
- }
- }
- return [
- 'pageSize'=> $pageSize,
- 'total'=>isset($list['total'])? $list['total'] : 0,
- 'list'=> isset($list['data'])? $list['data'] : []
- ];
- }
- public function getQuery($params)
- {
- $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;
- }
- return $this->model->with(['member'])->from("balance_logs as a")
- ->leftJoin('member as b','b.id','=','a.user_id')
- ->where($where)
- ->where(function ($query) use($params) {
- $keyword = isset($params['keyword']) ? $params['keyword'] : '';
- $userId = isset($params['user_id'])? $params['user_id'] : 0;
- if($userId){
- $query->where('a.user_id',$userId);
- }
- if ($keyword) {
- $query->where(function($query) use($keyword){
- $query->where('b.nickname','like',"%{$keyword}%")
- ->orWhere('b.mobile','like',"%{$keyword}%")
- ->orWhere('b.realname','like',"%{$keyword}%");
- });
- }
- $orderNo = isset($params['order_no'])? trim($params['order_no']) : '';
- if($orderNo){
- $query->where(function($query) use($orderNo){
- $query->where('a.order_no','like',"%{$orderNo}%");
- });
- }
- $account = isset($params['account'])? trim($params['account']) : '';
- if($account){
- $query->where(function($query) use($account){
- $query->where('a.account','like',"%{$account}%");
- });
- }
- })
- ->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));
- }
- });
- }
- /**
- * 收入提现
- * @param $userId
- * @param $params
- * @return array|false
- */
- public function withdraw($userId, $params)
- {
- // 参数验证
- $accountType = isset($params['account_type']) && $params['account_type']? intval($params['account_type']) : 1;
- $money = isset($params['money'])? floatval($params['money']) : 0;
- $accountId = isset($params['account_id'])? intval($params['account_id']) : 0;
- if($money<=0){
- $this->error = '请输入提现金额';
- return false;
- }
- if($accountId<=0){
- $this->error = '请选择收款账户';
- return false;
- }
- $openWithdraw = ConfigService::make()->getConfigByCode('withdraw_open',1);
- if(!$openWithdraw){
- $this->error = 2304;
- return false;
- }
- $withdrawMin = ConfigService::make()->getConfigByCode('withdraw_min',0.1);
- if($withdrawMin>0 && $money < $withdrawMin){
- $this->error = lang(2305,['money'=>$withdrawMin]);
- return false;
- }
- // 锁
- $cacheLockKey = "caches:members:withdraw:{$userId}";
- if(RedisService::get($cacheLockKey)){
- $this->error = 1034;
- return false;
- }
- $accountInfo = MemberBankModel::where(['id'=> $accountId,'user_id'=>$userId,'mark'=>1])->first();
- $realname = isset($accountInfo['realname'])?$accountInfo['realname'] : '';
- $account = isset($accountInfo['account'])?$accountInfo['account'] : '';
- $accountName = isset($accountInfo['account_name'])?$accountInfo['account_name'] : '';
- $accountRemark = isset($accountInfo['account_remark'])?$accountInfo['account_remark'] : '';
- if(empty($accountInfo) || empty($realname) || empty($accountName) || empty($account)){
- $this->error = '抱歉,当前收款账户错误请更换后重试';
- return false;
- }
- // 判断用户账号状态
- RedisService::set($cacheLockKey, ['user_id'=>$userId,'params'=>$params], rand(10,20));
- $userInfo = MemberModel::where(['id'=>$userId,'mark'=>1])->select(['id','balance','status'])->first();
- $status = isset($userInfo['status'])? $userInfo['status'] : 0;
- $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
- if(empty($userInfo) || $status != 1){
- $this->error = 1045;
- RedisService::clear($cacheLockKey);
- return false;
- }
- if($money > $balance){
- $this->error = '可提现金额不足';
- RedisService::clear($cacheLockKey);
- return false;
- }
- // 提现处理
- DB::beginTransaction();
- $updateData = ['balance'=>DB::raw("balance - {$money}"),'update_time'=>time()];
- // 会员账户
- if($accountType==1 && !MemberModel::where(['id'=> $userId])->update($updateData)){
- DB::rollBack();
- $this->error = '提现处理失败';
- RedisService::clear($cacheLockKey);
- return false;
- }
- $orderNo = get_order_num('JW');
- $order = [
- 'user_id'=> $userId,
- 'order_no'=> $orderNo,
- 'money'=> -$money,
- 'after_money'=> moneyFormat(max(0,$balance-$money), 2),
- 'type'=>2,
- 'account_type'=> $accountType,
- 'pay_type'=> 50,
- 'realname'=> $realname,
- 'account_name'=> $accountName,
- 'account'=> $account,
- 'account_remark'=> $accountRemark,
- 'date'=> date('Y-m-d'),
- 'create_time'=> time(),
- 'status'=>1,
- 'mark'=>1
- ];
- if(!$orderId = $this->model::insertGetId($order)){
- DB::rollBack();
- $this->error = '提现处理失败';
- RedisService::clear($cacheLockKey);
- return false;
- }
- $log = [
- 'user_id' => $userId,
- 'source_order_no' => $orderNo,
- 'type' => 4,
- 'money' => $money,
- 'after_money' => moneyFormat(max(0,$balance-$money), 2),
- 'date'=> date('Y-m-d'),
- 'create_time' => time(),
- 'remark' => "{$accountName}".'('.substr($account,-4,4).') '.format_name($realname),
- 'status' => 1,
- 'mark' => 1,
- ];
- if(!AccountLogModel::insertGetId($log)){
- DB::rollBack();
- $this->error = '提现处理失败';
- RedisService::clear($cacheLockKey);
- return false;
- }
- DB::commit();
- // 操作日志
- ActionLogModel::setRecord($userId,['type'=>2,'title'=>'余额提现','content'=>"姓名:{$realname},账号:{$accountName}/{$account}/{$accountRemark},提现{$money}元,单号:{$orderNo}",'module'=>'balanceLog']);
- ActionLogModel::record();
- $this->error = '提现申请成功,请耐心等候审核~';
- return ['id'=>$orderId,'money'=>$money];
- }
- }
|