| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557 |
- <?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\GoodsModel;
- 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)
- {
- // 参数验证
- $payType = isset($params['pay_type']) && $params['pay_type'] ? intval($params['pay_type']) : 10;
- $accountType = isset($params['type']) && $params['type'] ? intval($params['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 ($payType != 10 && $accountId <= 0) {
- $this->error = '请选择收款账户';
- return false;
- }
- $openWithdraw = ConfigService::make()->getConfigByCode("withdraw_{$accountType}_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;
- }
- if($accountId){
- $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;
- }
- }else{
- $accountName = '微信支付';
- $account = '微信零钱';
- $accountRemark = '';
- }
- // 判断用户账号状态
- $fields = [1=>'balance',2=>'property',4=>'ls_score'];
- $field = isset($fields[$accountType])? $fields[$accountType]:'balance';
- RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(10, 20));
- $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
- ->select(['id', 'balance','property','buy_type','withdraw_bonus','bonus_status','bd_score','ls_score', 'status'])
- ->first();
- $realname = isset($userInfo['realname']) ? $userInfo['realname'] : '';
- $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
- $bonusStatus = isset($userInfo['bonus_status']) ? $userInfo['bonus_status'] : 0;
- $withdrawBonus = isset($userInfo['withdraw_bonus']) ? $userInfo['withdraw_bonus'] : 0;
- $buyType = isset($userInfo['buy_type']) ? $userInfo['buy_type'] : 1;
- $balance = isset($userInfo[$field]) ? $userInfo[$field] : 0;
- if (empty($userInfo) || $status != 1) {
- $this->error = 2016;
- RedisService::clear($cacheLockKey);
- return false;
- }
- if($bonusStatus!=1){
- $buyType = max(1, $buyType-1);
- }
- if($accountType==1){
- // 提现额度
- $mealPrice = GoodsModel::where(['type'=>2,'mark'=>1])
- ->where('id','>', $buyType)
- ->orderBy('id','asc')
- ->value('price');
- $withdrawQuota = intval($mealPrice/0.27 * 0.73/100)*100;
- if(($money+$withdrawBonus)>$withdrawQuota){
- $quota = moneyFormat($withdrawQuota-$withdrawBonus,2);
- $this->error = "抱歉您的账号剩余可提现额度为:{$quota}元";
- RedisService::clear($cacheLockKey);
- return false;
- }
- }
- if ($money > $balance) {
- $this->error = '该账户可提现余额不足';
- RedisService::clear($cacheLockKey);
- return false;
- }
- // 计算实际到账金额
- $poolMoney = 0;
- $ptMoney = 0;
- $ptRate = 0;
- $poolRate = 0;
- $total = $money;
- $actualMoney = $money;
- if($accountType==2){
- // 提现金额
- $price = PriceService::make()->getTodayPrice(1);
- if($price<=0){
- $this->error = '请等候今日资产价格刷新后重试~';
- RedisService::clear($cacheLockKey);
- return false;
- }
- // 结算比例
- $rate = ConfigService::make()->getConfigByCode('withdraw_2_rate',0);
- $rate = $rate>0 && $rate<=100? $rate : 100;
- $total = moneyFormat($total * $price,2);
- $actualMoney = moneyFormat($total * $rate/100, 2);
- // 底池比例
- $poolRate = ConfigService::make()->getConfigByCode('withdraw_2_back_rate',0);
- $poolRate = $poolRate>0 && $poolRate<50? $poolRate : 0;
- $poolMoney = round($total * $poolRate/100, 2);
- // 运营账户比例
- $ptRate = ConfigService::make()->getConfigByCode('withdraw_2_pt_rate',0);
- $ptRate = $ptRate>0 && $ptRate<=50? $ptRate : 0;
- $ptMoney = round($total * $ptRate/100, 2);
- }
- // 手续费
- $feeRate = ConfigService::make()->getConfigByCode("withdraw_{$accountType}_fee",0);
- $feeRate = $feeRate>0 && $feeRate<=50? $feeRate : 0;
- $fee = round($actualMoney * $feeRate/100, 2);
- $actualMoney = moneyFormat($actualMoney - $fee, 2);
- $accountTypeName = ['账户','收益余额','数字资产','报单积分','绿色积分'][$accountType];
- $accountTypeName = $accountTypeName?$accountTypeName:'账户';
- // 提现处理
- DB::beginTransaction();
- $updateData = ["{$field}" => DB::raw("{$field} - {$money}"), 'update_time' => time()];
- if($accountType==1){
- $updateData['withdraw_bonus'] = DB::raw("withdraw_bonus + {$money}");
- }
- // 会员账户
- $userType = 1;
- if (!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,
- 'total' => $total,
- 'after_money' => moneyFormat(max(0, $balance - $money), 2),
- 'actual_money' => $actualMoney,
- 'fee' => $fee,
- 'pool_money' => $poolMoney,
- 'pool_rate' => $poolRate,
- 'pt_money' => $ptMoney,
- 'pt_rate' => $ptRate,
- 'user_type' => $userType,
- 'type' => 2,
- 'account_type' => $accountType,
- 'pay_type' => $payType,
- '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,
- 'user_type' => $userType,
- 'account_type' => $accountType,
- 'type' => 4,
- 'money' => -$money,
- 'after_money' => moneyFormat(max(0, $balance - $money), 2),
- 'date' => date('Y-m-d'),
- 'create_time' => time(),
- 'remark' => "提现到{$account}",
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!$accountId = AccountLogModel::insertGetId($log)) {
- DB::rollBack();
- $this->error = '提现处理失败';
- RedisService::clear($cacheLockKey);
- return false;
- }
- DB::commit();
- // 操作日志
- ActionLogModel::setRecord($userId, ['type' => 2, 'title' => "{$accountTypeName}提现", 'content' => "姓名:{$realname},账号:{$accountName}/{$account}/{$accountRemark},提现{$money}元,单号:{$orderNo}", 'module' => 'balanceLog']);
- ActionLogModel::record();
- RedisService::clear($cacheLockKey);
- $this->error = '提现申请成功,请耐心等候审核~';
- return ['id' => $orderId, 'aid' => $accountId, 'money' => $money];
- }
- /**
- * 积分转账
- * @param $userId
- * @param $params
- * @return array|false
- */
- public function transfer($userId, $params)
- {
- // 参数验证
- $money = isset($params['money']) ? floatval($params['money']) : 0;
- $accountType = isset($params['type']) && $params['type']? intval($params['type']) : 3;
- $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
- $remark = isset($params['remark']) ? trim($params['remark']) : '';
- if ($money <= 0) {
- $this->error = '请输入转账数量';
- return false;
- }
- if (empty($mobile)) {
- $this->error = '请输入对方手机账号';
- return false;
- }
- $openTransfer = ConfigService::make()->getConfigByCode("transfer_{$accountType}_open", 1);
- if (!$openTransfer) {
- $this->error = '转账功能未开放';
- return false;
- }
- // 锁
- $cacheLockKey = "caches:members:transfer:{$userId}_{$accountType}";
- if (RedisService::get($cacheLockKey)) {
- $this->error = 1034;
- return false;
- }
- // 判断用户账号状态
- $fields = [2=>'property',3=>'bd_score',4=>'ls_score'];
- $field = isset($fields[$accountType])? $fields[$accountType]:'bd_score';
- RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(10, 20));
- $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
- ->select(['id', 'balance','mobile','nickname','property','bd_score','ls_score', 'status'])
- ->first();
- $realname = isset($userInfo['realname']) ? $userInfo['realname'] : '';
- $nickname = isset($userInfo['nickname']) ? $userInfo['nickname'] : '';
- $userMobile = isset($userInfo['mobile']) &&$userInfo['mobile']? $userInfo['mobile'] : '';
- $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
- $balance = isset($userInfo[$field]) ? $userInfo[$field] : 0;
- if (empty($userInfo) || $status != 1) {
- $this->error = 2016;
- RedisService::clear($cacheLockKey);
- return false;
- }
- if ($money > $balance) {
- $this->error = '该账户积分余额不足';
- RedisService::clear($cacheLockKey);
- return false;
- }
- $total = $money;
- $actualMoney = $money;
- $accountTypeName = ['账户','收益余额','数字资产','报单积分','绿色积分'][$accountType];
- $accountTypeName = $accountTypeName?$accountTypeName:'账户';
- // 对方账户验证
- $transferAccount = MemberModel::where(['mobile' => $mobile, 'mark' => 1])
- ->select(['id', 'balance','property','bd_score','ls_score', 'status'])
- ->first();
- $transferUserId = isset($transferAccount['id']) ? $transferAccount['id'] : 0;
- $status = isset($transferAccount['status']) ? $transferAccount['status'] : 0;
- $transferBalance = isset($transferAccount[$field]) ? $transferAccount[$field] : 0;
- if (empty($transferAccount) || $status != 1 || $transferUserId<=0) {
- $this->error = '对方账户不存在或已冻结';
- RedisService::clear($cacheLockKey);
- return false;
- }
- // 转账处理
- DB::beginTransaction();
- $updateData = ["{$field}" => DB::raw("{$field} - {$money}"), 'update_time' => time()];
- // 扣除会员账户
- if (!MemberModel::where(['id' => $userId])->update($updateData)) {
- DB::rollBack();
- $this->error = '转账处理失败';
- RedisService::clear($cacheLockKey);
- return false;
- }
- $updateData = ["{$field}" => DB::raw("{$field} + {$money}"), 'update_time' => time()];
- if (!MemberModel::where(['mobile' => $mobile])->update($updateData)) {
- DB::rollBack();
- $this->error = '转账处理失败';
- RedisService::clear($cacheLockKey);
- return false;
- }
- $orderNo = get_order_num('TS');
- $order = [
- 'user_id' => $userId,
- 'order_no' => $orderNo,
- 'money' => $money,
- 'total' => $total,
- 'after_money' => moneyFormat(max(0, $balance - $money), 2),
- 'actual_money' => $actualMoney,
- 'user_type' => 1,
- 'type' => 3,
- 'account_type' => $accountType,
- 'pay_type' => 30,
- 'realname' => '',
- 'account_name' => $accountTypeName,
- 'account' => $mobile,
- 'remark' => $remark,
- 'date' => date('Y-m-d'),
- 'create_time' => time(),
- 'status' => 4,
- '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,
- 'user_type' => 1,
- 'account_type' => $accountType,
- 'type' => 9,
- 'money' => -$money,
- 'after_money' => moneyFormat(max(0, $balance - $money), 2),
- 'date' => date('Y-m-d'),
- 'create_time' => time(),
- 'remark' => "转账到{$mobile}",
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!$accountId = AccountLogModel::insertGetId($log)) {
- DB::rollBack();
- $this->error = '转账处理失败';
- RedisService::clear($cacheLockKey);
- return false;
- }
- $userMobileText = $userMobile?format_mobile($userMobile):$nickname;
- $log = [
- 'user_id' => $transferUserId,
- 'source_order_no' => $orderNo,
- 'user_type' => 1,
- 'account_type' => $accountType,
- 'type' => 9,
- 'money' => $money,
- 'after_money' => moneyFormat(max(0, $transferBalance + $money), 2),
- 'date' => date('Y-m-d'),
- 'create_time' => time(),
- 'remark' => "收到{$userMobileText}用户的{$accountTypeName}转账",
- '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' => "{$accountTypeName}积分转账", 'content' => "账户{$userMobileText}转至{$mobile},数量{$money},单号:{$orderNo}", 'module' => 'balanceLog']);
- ActionLogModel::record();
- RedisService::clear($cacheLockKey);
- $this->error = '转账成功~';
- return ['id' => $orderId, 'aid' => $accountId, 'money' => $money];
- }
- }
|